Fix last ChangeLog entry.
[gnulib.git] / tests / test-argmatch.c
blobfad7ee49593c0e3f1a48affd3162d2f1a7b1aa98
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>. */
20 #include <config.h>
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"
27 #endif
29 #include "argmatch.h"
31 #include <stdlib.h>
33 #include "macros.h"
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); }
44 #endif
46 enum backup_type
48 no_backups,
49 simple_backups,
50 numbered_existing_backups,
51 numbered_backups
54 static const char *const backup_args[] =
56 "no", "none", "off",
57 "simple", "never", "single",
58 "existing", "nil", "numbered-existing",
59 "numbered", "t", "newstyle",
60 NULL
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") },
79 { NULL, NULL }
82 static const argmatch_backup_arg argmatch_backup_args[] =
84 { "no", no_backups },
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 },
96 { NULL, no_backups }
99 const argmatch_backup_group_type argmatch_backup_group =
101 argmatch_backup_args,
102 argmatch_backup_docs,
103 N_("\
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"),
107 NULL
111 main (int argc, char *argv[])
113 #define CHECK(Input, Output) \
114 do { \
115 ASSERT (ARGMATCH (Input, backup_args, backup_vals) == Output); \
116 ASSERT (argmatch_backup_choice (Input) == Output); \
117 if (0 <= 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)) \
124 == val); \
126 } while (0)
128 #define CHECK_EXACT(Input, Output) \
129 do { \
130 ASSERT (ARGMATCH_EXACT (Input, backup_args) == Output); \
131 } while (0)
133 /* Not found. */
134 CHECK ("klingon", -1);
135 CHECK_EXACT ("klingon", -1);
137 /* Exact match. */
138 CHECK ("none", 1);
139 CHECK_EXACT ("none", 1);
140 CHECK ("nil", 7);
141 CHECK_EXACT ("nil", 7);
143 /* Too long. */
144 CHECK ("nilpotent", -1);
145 CHECK_EXACT ("nilpotent", -1);
147 /* Abbreviated. */
148 CHECK ("simpl", 3);
149 CHECK_EXACT ("simpl", -1);
150 CHECK ("simp", 3);
151 CHECK_EXACT ("simp", -1);
152 CHECK ("sim", 3);
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);
162 CHECK ("numbe", -2);
163 CHECK_EXACT ("numbe", -1);
164 CHECK ("numb", -2);
165 CHECK_EXACT ("numb", -1);
166 CHECK ("num", -2);
167 CHECK_EXACT ("num", -1);
168 CHECK ("nu", -2);
169 CHECK_EXACT ("nu", -1);
170 CHECK ("n", -2);
171 CHECK_EXACT ("n", -1);
173 /* Ambiguous abbreviated. */
174 CHECK ("ne", -2);
175 CHECK_EXACT ("ne", -1);
177 /* Ambiguous abbreviated, but same value ("single" and "simple"). */
178 CHECK ("si", 3);
179 CHECK_EXACT ("si", -1);
180 CHECK ("s", 3);
181 CHECK_EXACT ("s", -1);
183 #undef CHECK
184 #undef CHECK_EXACT
186 argmatch_backup_usage (stdout);
188 return test_exit_status;