• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <string.h>
3 #include <limits.h>
4 #include "test.h"
5 
6 #define TEST(r, f, x, m) ( \
7 	((r) = (f)) == (x) || \
8 	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
9 
10 #define TEST_S(s, x, m) ( \
11 	!strcmp((s),(x)) || \
12 	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
13 
14 #define TEST_F(x) ( \
15 	TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \
16 	TEST(t, d, (double)x, "%g != %g") )
17 
main(void)18 int main(void)
19 {
20 	int i;
21 	char a[100], b[100];
22 	int x, y, z, u, v;
23 	double d, t;
24 
25 	TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d");
26 	TEST_S(a, "hello,", "");
27 	TEST_S(b, "world", "");
28 
29 	TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d");
30 	TEST_S(a, "hell", "");
31 	TEST_S(b, "o,", "");
32 
33 	TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d");
34 	TEST_S(a, "hell", "");
35 	TEST_S(b, "o,", "");
36 
37 	a[8] = 'X';
38 	a[9] = 0;
39 	TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d");
40 	TEST_S(a, "hello, wX", "");
41 
42 	TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d");
43 	TEST(i, x, 56, "%d != %d");
44 	TEST(i, y, 789, "%d != %d");
45 	TEST_S(a, "56", "");
46 
47 	TEST(i, sscanf("011 0x100 11 0x100 100", "%i %i %o %x %x\n", &x, &y, &z, &u, &v), 5, "only %d fields, expected %d");
48 	TEST(i, x, 9, "%d != %d");
49 	TEST(i, y, 256, "%d != %d");
50 	TEST(i, z, 9, "%d != %d");
51 	TEST(i, u, 256, "%d != %d");
52 	TEST(i, v, 256, "%d != %d");
53 
54 	TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d");
55 	TEST(i, x, 20, "%d != %d");
56 
57 	TEST(i, sscanf("xyz", "%d %d\n", &x, &y), 0, "got %d fields, expected no match (%d)");
58 
59 	TEST(i, sscanf("", "%d %d\n", &x, &y), -1, "got %d fields, expected input failure (%d)");
60 
61 	TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d");
62 	TEST(i, x, 12, "%d != %d");
63 	TEST(i, y, 345, "%d != %d");
64 	TEST(i, z, 6, "%d != %d");
65 
66 	TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d");
67 	TEST(i, x, 0x12, "%d != %d");
68 
69 	TEST_F(123);
70 	TEST_F(123.0);
71 	TEST_F(123.0e+0);
72 	TEST_F(123.0e+4);
73 	TEST_F(1.234e1234);
74 	TEST_F(1.234e-1234);
75 	TEST_F(1.234e56789);
76 	TEST_F(1.234e-56789);
77 	TEST_F(-0.5);
78 	TEST_F(0.1);
79 	TEST_F(0.2);
80 	TEST_F(0.1e-10);
81 	TEST_F(0x1234p56);
82 
83 	TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)");
84 	TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)");
85 	return t_status;
86 }
87