root/maint/gnulib/tests/test-argmatch.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of exact or abbreviated match search.
   2    Copyright (C) 1990, 1998-1999, 2001-2021 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 <https://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 /* As of GCC 11.2.1, gcc -Wanalyzer-too-complex reports that main's
  23    use of CHECK macros expands to code that is too complicated for gcc
  24    -fanalyzer.  Suppress the resulting bogus warnings.  */
  25 #if 10 <= __GNUC__
  26 # pragma GCC diagnostic ignored "-Wanalyzer-null-argument"
  27 #endif
  28 
  29 #include "argmatch.h"
  30 
  31 #include <stdlib.h>
  32 
  33 #include "macros.h"
  34 
  35 # define N_(Msgid) (Msgid)
  36 
  37 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
  38    thus must link with a definition of that function.  Provide it here.  */
  39 #ifdef ARGMATCH_DIE_DECL
  40 
  41 _Noreturn ARGMATCH_DIE_DECL;
  42 ARGMATCH_DIE_DECL { exit (1); }
  43 
  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 ARGMATCH_DEFINE_GROUP(backup, enum backup_type)
  72 
  73 static const argmatch_backup_doc argmatch_backup_docs[] =
  74 {
  75   { "no",       N_("never make backups (even if --backup is given)") },
  76   { "numbered", N_("make numbered backups") },
  77   { "existing", N_("numbered if numbered backups exist, simple otherwise") },
  78   { "simple",   N_("always make simple backups") },
  79   { NULL, NULL }
  80 };
  81 
  82 static const argmatch_backup_arg argmatch_backup_args[] =
  83 {
  84   { "no",                no_backups },
  85   { "none",              no_backups },
  86   { "off",               no_backups },
  87   { "simple",            simple_backups },
  88   { "never",             simple_backups },
  89   { "single",            simple_backups },
  90   { "existing",          numbered_existing_backups },
  91   { "nil",               numbered_existing_backups },
  92   { "numbered-existing", numbered_existing_backups },
  93   { "numbered",          numbered_backups },
  94   { "t",                 numbered_backups },
  95   { "newstyle",          numbered_backups },
  96   { NULL, no_backups }
  97 };
  98 
  99 const argmatch_backup_group_type argmatch_backup_group =
 100 {
 101   argmatch_backup_args,
 102   argmatch_backup_docs,
 103   N_("\
 104 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
 105 The version control method may be selected via the --backup option or through\n\
 106 the VERSION_CONTROL environment variable.  Here are the values:\n"),
 107   NULL
 108 };
 109 
 110 int
 111 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113 #define CHECK(Input, Output)                                            \
 114   do {                                                                  \
 115     ASSERT (ARGMATCH (Input, backup_args, backup_vals) == Output);      \
 116     ASSERT (argmatch_backup_choice (Input) == Output);                  \
 117     if (0 <= Output)                                                    \
 118       {                                                                 \
 119         enum backup_type val                                            \
 120           = argmatch_backup_args[Output < 0 ? 0 : Output].val;          \
 121         ASSERT (*argmatch_backup_value ("test", Input) == val);         \
 122         ASSERT (*argmatch_backup_value ("test",                         \
 123                                         argmatch_backup_argument (&val)) \
 124                 == val);                                                \
 125       }                                                                 \
 126   } while (0)
 127 
 128   /* Not found.  */
 129   CHECK ("klingon", -1);
 130 
 131   /* Exact match.  */
 132   CHECK ("none", 1);
 133   CHECK ("nil", 7);
 134 
 135   /* Too long.  */
 136   CHECK ("nilpotent", -1);
 137 
 138   /* Abbreviated.  */
 139   CHECK ("simpl", 3);
 140   CHECK ("simp", 3);
 141   CHECK ("sim", 3);
 142 
 143   /* Exact match and abbreviated.  */
 144   CHECK ("numbered", 9);
 145   CHECK ("numbere", -2);
 146   CHECK ("number", -2);
 147   CHECK ("numbe", -2);
 148   CHECK ("numb", -2);
 149   CHECK ("num", -2);
 150   CHECK ("nu", -2);
 151   CHECK ("n", -2);
 152 
 153   /* Ambiguous abbreviated.  */
 154   CHECK ("ne", -2);
 155 
 156   /* Ambiguous abbreviated, but same value ("single" and "simple").  */
 157   CHECK ("si", 3);
 158   CHECK ("s", 3);
 159 #undef CHECK
 160 
 161   argmatch_backup_usage (stdout);
 162 
 163   return 0;
 164 }

/* [previous][next][first][last][top][bottom][index][help] */