A dynamic tracer for Linux

type_test.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. };
  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. struct t1_packed {
  66. char a;
  67. int b[5];
  68. char c[3];
  69. short d;
  70. } __attribute__((packed));
  71. static int struct_packed(void *_null)
  72. {
  73. struct tfield t1_fields[] = {
  74. { .name = "a", .type = &t_char },
  75. { .name = "b", .type = type_array_of(&t_int, 5) },
  76. { .name = "c", .type = type_array_of(&t_char, 3) },
  77. { .name = "d", .type = &t_short },
  78. { .type = &t_void }
  79. };
  80. struct type t_t1 = {
  81. .ttype = T_STRUCT,
  82. .sou = { .name = "t1", .fields = t1_fields, .packed = 1 },
  83. };
  84. unittest_eq(type_offsetof(&t_t1, "a"), offsetof(struct t1_packed, a));
  85. unittest_eq(type_offsetof(&t_t1, "b"), offsetof(struct t1_packed, b));
  86. unittest_eq(type_offsetof(&t_t1, "c"), offsetof(struct t1_packed, c));
  87. unittest_eq(type_offsetof(&t_t1, "d"), offsetof(struct t1_packed, d));
  88. unittest_eq(type_alignof(&t_t1), __alignof__(struct t1_packed));
  89. unittest_eq(type_sizeof(&t_t1), sizeof(struct t1_packed));
  90. return 0;
  91. }
  92. UNITTEST(struct_packed);
  93. int main(void)
  94. {
  95. struct unittest_ctx ctx = {
  96. .keep_going = 1,
  97. .verbose = 1,
  98. .ctx = NULL,
  99. };
  100. return unittests_run(&ctx) ? 1 : 0;
  101. }