github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/union.c (about) 1 // Tests for unions. 2 3 #include "tests.h" 4 #include <stdio.h> 5 6 union programming { 7 int constant; 8 char* pointer; 9 }; 10 11 union programming init_var() 12 { 13 union programming variable; 14 char* s = "Programming in Software Development."; 15 16 variable.pointer = s; 17 is_streq(variable.pointer, "Programming in Software Development."); 18 19 variable.constant = 123; 20 is_eq(variable.constant, 123); 21 22 return variable; 23 } 24 25 void pass_by_ref(union programming* addr) 26 { 27 char* s = "Show string member."; 28 int v = 123 + 456; 29 30 addr->constant += 456; 31 is_eq(addr->constant, v); 32 33 addr->pointer = s; 34 is_streq(addr->pointer, "Show string member."); 35 } 36 37 void var_by_val(union programming value) 38 { 39 value.constant++; 40 41 is_eq(value.constant, 124); 42 } 43 44 struct SHA3 { 45 union { 46 double iY; 47 double dY; 48 } uY; 49 float ffY; 50 }; 51 52 union unknown { 53 double i2; 54 double d2; 55 }; 56 struct SHA32 { 57 union unknown u2; 58 float ff2; 59 }; 60 61 void union_inside_struct() 62 { 63 diag("Union inside struct"); 64 struct SHA3 sha; 65 sha.ffY = 12.444; 66 sha.uY.iY = 4; 67 is_eq(sha.uY.iY, 4); 68 is_eq(sha.uY.dY, 4); 69 is_eq(sha.ffY, 12.444); 70 71 struct SHA32 sha2; 72 sha2.ff2 = 12.444; 73 sha2.u2.i2 = 4; 74 is_eq(sha2.u2.i2, 4); 75 is_eq(sha2.u2.d2, 4); 76 is_eq(sha2.ff2, 12.444); 77 pass("ok"); 78 79 union unknown data[2]; 80 for (int i = 0; i < 2; i++) { 81 data[i].i2 = i + 3; 82 is_true(data[i].d2 != 0); 83 } 84 (void)(data); 85 } 86 87 typedef union myunion myunion; 88 typedef union myunion { 89 double PI; 90 int B; 91 } MYUNION; 92 93 typedef union { 94 double PI; 95 int B; 96 } MYUNION2; 97 98 void union_typedef() 99 { 100 diag("Typedef union"); 101 union myunion m; 102 double v = 3.14; 103 m.PI = v; 104 is_eq(m.PI, 3.14); 105 is_true(m.B != 0); 106 is_eq(v, 3.14); 107 v += 1.0; 108 is_eq(v, 4.14); 109 is_eq(m.PI, 3.14); 110 111 MYUNION mm; 112 mm.PI = 3.14; 113 is_eq(mm.PI, 3.14); 114 is_true(mm.B != 0); 115 116 myunion mmm; 117 mmm.PI = 3.14; 118 is_eq(mmm.PI, 3.14); 119 is_true(mmm.B != 0); 120 121 MYUNION2 mmmm; 122 mmmm.PI = 3.14; 123 is_eq(mmmm.PI, 3.14); 124 is_true(mmmm.B != 0); 125 } 126 127 typedef struct FuncDestructor FuncDestructor; 128 struct FuncDestructor { 129 int i; 130 }; 131 typedef struct FuncDef FuncDef; 132 struct FuncDef { 133 int i; 134 union { 135 FuncDef* pHash; 136 FuncDestructor* pDestructor; 137 } u; 138 }; 139 140 void union_inside_struct2() 141 { 142 FuncDef f; 143 FuncDestructor fd; 144 fd.i = 100; 145 f.u.pDestructor = &fd; 146 147 FuncDestructor* p_fd = f.u.pDestructor; 148 is_eq((*p_fd).i, 100); 149 150 is_true(f.u.pHash != NULL); 151 is_true(f.u.pDestructor != NULL); 152 int vHash = (*f.u.pHash).i; 153 is_eq(vHash, 100); 154 is_eq((*f.u.pHash).i, 100); 155 } 156 157 union UPNT { 158 int* a; 159 int* b; 160 int* c; 161 }; 162 163 void union_pointers() 164 { 165 union UPNT u; 166 int v = 32; 167 u.a = &v; 168 is_eq(*u.a, 32); 169 is_eq(*u.b, 32); 170 is_eq(*u.c, 32); 171 pass("ok") 172 } 173 174 union UPNTF { 175 int (*f1)(int); 176 int (*f2)(int); 177 }; 178 179 int union_function(int a) 180 { 181 return a + 1; 182 } 183 184 void union_func_pointers() 185 { 186 union UPNTF u; 187 u.f1 = union_function; 188 is_eq(u.f1(21), 22); 189 is_eq(u.f2(21), 22); 190 } 191 192 union array_union { 193 float a[2]; 194 float b[2]; 195 }; 196 197 void union_array() 198 { 199 union array_union arr; 200 arr.a[0] = 12; 201 arr.b[1] = 14; 202 is_eq(arr.a[0], 12); 203 is_eq(arr.a[1], 14); 204 is_eq(arr.b[0], 12); 205 is_eq(arr.b[1], 14); 206 } 207 208 typedef int ii; 209 typedef struct SHA SHA; 210 struct SHA { 211 union { 212 ii s[25]; 213 unsigned char x[100]; 214 } u; 215 unsigned uuu; 216 }; 217 218 void union_arr_in_str() 219 { 220 SHA sha; 221 sha.uuu = 15; 222 is_eq(sha.uuu, 15); 223 for (int i = 0; i < 25; i++) 224 sha.u.s[0] = 0; 225 is_eq(sha.u.s[0], 0); 226 is_true(sha.u.x[0] == 0); 227 for (int i = 0; i < 6; i++) { 228 sha.u.s[i] = (ii)(4); 229 sha.u.s[i] = (ii)(42) + sha.u.s[i]; 230 } 231 is_eq(sha.u.s[5], 46); 232 is_true(sha.u.x[0] != 0); 233 } 234 235 union un_struct { 236 struct { 237 short a; 238 short b; 239 } str; 240 long l; 241 }; 242 243 void union_with_struct() 244 { 245 union un_struct u; 246 u.str.a = 12; 247 u.str.b = 45; 248 is_eq(u.str.a, 12); 249 is_eq(u.str.b, 45); 250 is_true(u.l > 0); 251 } 252 253 struct suf { 254 union { 255 int* i; 256 void (*sa)(int, double*, void*); 257 } uf; 258 }; 259 260 void union_with_func() 261 { 262 struct suf s; 263 int a = 42; 264 s.uf.i = &a; 265 is_not_null(s.uf.i); 266 is_not_null(s.uf.sa); 267 (void)(s.uf); 268 } 269 270 int main() 271 { 272 plan(50); 273 274 union programming variable; 275 276 variable = init_var(); 277 var_by_val(variable); 278 pass_by_ref(&variable); 279 280 union_inside_struct(); 281 union_typedef(); 282 union_inside_struct2(); 283 union_pointers(); 284 union_func_pointers(); 285 union_array(); 286 union_arr_in_str(); 287 union_with_struct(); 288 union_with_func(); 289 290 done_testing(); 291 }