#include "type.h" #include "unittest.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; } *t1 = NULL; 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 = &t_void } }; 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); int main(void) { struct unittest_ctx ctx = { .keep_going = 1, .verbose = 1, .ctx = NULL, }; return unittests_run(&ctx) ? 1 : 0; }