github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/init.c (about) 1 // Initialization structs, arrays, ... 2 3 #include "tests.h" 4 #include <stdio.h> 5 6 #define START_TEST(t) \ 7 diag(#t); \ 8 test_##t(); 9 10 void test_array_float() 11 { 12 float a[] = { 2.2, 3.3, 4.4 }; 13 is_eq(a[0], 2.2); 14 is_eq(a[1], 3.3); 15 is_eq(a[2], 4.4); 16 } 17 18 void test_array_char() 19 { 20 char a[] = { 'a', 'b', 'c' }; 21 is_eq(a[0], 'a'); 22 is_eq(a[1], 'b'); 23 is_eq(a[2], 'c'); 24 } 25 26 void test_struct_init() 27 { 28 struct s { 29 int i; 30 char c; 31 }; 32 33 struct s a[] = { { 1, 'a' }, { 2, 'b' } }; 34 is_eq(a[0].i, 1); 35 is_eq(a[0].c, 'a'); 36 is_eq(a[1].i, 2); 37 is_eq(a[1].c, 'b'); 38 39 struct s c[2][3] = { { { 1, 'a' }, { 2, 'b' }, { 3, 'c' } }, { { 4, 'd' }, { 5, 'e' }, { 6, 'f' } } }; 40 is_eq(c[1][1].i, 5); 41 is_eq(c[1][1].c, 'e'); 42 } 43 44 void test_matrix_double() 45 { 46 int a[2][3] = { { 5, 6, 7 }, { 50, 60, 70 } }; 47 is_eq(a[1][2], 70); 48 } 49 50 static char* kmap_fa[256] = { 51 [0] = "fa", 52 ['`'] = "@", 53 ['1'] = "abc", 54 }; 55 void test_equals_chars() 56 { 57 is_streq(kmap_fa[0], "fa"); 58 is_streq(kmap_fa['`'], "@"); 59 is_streq(kmap_fa['1'], "abc"); 60 for (int i = 0; i < 256; i++) { 61 kmap_fa[i] = "Y"; 62 } 63 } 64 65 static char* di[][2] = { 66 { "cq", ";" }, 67 { "pl", "+" }, 68 { "hy", "-" }, 69 { "sl", "/" }, 70 }; 71 void test_di() 72 { 73 is_streq(di[0][0], "cq"); 74 is_streq(di[1][0], "pl"); 75 is_streq(di[2][0], "hy"); 76 is_streq(di[3][0], "sl"); 77 is_streq(di[0][1], ";"); 78 is_streq(di[1][1], "+"); 79 is_streq(di[2][1], "-"); 80 is_streq(di[3][1], "/"); 81 } 82 83 int ec_print(char* s) 84 { 85 printf("%s\n", s); 86 return 32; 87 } 88 int ec_insert(char* s) 89 { 90 printf("%s\n", s); 91 return 42; 92 } 93 static struct excmd { 94 char* abbr; 95 char* name; 96 int (*ec)(char* s); 97 } excmds[] = { 98 { "p", "print", ec_print }, 99 { "a", "append", ec_insert }, 100 {}, 101 }; 102 void test_ex() 103 { 104 is_streq(excmds[0].abbr, "p") 105 is_streq(excmds[0].name, "print") 106 is_eq(excmds[0].ec("0"), 32); 107 108 is_streq(excmds[1].abbr, "a") 109 is_streq(excmds[1].name, "append") 110 is_eq(excmds[1].ec("1"), 42); 111 112 is_true(excmds[2].abbr == NULL); 113 is_true(excmds[2].name == NULL); 114 is_true(excmds[2].ec == NULL); 115 } 116 117 #define test_part_array_pointer(type) \ 118 { \ 119 diag("test_part_array_pointer"); \ 120 diag(#type); \ 121 type a0 = 1; \ 122 type a1 = 3; \ 123 type aZ = 4; \ 124 type* d[5] = { &a0, &a1 }; \ 125 is_eq(*d[0], a0); \ 126 is_eq(*d[1], a1); \ 127 is_true(d[4] == NULL); \ 128 for (int i = 0; i < 5; i++) { \ 129 d[i] = &aZ; \ 130 } \ 131 is_eq(*d[0], aZ); \ 132 is_eq(*d[1], aZ); \ 133 } 134 135 #define test_part_array(type) \ 136 { \ 137 diag("test_part_array"); \ 138 diag(#type); \ 139 type d[5] = { 1, 3 }; \ 140 is_eq(d[0], 1); \ 141 is_eq(d[1], 3); \ 142 for (int i = 0; i < 5; i++) { \ 143 d[i] = (float)i; \ 144 } \ 145 is_eq(d[0], 0); \ 146 is_eq(d[1], 1); \ 147 } 148 149 void test_partly() 150 { 151 // Test partly initialization of array 152 test_part_array(char); 153 test_part_array(double); 154 test_part_array(float); 155 test_part_array(int); 156 test_part_array(long double); 157 test_part_array(long long); 158 test_part_array(signed char); 159 test_part_array(unsigned long); 160 161 // Test partly initialization of array pointer 162 test_part_array_pointer(char); 163 test_part_array_pointer(double); 164 test_part_array_pointer(float); 165 test_part_array_pointer(int); 166 test_part_array_pointer(long double); 167 test_part_array_pointer(long long); 168 test_part_array_pointer(signed char); 169 test_part_array_pointer(unsigned long); 170 } 171 172 void test_FILE() 173 { 174 FILE* F[3] = { stderr }; 175 is_true(F[0] == stderr); 176 for (int i = 0; i < 3; i++) { 177 F[i] = stdout; 178 } 179 is_true(F[1] == stdout); 180 } 181 182 void test_void() 183 { 184 void* v[3] = { stderr }; 185 is_true((FILE*)(v[0]) == stderr); 186 double r = 42; 187 for (int i = 0; i < 3; i++) { 188 v[i] = &r; 189 } 190 is_eq(*(double*)(v[1]), r); 191 } 192 193 static int xai, xaw; 194 static struct option { 195 char* abbr; 196 char* name; 197 int* vars; 198 } options[] = { 199 { "ai", "autoindent", &xai }, 200 { "aw", "autowrite", &xaw }, 201 {}, 202 }; 203 void test_options() 204 { 205 is_streq(options[0].abbr, "ai"); 206 is_streq(options[0].name, "autoindent"); 207 is_not_null(options[0].vars); 208 209 is_streq(options[1].abbr, "aw"); 210 is_streq(options[1].name, "autowrite"); 211 is_not_null(options[1].vars); 212 213 is_true(options[2].abbr == NULL); 214 is_true(options[2].name == NULL); 215 is_true(options[2].vars == NULL); 216 } 217 218 static struct hig { 219 char* ft; 220 int att[16]; 221 char* pat; 222 int end; 223 } higs[] = { 224 {}, 225 { "c", { 5 }, "q" }, 226 { "2", { 4 }, "w" }, 227 {}, 228 }; 229 void test_hig() 230 { 231 is_true(higs[0].ft == NULL); 232 // TODO : is_true (higs[0].att[0] == NULL); 233 is_true(higs[0].pat == NULL); 234 235 is_streq(higs[1].ft, "c"); 236 is_eq(higs[1].att[0], 5); 237 is_streq(higs[1].pat, "q"); 238 239 is_streq(higs[2].ft, "2"); 240 is_eq(higs[2].att[0], 4); 241 is_streq(higs[2].pat, "w"); 242 243 is_true(higs[3].ft == NULL); 244 // TODO : is_true (higs[3].att[0] == NULL); 245 is_true(higs[3].pat == NULL); 246 } 247 248 typedef struct hig hug; 249 void test_hug() 250 { 251 hug hugs[] = { 252 { "c", { 5 }, "q" }, 253 { "2", { 4 }, "w" }, 254 }; 255 is_streq(hugs[0].ft, "c"); 256 is_eq(hugs[0].att[0], 5); 257 is_streq(hugs[0].pat, "q"); 258 259 is_streq(hugs[1].ft, "2"); 260 is_eq(hugs[1].att[0], 4); 261 is_streq(hugs[1].pat, "w"); 262 } 263 264 struct poz { 265 char y[5]; 266 char* c; 267 double d[2]; 268 struct poz* ppt; 269 struct hig parr[3]; 270 hug h[9]; 271 } pozes[] = { 272 { "dream", "home", { 1, 2 } }, 273 { "hold", "a", { 0, 42 } }, 274 {}, 275 }; 276 void test_poz() 277 { 278 is_streq(pozes[0].y, "dream"); 279 is_streq(pozes[0].c, "home"); 280 is_eq(pozes[0].d[0], 1); 281 is_eq(pozes[0].d[1], 2); 282 283 is_streq(pozes[1].y, "hold"); 284 is_streq(pozes[1].c, "a"); 285 is_eq(pozes[1].d[0], 0); 286 is_eq(pozes[1].d[1], 42); 287 288 is_not_null(&pozes[2]); 289 } 290 291 void test_ab() 292 { 293 char* num[3] = { { "123" }, { "987" }, { "456" } }; 294 is_streq(num[0], "123"); 295 is_streq(num[1], "987"); 296 is_streq(num[2], "456"); 297 } 298 299 void test_vti() 300 { 301 int aqq[1][1] = { { 5 } }; 302 is_eq(aqq[0][0], 5); 303 } 304 305 void test_bm() 306 { 307 char brac[4][3][2] = { { "1", "2" } }; 308 is_streq(brac[0][1], "2"); 309 } 310 311 int main() 312 { 313 plan(148); 314 315 START_TEST(partly); 316 317 // Test partly initialization of FILE 318 START_TEST(FILE) 319 320 // Test partly initialization of void 321 START_TEST(void) 322 323 START_TEST(array_float); 324 START_TEST(array_char); 325 START_TEST(struct_init); 326 START_TEST(matrix_double); 327 START_TEST(equals_chars); 328 START_TEST(di); 329 START_TEST(options); 330 START_TEST(ex); 331 START_TEST(hig); 332 START_TEST(hug); 333 START_TEST(poz); 334 START_TEST(ab); 335 START_TEST(vti); 336 START_TEST(bm); 337 338 done_testing(); 339 }