summaryrefslogtreecommitdiff
path: root/minix/tests/test45.h
blob: 62ddcffb7abceb64908c77671528102b75ca46fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define GLUE_HELPER(x, y) x ## _ ## y
#define GLUE(x, y) GLUE_HELPER(x, y)
#define TOSTRING(x) #x

static const char *GLUE(make_string, TYPE_FUNC)(TYPE value, int base)
{
	static char buffer[66];
	char *s; /* allows 64-bit base 2 value with minus and null */
	TYPEU valuetemp;
	
	/* build number string in proper base, work backwards, starting with null */
	s = buffer + sizeof(buffer);
	*--s = 0;

	/* fill in the digits */
	valuetemp = (value < 0) ? -value : value;
	do
	{
		*--s = "0123456789abcdefghijklmnopqrstuvwxyz"[valuetemp % base];
		valuetemp /= base;
	} while (valuetemp);

	/* add sign if needed */
	if (value < 0)
		*--s = '-';

	return s;
}

static void GLUE(e, TYPE_FUNC)(int n, const char *s, TYPE result, int base)
{
	/* watch out: don't overwrite the static buffer in make_string */
	printf("Subtest %s, error %d, errno=%d, s=\"%s\", base=%d, ", TOSTRING(TYPE_FUNC), n, errno, s, base);
	printf("result=%s\n", GLUE(make_string, TYPE_FUNC)(result, base));
	e(7);
}

static void GLUE(test_string, TYPE_FUNC)(const char *s, TYPE value, int base)
{
	char *end;
	TYPE result;

	/* must convert the entire string, resulting in the requested value */
	result = TYPE_FUNC(s, &end, base);
	if (result != value) GLUE(e, TYPE_FUNC)(1, s, result, base);
	if (*end) GLUE(e, TYPE_FUNC)(2, s, result, base);
}

static void GLUE(test_value_with_base, TYPE_FUNC)(TYPE value, int base)
{
	const char *s;

	/* convert to string, then convert back */
	s = GLUE(make_string, TYPE_FUNC)(value, base);
	GLUE(test_string, TYPE_FUNC)(s, value, base);
}

static void GLUE(test_value, TYPE_FUNC)(TYPE value)
{
	int base;

	/* let's get all our bases covered */
	for (base = 2; base <= 36; base++)
		GLUE(test_value_with_base, TYPE_FUNC)(value, base);
}

static void GLUE(test, TYPE_FUNC)(void)
{
	int base, i;
	TYPE value, valuenext;

	/* check 0x0000.... and 0xffff.... */
	value = 0;
	for (i = 0; i < 0x10000; i++)
	{
		/* test current value */
		GLUE(test_value, TYPE_FUNC)(value);
		GLUE(test_value, TYPE_FUNC)(-value);
		value++;
	}

	/* check 0x8000.... and 0x7fff.... */
	value = 0;
	value = ((~value) << 1) >> 1;
	for (i = 0; i < 0x10000; i++)
	{
		/* test current value */
		GLUE(test_value, TYPE_FUNC)(value);
		GLUE(test_value, TYPE_FUNC)(-value);
		value++;
	}

	/* check powers of possible bases */
	for (base = 2; base <= 36; base++)
	{
		value = 1;
		while (1)
		{
			/* test current value with offsets */
			for (i = -36; i <= 36; i++)
			{
				GLUE(test_value, TYPE_FUNC)(value + i);
				GLUE(test_value, TYPE_FUNC)(-value + i);
			}

			/* stop after overflow */
			valuenext = value * base;
			if (valuenext <= value)
				break;

			value = valuenext;
		}
	}

	/* automatic base */
	GLUE(test_string, TYPE_FUNC)("10",  10,  0);
	GLUE(test_string, TYPE_FUNC)("010", 010, 0);
	GLUE(test_string, TYPE_FUNC)("010", 010, 8);
	GLUE(test_string, TYPE_FUNC)("0x10", 0x10, 0);
	GLUE(test_string, TYPE_FUNC)("0X10", 0X10, 0);
	GLUE(test_string, TYPE_FUNC)("0x10", 0x10, 16);
	GLUE(test_string, TYPE_FUNC)("0X10", 0X10, 16);

	/* ignore plus sign, leading spaces and zeroes */
	GLUE(test_string, TYPE_FUNC)("10", 10, 10);
	GLUE(test_string, TYPE_FUNC)("010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" 10", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" 010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" 0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("\t10", 10, 10);
	GLUE(test_string, TYPE_FUNC)("\t010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("\t0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" \t10", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" \t010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" \t0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("+10", 10, 10);
	GLUE(test_string, TYPE_FUNC)("+010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("+0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" +10", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" +010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" +0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("\t+10", 10, 10);
	GLUE(test_string, TYPE_FUNC)("\t+010", 10, 10);
	GLUE(test_string, TYPE_FUNC)("\t+0010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" \t+10", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" \t+010", 10, 10);
	GLUE(test_string, TYPE_FUNC)(" \t+0010", 10, 10);
}

#undef GLUE_HELPER
#undef GLUE
#undef TOSTRING