str: Make str_format_26adic() able to use lowercase.
[pspp] / tests / libpspp / str-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008, 2010, 2014 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 #include <config.h>
18
19 #include <libpspp/str.h>
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 \f
25 /* Exit with a failure code.
26    (Place a breakpoint on this function while debugging.) */
27 static void
28 check_die (void)
29 {
30   exit (EXIT_FAILURE);
31 }
32 \f
33 static void
34 check_26adic (unsigned long int number, const char *expected_string)
35 {
36   char string[8];
37   str_format_26adic (number, true, string, sizeof string);
38   if (strcmp (string, expected_string))
39     {
40       printf ("base-26 of %lu: expected \"%s\", got \"%s\"\n",
41               number, expected_string, string);
42       check_die ();
43     }
44 }
45
46 static void
47 test_format_26adic (void)
48 {
49   check_26adic (0, "");
50   check_26adic (1, "A");
51   check_26adic (2, "B");
52   check_26adic (26, "Z");
53   check_26adic (27, "AA");
54   check_26adic (28, "AB");
55   check_26adic (29, "AC");
56   check_26adic (18278, "ZZZ");
57   check_26adic (18279, "AAAA");
58   check_26adic (19010, "ABCD");
59 }
60 \f
61 /* Main program. */
62
63 struct test
64   {
65     const char *name;
66     const char *description;
67     void (*function) (void);
68   };
69
70 static const struct test tests[] =
71   {
72     {
73       "format-26adic",
74       "format 26-adic strings",
75       test_format_26adic
76     }
77   };
78
79 enum { N_TESTS = sizeof tests / sizeof *tests };
80
81 int
82 main (int argc, char *argv[])
83 {
84   int i;
85
86   if (argc != 2)
87     {
88       fprintf (stderr, "exactly one argument required; use --help for help\n");
89       return EXIT_FAILURE;
90     }
91   else if (!strcmp (argv[1], "--help"))
92     {
93       printf ("%s: test string library\n"
94               "usage: %s TEST-NAME\n"
95               "where TEST-NAME is one of the following:\n",
96               argv[0], argv[0]);
97       for (i = 0; i < N_TESTS; i++)
98         printf ("  %s\n    %s\n", tests[i].name, tests[i].description);
99       return 0;
100     }
101   else
102     {
103       for (i = 0; i < N_TESTS; i++)
104         if (!strcmp (argv[1], tests[i].name))
105           {
106             tests[i].function ();
107             return 0;
108           }
109
110       fprintf (stderr, "unknown test %s; use --help for help\n", argv[1]);
111       return EXIT_FAILURE;
112     }
113 }