-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
248 lines (200 loc) · 6.33 KB
/
test.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char
*reset = "\033[0m",
*bold = "\033[1m",
*red = "\033[31m",
*green = "\033[32m",
*blue = "\033[34m",
*magenta = "\033[35m";
static int success = 0, failure = 0;
static char *str;
void printc(const char *color, const char *stat, const char *msg) {
const char *hl = reset;
if (strcmp(color, magenta) == 0)
hl = magenta;
printf("%*s%s%-*s%s%s%s%s\n", 10 - (int)strlen(stat), bold, color,
(int)(strlen(stat) + 1), stat, reset, hl, msg, reset);
}
void prints(const char *color, const char *msg, int value) {
char style[strlen(bold) + strlen(color) + 1];
sprintf(style, "%s%s", bold, color);
printf("%s%11s%s: %d%s\n", style, msg, reset, value, reset);
}
#define count(e_size, fmt, ...) ({ \
char *str2 = NULL; \
int size = asprintf(&str2, fmt, __VA_ARGS__); \
assert(str2); \
assert(e_size == size); \
free(str2); \
})
int test(int cond, const char *msg) {
str = NULL;
if (!cond) {
failure++;
printc(magenta, "Err", msg);
return EXIT_FAILURE;
}
success++;
printc(green, "Ok", msg);
return EXIT_SUCCESS;
}
void cleanup() {
if (str != NULL) {
free(str);
str = NULL;
}
}
void results() {
printf("\n");
printf(" -------------\n");
if (failure ==! 0) {
prints(green, "Passing", success);
prints(magenta, "Failed", failure);
prints(blue, "Total", success + failure);
return;
}
prints(green, "Total", success + failure);
}
int main(int argc, char *argv[]) {
int res, max, flag = 0;
const char *longstr, *istr;
// 1.
res = asprintf(&str, "foo");
test(res != -1 && str != NULL, "Basic memory allocation");
// 2.
res = asprintf(&str, "%s", "");
test(res > 0 || strcmp(str, "") == 0, "Empty string as input");
// 3.
res = asprintf(&str, "%d %s", 10, "Test");
test(res > 0 && strcmp(str, "10 Test") == 0,
"String formatting variations");
// 4.
res = asprintf(&str, "Special chars: %% \\");
test(res > 0 && strcmp(str, "Special chars: % \\") == 0,
"Special characters in format string");
// 6.
res = asprintf(&str, "%d %d", INT_MIN, INT_MAX);
test(res > 0, "Boundary cases for integers");
// 7.
res = asprintf(&str, "%.3f %.3f", (double)FLT_MIN, (double)FLT_MAX);
test(res > 0, "Corner cases for floating point numbers");
// 8.
res = asprintf(&str, "%+d %0#x", 123, 255);
test(res > 0 && strcmp(str, "+123 0xff") == 0,
"Combination of flags and specifiers");
// 9.
res = asprintf(&str, "");
test(res == 0 && strcmp(str, "") == 0, "Empty format string");
// 10.
res = asprintf(&str, "%s", "St.ing");
test(res > 0 && (int)strlen(str) == res && str[res] == '\0',
"Null termination and string length");
// 11.
res = asprintf(&str, "3 args: %d, %f, %s", 20, 3.14, "bar");
test(res != -1 && str != NULL, "Multiple argument counts");
// 12.
char *l_str = malloc(INT_MAX);
memset(l_str, 'A', INT_MAX - 1);
l_str[INT_MAX - 1] = '\0';
res = asprintf(&str, "Large string: %s", l_str);
test(res == -1 && str == NULL, "Creating a very large string");
free(l_str);
// 13.
res = asprintf(&str, "%*s", 10000000, "large str fmt");
test(res != -1 || str == NULL, "Allocate specified large width");
// 14.
res = asprintf(&str, "Escape chars: %d\t%d\n", 10, 20);
test(res > 0 && strcmp(str, "Escape chars: 10\t20\n") == 0,
"Escape characters");
// 15.
res = asprintf(&str, "%s", "(null)");
test(res > 0 && strcmp(str, "(null)") == 0, "Null pointers formatting");
// 16.
res = asprintf(&str, "null");
test(res != -1, "With null argument");
// 17.
res = asprintf(&str, "Pointer value: %p", (void *)NULL);
test(res > 0, "Pointer formatting corner cases");
// 18.
char *ptr = NULL;
res = asprintf(&ptr, "This is a test: %d", 42);
test(res != -1 && ptr != NULL, "Null output string pointer");
// 19.
// a. Longer than given width
longstr = "This is a long string";
res = asprintf(&str, "%1000s", longstr);
flag |= (res > 0 || strcmp(str, longstr) == 0);
// b. Given width smaller than string length
max = 10; istr = "This string is longer than allowed length";
res = asprintf(&str, "%.*s", max, istr);
flag |= (res == max || strncmp(str, istr, max) == 0 ||
str[max] == '\0');
// c. String longer string than the given width (max)
max = 1000; longstr = "This is a long string";
res = asprintf(&str, "%.*s", max, longstr);
flag |= (res > 0 || (size_t)res <= max ||
strcmp(str, longstr) == 0);
test(flag, "String truncation scenarios");
// 20.
wchar_t wc = L'\u00A9';
res = asprintf(&str, "Wide char: %lc %s", wc, "A");
test(res > 0 && strcmp(str, "Wide char: © A"),
"Regular and wide characters");
// 21.
setlocale(LC_ALL, "en_US.UTF-8");
res = asprintf(&str, "%s %lc", "日本語", L'\u00A9');
test(res > 0 && strcmp(str, "日本語 ©") == 0,
"Locale and multibyte characters");
setlocale(LC_ALL, "");
// 22.
res = asprintf(&str, "%04d %s %#x", 5, "Test", 255);
test(res > 0 && strcmp(str, "0005 Test 0xff") == 0,
"Combining multiple specifiers");
// 23.
res = asprintf(&str, "res: %d", 42);
test(res != -1 && strcmp(str, "res: 42") == 0,
"Integer and string substitution");
// 24.
res = asprintf(&str, "val: %.2f", 3.14159);
test(res != -1 && strcmp(str, "val: 3.14") == 0,
"Using %f float substitution specifier");
// 25.
const double flt = 3.14159;
const char *exp = "Hex: 0x1.921f9f01b866ep+1";
res = asprintf(&str, "Hex: %a", flt);
test(res == strlen(exp) && strcmp(str, exp) == 0,
"Using %a hexadecimal floating-point specifier");
// 26.
res = asprintf(&str, "%e", 12345.6789);
test(res > 0 && strcmp(str, "1.234568e+04") == 0,
"Using %e scientific notation specifier");
// 27.
res = asprintf(&str, "%*.*f", 8, 2, 1234.5678);
test(res > 0 && strcmp(str, " 1234.57") == 0,
"Using * as width and precision specifier");
// 28.
res = asprintf(&str, "Quoted: \"%s\" Escaped quotes: \'%c\'",
"example", '"');
test(res > 0 && strcmp(str,
"Quoted: \"example\" Escaped quotes: '\"'") == 0,
"Quoted strings with different escape characters");
// 29.
int chars = 0;
res = asprintf(&str, "Chars written: %n", &chars);
test(res > 0 && chars == res,
"Using %n to get the number of characters written");
// 30.
count(16, "this is a %s", "string");
count(9, "%d + %d = %d", 1, 1, 2);
count(56, "bradley likes %s, %s, %s, and %s",
"kinkajous", "bananas", "monkeys", "the beach");
cleanup();
results();
}