Better ASSERT macro.
[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 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "argmatch.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "progname.h"
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 enum backup_type
44 {
45   no_backups,
46   simple_backups,
47   numbered_existing_backups,
48   numbered_backups
49 };
50
51 static const char *const backup_args[] =
52 {
53   "no", "none", "off",
54   "simple", "never", "single",
55   "existing", "nil", "numbered-existing",
56   "numbered", "t", "newstyle",
57   NULL
58 };
59
60 static const enum backup_type backup_vals[] =
61 {
62   no_backups, no_backups, no_backups,
63   simple_backups, simple_backups, simple_backups,
64   numbered_existing_backups, numbered_existing_backups, numbered_existing_backups,
65   numbered_backups, numbered_backups, numbered_backups
66 };
67
68 int
69 main (int argc, char *argv[])
70 {
71   set_program_name (argv[0]);
72
73   /* Not found.  */
74   ASSERT (ARGMATCH ("klingon", backup_args, backup_vals) == -1);
75
76   /* Exact match.  */
77   ASSERT (ARGMATCH ("none", backup_args, backup_vals) == 1);
78   ASSERT (ARGMATCH ("nil", backup_args, backup_vals) == 7);
79
80   /* Too long.  */
81   ASSERT (ARGMATCH ("nilpotent", backup_args, backup_vals) == -1);
82
83   /* Abbreviated.  */
84   ASSERT (ARGMATCH ("simpl", backup_args, backup_vals) == 3);
85   ASSERT (ARGMATCH ("simp", backup_args, backup_vals) == 3);
86   ASSERT (ARGMATCH ("sim", backup_args, backup_vals) == 3);
87
88   /* Exact match and abbreviated.  */
89   ASSERT (ARGMATCH ("numbered", backup_args, backup_vals) == 9);
90   ASSERT (ARGMATCH ("numbere", backup_args, backup_vals) == -2);
91   ASSERT (ARGMATCH ("number", backup_args, backup_vals) == -2);
92   ASSERT (ARGMATCH ("numbe", backup_args, backup_vals) == -2);
93   ASSERT (ARGMATCH ("numb", backup_args, backup_vals) == -2);
94   ASSERT (ARGMATCH ("num", backup_args, backup_vals) == -2);
95   ASSERT (ARGMATCH ("nu", backup_args, backup_vals) == -2);
96   ASSERT (ARGMATCH ("n", backup_args, backup_vals) == -2);
97
98   /* Ambiguous abbreviated.  */
99   ASSERT (ARGMATCH ("ne", backup_args, backup_vals) == -2);
100
101   /* Ambiguous abbreviated, but same value.  */
102   ASSERT (ARGMATCH ("si", backup_args, backup_vals) == 3);
103   ASSERT (ARGMATCH ("s", backup_args, backup_vals) == 3);
104
105   return 0;
106 }
107