unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings.
[gnulib.git] / tests / test-argmatch.c
blob24a05008c7de5c670fca3b6773fbd168dd43c98e
1 /* Test of exact or abbreviated match search.
2 Copyright (C) 1990, 1998-1999, 2001-2020 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 #include "argmatch.h"
24 #include <stdlib.h>
26 #include "macros.h"
28 # define N_(Msgid) (Msgid)
30 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
31 thus must link with a definition of that function. Provide it here. */
32 #ifdef ARGMATCH_DIE_DECL
34 _Noreturn ARGMATCH_DIE_DECL;
35 ARGMATCH_DIE_DECL { exit (1); }
37 #endif
39 enum backup_type
41 no_backups,
42 simple_backups,
43 numbered_existing_backups,
44 numbered_backups
47 static const char *const backup_args[] =
49 "no", "none", "off",
50 "simple", "never", "single",
51 "existing", "nil", "numbered-existing",
52 "numbered", "t", "newstyle",
53 NULL
56 static const enum backup_type backup_vals[] =
58 no_backups, no_backups, no_backups,
59 simple_backups, simple_backups, simple_backups,
60 numbered_existing_backups, numbered_existing_backups, numbered_existing_backups,
61 numbered_backups, numbered_backups, numbered_backups
64 ARGMATCH_DEFINE_GROUP(backup, enum backup_type);
66 static const argmatch_backup_doc argmatch_backup_docs[] =
68 { "no", N_("never make backups (even if --backup is given)") },
69 { "numbered", N_("make numbered backups") },
70 { "existing", N_("numbered if numbered backups exist, simple otherwise") },
71 { "simple", N_("always make simple backups") },
72 { NULL, NULL }
75 static const argmatch_backup_arg argmatch_backup_args[] =
77 { "no", no_backups },
78 { "none", no_backups },
79 { "off", no_backups },
80 { "simple", simple_backups },
81 { "never", simple_backups },
82 { "single", simple_backups },
83 { "existing", numbered_existing_backups },
84 { "nil", numbered_existing_backups },
85 { "numbered-existing", numbered_existing_backups },
86 { "numbered", numbered_backups },
87 { "t", numbered_backups },
88 { "newstyle", numbered_backups },
89 { NULL, no_backups }
92 const argmatch_backup_group_type argmatch_backup_group =
94 argmatch_backup_args,
95 argmatch_backup_docs,
96 N_("\
97 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
98 The version control method may be selected via the --backup option or through\n\
99 the VERSION_CONTROL environment variable. Here are the values:\n"),
100 NULL
104 main (int argc, char *argv[])
106 #define CHECK(Input, Output) \
107 do { \
108 ASSERT (ARGMATCH (Input, backup_args, backup_vals) == Output); \
109 ASSERT (argmatch_backup_choice (Input) == Output); \
110 if (0 <= Output) \
112 enum backup_type val = argmatch_backup_args[Output].val; \
113 ASSERT (*argmatch_backup_value ("test", Input) == val); \
114 ASSERT (*argmatch_backup_value ("test", \
115 argmatch_backup_argument (&val)) \
116 == val); \
118 } while (0)
120 /* Not found. */
121 CHECK ("klingon", -1);
123 /* Exact match. */
124 CHECK ("none", 1);
125 CHECK ("nil", 7);
127 /* Too long. */
128 CHECK ("nilpotent", -1);
130 /* Abbreviated. */
131 CHECK ("simpl", 3);
132 CHECK ("simp", 3);
133 CHECK ("sim", 3);
135 /* Exact match and abbreviated. */
136 CHECK ("numbered", 9);
137 CHECK ("numbere", -2);
138 CHECK ("number", -2);
139 CHECK ("numbe", -2);
140 CHECK ("numb", -2);
141 CHECK ("num", -2);
142 CHECK ("nu", -2);
143 CHECK ("n", -2);
145 /* Ambiguous abbreviated. */
146 CHECK ("ne", -2);
148 /* Ambiguous abbreviated, but same value ("single" and "simple"). */
149 CHECK ("si", 3);
150 CHECK ("s", 3);
151 #undef CHECK
153 argmatch_backup_usage (stdout);
155 return 0;