github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/stddef.c (about) 1 #include "tests.h" 2 #include <stddef.h> 3 4 struct foo { 5 char a; 6 char b[10]; 7 char c; 8 }; 9 10 void test_offset() 11 { 12 is_eq((int)offsetof(struct foo, a), 0); 13 is_eq((int)offsetof(struct foo, b), 1); 14 is_eq((int)offsetof(struct foo, c), 11); 15 is_eq((int)offsetof(struct 16 foo /* 17 comments */ 18 , 19 // single comment 20 b), 21 1); 22 is_eq((int)offsetof(struct foo, c), 11); 23 } 24 25 void test_ptrdiff_t() 26 { 27 { 28 diag("ptrdiff_t : int"); 29 int numbers[100]; 30 int *p1 = &numbers[18], *p2 = &numbers[29]; 31 if (p1 == NULL || p2 == NULL) { 32 fail("NULL fail"); 33 } 34 ptrdiff_t diff = p2 - p1; 35 is_eq(diff, 11); 36 } 37 { 38 diag("ptrdiff_t: long long"); 39 long long numbers[100]; 40 long long *p1 = &numbers[18], *p2 = &numbers[29]; 41 ptrdiff_t diff = p2 - p1; 42 is_eq(diff, 11); 43 } 44 } 45 46 void test_sizet() 47 { 48 diag("size_t"); 49 const size_t N = 100; 50 int numbers[N]; 51 for (size_t ndx = 0; ndx < N; ++ndx) 52 numbers[ndx] = ndx; 53 size_t size = sizeof numbers; 54 is_eq(size, 400); 55 } 56 57 int main() 58 { 59 plan(8); 60 61 test_offset(); 62 test_ptrdiff_t(); 63 test_sizet(); 64 65 done_testing(); 66 }