xref: /openbsd/regress/lib/libc/gcvt/gcvt_test.c (revision 5af055cd)
1 /*	$OpenBSD: gcvt_test.c,v 1.4 2010/09/18 20:29:15 millert Exp $	*/
2 
3 /*
4  * Public domain, 2010, Todd C. Miller <Todd.Miller@courtesan.com>
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 static struct test_vector {
12 	double d;
13 	int ndig;
14 	char *expect;
15 } test_vectors[] = {
16 	/* adapted from perl's Configure test */
17 	{ 0.1, 8, "0.1" },
18 	{ 0.01, 8, "0.01" },
19 	{ 0.001, 8, "0.001" },
20 	{ 0.0001, 8, "0.0001" },
21 	{ 0.00009, 8, "9e-05" },
22 	{ 1.0, 8, "1" },
23 	{ 1.1, 8, "1.1" },
24 	{ 1.01, 8, "1.01" },
25 	{ 1.001, 8, "1.001" },
26 	{ 1.0001, 8, "1.0001" },
27 	{ 1.00001, 8, "1.00001" },
28 	{ 1.000001, 8, "1.000001" },
29 	{ 0.0, 8, "0" },
30 	{ -1.0, 8, "-1" },
31 	{ 100000.0, 8, "100000" },
32 	{ -100000.0, 8, "-100000" },
33 	{ 123.456, 8, "123.456" },
34 	{ 1e34, 8, "1e+34" },
35 	/* adapted from emx */
36 	{ 0.0, -1, "0" },
37 	{ 0.0, 0, "0" },
38 	{ 0.0, 1, "0" },
39 	{ 0.0, 2, "0" },
40 	{ 1.0, -1, "1" },
41 	{ 1.0, 0, "1" },
42 	{ 1.0, 2, "1" },
43 	{ 1.0, 10, "1" },
44 	{ 1.236, -1, "1.236" },
45 	{ 1.236, 0, "1" },
46 	{ 1.236, 1, "1" },
47 	{ 1.236, 2, "1.2" },
48 	{ 1.236, 3, "1.24" },
49 	{ 1.236, 4, "1.236" },
50 	{ 1.236, 5, "1.236" },
51 	{ 1.236, 6, "1.236" },
52 	{ 12.36, -1, "12.36" },
53 	{ 12.36, 0, "1e+01" },
54 	{ 12.36, 1, "1e+01" },
55 	{ 12.36, 2, "12" },
56 	{ 12.36, 3, "12.4" },
57 	{ 12.36, 4, "12.36" },
58 	{ 12.36, 5, "12.36" },
59 	{ 12.36, 6, "12.36" },
60 	{ 123.6, -1, "123.6" },
61 	{ 123.6, 0, "1e+02" },
62 	{ 123.6, 1, "1e+02" },
63 	{ 123.6, 2, "1.2e+02" },
64 	{ 123.6, 3, "124" },
65 	{ 123.6, 4, "123.6" },
66 	{ 123.6, 5, "123.6" },
67 	{ 123.6, 6, "123.6" },
68 	{ 1236.0, -1, "1236" },
69 	{ 1236.0, 0, "1e+03" },
70 	{ 1236.0, 1, "1e+03" },
71 	{ 1236.0, 2, "1.2e+03" },
72 	{ 1236.0, 3, "1.24e+03" },
73 	{ 1236.0, 4, "1236" },
74 	{ 1236.0, 5, "1236" },
75 	{ 1236.0, 6, "1236" },
76 	{ 1e100, 10, "1e+100" },
77 	{ 1e100, 20, "1.0000000000000000159e+100" },
78 	{ 0.01236, -1, "0.01236" },
79 	{ 0.01236, 0, "0.01" },
80 	{ 0.01236, 1, "0.01" },
81 	{ 0.01236, 2, "0.012" },
82 	{ 0.01236, 3, "0.0124" },
83 	{ 0.01236, 4, "0.01236" },
84 	{ 1e-100, 20, "1.00000000000000002e-100" },
85 	{ 1e-100, -1, "1e-100" },
86 	{ -1.2, 5, "-1.2" },
87 	{ -0.03, 5, "-0.03" },
88 	{ 0.1, 1, "0.1" },
89 	{ 0.1, 0, "0.1" },
90 	{ 0.099999, 10, "0.099999" },
91 	{ 0.99999, 10, "0.99999" },
92 };
93 
94 #define NTESTVEC (sizeof(test_vectors) / sizeof(test_vectors[0]))
95 
96 static int
97 dotest(struct test_vector *tv)
98 {
99 	char buf[256], *got;
100 
101 	got = gcvt(tv->d, tv->ndig, buf);
102 	if (strcmp(tv->expect, got) != 0) {
103 		fprintf(stderr, "%g @ %d: expected %s, got %s\n",
104 		    tv->d, tv->ndig, tv->expect, got);
105 		return 1;
106 	}
107 	return 0;
108 }
109 
110 int
111 main(int argc, char *argv[])
112 {
113 	int i, failures = 0;
114 
115 	for (i = 0; i < NTESTVEC; i++) {
116 		failures += dotest(&test_vectors[i]);
117 	}
118 
119 	return failures;
120 }
121