Imported Upstream version 3.8
[platform/upstream/diffutils.git] / gnulib-tests / test-argmatch.c
1 /* Test of exact or abbreviated match search.
2    Copyright (C) 1990, 1998-1999, 2001-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007, based on test code
18    by David MacKenzie <djm@gnu.ai.mit.edu>.  */
19
20 #include <config.h>
21
22 #include "argmatch.h"
23
24 #include <stdlib.h>
25
26 #include "macros.h"
27
28 # define N_(Msgid) (Msgid)
29
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
33
34 _Noreturn ARGMATCH_DIE_DECL;
35 ARGMATCH_DIE_DECL { exit (1); }
36
37 #endif
38
39 enum backup_type
40 {
41   no_backups,
42   simple_backups,
43   numbered_existing_backups,
44   numbered_backups
45 };
46
47 static const char *const backup_args[] =
48 {
49   "no", "none", "off",
50   "simple", "never", "single",
51   "existing", "nil", "numbered-existing",
52   "numbered", "t", "newstyle",
53   NULL
54 };
55
56 static const enum backup_type backup_vals[] =
57 {
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
62 };
63
64 ARGMATCH_DEFINE_GROUP(backup, enum backup_type)
65
66 static const argmatch_backup_doc argmatch_backup_docs[] =
67 {
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 }
73 };
74
75 static const argmatch_backup_arg argmatch_backup_args[] =
76 {
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 }
90 };
91
92 const argmatch_backup_group_type argmatch_backup_group =
93 {
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
101 };
102
103 int
104 main (int argc, char *argv[])
105 {
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)                                                    \
111       {                                                                 \
112         enum backup_type val                                            \
113           = argmatch_backup_args[Output < 0 ? 0 : Output].val;          \
114         ASSERT (*argmatch_backup_value ("test", Input) == val);         \
115         ASSERT (*argmatch_backup_value ("test",                         \
116                                         argmatch_backup_argument (&val)) \
117                 == val);                                                \
118       }                                                                 \
119   } while (0)
120
121   /* Not found.  */
122   CHECK ("klingon", -1);
123
124   /* Exact match.  */
125   CHECK ("none", 1);
126   CHECK ("nil", 7);
127
128   /* Too long.  */
129   CHECK ("nilpotent", -1);
130
131   /* Abbreviated.  */
132   CHECK ("simpl", 3);
133   CHECK ("simp", 3);
134   CHECK ("sim", 3);
135
136   /* Exact match and abbreviated.  */
137   CHECK ("numbered", 9);
138   CHECK ("numbere", -2);
139   CHECK ("number", -2);
140   CHECK ("numbe", -2);
141   CHECK ("numb", -2);
142   CHECK ("num", -2);
143   CHECK ("nu", -2);
144   CHECK ("n", -2);
145
146   /* Ambiguous abbreviated.  */
147   CHECK ("ne", -2);
148
149   /* Ambiguous abbreviated, but same value ("single" and "simple").  */
150   CHECK ("si", 3);
151   CHECK ("s", 3);
152 #undef CHECK
153
154   argmatch_backup_usage (stdout);
155
156   return 0;
157 }