Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-argmatch.c
1 /* Test of exact or abbreviated match search.
2    Copyright (C) 1990, 1998-1999, 2001-2008 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 <http://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 <stdio.h>
25 #include <stdlib.h>
26
27 #include "progname.h"
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
42    thus must link with a definition of that function.  Provide it here.  */
43 #ifdef ARGMATCH_DIE_DECL
44 ARGMATCH_DIE_DECL { exit (1); }
45 #endif
46
47 enum backup_type
48 {
49   no_backups,
50   simple_backups,
51   numbered_existing_backups,
52   numbered_backups
53 };
54
55 static const char *const backup_args[] =
56 {
57   "no", "none", "off",
58   "simple", "never", "single",
59   "existing", "nil", "numbered-existing",
60   "numbered", "t", "newstyle",
61   NULL
62 };
63
64 static const enum backup_type backup_vals[] =
65 {
66   no_backups, no_backups, no_backups,
67   simple_backups, simple_backups, simple_backups,
68   numbered_existing_backups, numbered_existing_backups, numbered_existing_backups,
69   numbered_backups, numbered_backups, numbered_backups
70 };
71
72 int
73 main (int argc, char *argv[])
74 {
75   set_program_name (argv[0]);
76
77   /* Not found.  */
78   ASSERT (ARGMATCH ("klingon", backup_args, backup_vals) == -1);
79
80   /* Exact match.  */
81   ASSERT (ARGMATCH ("none", backup_args, backup_vals) == 1);
82   ASSERT (ARGMATCH ("nil", backup_args, backup_vals) == 7);
83
84   /* Too long.  */
85   ASSERT (ARGMATCH ("nilpotent", backup_args, backup_vals) == -1);
86
87   /* Abbreviated.  */
88   ASSERT (ARGMATCH ("simpl", backup_args, backup_vals) == 3);
89   ASSERT (ARGMATCH ("simp", backup_args, backup_vals) == 3);
90   ASSERT (ARGMATCH ("sim", backup_args, backup_vals) == 3);
91
92   /* Exact match and abbreviated.  */
93   ASSERT (ARGMATCH ("numbered", backup_args, backup_vals) == 9);
94   ASSERT (ARGMATCH ("numbere", backup_args, backup_vals) == -2);
95   ASSERT (ARGMATCH ("number", backup_args, backup_vals) == -2);
96   ASSERT (ARGMATCH ("numbe", backup_args, backup_vals) == -2);
97   ASSERT (ARGMATCH ("numb", backup_args, backup_vals) == -2);
98   ASSERT (ARGMATCH ("num", backup_args, backup_vals) == -2);
99   ASSERT (ARGMATCH ("nu", backup_args, backup_vals) == -2);
100   ASSERT (ARGMATCH ("n", backup_args, backup_vals) == -2);
101
102   /* Ambiguous abbreviated.  */
103   ASSERT (ARGMATCH ("ne", backup_args, backup_vals) == -2);
104
105   /* Ambiguous abbreviated, but same value.  */
106   ASSERT (ARGMATCH ("si", backup_args, backup_vals) == 3);
107   ASSERT (ARGMATCH ("s", backup_args, backup_vals) == 3);
108
109   return 0;
110 }