some coverity fixes.
[minix.git] / test / test45.h
blob3e21fb354c1ff7de1c9526314304d62164109cad
1 #define GLUE_HELPER(x, y) x ## _ ## y
2 #define GLUE(x, y) GLUE_HELPER(x, y)
3 #define TOSTRING(x) #x
5 static const char *GLUE(make_string, TYPE_FUNC)(TYPE value, int base)
7 static char buffer[66];
8 char *s; /* allows 64-bit base 2 value with minus and null */
9 TYPEU valuetemp;
11 /* build number string in proper base, work backwards, starting with null */
12 s = buffer + sizeof(buffer);
13 *--s = 0;
15 /* fill in the digits */
16 valuetemp = (value < 0) ? -value : value;
19 *--s = "0123456789abcdefghijklmnopqrstuvwxyz"[valuetemp % base];
20 valuetemp /= base;
21 } while (valuetemp);
23 /* add sign if needed */
24 if (value < 0)
25 *--s = '-';
27 return s;
30 static void GLUE(e, TYPE_FUNC)(int n, const char *s, TYPE result, int base)
32 /* watch out: don't overwrite the static buffer in make_string */
33 printf("Subtest %s, error %d, errno=%d, s=\"%s\", base=%d, ", TOSTRING(TYPE_FUNC), n, errno, s, base);
34 printf("result=%s\n", GLUE(make_string, TYPE_FUNC)(result, base));
35 if (errct++ > MAX_ERROR)
37 printf("Too many errors; test aborted\n");
38 exit(1);
42 static void GLUE(test_string, TYPE_FUNC)(const char *s, TYPE value, int base)
44 char *end;
45 TYPE result;
47 /* must convert the entire string, resulting in the requested value */
48 result = TYPE_FUNC(s, &end, base);
49 if (result != value) GLUE(e, TYPE_FUNC)(1, s, result, base);
50 if (*end) GLUE(e, TYPE_FUNC)(2, s, result, base);
53 static void GLUE(test_value_with_base, TYPE_FUNC)(TYPE value, int base)
55 const char *s;
57 /* convert to string, then convert back */
58 s = GLUE(make_string, TYPE_FUNC)(value, base);
59 GLUE(test_string, TYPE_FUNC)(s, value, base);
62 static void GLUE(test_value, TYPE_FUNC)(TYPE value)
64 int base;
66 /* let's get all our bases covered */
67 for (base = 2; base <= 36; base++)
68 GLUE(test_value_with_base, TYPE_FUNC)(value, base);
71 static void GLUE(test, TYPE_FUNC)(void)
73 int base, i;
74 TYPE value, valuenext;
76 /* check 0x0000.... and 0xffff.... */
77 value = 0;
78 for (i = 0; i < 0x10000; i++)
80 /* test current value */
81 GLUE(test_value, TYPE_FUNC)(value);
82 GLUE(test_value, TYPE_FUNC)(-value);
83 value++;
86 /* check 0x8000.... and 0x7fff.... */
87 value = 0;
88 value = ((~value) << 1) >> 1;
89 for (i = 0; i < 0x10000; i++)
91 /* test current value */
92 GLUE(test_value, TYPE_FUNC)(value);
93 GLUE(test_value, TYPE_FUNC)(-value);
94 value++;
97 /* check powers of possible bases */
98 for (base = 2; base <= 36; base++)
100 value = 1;
101 while (1)
103 /* test current value with offsets */
104 for (i = -36; i <= 36; i++)
106 GLUE(test_value, TYPE_FUNC)(value + i);
107 GLUE(test_value, TYPE_FUNC)(-value + i);
110 /* stop after overflow */
111 valuenext = value * base;
112 if (valuenext <= value)
113 break;
115 value = valuenext;
119 /* automatic base */
120 GLUE(test_string, TYPE_FUNC)("10", 10, 0);
121 GLUE(test_string, TYPE_FUNC)("010", 010, 0);
122 GLUE(test_string, TYPE_FUNC)("010", 010, 8);
123 GLUE(test_string, TYPE_FUNC)("0x10", 0x10, 0);
124 GLUE(test_string, TYPE_FUNC)("0X10", 0X10, 0);
125 GLUE(test_string, TYPE_FUNC)("0x10", 0x10, 16);
126 GLUE(test_string, TYPE_FUNC)("0X10", 0X10, 16);
128 /* ignore plus sign, leading spaces and zeroes */
129 GLUE(test_string, TYPE_FUNC)("10", 10, 10);
130 GLUE(test_string, TYPE_FUNC)("010", 10, 10);
131 GLUE(test_string, TYPE_FUNC)("0010", 10, 10);
132 GLUE(test_string, TYPE_FUNC)(" 10", 10, 10);
133 GLUE(test_string, TYPE_FUNC)(" 010", 10, 10);
134 GLUE(test_string, TYPE_FUNC)(" 0010", 10, 10);
135 GLUE(test_string, TYPE_FUNC)("\t10", 10, 10);
136 GLUE(test_string, TYPE_FUNC)("\t010", 10, 10);
137 GLUE(test_string, TYPE_FUNC)("\t0010", 10, 10);
138 GLUE(test_string, TYPE_FUNC)(" \t10", 10, 10);
139 GLUE(test_string, TYPE_FUNC)(" \t010", 10, 10);
140 GLUE(test_string, TYPE_FUNC)(" \t0010", 10, 10);
141 GLUE(test_string, TYPE_FUNC)("+10", 10, 10);
142 GLUE(test_string, TYPE_FUNC)("+010", 10, 10);
143 GLUE(test_string, TYPE_FUNC)("+0010", 10, 10);
144 GLUE(test_string, TYPE_FUNC)(" +10", 10, 10);
145 GLUE(test_string, TYPE_FUNC)(" +010", 10, 10);
146 GLUE(test_string, TYPE_FUNC)(" +0010", 10, 10);
147 GLUE(test_string, TYPE_FUNC)("\t+10", 10, 10);
148 GLUE(test_string, TYPE_FUNC)("\t+010", 10, 10);
149 GLUE(test_string, TYPE_FUNC)("\t+0010", 10, 10);
150 GLUE(test_string, TYPE_FUNC)(" \t+10", 10, 10);
151 GLUE(test_string, TYPE_FUNC)(" \t+010", 10, 10);
152 GLUE(test_string, TYPE_FUNC)(" \t+0010", 10, 10);
155 #undef GLUE_HELPER
156 #undef GLUE
157 #undef TOSTRING