aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/ansi/sscanf.c
blob: f3a881fceb55f1f87488f8658c42eec60971d831 (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
#include <stdio.h>
#include <string.h>
#include <assert.h>

struct format_test_cases {
	const char *format;
	const char *data;
	int expected_int;
	enum {
		T_INT,
		T_UINT,
		T_CHAR,
		T_NONE,
	} type;
	int ret;
} formats[] = {
	{"%i", "0x420", 0x420, T_INT, 1},
	{"%i", "0420", 0420, T_INT, 1},
	{"%i", "420", 420, T_INT, 1},
	{"%i", "-420", -420, T_INT, 1},
	{"%d", "-12345", -12345, T_INT, 1},
	{"%u", "69", 69, T_UINT, 1},
	{"%u", "0420", 420, T_UINT, 1},
	{"%o", "0420", 0420, T_UINT, 1},
	{"%x", "0xCB7", 0xCB7, T_UINT, 1},
	{"%%", "%", 0, T_NONE, 0},
	{"%c", "         I am not a fan of this solution.", ' ', T_CHAR, 1},
	{" %c", "           CBT (capybara therapy)", 'C', T_CHAR, 1},
};

#pragma GCC diagnostic ignored "-Wformat-security"

static void test_matrix() {
	for(size_t i = 0; i < (sizeof(formats) / sizeof(*formats)); i++) {
		struct format_test_cases *f = &formats[i];
		int ret = -1;
		int data_int;
		unsigned int data_uint;
		char data_char;

		switch(f->type) {
			case T_INT: {
				ret = sscanf(f->data, f->format, &data_int);
				assert(data_int == f->expected_int);
				break;
			}
			case T_UINT: {
				ret = sscanf(f->data, f->format, &data_uint);
				assert(data_uint == (unsigned int) f->expected_int);
				break;
			}
			case T_CHAR: {
				ret = sscanf(f->data, f->format, &data_char);
				assert(data_char == (unsigned char) f->expected_int);
				break;
			}
			case T_NONE: {
				ret = sscanf(f->data, f->format);
				break;
			}

		}

		assert(ret == f->ret);
	}
}

int main() {
	{
		int x = 0;
		char buf[] = "12345";
		sscanf(buf, "%d", &x);
		assert(x == 12345);
	}

	{
		char c;
		int n1;
		int n2;
		char buf[] = "z$ 7 5 440";;
		int count = sscanf(buf, "%*c%c %d %*d %d", &c, &n1, &n2);
		assert(count == 3);
		assert(c == '$');
		assert(n1 == 7);
		assert(n2 == 440);
	}

	{
		// From dsda-doom
		char buf[] = "process_priority               0\n";
		char def[80], strparm[128];
		memset(def, '!', 80);
		memset(strparm, '!', 128);
		sscanf(buf, "%s %[^\n]\n", def, strparm);
		assert(!strcmp(def, "process_priority"));
		assert(!strcmp(strparm, "0"));
	}

	{
		char buf[] = "fffff100";
		unsigned long y = 0;
		sscanf(buf, "%lx", &y);
		assert(y == 0xfffff100);
	}

#if !defined(__i386__)
	{
		char buf[] = "fffffffff100";
		unsigned long y = 0;
		sscanf(buf, "%lx", &y);
		assert(y == 0xfffffffff100);
	}
#endif

	{
		char buf[] = "410dc000";
		unsigned long y = 0;
		sscanf(buf, "%lx", &y);
		assert(y == 0x410dc000);
	}

	{
		// From webkitgtk
		char buf[] = "MemTotal:       16299664 kB\n";
		char token[51] = {0};
		size_t amount = 0;
		int ret = sscanf(buf, "%50s%zukB", token, &amount);
		assert(ret == 2);
		assert(!strcmp(token, "MemTotal:"));
		assert(amount == 16299664);
	}

	{
		char buf[] = "SIGINT";
		int sig;
		int ret = sscanf(buf, "%d", &sig);
		assert(!ret);
	}

	test_matrix();

	return 0;
}