Skip to content

Commit

Permalink
tests/slicing/sources/bitcast{2,3}: do not rely on int having size 4
Browse files Browse the repository at this point in the history
Use 0x1b instead of 0xab as left shift of 0xab by 24 places cannot be
represented in signed int.
  • Loading branch information
lzaoral committed Oct 29, 2021
1 parent 4555ab7 commit 6fc8e8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions tests/slicing/sources/bitcast2.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
_Static_assert(sizeof(int) == 4, "This test assumes sizeof(int) == 4");
#include <limits.h>

/* test accessing bytes in int */
int main(void) {
int a;
a = 0;
int a = 0;
char *byte = (char *) &a;
byte[0] = 0xab;
byte[1] = 0xab;
byte[2] = 0xab;
byte[3] = 0xab;
test_assert(a == 0xabababab);

int result = 0;
for (unsigned i = 0; i < sizeof(int); i++)
result |= (byte[i] = 0x1b) << i * CHAR_BIT;

test_assert(a == result);

return 0;
}
18 changes: 9 additions & 9 deletions tests/slicing/sources/bitcast3.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
_Static_assert(sizeof(int) == 4, "This test assumes sizeof(int) == 4");
#include <limits.h>

union BYTE {
int i;
char b[4];
char b[sizeof(int)];
};

/* test accessing bytes in int */
int main(void) {
union BYTE B;
B.i = 0;
B.b[0] = 0xab;
B.b[1] = 0xab;
B.b[2] = 0xab;
B.b[3] = 0xab;
test_assert(B.i == 0xabababab);
union BYTE B = {.i = 0};

int result = 0;
for (unsigned i = 0; i < sizeof(int); i++)
result |= (B.b[i] = 0x1b) << i * CHAR_BIT;

test_assert(B.i == result);

return 0;
}

0 comments on commit 6fc8e8c

Please sign in to comment.