| 1 | struct Z0 {} |
| 2 | |
| 3 | struct Z1 { |
| 4 | padding1 char |
| 5 | } |
| 6 | |
| 7 | struct Z2 { |
| 8 | padding1 char |
| 9 | padding2 char |
| 10 | } |
| 11 | |
| 12 | struct Z3 { |
| 13 | padding1 char |
| 14 | padding2 char |
| 15 | padding3 char |
| 16 | } |
| 17 | |
| 18 | struct Z4 { |
| 19 | padding1 char |
| 20 | padding2 char |
| 21 | padding3 char |
| 22 | padding4 char |
| 23 | } |
| 24 | |
| 25 | fn test_struct_sizes() { |
| 26 | assert dump(sizeof(Z0)) <= 1 // valid for all |
| 27 | $if tinyc { |
| 28 | // TCC has no problems with 0 sized structs in almost cases, |
| 29 | // except when they are used in fixed arrays, or their address is taken, |
| 30 | // in which case, it produces a compilation error. To avoid it, for it |
| 31 | // empty structs are 1 byte in size. |
| 32 | assert dump(sizeof(Z0)) == 1 |
| 33 | } |
| 34 | $if msvc { |
| 35 | // MSVC seems to have no way at all to have empty structs in C mode. It produces the following error: |
| 36 | // `error c2016: C requires that a struct or union have at least one member`. |
| 37 | // Note that MSVC allows empty structs in C++ mode, but that has other restrictions, |
| 38 | // and is not suitable for the generated code of most V programs. Besides, even in C++ mode, the size of |
| 39 | // an empty struct is still 1, not 0. |
| 40 | // For that reason, empty structs are 1 byte in size for MSVC too. |
| 41 | assert dump(sizeof(Z0)) == 1 |
| 42 | } |
| 43 | $if clang { |
| 44 | assert dump(sizeof(Z0)) == 0 |
| 45 | } |
| 46 | $if gcc { |
| 47 | assert dump(sizeof(Z0)) == 0 |
| 48 | } |
| 49 | assert dump(sizeof(Z1)) < sizeof(Z2) |
| 50 | assert dump(sizeof(Z2)) < sizeof(Z3) |
| 51 | assert dump(sizeof(Z3)) < sizeof(Z4) |
| 52 | assert dump(sizeof(Z4)) == 4 |
| 53 | } |
| 54 | |