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