github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/sizeof.c (about) 1 // This file contains tests for the sizeof() function and operator. 2 3 #include "tests.h" 4 #include <stdio.h> 5 6 #define is_not_less(arg1, arg2) \ 7 is_true(arg1 >= arg2) // printf("arg1 = %d\n",arg1); 8 9 #define check_sizes(type, size) \ 10 is_not_less(sizeof(type), size); \ 11 is_not_less(sizeof(unsigned type), size); \ 12 is_not_less(sizeof(signed type), size); \ 13 is_not_less(sizeof(const type), size); \ 14 is_not_less(sizeof(volatile type), size); 15 16 #define FLOAT(type, size) \ 17 is_not_less(sizeof(type), size); 18 19 #define OTHER(type, size) \ 20 is_not_less(sizeof(type), size); 21 22 // We print the variable so that the compiler doesn't complain that the variable 23 // is unused. 24 #define VARIABLE(v, p) \ 25 printf("%s = (%d) %d bytes\n", #v, p, sizeof(v)); 26 27 struct MyStruct { 28 double a, aa, aaa, aaaa; 29 char b; 30 char c; 31 }; 32 33 union MyUnion { 34 long double a; 35 char b; 36 int c; 37 }; 38 39 short a; 40 int b; 41 42 struct MyNums { 43 char name[100]; 44 int size; 45 int numbers[]; 46 }; 47 48 struct s { 49 FILE* p; 50 }; 51 52 typedef struct erow { 53 int idx; /* Row index in the file, zero-based. */ 54 int size; /* Size of the row, excluding the null term. */ 55 int rsize; /* Size of the rendered row. */ 56 char* chars; /* Row content. */ 57 char* render; /* Row content "rendered" for screen (for TABs). */ 58 unsigned char* hl; /* Syntax highlight type for each character in render.*/ 59 int hl_oc; /* Row had open comment at end in last syntax highlight 60 check. */ 61 } erow; 62 63 typedef struct part1_erow { 64 int part; 65 } part1_erow; 66 typedef struct part1a_erow { 67 int part; 68 int part2; 69 } part1a_erow; 70 typedef struct part1b_erow { 71 int part; 72 int part2; 73 int part3; 74 } part1b_erow; 75 typedef struct part1c_erow { 76 int part; 77 int part2; 78 int part3; 79 char* part4; 80 } part1c_erow; 81 typedef struct part1d_erow { 82 int part; 83 int part2; 84 int part3; 85 char* part4; 86 char* part5; 87 } part1d_erow; 88 typedef struct part1e_erow { 89 int part; 90 int part2; 91 int part3; 92 char* part4; 93 char* part5; 94 unsigned char* part6; 95 } part1e_erow; 96 97 typedef struct part2_erow { 98 char* part; 99 } part2_erow; 100 typedef struct part2a_erow { 101 char* part; 102 char* part2; 103 } part2a_erow; 104 105 typedef struct part3_erow { 106 unsigned char* part; 107 } part3_erow; 108 109 struct editorSyntax { 110 char** filematch; 111 char** keywords; 112 char singleline_comment_start[2]; 113 char multiline_comment_start[3]; 114 char multiline_comment_end[3]; 115 int flags; 116 }; 117 118 /* C / C++ */ 119 char* C_HL_extensions[] = { ".c", ".cpp", NULL }; 120 char* C_HL_keywords[] = { 121 /* A few C / C++ keywords */ 122 "switch", "if", "while", "for", "break", "continue", "return", "else", 123 "struct", "union", "typedef", "static", "enum", "class", 124 /* C types */ 125 "int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|", 126 "void|", NULL 127 }; 128 129 #define HL_HIGHLIGHT_STRINGS (1 << 0) 130 #define HL_HIGHLIGHT_NUMBERS (1 << 1) 131 132 /* Here we define an array of syntax highlights by extensions, keywords, 133 * comments delimiters and flags. */ 134 struct editorSyntax HLDB[] = { 135 { /* C / C++ */ 136 C_HL_extensions, 137 C_HL_keywords, 138 "//", "/*", "*/", 139 HL_HIGHLIGHT_STRINGS | HL_HIGHLIGHT_NUMBERS } 140 }; 141 142 #define HLDB_ENTRIES ((sizeof(HLDB)) / (sizeof(HLDB[0]))) 143 144 typedef struct SP SP; 145 146 struct SP { 147 union SP_UN { 148 int i; 149 char l; 150 }; 151 double d; 152 char c; 153 }; 154 155 typedef struct SP Mem; 156 typedef SP Mem2; 157 158 int main() 159 { 160 plan(84); 161 162 diag("null"); 163 void* pntnull = NULL; 164 is_eq(sizeof(pntnull), 8); 165 (void)pntnull; 166 167 diag("Integer types"); 168 check_sizes(char, 1); 169 check_sizes(short, 2); 170 check_sizes(int, 4); 171 check_sizes(long, 8); 172 check_sizes(long int, 8); 173 check_sizes(long long, 8); 174 check_sizes(long long int, 8); 175 176 diag("Floating-point types"); 177 is_not_less(sizeof(float), 4); 178 is_not_less(sizeof(double), 8); 179 is_not_less(sizeof(long double), 16); 180 181 diag("Other types"); 182 is_not_less(sizeof(void), 1); 183 184 diag("Pointers"); 185 is_not_less(sizeof(char*), 8); 186 is_not_less(sizeof(char*), 8); 187 is_not_less(sizeof(short**), 8); 188 is_not_less(sizeof(long double**), 8); 189 190 diag("Variables"); 191 a = 123; 192 b = 456; 193 struct MyStruct s1; 194 s1.b = 0; 195 union MyUnion u1; 196 u1.b = 0; 197 198 is_not_less(sizeof(a), 2); 199 is_not_less(sizeof(b), 4); 200 is_not_less(sizeof(s1), 40); 201 is_not_less(sizeof(u1), 16); 202 203 diag("Structures"); 204 is_not_less(sizeof(struct MyStruct), 40); 205 is_not_less(sizeof(struct MyStruct*), 8); 206 207 diag("Unions"); 208 is_not_less(sizeof(union MyUnion), 16); 209 is_not_less(sizeof(union MyUnion*), 8); 210 211 diag("Function pointers"); 212 is_not_less(sizeof(main), 1); 213 214 diag("Arrays"); 215 char c[3] = { 'a', 'b', 'c' }; 216 c[0] = 'a'; 217 is_not_less(sizeof(c), 3); 218 219 int* d[3]; 220 d[0] = &b; 221 is_not_less(sizeof(d), 24); 222 223 int** e[4]; 224 e[0] = d; 225 is_not_less(sizeof(e), 32); 226 227 const char* const f[] = { "a", "b", "c", "d", "e", "f" }; 228 is_not_less(sizeof(f), 48); 229 is_streq(f[1], "b"); 230 231 diag("MyNums"); 232 is_not_less(sizeof(struct MyNums), 104); 233 234 diag("FILE *"); 235 is_not_less(sizeof(FILE*), 8); 236 is_not_less(sizeof(struct s), 8); 237 238 diag("erow from kilo editor"); 239 is_not_less(sizeof(part1_erow), 4); 240 241 is_not_less(sizeof(part1a_erow), 8); 242 is_not_less(sizeof(part1b_erow), 12); 243 is_not_less(sizeof(part1c_erow), 24); 244 is_not_less(sizeof(part1d_erow), 32); 245 is_not_less(sizeof(part1e_erow), 40); 246 is_not_less(sizeof(erow), 48); 247 248 is_not_less(sizeof(part2_erow), 8); 249 is_not_less(sizeof(part2a_erow), 16); 250 is_not_less(sizeof(part3_erow), 8); 251 252 diag("HLDB"); 253 is_not_less(sizeof(HLDB), 32); 254 is_not_less(sizeof(HLDB[0]), 32); 255 is_true(sizeof(HLDB) == sizeof(HLDB[0])); 256 is_eq((HLDB_ENTRIES), 1); 257 258 diag("sqlite examples"); 259 is_not_less(sizeof(union SP_UN), 4); 260 Mem m; 261 is_not_less(sizeof(m), 16); 262 is_not_less(sizeof(m), sizeof(double) + sizeof(char)); 263 (void)(m); 264 Mem2 m2; 265 is_not_less(sizeof(m2), 16); 266 is_not_less(sizeof(m2), sizeof(double) + sizeof(char)); 267 (void)(m2); 268 SP s; 269 is_not_less(sizeof(s), 16); 270 is_not_less(sizeof(s), sizeof(double) + sizeof(char)); 271 (void)(s); 272 union SP_UN sp; 273 is_not_less(sizeof(sp), 4); 274 is_not_less(sizeof(sp), sizeof(int)); 275 (void)(sp); 276 277 done_testing(); 278 }