A dynamic tracer for Linux

type_test.c 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "type.h"
  2. #include "unittest.h"
  3. static int sizeof_basic(void *_null)
  4. {
  5. struct type *voidp, *map;
  6. unittest_eq(type_sizeof(&t_void), sizeof(void));
  7. unittest_eq(type_sizeof(&t_char), sizeof(char));
  8. unittest_eq(type_sizeof(&t_short), sizeof(short));
  9. unittest_eq(type_sizeof(&t_int), sizeof(int));
  10. unittest_eq(type_sizeof(&t_long), sizeof(long));
  11. unittest_eq(type_sizeof(&t_llong), sizeof(long long));
  12. voidp = type_ptr_of(&t_void);
  13. unittest_eq(type_sizeof(voidp), sizeof(void *));
  14. map = type_map_of(&t_short, &t_short);
  15. unittest_eq(type_sizeof(map), sizeof(int));
  16. return 0;
  17. }
  18. UNITTEST(sizeof_basic);
  19. static int alignof_basic(void *_null)
  20. {
  21. struct type *voidp, *map, *array;
  22. unittest_eq(type_alignof(&t_void), __alignof__(void));
  23. unittest_eq(type_alignof(&t_char), __alignof__(char));
  24. unittest_eq(type_alignof(&t_short), __alignof__(short));
  25. unittest_eq(type_alignof(&t_int), __alignof__(int));
  26. unittest_eq(type_alignof(&t_long), __alignof__(long));
  27. unittest_eq(type_alignof(&t_llong), __alignof__(long long));
  28. voidp = type_ptr_of(&t_void);
  29. unittest_eq(type_alignof(voidp), __alignof__(void *));
  30. map = type_map_of(&t_short, &t_short);
  31. unittest_eq(type_alignof(map), __alignof__(int));
  32. array = type_array_of(&t_int, 5);
  33. unittest_eq(type_alignof(array), __alignof__(int[5]));
  34. return 0;
  35. }
  36. UNITTEST(alignof_basic);
  37. struct t1 {
  38. char a;
  39. int b[5];
  40. char c[3];
  41. short d;
  42. } *t1 = NULL;
  43. static int struct_basic(void *_null)
  44. {
  45. struct tfield t1_fields[] = {
  46. { .name = "a", .type = &t_char },
  47. { .name = "b", .type = type_array_of(&t_int, 5) },
  48. { .name = "c", .type = type_array_of(&t_char, 3) },
  49. { .name = "d", .type = &t_short },
  50. { .type = &t_void }
  51. };
  52. struct type t_t1 = {
  53. .ttype = T_STRUCT,
  54. .sou = { .name = "t1", .fields = t1_fields },
  55. };
  56. unittest_eq(type_offsetof(&t_t1, "a"), offsetof(struct t1, a));
  57. unittest_eq(type_offsetof(&t_t1, "b"), offsetof(struct t1, b));
  58. unittest_eq(type_offsetof(&t_t1, "c"), offsetof(struct t1, c));
  59. unittest_eq(type_offsetof(&t_t1, "d"), offsetof(struct t1, d));
  60. unittest_eq(type_alignof(&t_t1), __alignof__(struct t1));
  61. unittest_eq(type_sizeof(&t_t1), sizeof(struct t1));
  62. return 0;
  63. }
  64. UNITTEST(struct_basic);
  65. int main(void)
  66. {
  67. struct unittest_ctx ctx = {
  68. .keep_going = 1,
  69. .verbose = 1,
  70. .ctx = NULL,
  71. };
  72. return unittests_run(&ctx) ? 1 : 0;
  73. }