| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #include "unittest.h"
- #include "type.h"
- static int sizeof_basic(void *_null)
- {
- struct type *voidp, *map;
- unittest_eq(type_sizeof(&t_void), sizeof(void));
- unittest_eq(type_sizeof(&t_char), sizeof(char));
- unittest_eq(type_sizeof(&t_short), sizeof(short));
- unittest_eq(type_sizeof(&t_int), sizeof(int));
- unittest_eq(type_sizeof(&t_long), sizeof(long));
- unittest_eq(type_sizeof(&t_llong), sizeof(long long));
- voidp = type_ptr_of(&t_void);
- unittest_eq(type_sizeof(voidp), sizeof(void *));
- map = type_map_of(&t_short, &t_short);
- unittest_eq(type_sizeof(map), sizeof(int));
-
- return 0;
- }
- UNITTEST(sizeof_basic);
- static int alignof_basic(void *_null)
- {
- struct type *voidp, *map, *array;
- unittest_eq(type_alignof(&t_void), __alignof__(void));
- unittest_eq(type_alignof(&t_char), __alignof__(char));
- unittest_eq(type_alignof(&t_short), __alignof__(short));
- unittest_eq(type_alignof(&t_int), __alignof__(int));
- unittest_eq(type_alignof(&t_long), __alignof__(long));
- unittest_eq(type_alignof(&t_llong), __alignof__(long long));
- voidp = type_ptr_of(&t_void);
- unittest_eq(type_alignof(voidp), __alignof__(void *));
- map = type_map_of(&t_short, &t_short);
- unittest_eq(type_alignof(map), __alignof__(int));
- array = type_array_of(&t_int, 5);
- unittest_eq(type_alignof(array), __alignof__(int[5]));
- return 0;
- }
- UNITTEST(alignof_basic);
- struct t1 {
- char a;
- int b[5];
- char c[3];
- short d;
- };
- static int struct_basic(void *_null)
- {
- struct tfield t1_fields[] = {
- { .name = "a", .type = &t_char },
- { .name = "b", .type = type_array_of(&t_int, 5) },
- { .name = "c", .type = type_array_of(&t_char, 3) },
- { .name = "d", .type = &t_short },
- { .type = NULL }
- };
- struct type t_t1 = {
- .ttype = T_STRUCT,
- .sou = { .name = "t1", .fields = t1_fields },
- };
- unittest_eq(type_offsetof(&t_t1, "a"), offsetof(struct t1, a));
- unittest_eq(type_offsetof(&t_t1, "b"), offsetof(struct t1, b));
- unittest_eq(type_offsetof(&t_t1, "c"), offsetof(struct t1, c));
- unittest_eq(type_offsetof(&t_t1, "d"), offsetof(struct t1, d));
- unittest_eq(type_alignof(&t_t1), __alignof__(struct t1));
- unittest_eq(type_sizeof(&t_t1), sizeof(struct t1));
- return 0;
- }
- UNITTEST(struct_basic);
- struct t1_packed {
- char a;
- int b[5];
- char c[3];
- short d;
- } __attribute__((packed));
- static int struct_packed(void *_null)
- {
- struct tfield t1_fields[] = {
- { .name = "a", .type = &t_char },
- { .name = "b", .type = type_array_of(&t_int, 5) },
- { .name = "c", .type = type_array_of(&t_char, 3) },
- { .name = "d", .type = &t_short },
- { .type = NULL }
- };
- struct type t_t1 = {
- .ttype = T_STRUCT,
- .sou = { .name = "t1", .fields = t1_fields, .packed = 1 },
- };
- unittest_eq(type_offsetof(&t_t1, "a"), offsetof(struct t1_packed, a));
- unittest_eq(type_offsetof(&t_t1, "b"), offsetof(struct t1_packed, b));
- unittest_eq(type_offsetof(&t_t1, "c"), offsetof(struct t1_packed, c));
- unittest_eq(type_offsetof(&t_t1, "d"), offsetof(struct t1_packed, d));
- unittest_eq(type_alignof(&t_t1), __alignof__(struct t1_packed));
- unittest_eq(type_sizeof(&t_t1), sizeof(struct t1_packed));
- return 0;
- }
- UNITTEST(struct_packed);
- int main(void)
- {
- struct unittest_ctx ctx = {
- .keep_going = 1,
- .verbose = 1,
- .ctx = NULL,
- };
- return unittests_run(&ctx) ? 1 : 0;
- }
|