Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-argmatch.c
1 /* Test of exact or abbreviated match search.
2    Copyright (C) 1990, 1998-1999, 2001-2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007, based on test code
19    by David MacKenzie <djm@gnu.ai.mit.edu>.  */
20
21 #include <config.h>
22
23 #include "argmatch.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "progname.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 enum backup_type
42 {
43   no_backups,
44   simple_backups,
45   numbered_existing_backups,
46   numbered_backups
47 };
48
49 static const char *const backup_args[] =
50 {
51   "no", "none", "off",
52   "simple", "never", "single",
53   "existing", "nil", "numbered-existing",
54   "numbered", "t", "newstyle",
55   NULL
56 };
57
58 static const enum backup_type backup_vals[] =
59 {
60   no_backups, no_backups, no_backups,
61   simple_backups, simple_backups, simple_backups,
62   numbered_existing_backups, numbered_existing_backups, numbered_existing_backups,
63   numbered_backups, numbered_backups, numbered_backups
64 };
65
66 int
67 main (int argc, char *argv[])
68 {
69   set_program_name (argv[0]);
70
71   /* Not found.  */
72   ASSERT (ARGMATCH ("klingon", backup_args, backup_vals) == -1);
73
74   /* Exact match.  */
75   ASSERT (ARGMATCH ("none", backup_args, backup_vals) == 1);
76   ASSERT (ARGMATCH ("nil", backup_args, backup_vals) == 7);
77
78   /* Too long.  */
79   ASSERT (ARGMATCH ("nilpotent", backup_args, backup_vals) == -1);
80
81   /* Abbreviated.  */
82   ASSERT (ARGMATCH ("simpl", backup_args, backup_vals) == 3);
83   ASSERT (ARGMATCH ("simp", backup_args, backup_vals) == 3);
84   ASSERT (ARGMATCH ("sim", backup_args, backup_vals) == 3);
85
86   /* Exact match and abbreviated.  */
87   ASSERT (ARGMATCH ("numbered", backup_args, backup_vals) == 9);
88   ASSERT (ARGMATCH ("numbere", backup_args, backup_vals) == -2);
89   ASSERT (ARGMATCH ("number", backup_args, backup_vals) == -2);
90   ASSERT (ARGMATCH ("numbe", backup_args, backup_vals) == -2);
91   ASSERT (ARGMATCH ("numb", backup_args, backup_vals) == -2);
92   ASSERT (ARGMATCH ("num", backup_args, backup_vals) == -2);
93   ASSERT (ARGMATCH ("nu", backup_args, backup_vals) == -2);
94   ASSERT (ARGMATCH ("n", backup_args, backup_vals) == -2);
95
96   /* Ambiguous abbreviated.  */
97   ASSERT (ARGMATCH ("ne", backup_args, backup_vals) == -2);
98
99   /* Ambiguous abbreviated, but same value.  */
100   ASSERT (ARGMATCH ("si", backup_args, backup_vals) == 3);
101   ASSERT (ARGMATCH ("s", backup_args, backup_vals) == 3);
102
103   return 0;
104 }