github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/operators.c (about) 1 #include "tests.h" 2 #include <stdio.h> 3 4 // TODO: More comprehensive operator tests 5 // https://github.com/Konstantin8105/c4go/issues/143 6 7 void empty() { ; } 8 9 int sAdd(char* opt) 10 { 11 int l = strlen(opt) + 12; 12 return l; 13 } 14 15 int sMul(char* opt) 16 { 17 int l = strlen(opt) * 12; 18 return l; 19 } 20 21 int sMin(char* opt) 22 { 23 int l = strlen(opt) - 12; 24 return l; 25 } 26 27 int sDiv(char* opt) 28 { 29 int l = strlen(opt) / 12; 30 return l; 31 } 32 33 int simple_repeat(int a) 34 { 35 return a; 36 } 37 38 double* return_null() 39 { 40 return NULL; 41 } 42 43 int f_sizeof(int i) 44 { 45 return i; 46 } 47 48 int reteg(int a) 49 { 50 int arr[5]; 51 for (int i = 0; i < 5; i++) { 52 arr[i] = i; 53 } 54 int* ptr; 55 ptr = &arr[1]; 56 (void)(ptr); 57 return *ptr = a + 1; 58 } 59 60 void view(int c) 61 { 62 printf("%d\n", c); 63 } 64 65 enum Bool { false = 0, 66 true = 1 }; 67 typedef enum Bool bool; 68 static bool valGlobBool = true; 69 static int valGlobInt = 42; 70 static double valGlobDouble = 45.0; 71 static bool restricted = true; 72 73 typedef unsigned int u00; 74 struct UUU000 { 75 u00 u[2]; 76 }; 77 78 int main() 79 { 80 plan(161);//6); 81 82 is_eq(valGlobInt, 42); 83 is_eq(valGlobDouble, 45); 84 is_eq(valGlobBool, 1); 85 is_eq(restricted, 1); 86 87 int i = 10; 88 signed char j = 1; 89 float f = 3.14159f; 90 double d = 0.0; 91 char c = 'A'; 92 93 i %= 10; 94 is_eq(i, 0); 95 96 i += 10; 97 is_eq(i, 10); 98 99 i -= 2; 100 is_eq(i, 8); 101 102 i *= 2; 103 is_eq(i, 16); 104 105 i /= 4; 106 is_eq(i, 4); 107 108 i <<= 2; 109 is_eq(i, 16); 110 111 i >>= 2; 112 is_eq(i, 4); 113 114 i ^= 0xCFCF; 115 is_eq(i, 53195); 116 117 i |= 0xFFFF; 118 is_eq(i, 65535); 119 120 i &= 0x0000; 121 is_eq(i, 0); 122 123 diag("Other types"); 124 125 f += 1.0f; 126 is_eq(f, 4.14159); 127 128 d += 1.25f; 129 is_eq(d, 1.25); 130 131 i -= 255l; 132 is_eq(i, -255); 133 134 i += 'A'; 135 is_eq(i, -190); 136 137 c += 11; 138 is_eq(c, 76); 139 140 diag("Shift with signed int"); 141 142 i = 4 << j; 143 is_eq(i, 8); 144 145 i = 8 >> j; 146 is_eq(i, 4); 147 148 i <<= j; 149 is_eq(i, 8); 150 151 i >>= j; 152 is_eq(i, 4); 153 154 diag("Operator equal for 1 variables"); 155 int x; 156 x = 42; 157 is_eq(x, 42); 158 159 diag("Operator equal for 2 variables"); 160 int y; 161 x = y = 1; 162 is_eq(x, 1); 163 is_eq(y, 1); 164 165 diag("Operator comma in initialization"); 166 int x2 = 0, y2 = 1; 167 is_eq(x2, 0); 168 is_eq(y2, 1); 169 170 diag("Operator equal for 3 variables"); 171 int a, b, c2; 172 a = b = c2 = 3; 173 is_eq(a, 3); 174 is_eq(b, 3); 175 is_eq(c2, 3); 176 177 diag("Huge comma problem for Equal operator"); 178 int q, w, e; 179 q = 7, w = q + 3, e = q + w; 180 is_eq(q, 7); 181 is_eq(w, 10); 182 is_eq(e, 17); 183 184 diag("Huge comma problem for Equal operator with Multiplication"); 185 float qF, wF, eF; 186 qF = 7., wF = qF * 3., eF = qF * wF; 187 float expectedQ = 7.; 188 float expectedW = 7. * 3.; 189 float expectedE = 7. * (7. * 3.); 190 is_eq(qF, expectedQ); 191 is_eq(wF, expectedW); 192 is_eq(eF, expectedE); 193 194 diag("Statement expressions"); 195 int s1 = ({ 2; }); 196 is_eq(s1, 2); 197 is_eq(({ int foo = s1 * 3; foo + 1; }), 7); 198 199 diag("Not allowable var name for Go"); 200 int type = 42; 201 is_eq(type, 42); 202 203 diag("Go keywords inside C code"); 204 { 205 int chan = 42; 206 is_eq(chan, 42); 207 } 208 { 209 int defer = 42; 210 is_eq(defer, 42); 211 } 212 { 213 int fallthrough = 42; 214 is_eq(fallthrough, 42); 215 } 216 { 217 int func = 42; 218 is_eq(func, 42); 219 } 220 { 221 int go = 42; 222 is_eq(go, 42); 223 } 224 { 225 int import = 42; 226 is_eq(import, 42); 227 } 228 { 229 int interface = 42; 230 is_eq(interface, 42); 231 } 232 { 233 int map = 42; 234 is_eq(map, 42); 235 } 236 { 237 int package = 42; 238 is_eq(package, 42); 239 } 240 { 241 int range = 42; 242 is_eq(range, 42); 243 } 244 { 245 int select = 42; 246 is_eq(select, 42); 247 } 248 { 249 int type = 42; 250 is_eq(type, 42); 251 } 252 { 253 int var = 42; 254 is_eq(var, 42); 255 } 256 { 257 int _ = 42; 258 is_eq(_, 42); 259 } 260 261 // checking is_eq is no need, because if "(void)(az)" not transpile, 262 // then go build return fail - value is not used 263 diag("CStyleCast <ToVoid>"); 264 { 265 char** az; 266 (void)(az); 267 } 268 { 269 double* const* az; 270 (void)(az); 271 } 272 { 273 int** az; 274 (void)(az); 275 } 276 { 277 float* volatile* az; 278 (void)(az); 279 } 280 281 diag("CStyleCast <ToVoid> with comma"); 282 { 283 unsigned int* ui; 284 (void)(empty(), ui); 285 } 286 { 287 long int* li; 288 int counter_li = 0; 289 (void)(counter_li++, empty(), li); 290 is_eq(counter_li, 1); 291 } 292 293 diag("switch with initialization"); 294 switch (0) { 295 int ii; 296 case 0: { 297 ii = 42; 298 is_eq(ii, 42); 299 } 300 case 1: { 301 ii = 50; 302 is_eq(ii, 50); 303 } 304 } 305 switch (1) { 306 int ia; 307 case 0: { 308 ia = 42; 309 is_eq(ia, 42); 310 } 311 case 1: { 312 ia = 60; 313 is_eq(ia, 60); 314 } 315 } 316 317 diag("Binary operators for definition function"); 318 is_eq(sAdd("rrr"), 15); 319 is_eq(sMul("rrr"), 36); 320 is_eq(sMin("rrrrrrrrrrrrr"), 1); 321 is_eq(sDiv("rrrrrrrrrrrr"), 1); 322 323 diag("Operators +=, -=, *= , /= ... inside []"); 324 { 325 int a[3]; 326 a[0] = 5; 327 a[1] = 9; 328 a[2] = -13; 329 int iterator = 0; 330 is_eq(a[iterator++], 5); 331 is_eq(a[iterator], 9); 332 is_eq(a[++iterator], -13); 333 is_eq(a[iterator -= 2], 5); 334 is_eq(a[iterator += 1], 9); 335 is_eq(a[(iterator = 0, iterator)], 5); 336 is_eq(simple_repeat((iterator = 42, iterator)), 42); 337 is_eq(simple_repeat((iterator = 42, ++iterator, iterator)), 43); 338 int b = 0; 339 for (iterator = 0; b++, iterator < 2; iterator++, iterator--, iterator++) { 340 pass("iterator in for"); 341 } 342 is_eq(b, 3); 343 iterator = 0; 344 if (i++ > 0) { 345 pass("i++ > 0 is pass"); 346 } 347 } 348 diag("Equals a=b=c=..."); 349 { 350 int a, b, c, d; 351 a = b = c = d = 42; 352 is_eq(a, 42); 353 is_eq(d, 42); 354 } 355 { 356 double a, b, c, d; 357 a = b = c = d = 42; 358 is_eq(a, 42); 359 is_eq(d, 42); 360 } 361 { 362 int a, b, c, d = a = b = c = 42; 363 is_eq(a, 42); 364 is_eq(d, 42); 365 } 366 { 367 double a, b, c, d = a = b = c = 42; 368 is_eq(a, 42); 369 is_eq(d, 42); 370 } 371 { 372 double a[3]; 373 a[0] = a[1] = a[2] = -13; 374 is_eq(a[0], -13); 375 is_eq(a[2], -13); 376 } 377 { 378 double a[3]; 379 a[0] = a[1] = a[2] = -13; 380 double b[3]; 381 b[0] = b[1] = b[2] = 5; 382 383 b[0] = a[0] = 42; 384 is_eq(a[0], 42); 385 is_eq(b[0], 42); 386 } 387 { 388 double v1 = 12; 389 int v2 = -6; 390 double* b = &v1; 391 int* a = &v2; 392 *b = *a = 42; 393 is_eq(*a, 42); 394 is_eq(*b, 42); 395 } 396 { 397 int yy = 0; 398 if ((yy = simple_repeat(42)) > 3) { 399 pass("ok") 400 } 401 } 402 diag("pointer in IF"); 403 double* cd; 404 if ((cd = return_null()) == NULL) { 405 pass("ok"); 406 } 407 (void)(cd); 408 409 diag("increment for char"); 410 { 411 char N = 'g'; 412 int aaa = 0; 413 if ((aaa++, N--, aaa += 3, N) == 102) { 414 pass("ok"); 415 } 416 (void)(aaa); 417 } 418 diag("Comma with operations"); 419 { 420 int x, y, z; 421 x = y = z = 1; 422 x <<= y <<= z <<= 1; 423 is_eq(x, 16); 424 is_eq(y, 4); 425 is_eq(z, 2); 426 } 427 { 428 int x, y, z; 429 x = y = z = 1000; 430 x /= y /= z /= 2; 431 is_eq(x, 500); 432 is_eq(y, 2); 433 is_eq(z, 500); 434 } 435 { 436 int x, y, z; 437 x = y = z = 3; 438 x *= y *= z *= 2; 439 is_eq(x, 54); 440 is_eq(y, 18); 441 is_eq(z, 6); 442 } 443 diag("char + bool"); 444 { 445 char prefix = 'W'; 446 char* buf = "text"; 447 char* v; 448 v = buf + (prefix != 0); 449 is_not_null(v); 450 is_streq(v, "ext"); 451 } 452 453 diag("Bitwise complement operator ~"); 454 { 455 int i = 35; 456 int o = ~(i); 457 is_eq(o, -36); 458 is_eq(~-12, 11); 459 } 460 461 diag("summ of bools"); 462 { 463 int u = 0; 464 is_true(u == 0); 465 u += (1 != 0); 466 is_true(u == 1); 467 } 468 469 diag("summ of sizeof"); 470 { 471 int x = sizeof(char); 472 is_true(x == 1); 473 x = x + sizeof(char); 474 is_true(x == 2); 475 x += sizeof(char) + sizeof(char); 476 is_true(x == 4); 477 x = sizeof(char) * 5 + sizeof(char); 478 is_true(x == 6); 479 x = f_sizeof(sizeof(int)); 480 printf("%d\n", x); 481 int y[2]; 482 y[0] = 2; 483 is_true(y[0] == 2); 484 is_true(y[sizeof(char) - 1] == 2); 485 y[1] = 5; 486 is_true(y[1] == 5); 487 is_true(y[sizeof(char)] == 5); 488 } 489 diag("function with equal in return"); 490 { 491 int a = 42; 492 a = reteg(a); 493 is_eq(a, 43); 494 } 495 diag("equal in function"); 496 { 497 int a[2]; 498 a[0] = -1; 499 a[1] = 42; 500 int b = a[0]; 501 b += reteg((*a)++); 502 is_eq(a[1], 42); 503 } 504 diag("operation Not in if"); 505 { 506 int addr = 0; 507 if (!addr++) { 508 is_eq(addr, 1); 509 } 510 } 511 diag("compare char pointer"); 512 { 513 char* b = "happy new code"; 514 is_true(&b[3] > &b[0]); 515 is_true(&b[3] == &b[3]); 516 is_true(&b[3] < &b[4]); 517 } 518 diag("kilo.c example"); 519 { 520 unsigned int flag = 100; 521 flag &= ~(2 | 256 | 1024); 522 is_eq(flag, 100); 523 } 524 diag("unary + - "); 525 { 526 int c = 90; 527 view(+c); 528 view(-c); 529 is_eq(c, 90); 530 } 531 diag("operation |= for enum"); 532 { 533 enum Sflags { 534 SGG = 0x01, 535 SGP = 0x02, 536 SGR = 0x04, 537 SGF = 0x08 538 } sflags 539 = 0; 540 is_eq(sflags, 0); 541 sflags |= SGG; 542 is_eq(sflags, 1); 543 sflags |= SGR; 544 is_eq(sflags, 5); 545 } 546 diag("equal paren"); 547 { 548 int a, b; 549 a = b = 42; 550 is_eq(a, 42); 551 is_eq(b, 42); 552 a = (b = 42); 553 is_eq(a, 42); 554 is_eq(b, 42); 555 } 556 diag("equal paren pointer"); 557 { 558 int* a; 559 int* b; 560 int val = 45; 561 a = b = &val; 562 is_eq(*a, val); 563 is_eq(*b, val); 564 val = 42; 565 a = (b = &val); 566 is_eq(*a, val); 567 is_eq(*b, val); 568 } 569 diag("equal paren u00"); 570 { 571 u00 a, b; 572 a = b = 44; 573 is_eq(a, 44); 574 is_eq(b, 44); 575 a = (b = 42); 576 is_eq(a, 42); 577 is_eq(b, 42); 578 } 579 diag("equal paren UUU000"); 580 { 581 u00 a = 150; 582 struct UUU000 bs; 583 bs.u[0] = 100; 584 a = bs.u[0] = 44; 585 is_eq(a, 44); 586 is_eq(bs.u[0], 44); 587 a = (bs.u[0] = 42); 588 is_eq(a, 42); 589 is_eq(bs.u[0], 42); 590 } 591 diag("equal paren UUU000 pointer"); 592 { 593 u00 a = 150; 594 u00 b = 100; 595 u00* pb = &b; 596 a = (*pb = 42); 597 is_eq(a, 42); 598 is_eq(b, 42); 599 600 // check more complex 601 a = (*(pb) = (45)); 602 is_eq(a, 45); 603 is_eq(b, 45); 604 } 605 diag("equal paren member"); 606 { 607 struct sd1 { 608 int* mark1; 609 }; 610 int a1 = 900; 611 struct sd1 c1; 612 int F = 76; 613 c1.mark1 = &F; 614 struct sd1* b1 = &c1; 615 a1 = (*(b1->mark1) = (43)); 616 is_eq(a1, 43); 617 is_eq(*(b1->mark1), 43); 618 } 619 diag("equal paren member 2"); 620 { 621 struct sd2 { 622 int* mark; 623 }; 624 int a = 900; 625 struct sd2 c; 626 int F = 98; 627 c.mark = &F; 628 struct sd2* b = &c; 629 a = (*(b->mark + 0) = (43)); 630 is_eq(a, 43); 631 is_eq(*(b->mark), 43); 632 } 633 634 diag("+= paren member"); 635 { 636 struct sd10 { 637 int* mark10; 638 }; 639 int a10 = 900; 640 struct sd10 c10; 641 int F = 76; 642 c10.mark10 = &F; 643 struct sd10* b10 = &c10; 644 a10 = (*(b10->mark10) += (43)); 645 is_eq(a10, 43 + 76); 646 is_eq(*(b10->mark10), 43 + 76); 647 } 648 diag("+= paren member 2"); 649 { 650 struct sd20 { 651 int* mark; 652 }; 653 int a = 900; 654 struct sd20 c; 655 int F = 98; 656 c.mark = &F; 657 struct sd20* b = &c; 658 a = (*(b->mark + 0) += (43)); 659 is_eq(a, 43 + 98); 660 is_eq(*(b->mark), 43 + 98); 661 } 662 diag("++ conv"); 663 { 664 int a = 90; 665 long b = (long)++a; 666 is_eq(a, 91); 667 is_eq(b, 91); 668 } 669 diag("chars compare"); 670 { 671 char* s = "building\x00"; 672 char* r = "build\x00"; 673 if (*r) { 674 pass("r is valid"); 675 } 676 if (*s) { 677 pass("s is valid"); 678 } 679 char qwe[4]; 680 if (*r && s + 2 < s + sizeof(qwe)) { 681 pass("expression is valid"); 682 } 683 (void)(qwe); 684 if (*r == ' ') { 685 fail("not valid comparing"); 686 } 687 if (*r == 'b') { 688 pass("valid comparing"); 689 } 690 while (*r && s + 2 < s + 4) { 691 pass("while pass") break; 692 } 693 } 694 diag("pointer with & "); 695 { 696 double a = 43; 697 double* ptr = &a; 698 int pos = (int)((unsigned long)(ptr) & (unsigned long)3l); 699 (void)pos; 700 (void)ptr; 701 } 702 // { 703 // diag("negative unsigned"); 704 // unsigned ux; 705 // ux = 1; 706 // is_eq(ux, 1); 707 // ux = -1; 708 // is_eq(ux, 4294967295); 709 // ux = -1l; 710 // is_eq(ux, 4294967295); 711 // ux = -1u; 712 // is_eq(ux, 4294967295); 713 // ux = -1ll; 714 // is_eq(ux, 4294967295); 715 // } 716 diag("simplificator"); 717 { 718 char* s = "words"; 719 int c = (unsigned char)*s++; 720 is_eq(c, 119); 721 } 722 723 done_testing(); 724 } 725 726 struct wordStr { 727 char* w; 728 }; 729 730 static const struct wordStr* wordQImpl(const char* ch, int y) 731 { 732 return NULL; 733 } 734 735 static const struct wordStr* (*const wordQ)(const char*, int) = wordQImpl; 736 737 int v2( 738 int* db, 739 unsigned mTrace, 740 int (*xTrace)(unsigned, void*, void*, void*), 741 void* pArg) 742 { 743 return 0; 744 }