github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/array.c (about) 1 // Array examples 2 3 #include "tests.h" 4 #include <stdlib.h> 5 6 #define START_TEST(t) \ 7 diag(#t); \ 8 test_##t(); 9 10 void test_intarr() 11 { 12 int a[3]; 13 a[0] = 5; 14 a[1] = 9; 15 a[2] = -13; 16 17 is_eq(a[0], 5); 18 is_eq(a[1], 9); 19 is_eq(a[2], -13); 20 } 21 22 void test_doublearr() 23 { 24 double a[2]; 25 a[0] = 1.2; 26 a[1] = 7; // different type 27 28 is_eq(a[0], 1.2); 29 is_eq(a[1], 7.0); 30 } 31 32 void test_intarr_init() 33 { 34 int a[] = { 10, 20, 30 }; 35 is_eq(a[0], 10); 36 is_eq(a[1], 20); 37 is_eq(a[2], 30); 38 } 39 40 void test_floatarr_init() 41 { 42 float a[] = { 2.2, 3.3, 4.4 }; 43 is_eq(a[0], 2.2); 44 is_eq(a[1], 3.3); 45 is_eq(a[2], 4.4); 46 } 47 48 void test_chararr_init() 49 { 50 char a[] = { 97, 98, 99 }; 51 is_eq(a[0], 'a'); 52 is_eq(a[1], 'b'); 53 is_eq(a[2], 'c'); 54 } 55 56 void test_chararr_init2() 57 { 58 char a[] = { 'a', 'b', 'c' }; 59 is_eq(a[0], 'a'); 60 is_eq(a[1], 'b'); 61 is_eq(a[2], 'c'); 62 } 63 64 void test_exprarr() 65 { 66 int a[] = { 2 ^ 1, 3 & 1, 4 | 1, (5 + 1) / 2 }; 67 is_eq(a[0], 3); 68 is_eq(a[1], 1); 69 is_eq(a[2], 5); 70 is_eq(a[3], 3); 71 } 72 73 struct s { 74 int i; 75 char c; 76 }; 77 78 void test_structarr() 79 { 80 struct s a[] = { { 1, 'a' }, { 2, 'b' } }; 81 is_eq(a[0].i, 1); 82 is_eq(a[0].c, 'a'); 83 is_eq(a[1].i, 2); 84 is_eq(a[1].c, 'b'); 85 86 struct s b[] = { (struct s){ 1, 'a' }, (struct s){ 2, 'b' } }; 87 is_eq(b[0].i, 1); 88 is_eq(b[0].c, 'a'); 89 is_eq(b[1].i, 2); 90 is_eq(b[1].c, 'b'); 91 } 92 93 long dummy(char foo[42]) 94 { 95 return sizeof(foo); 96 } 97 98 void test_argarr() 99 { 100 char abc[1]; 101 is_eq(8, dummy(abc)); 102 } 103 104 void test_multidim() 105 { 106 int a[2][3] = { { 5, 6, 7 }, { 50, 60, 70 } }; 107 is_eq(a[1][2], 70); 108 109 // omit array length 110 int b[][3][2] = { { { 1, 2 }, { 3, 4 }, { 5, 6 } }, 111 { { 6, 5 }, { 4, 3 }, { 2, 1 } } }; 112 is_eq(b[1][1][0], 4); 113 // 2 * 3 * 2 * sizeof(int32) 114 is_eq(sizeof(b), 48); 115 116 struct s c[2][3] = { { { 1, 'a' }, { 2, 'b' }, { 3, 'c' } }, { { 4, 'd' }, { 5, 'e' }, { 6, 'f' } } }; 117 is_eq(c[1][1].i, 5); 118 is_eq(c[1][1].c, 'e'); 119 c[1][1] = c[0][0]; 120 is_eq(c[1][1].i, 1); 121 is_eq(c[1][1].c, 'a'); 122 } 123 124 void test_ptrarr() 125 { 126 int b = 22; 127 128 int* d[3]; 129 d[1] = &b; 130 is_eq(*(d[1]), 22); 131 132 int** e[4]; 133 e[0] = d; 134 is_eq(*(e[0][1]), 22); 135 } 136 137 void test_stringarr_init() 138 { 139 char* a[] = { "a", "bc", "def" }; 140 is_streq(a[0], "a"); 141 is_streq(a[1], "bc"); 142 is_streq(a[2], "def"); 143 } 144 145 void test_partialarr_init() 146 { 147 // Last 2 values are filled with zeros 148 double a[4] = { 1.1, 2.2 }; 149 is_eq(a[2], 0.0); 150 is_eq(a[3], 0.0); 151 152 struct s b[3] = { { 97, 'a' } }; 153 is_eq(b[0].i, 97); 154 is_eq(b[2].i, 0); 155 is_eq(b[2].c, 0); 156 } 157 158 extern int arrayEx[]; 159 int arrayEx[4] = { 1, 2, 3, 4 }; 160 161 int ff() { return 3; } 162 163 double rep_double(double a) 164 { 165 return a; 166 } 167 168 int rep_int(int a) 169 { 170 return a; 171 } 172 173 void zero(int* a, int* b, int* c) 174 { 175 *a = *b = *c = 0; 176 } 177 178 float* next_pointer(float* v) 179 { 180 long l = 1; 181 long p = 2; 182 (void)(l); 183 (void)(p); 184 return p - p + v + l; 185 } 186 187 double* dvector(long nl, long nh) 188 { 189 double* v; 190 v = (double*)malloc((size_t)((nh - nl + 1 + 1) * sizeof(double))); 191 for (int i = 0; i < nh - nl; i++) { 192 *(v + i) = 42.0; 193 } 194 return v - nl + 1; 195 } 196 197 typedef struct s structs; 198 void test_pointer_arith_size_t() 199 { 200 size_t size = 1; 201 char* left_ptr; 202 char arr[3]; 203 arr[0] = 'a'; 204 arr[1] = 'b'; 205 arr[2] = 'c'; 206 left_ptr = &arr; 207 is_eq(*left_ptr, arr[0]); 208 left_ptr = left_ptr + size; 209 is_eq(*left_ptr, arr[1]); 210 left_ptr += size; 211 is_eq(*left_ptr, arr[2]); 212 213 // tests for pointer to struct with size > 1 214 structs a[] = { { 1, 'a' }, { 2, 'b' }, { 3, 'c' } }; 215 is_eq(a[0].i, 1); 216 is_eq(a[0].c, 'a'); 217 is_eq(a[1].i, 2); 218 is_eq(a[1].c, 'b'); 219 is_eq(a[2].i, 3); 220 is_eq(a[2].c, 'c'); 221 structs* ps = &a; 222 structs* ps2; 223 is_eq(ps->i, 1); 224 ps2 = ps + size; 225 is_eq(ps2->i, 2); 226 ps2 += size; 227 is_eq(ps2->i, 3); 228 ps2 -= size; 229 is_eq(ps2->i, 2); 230 } 231 232 void test_pointer_minus_pointer() 233 { 234 { 235 diag("char type"); 236 char* left_ptr; 237 char* right_ptr; 238 char arr[300]; 239 left_ptr = &arr[0]; 240 right_ptr = &arr[200]; 241 is_eq(right_ptr - left_ptr, 200); 242 } 243 { 244 diag("long long type"); 245 long long* left_ptr; 246 long long* right_ptr; 247 long long arr[300]; 248 left_ptr = &arr[0]; 249 right_ptr = &arr[200]; 250 is_eq(right_ptr - left_ptr, 200); 251 } 252 } 253 254 typedef unsigned char pcre_uchar; 255 typedef unsigned char pcre_uint8; 256 typedef unsigned short pcre_uint16; 257 typedef unsigned int pcre_uint32; 258 259 #define PT_ANY 0 /* Any property - matches all chars */ 260 #define PT_SC 4 /* Script (e.g. Han) */ 261 262 #define CHAR_B 'b' 263 264 void test_array_to_value_int() 265 { 266 int aqq[1][1] = { { 5 } }; 267 int** pz; 268 int c; 269 /////////////// 270 pz = aqq; 271 c = 99999; 272 c = *pz; 273 is_eq(c, 5.0); 274 /////////////// 275 (void)pz; 276 (void)c; 277 /////////////// 278 } 279 280 void test_array_to_value_unsigned() 281 { 282 unsigned aqq[1][1] = { { 5 } }; 283 unsigned** pz; 284 unsigned c; 285 /////////////// 286 pz = aqq; 287 c = 99999; 288 c = *pz; 289 is_eq(c, 5.0); 290 /////////////// 291 (void)pz; 292 (void)c; 293 /////////////// 294 } 295 296 void test_array_to_value_long() 297 { 298 long aqq[1][1] = { { 5 } }; 299 long** pz; 300 long c; 301 /////////////// 302 pz = aqq; 303 c = 99999; 304 c = *pz; 305 is_eq(c, 5.0); 306 /////////////// 307 (void)pz; 308 (void)c; 309 /////////////// 310 } 311 312 void test_array_to_value_char() 313 { 314 long aqq[1][1] = { { 5 } }; 315 long** pz; 316 long c; 317 /////////////// 318 pz = aqq; 319 c = 99; 320 c = *pz; 321 is_eq(c, 5.0); 322 /////////////// 323 (void)pz; 324 (void)c; 325 /////////////// 326 } 327 328 void test_array_to_value_double() 329 { 330 long aqq[1][1] = { { 5 } }; 331 long** pz; 332 long c; 333 /////////////// 334 pz = aqq; 335 c = 99999; 336 c = *pz; 337 is_eq(c, 5.0); 338 /////////////// 339 (void)pz; 340 (void)c; 341 /////////////// 342 } 343 344 #define SIZE 3 345 void test_size_pointer() 346 { 347 int A[2][SIZE] = { { 10, 20, 30 }, { 40, 50, 60 } }; 348 int B[4][SIZE] = { { 0, 1, 2 }, { 3, 0, 4 }, { 5, 6, 0 }, { 7, 8, 9 } }; 349 int(*pnt)[SIZE]; 350 pnt = A; 351 is_eq(pnt[1][2], A[1][2]); 352 is_eq(pnt[0][2], A[0][2]); 353 pnt = B; 354 is_eq(pnt[1][2], B[1][2]); 355 is_eq(pnt[0][2], B[0][2]); 356 } 357 358 #define STACK_SIZE 512 359 #define STACK_PUSH(A) (STACK[SP++] = A) 360 static unsigned short STACK[STACK_SIZE]; 361 static unsigned int SP = 0; 362 void test_array_increment() 363 { 364 double a[10]; 365 for (int i = 0; i < 10; i++) 366 a[i] = i; 367 is_eq(a[4], 4); 368 int pc = 4; 369 is_eq(pc, 4); 370 is_eq(a[pc++], 4); 371 is_eq(pc, 5); 372 unsigned short pc2 = 0; 373 STACK_PUSH(pc2); 374 is_eq(SP, 1); 375 } 376 377 struct MyStruct { 378 int number; 379 char symbol; 380 }; 381 void test_struct_init() 382 { 383 struct MyStruct objA = { .symbol = 'A', .number = 100 }; 384 struct MyStruct objB = { .number = 200 }; 385 is_eq(objA.symbol, 'A'); 386 is_eq(objA.number, 100); 387 is_eq(objB.number, 200); 388 } 389 390 struct parg_option { 391 const char* name; 392 int has_arg; 393 int* flag; 394 int val; 395 }; 396 397 static const struct parg_option po_def[] = { 398 { "noarg", 1, NULL, 'n' }, 399 { "optarg", 2, NULL, 'o' }, 400 { "reqarg", 3, NULL, 'r' }, 401 { "foo", 4, NULL, 'f' }, 402 { "foobar", 5, NULL, 'b' }, 403 { 0, 6, 0, 0 } 404 }; 405 406 void test_parg_struct() 407 { 408 is_eq(po_def[0].has_arg, 1); 409 is_eq(po_def[5].has_arg, 6); 410 is_streq(po_def[1].name, "optarg"); 411 } 412 413 int function_array_field(int a) 414 { 415 return a + 1; 416 } 417 void test_function_array() 418 { 419 struct fa { 420 int (*pf)(int); 421 }; 422 struct fa f[10]; 423 int i = 0; 424 for (i = 0; i < 10; i++) { 425 f[i].pf = function_array_field; 426 } 427 int y = 42; 428 for (i = 0; i < 10; i++) { 429 y = ((f[i]).pf)(y); 430 } 431 is_eq(y, 52); 432 } 433 434 void test_string_array() 435 { 436 { 437 diag("point 0"); 438 struct line_t { 439 struct line_t* last; 440 struct line_t* next; 441 int pos; 442 }; 443 struct line_t l1; 444 l1.last = NULL; 445 l1.next = NULL; 446 struct line_t l2; 447 l2.last = &l1; 448 l2.next = NULL; 449 is_true(l2.last == &l1); 450 } 451 { 452 diag("point 1"); 453 char ch_arr[3][10] = { "spike", "tom", "jerry" }; 454 printf("%s\n", (*(ch_arr + 0) + 0)); 455 printf("%s\n", (*(ch_arr + 0) + 1)); 456 printf("%s\n", (*(ch_arr + 1) + 2)); 457 } 458 // TODO 459 // { 460 // diag("point 2"); 461 // // see https://stackoverflow.com/questions/6812242/defining-and-iterating-through-array-of-strings-in-c 462 // char *numbers[] = {"One", "Two", "Three", ""}, **n; 463 // n = numbers; 464 // while (*n != "") { 465 // printf ("%s\n", *n++); 466 // } 467 // } 468 { 469 diag("point 3"); 470 // see https://stackoverflow.com/questions/6812242/defining-and-iterating-through-array-of-strings-in-c 471 static const char* strings[] = { "asdf", "asdfasdf", 0 }; 472 const char** ptr = strings; 473 while (*ptr != 0) { 474 printf("%s \n", *ptr); 475 ++ptr; 476 } 477 } 478 { 479 diag("point 4"); 480 // see https://codereview.stackexchange.com/questions/71119/printing-the-contents-of-a-string-array-using-pointers 481 char* names[] = { "John", "Mona", "Lisa", "Frank" }; 482 for (int i = 0; i < 4; ++i) { 483 char* pos = names[i]; 484 while (*pos != '\0') { 485 printf("%c", *(pos++)); 486 } 487 printf("\n"); 488 } 489 } 490 { 491 diag("point 5"); 492 const char* names[] = { "John", "Mona", "Lisa", "Frank", NULL }; 493 for (int i = 0; names[i]; ++i) { 494 const char* ch = names[i]; 495 while (*ch) { 496 putchar(*ch++); 497 } 498 putchar('\n'); 499 } 500 } 501 { 502 diag("point 6"); 503 const char* names[] = { "John", "Mona", "Lisa", "Frank", NULL }; 504 for (const char** pNames = names; *pNames; pNames++) { 505 const char* pName = *pNames; 506 while (*pName) { 507 putchar(*pName++); 508 } 509 putchar('\n'); 510 } 511 } 512 { 513 diag("point 7"); 514 char* names[] = { "John", "Mona", "Lisa", "Frank" }; 515 int elements = sizeof(names) / sizeof(names[0]); 516 for (int i = 0; i < elements; i++) { 517 char* p = names[i]; 518 while (*p) 519 putchar(*p++); 520 putchar('\n'); 521 } 522 } 523 { 524 diag("point 8"); 525 int array[] = { 5, 2, 9, 7, 15 }; 526 int i = 0; 527 array[i]++; 528 printf("%d %d\n", i, array[i]); 529 array[i]++; 530 printf("%d %d\n", i, array[i]); 531 array[i++]; 532 printf("%d %d\n", i, array[i]); 533 array[i++]; 534 printf("%d %d\n", i, array[i]); 535 } 536 } 537 538 void test_typedef_pointer() 539 { 540 typedef double* pd; 541 double v[2] = { 42., -42. }; 542 { 543 diag("typedef_pointer : 1"); 544 pd p = &v[0]; 545 p++; 546 is_eq(*p, v[1]); 547 } 548 { 549 diag("typedef_pointer : 2"); 550 pd p = v; 551 p += 1; 552 is_eq(*p, v[1]); 553 } 554 { 555 diag("typedef_pointer : 3"); 556 pd p = &v[1] - 1; 557 p = 0 + p + 0 + 1; 558 is_eq(*p, v[1]); 559 } 560 { 561 diag("typedef_pointer : 4"); 562 pd p = 0 + v + 1 + 0 - 1; // v[0] 563 p = 0 + p + 0 + 1; // TODO - 0 + 1 - 1; // p = p + 1 564 is_eq(*p, v[1]); 565 } 566 { 567 diag("typedef pointer char"); 568 typedef char* tcp; 569 char* word = "Hello"; 570 tcp w = word; 571 is_streq(w, "Hello"); 572 tcp p = w + 2; 573 is_streq(p, "llo"); 574 p = &(p[-1]); 575 is_streq(p, "ello"); 576 p = w + 2; 577 p = p - 1; 578 is_streq(p, "ello"); 579 is_eq((int)(p[-1]), (int)('H')); 580 581 unsigned char ch = p[-1]; 582 is_eq(ch, 'H'); 583 } 584 { 585 diag("typedef pointer const char"); 586 typedef char* tcp3; 587 char const* word = "Hello"; 588 tcp3 w = word; 589 is_streq(w, "Hello"); 590 { 591 tcp3 p = w + 2; 592 is_streq(p, "llo"); 593 (void)(p); 594 } 595 { 596 tcp3 const p = w + 2; 597 is_streq(&(p[-1]), "ello"); 598 (void)(p); 599 } 600 { 601 tcp3 const p = w + 2 - 1; 602 is_streq(p, "ello"); 603 is_eq((int)(p[-1]), (int)('H')); 604 (void)(p); 605 } 606 { 607 tcp3 p = w + 2 - 1; 608 unsigned char ch = p[-1]; 609 is_eq(ch, 'H'); 610 (void)(p); 611 } 612 } 613 { 614 diag("typedef pointer short"); 615 typedef short* tcp2; 616 short word[] = { 12, 13, 24, 45, 11 }; 617 tcp2 w = word; 618 tcp2 p = w + 2; 619 is_eq(*p, 24); 620 p = &(p[-1]); 621 is_eq(*p, 13); 622 p = w + 2; 623 p = p - 1; 624 is_eq(*p, 13); 625 is_eq((int)(p[-1]), (int)(12)); 626 627 unsigned char ch = p[-1]; 628 is_eq(ch, 12); 629 } 630 } 631 632 // TODO : it is not Ok for Debug case 633 void test_double_array() 634 { 635 // see https://forums.macrumors.com/threads/understanding-double-pointers-in-c.701091/ 636 int twod[5][5] = { { 2, 4, 6, 7, 77 }, { 8, 10, 12, 13, 133 }, { 14, 16, 18, 19, 199 }, { 20, 22, 24, 25, 255 }, { 26, 28, 30, 32, 322 } }; 637 printf("%d\n", twod[0][0]); 638 int* pp; 639 pp = twod[0]; 640 int** p = &pp; 641 (void)(p); 642 printf("%d\n", p[0][0]); 643 644 printf(" p is: %d\n", **p); 645 printf("*p + 1 is: %d\n", *(*p + 1)); 646 { 647 diag("cases 1:"); 648 int* pp = *p; 649 printf(" 1: %d\n", *(pp++)); 650 printf(" 2: %d\n", *(pp++)); 651 printf(" 3: %d\n", *(pp++)); 652 } 653 // TODO : view_matrix(p,4,3); 654 655 p = &pp; 656 { 657 diag("cases 1a:"); 658 int* pp = *p; 659 printf(" 1: %d\n", (*pp)++); 660 printf(" 2: %d\n", (*pp)++); 661 printf(" 3: %d\n", (*pp)++); 662 } 663 // TODO : view_matrix(p,4,3); 664 665 p = &pp; 666 { 667 diag("cases 2:"); 668 int** pp = p; 669 printf(" 1: %d\n", *((*(pp))++)); 670 printf(" 2: %d\n", *((*(pp))++)); 671 printf(" 3: %d\n", *((*(pp))++)); 672 } 673 // TODO : view_matrix(p,4,3); 674 675 p = &pp; 676 // { 677 // diag("cases 3:"); 678 // int** pp = p; 679 // printf(" 1: %d\n", (*pp)[0]); 680 // printf(" 2: %d\n", (*pp)[1]); 681 // printf(" 3: %d\n", (*pp)[2]); 682 // } 683 // TODO : view_matrix(p,4,3); 684 685 p = &pp; 686 // { 687 // diag("cases 4:"); 688 // int** pp = p; 689 // printf(" 1: %d\n", *(*((pp)++))); 690 // printf(" 2: %d\n", *(*((pp)++))); 691 // printf(" 3: %d\n", *(*((pp)++))); 692 // } 693 // TODO : view_matrix(p,4,3); 694 695 p = &pp; 696 // { 697 // diag("cases 5:"); 698 // int** pp = p; 699 // printf(" 1: %d\n", *((*pp)++)); 700 // printf(" 2: %d\n", *((*pp)++)); 701 // printf(" 3: %d\n", *((*pp)++)); 702 // } 703 // TODO : view_matrix(p,4,3); 704 705 p = &pp; 706 // { 707 // diag("cases 6:"); 708 // int** pp = p; 709 // printf(" 1: %d\n", pp[0][0]); 710 // printf(" 2: %d\n", pp[0][1]); 711 // printf(" 3: %d\n", pp[0][2]); 712 // } 713 // TODO : view_matrix(p,4,3); 714 } 715 716 static void trans(char* p) 717 { 718 printf("trans = `%s`\n", p); 719 } 720 721 void test_func_byte() 722 { 723 char* const gameOver = "game over"; 724 trans(gameOver); 725 char* gameOver2 = "game over"; 726 trans(gameOver2); 727 } 728 729 void test_negative_index() 730 { 731 double ad[5] = { 1., 2., 4., 5., 6.0 }; 732 is_eq(ad[0], 1.0); 733 double* p = ad; 734 p += 3; 735 is_eq(*p, 5.0); 736 is_eq(p[-1], 4.0); 737 double* ds = &(p[-1]); 738 is_eq(ds[-1], 2.0); 739 } 740 741 void test_matrix_init() 742 { 743 int rows = 2; 744 int cols = 3; 745 int i, j; 746 double** m; 747 748 m = (double**)malloc((unsigned)rows * sizeof(double*)); 749 for (i = 0; i < rows; i++) { 750 m[i] = (double*)malloc((unsigned)cols * sizeof(double)); 751 } 752 753 for (i = 0; i < rows; i++) { 754 for (j = 0; j < cols; j++) { 755 printf("init [%d , %d]\n", i, j); 756 m[i][j] = i * cols + j; 757 } 758 } 759 760 for (i = 0; i < rows; i++) { 761 for (j = 0; j < cols; j++) { 762 is_eq(m[i][j], i * cols + j); 763 } 764 } 765 } 766 767 struct someR { 768 unsigned long* ul; 769 }; 770 771 void test_post_pointer() 772 { 773 struct someR R; 774 unsigned long ull[6] = { 2, 4, 8, 10, 12, 34 }; 775 R.ul = ull; 776 struct someR* pR = &R; 777 for (int i = 0; i < 5; i++) { 778 printf("%d\n", (int)(*pR->ul)); 779 is_eq(ull[i], *pR->ul); 780 if (i < 4) { 781 pR->ul++; 782 } 783 } 784 } 785 786 struct po_ni { 787 int* fl; 788 }; 789 790 void test_array_nil() 791 { 792 struct po_ni ss; 793 ss.fl = NULL; 794 struct po_ni* s = &ss; 795 if (s->fl) { 796 fail("array_nil"); 797 } else { 798 pass("array_nil"); 799 } 800 if (s->fl == NULL) { 801 pass("array_nil"); 802 } else { 803 fail("array_nil"); 804 } 805 if (s->fl != NULL) { 806 fail("array_nil"); 807 } else { 808 pass("array_nil"); 809 } 810 811 int y = 42; 812 ss.fl = &y; 813 if (s->fl) { 814 pass("array_nil"); 815 } else { 816 fail("array_nil"); 817 } 818 if (s->fl == NULL) { 819 fail("array_nil"); 820 } else { 821 pass("array_nil"); 822 } 823 if (s->fl != NULL) { 824 pass("array_nil"); 825 } else { 826 fail("array_nil"); 827 } 828 } 829 830 int main() 831 { 832 plan(222); 833 834 test_parg_struct(); 835 START_TEST(struct_init); 836 START_TEST(array_increment); 837 START_TEST(array_to_value_int); 838 START_TEST(array_to_value_unsigned); 839 START_TEST(array_to_value_long); 840 START_TEST(array_to_value_char); 841 START_TEST(array_to_value_double); 842 START_TEST(size_pointer); 843 START_TEST(intarr); 844 START_TEST(doublearr); 845 START_TEST(intarr_init); 846 START_TEST(floatarr_init); 847 START_TEST(chararr_init); 848 START_TEST(chararr_init2); 849 START_TEST(exprarr); 850 START_TEST(structarr); 851 START_TEST(argarr); 852 START_TEST(multidim); 853 START_TEST(ptrarr); 854 START_TEST(stringarr_init); 855 START_TEST(partialarr_init); 856 START_TEST(function_array); 857 START_TEST(typedef_pointer); 858 859 diag("arrayEx"); 860 is_eq(arrayEx[1], 2.0); 861 862 diag("Array arithmetic"); 863 float a[5]; 864 a[0] = 42.; 865 is_eq(a[0], 42.); 866 a[0 + 1] = 42.; 867 is_eq(a[1], 42); 868 a[2] = 42.; 869 is_eq(a[2], 42); 870 871 diag("Pointer arithmetic. Part 1"); 872 float* b; 873 b = (float*)calloc(5, sizeof(float)); 874 875 *b = 42.; 876 is_eq(*(b + 0), 42.); 877 878 *(b + 1) = 42.; 879 is_eq(*(b + 1), 42.); 880 *(2 + b) = 42.; 881 is_eq(*(b + 2), 42.); 882 883 *(b + ff()) = 45.; 884 is_eq(*(b + 3), 45.); 885 *(ff() + b + 1) = 46.; 886 is_eq(*(b + 4), 46.); 887 888 *(b + (0 ? 1 : 2)) = -1.; 889 is_eq(*(b + 2), -1); 890 891 *(b + 0) = 1; 892 *(b + (int)(*(b + 0)) - 1) = 35; 893 is_eq(*(b + 0), 35); 894 895 *(b + (int)((float)(2))) = -45; 896 is_eq(*(b + 2), -45); 897 898 *(b + 1 + 3 + 1 - 5 * 1 + ff() - 3) = -4.0; 899 is_eq(*(b + 0), -4.0); 900 is_eq(*b, -4.0); 901 902 is_eq((*(b + 1 + 3 + 1 - 5 * 1 + ff() - 3 + 1) = -48.0, *(b + 1)), -48.0); 903 { 904 int rrr; 905 (void)(rrr); 906 } 907 908 diag("Pointer arithmetic. Part 2"); 909 { 910 float* arr; 911 arr = (float*)calloc(1 + 1, sizeof(float)); 912 is_true(arr != NULL); 913 (void)(arr); 914 } 915 { 916 float* arr; 917 arr = (float*)calloc(1 + ff(), sizeof(float)); 918 is_true(arr != NULL); 919 (void)(arr); 920 } 921 { 922 float* arr; 923 arr = (float*)calloc(ff() + ff(), sizeof(float)); 924 is_true(arr != NULL); 925 (void)(arr); 926 } 927 { 928 float* arr; 929 arr = (float*)calloc(ff() + 1 + 0 + 0 + 1 * 0, sizeof(float)); 930 is_true(arr != NULL); 931 (void)(arr); 932 } 933 934 diag("Pointer to Pointer. 1"); 935 { 936 double Var = 42; 937 double** PPptr1; 938 double* PPptr2; 939 PPptr2 = &Var; 940 PPptr1 = &PPptr2; 941 is_eq(**PPptr1, Var) 942 Var 943 = 43; 944 is_eq(**PPptr1, Var)(void)(PPptr1); 945 (void)(PPptr2); 946 } 947 diag("Pointer to Pointer. 2"); 948 { 949 double Var = 42.0, **PPptr1, *PPptr2; 950 PPptr2 = &Var; 951 PPptr1 = &PPptr2; 952 is_eq(**PPptr1, Var) 953 Var 954 = 43.0; 955 is_eq(**PPptr1, Var)(void)(PPptr1); 956 (void)(PPptr2); 957 } 958 diag("Pointer to Pointer. 3"); 959 { 960 int i = 50; 961 int** ptr1; 962 int* ptr2; 963 ptr2 = &i; 964 ptr1 = &ptr2; 965 is_eq(**ptr1, i); 966 is_eq(*ptr2, i); 967 } 968 diag("Pointer to Pointer. 4"); 969 { 970 double arr[5] = { 10., 20., 30., 40., 50. }; 971 double* ptr; 972 ptr = &arr; 973 is_eq(*ptr, 10.); 974 ++ptr; 975 is_eq(*ptr, 20.); 976 } 977 diag("Pointer to Pointer. 5"); 978 { 979 double arr[5] = { 10., 20., 30., 40., 50. }; 980 double* ptr; 981 ptr = &arr; 982 is_eq(*ptr, 10.); 983 ptr += 1; 984 is_eq(*ptr, 20.); 985 } 986 diag("Pointer to Pointer. 6"); 987 { 988 int arr[5] = { 10, 20, 30, 40, 50 }; 989 int* ptr; 990 ptr = &arr; 991 is_eq(*ptr, 10); 992 ptr = 1 + ptr; 993 is_eq(*ptr, 20); 994 } 995 diag("Pointer to Pointer. 7"); 996 { 997 double arr[5] = { 10., 20., 30., 40., 50. }; 998 double* ptr; 999 ptr = &arr; 1000 is_eq(*ptr, 10.); 1001 ptr = 1 + ptr; 1002 is_eq(*ptr, 20.); 1003 } 1004 diag("Pointer to Pointer. 8"); 1005 { 1006 double arr[5] = { 10., 20., 30., 40., 50. }; 1007 double* ptr; 1008 ptr = &arr; 1009 is_eq(*ptr, 10.); 1010 ptr++; 1011 is_eq(*ptr, 20.); 1012 } 1013 diag("Pointer to Pointer. 9"); 1014 { 1015 double arr[5] = { 10., 20., 30., 40., 50. }; 1016 double* ptr; 1017 ptr = &arr[2]; 1018 is_eq(*ptr, 30.); 1019 ptr = ptr - 1; 1020 is_eq(*ptr, 20.); 1021 } 1022 diag("Pointer to Pointer. 10"); 1023 { 1024 double arr[5] = { 10., 20., 30., 40., 50. }; 1025 double* ptr; 1026 ptr = &arr[2]; 1027 is_eq(*ptr, 30.); 1028 ptr -= 1; 1029 is_eq(*ptr, 20.); 1030 } 1031 diag("Pointer to Pointer. 11"); 1032 { 1033 double arr[5] = { 10., 20., 30., 40., 50. }; 1034 double* ptr; 1035 ptr = &arr[2]; 1036 is_eq(*ptr, 30.); 1037 ptr--; 1038 is_eq(*ptr, 20.); 1039 } 1040 diag("Pointer to Pointer. 12"); 1041 { 1042 double arr[5] = { 10., 20., 30., 40., 50. }; 1043 double* ptr; 1044 int i = 0; 1045 for (ptr = &arr[0]; i < 5; ptr++) { 1046 is_eq(*ptr, arr[i]); 1047 i++; 1048 } 1049 } 1050 diag("Pointer to Pointer. 13"); 1051 { 1052 struct temp_str { 1053 double* qwe; 1054 }; 1055 struct temp_str t; 1056 double a[5] = { 10., 20., 30., 40., 50. }; 1057 t.qwe = &a[0]; 1058 double* ptr; 1059 int i = 0; 1060 for (ptr = &t.qwe[0]; i < 5; ptr++) { 1061 is_eq(*ptr, t.qwe[i]); 1062 i++; 1063 } 1064 for (ptr = t.qwe; i < 5; ptr++) { 1065 is_eq(*ptr, t.qwe[i]); 1066 i++; 1067 } 1068 ptr = 0 + t.qwe + 1; 1069 is_eq(*ptr, t.qwe[1]); 1070 } 1071 diag("Operation += 1 for double array"); 1072 { 1073 float** m; 1074 m = (float**)malloc(5 * sizeof(float*)); 1075 is_not_null(m); 1076 m[0] = (float*)malloc(10 * sizeof(float)); 1077 m[1] = (float*)malloc(10 * sizeof(float)); 1078 m[0] += 1; 1079 (void)(m); 1080 pass("ok"); 1081 } 1082 diag("*Pointer = 0"); 1083 { 1084 int a, b, c; 1085 a = b = c = 10; 1086 is_eq(a, 10); 1087 zero(&a, &b, &c); 1088 is_eq(a, 0); 1089 is_eq(b, 0); 1090 is_eq(c, 0); 1091 pass("ok"); 1092 } 1093 diag("pointer + long"); 1094 { 1095 float* v = (float*)malloc(5 * sizeof(float)); 1096 *(v + 0) = 5; 1097 *(v + 1) = 6; 1098 is_eq(*(next_pointer(v)), 6); 1099 } 1100 diag("create array"); 1101 { 1102 double* arr = dvector(1, 12); 1103 is_not_null(arr); 1104 is_eq(arr[1], 42.0); 1105 is_eq(arr[9], 42.0); 1106 (void)(arr); 1107 } 1108 1109 diag("Increment inside array 1"); 1110 { 1111 float f[4] = { 1.2, 2.3, 3.4, 4.5 }; 1112 int iter = 0; 1113 is_eq(f[iter++], 1.2); 1114 is_eq(f[iter += 1], 3.4); 1115 is_eq(f[--iter], 2.3); 1116 } 1117 diag("Increment inside array 2"); 1118 { 1119 struct struct_I_A { 1120 double* arr; 1121 int* pos; 1122 }; 1123 struct struct_I_A siia[2]; 1124 { 1125 double t_arr[5]; 1126 siia[0].arr = t_arr; 1127 } 1128 { 1129 double t_arr[5]; 1130 siia[1].arr = t_arr; 1131 } 1132 { 1133 int t_pos[1]; 1134 siia[0].pos = t_pos; 1135 } 1136 { 1137 int t_pos[1]; 1138 siia[1].pos = t_pos; 1139 } 1140 int t = 0; 1141 int ii, jj; 1142 int one = 1; 1143 1144 siia[0].arr[0] = 45.; 1145 siia[0].arr[1] = 35.; 1146 siia[0].arr[2] = 25.; 1147 1148 siia[0].pos[0] = 0; 1149 ii = -1; 1150 jj = -1; 1151 is_eq(siia[0].arr[(t++, siia[jj += one].pos[ii += one] += one, siia[jj].pos[ii])], 35.); 1152 1153 siia[0].pos[0] = 0; 1154 ii = -1; 1155 jj = -1; 1156 is_eq(siia[0].arr[(t++, siia[++jj].pos[++ii]++, siia[jj].pos[ii])], 35.); 1157 1158 siia[0].pos[0] = 2; 1159 ii = -1; 1160 jj = -1; 1161 is_eq(siia[0].arr[(t++, siia[0].pos[ii += 1] -= 1, siia[0].pos[ii])], 35.); 1162 1163 siia[0].pos[0] = 2; 1164 ii = -1; 1165 jj = -1; 1166 is_eq(siia[0].arr[(t++, siia[0].pos[ii += 1]--, siia[0].pos[ii])], 35.); 1167 1168 is_eq(t, 4); 1169 (void)(t); 1170 } 1171 diag("Increment inside array 3"); 1172 { 1173 struct struct_I_A3 { 1174 double* arr; 1175 int pos; 1176 }; 1177 struct struct_I_A3 siia[2]; 1178 { 1179 double t_arr[5]; 1180 siia[0].arr = t_arr; 1181 } 1182 { 1183 double t_arr[5]; 1184 siia[1].arr = t_arr; 1185 } 1186 1187 siia[0].arr[0] = 45.; 1188 siia[0].arr[1] = 35.; 1189 siia[0].arr[2] = 25.; 1190 1191 siia[0].pos = 0; 1192 is_eq(siia[0].arr[siia[0].pos += 1], 35.); 1193 1194 siia[0].pos = 0; 1195 is_eq(siia[0].arr[siia[0].pos++], 45.); 1196 1197 siia[0].pos = 0; 1198 is_eq(siia[0].arr[++siia[0].pos], 35.); 1199 1200 siia[0].pos = 2; 1201 is_eq(siia[0].arr[siia[0].pos -= 1], 35.); 1202 1203 siia[0].pos = 2; 1204 is_eq(siia[0].arr[siia[0].pos--], 25.); 1205 } 1206 diag("Increment inside array 4"); 1207 { 1208 struct struct_I_A4 { 1209 double* arr; 1210 int pos; 1211 }; 1212 struct struct_I_A4 siia[2]; 1213 { 1214 double t_arr[5]; 1215 siia[0].arr = t_arr; 1216 } 1217 { 1218 double t_arr[5]; 1219 siia[1].arr = t_arr; 1220 } 1221 int t = 0; 1222 1223 siia[0].arr[0] = 45.; 1224 siia[0].arr[1] = 35.; 1225 siia[0].arr[2] = 25.; 1226 1227 siia[0].pos = 0; 1228 is_eq(siia[0].arr[(t++, siia[0].pos += 1)], 35.); 1229 1230 siia[0].pos = 0; 1231 is_eq(siia[0].arr[(t++, siia[0].pos++)], 45.); 1232 1233 siia[0].pos = 2; 1234 is_eq(siia[0].arr[(t++, siia[0].pos -= 1)], 35.); 1235 1236 siia[0].pos = 2; 1237 is_eq(siia[0].arr[(t++, siia[0].pos--)], 25.); 1238 1239 is_eq(t, 4); 1240 (void)(t); 1241 } 1242 diag("Increment inside array 5"); 1243 { 1244 struct struct_I_A5 { 1245 double* arr; 1246 int pos; 1247 }; 1248 struct struct_I_A5 siia[2]; 1249 { 1250 double t_arr[5]; 1251 siia[0].arr = t_arr; 1252 } 1253 { 1254 double t_arr[5]; 1255 siia[1].arr = t_arr; 1256 } 1257 int t = 0; 1258 1259 siia[0].arr[0] = 45.; 1260 siia[0].arr[1] = 35.; 1261 siia[0].arr[2] = 25.; 1262 1263 siia[0].pos = 0; 1264 is_eq(siia[0].arr[(t++, siia[0].pos += 1, siia[0].pos)], 35.); 1265 1266 siia[0].pos = 0; 1267 is_eq(siia[0].arr[(t++, siia[0].pos++, siia[0].pos)], 35.); 1268 1269 siia[0].pos = 2; 1270 is_eq(siia[0].arr[(t++, siia[0].pos -= 1, siia[0].pos)], 35.); 1271 1272 siia[0].pos = 2; 1273 is_eq(siia[0].arr[(t++, siia[0].pos--, siia[0].pos)], 35.); 1274 1275 is_eq(t, 4); 1276 (void)(t); 1277 } 1278 diag("Increment inside array 6"); 1279 { 1280 struct struct_I_A6 { 1281 double* arr; 1282 int* pos; 1283 }; 1284 struct struct_I_A6 siia[2]; 1285 { 1286 double t_arr[5]; 1287 siia[0].arr = t_arr; 1288 } 1289 { 1290 double t_arr[5]; 1291 siia[1].arr = t_arr; 1292 } 1293 { 1294 int t_pos[1]; 1295 siia[0].pos = t_pos; 1296 } 1297 { 1298 int t_pos[1]; 1299 siia[1].pos = t_pos; 1300 } 1301 int t = 0; 1302 1303 siia[0].arr[0] = 45.; 1304 siia[0].arr[1] = 35.; 1305 siia[0].arr[2] = 25.; 1306 1307 siia[0].pos[0] = 0; 1308 is_eq(siia[0].arr[(t++, siia[0].pos[0] += 1)], 35.); 1309 1310 siia[0].pos[0] = 0; 1311 is_eq(siia[0].arr[(t++, siia[0].pos[0]++)], 45.); 1312 1313 siia[0].pos[0] = 2; 1314 is_eq(siia[0].arr[(t++, siia[0].pos[0] -= 1)], 35.); 1315 1316 siia[0].pos[0] = 2; 1317 is_eq(siia[0].arr[(t++, siia[0].pos[0]--)], 25.); 1318 1319 is_eq(t, 4); 1320 (void)(t); 1321 } 1322 1323 START_TEST(pointer_arith_size_t); 1324 START_TEST(pointer_minus_pointer); 1325 1326 diag("calloc with struct"); 1327 { 1328 struct cws { 1329 float* w; 1330 int nw; 1331 }; 1332 struct cws t; 1333 t.nw = 5; 1334 t.w = (float*)calloc(t.nw, sizeof(*t.w)); 1335 is_not_null(t.w); 1336 (void)(t); 1337 } 1338 { 1339 diag("[][]char += 1"); 1340 char w1[] = "hello"; 1341 char w2[] = "world"; 1342 char w3[] = "people"; 1343 char* p1 = w1; 1344 char* p2 = w2; 1345 char* p3 = w3; 1346 char* pa[3] = { p1, p2, p3 }; 1347 char** pnt = pa; 1348 char** pnt2 = pa; 1349 *pnt += 1; 1350 is_streq(*pnt, "ello"); 1351 (*pnt2)++; 1352 is_streq(*pnt2, "llo"); 1353 } 1354 { 1355 diag("pnt of value : size_t"); 1356 size_t len = 42; 1357 size_t* l = &len; 1358 is_eq(*l, len); 1359 } 1360 { 1361 diag("pnt of value : ssize_t"); 1362 ssize_t len = 42; 1363 ssize_t* l = &len; 1364 is_eq(*l, len); 1365 } 1366 START_TEST(string_array); 1367 START_TEST(double_array); 1368 START_TEST(func_byte); 1369 START_TEST(negative_index); 1370 START_TEST(matrix_init); 1371 START_TEST(post_pointer); 1372 START_TEST(array_nil); 1373 1374 done_testing(); 1375 }