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