1 /* Test of exact or abbreviated match search.
2 Copyright (C) 1990, 1998-1999, 2001-2025 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007, based on test code
18 by David MacKenzie <djm@gnu.ai.mit.edu>. */
22 /* As of GCC 11.2.1, gcc -Wanalyzer-too-complex reports that main's
23 use of CHECK macros expands to code that is too complicated for gcc
24 -fanalyzer. Suppress the resulting bogus warnings. */
25 #if _GL_GNUC_PREREQ (10, 0)
26 # pragma GCC diagnostic ignored "-Wanalyzer-null-argument"
35 # define N_(Msgid) (Msgid)
37 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
38 thus must link with a definition of that function. Provide it here. */
39 #ifdef ARGMATCH_DIE_DECL
41 _Noreturn ARGMATCH_DIE_DECL
;
42 ARGMATCH_DIE_DECL
{ exit (1); }
50 numbered_existing_backups
,
54 static const char *const backup_args
[] =
57 "simple", "never", "single",
58 "existing", "nil", "numbered-existing",
59 "numbered", "t", "newstyle",
63 static const enum backup_type backup_vals
[] =
65 no_backups
, no_backups
, no_backups
,
66 simple_backups
, simple_backups
, simple_backups
,
67 numbered_existing_backups
, numbered_existing_backups
, numbered_existing_backups
,
68 numbered_backups
, numbered_backups
, numbered_backups
71 ARGMATCH_DEFINE_GROUP(backup
, enum backup_type
)
73 static const argmatch_backup_doc argmatch_backup_docs
[] =
75 { "no", N_("never make backups (even if --backup is given)") },
76 { "numbered", N_("make numbered backups") },
77 { "existing", N_("numbered if numbered backups exist, simple otherwise") },
78 { "simple", N_("always make simple backups") },
82 static const argmatch_backup_arg argmatch_backup_args
[] =
85 { "none", no_backups
},
86 { "off", no_backups
},
87 { "simple", simple_backups
},
88 { "never", simple_backups
},
89 { "single", simple_backups
},
90 { "existing", numbered_existing_backups
},
91 { "nil", numbered_existing_backups
},
92 { "numbered-existing", numbered_existing_backups
},
93 { "numbered", numbered_backups
},
94 { "t", numbered_backups
},
95 { "newstyle", numbered_backups
},
99 const argmatch_backup_group_type argmatch_backup_group
=
101 argmatch_backup_args
,
102 argmatch_backup_docs
,
104 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
105 The version control method may be selected via the --backup option or through\n\
106 the VERSION_CONTROL environment variable. Here are the values:\n"),
111 main (int argc
, char *argv
[])
113 #define CHECK(Input, Output) \
115 ASSERT (ARGMATCH (Input, backup_args, backup_vals) == Output); \
116 ASSERT (argmatch_backup_choice (Input) == Output); \
119 enum backup_type val \
120 = argmatch_backup_args[Output < 0 ? 0 : Output].val; \
121 ASSERT (*argmatch_backup_value ("test", Input) == val); \
122 ASSERT (*argmatch_backup_value ("test", \
123 argmatch_backup_argument (&val)) \
128 #define CHECK_EXACT(Input, Output) \
130 ASSERT (ARGMATCH_EXACT (Input, backup_args) == Output); \
134 CHECK ("klingon", -1);
135 CHECK_EXACT ("klingon", -1);
139 CHECK_EXACT ("none", 1);
141 CHECK_EXACT ("nil", 7);
144 CHECK ("nilpotent", -1);
145 CHECK_EXACT ("nilpotent", -1);
149 CHECK_EXACT ("simpl", -1);
151 CHECK_EXACT ("simp", -1);
153 CHECK_EXACT ("sim", -1);
155 /* Exact match and abbreviated. */
156 CHECK ("numbered", 9);
157 CHECK_EXACT ("numbered", 9);
158 CHECK ("numbere", -2);
159 CHECK_EXACT ("numbere", -1);
160 CHECK ("number", -2);
161 CHECK_EXACT ("number", -1);
163 CHECK_EXACT ("numbe", -1);
165 CHECK_EXACT ("numb", -1);
167 CHECK_EXACT ("num", -1);
169 CHECK_EXACT ("nu", -1);
171 CHECK_EXACT ("n", -1);
173 /* Ambiguous abbreviated. */
175 CHECK_EXACT ("ne", -1);
177 /* Ambiguous abbreviated, but same value ("single" and "simple"). */
179 CHECK_EXACT ("si", -1);
181 CHECK_EXACT ("s", -1);
186 argmatch_backup_usage (stdout
);
188 return test_exit_status
;