test_wire.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include "si_test.h"
  4. #include "si_wire.h"
  5. TEST_FUNC(siwire_new) {
  6. SiWire * wire = siwire_new("Hello");
  7. TEST_NOTNULL(wire);
  8. TEST_NOTNULL(siwire_cstr(wire));
  9. TEST_FALSE(siwire_const_p(wire));
  10. TEST_INTEQ(siwire_size(wire), 5);
  11. TEST_INTEQ(siwire_room(wire), 11);
  12. TEST_ASSERT(siwire_puts(wire) > 0);
  13. siwire_free(wire);
  14. TEST_DONE();
  15. }
  16. TEST_FUNC(siwire_const) {
  17. SiWire * wire = siwire_const("Hello");
  18. TEST_NOTNULL(wire);
  19. TEST_TRUE(siwire_const_p(wire));
  20. TEST_NOTNULL(siwire_cstr(wire));
  21. TEST_INTEQ(siwire_size(wire), 5);
  22. TEST_INTEQ(siwire_room(wire), 0);
  23. TEST_ASSERT(siwire_puts(wire) > 0);
  24. siwire_free(wire);
  25. TEST_DONE();
  26. }
  27. TEST_FUNC(siwire_dup) {
  28. SiWire * wire = siwire_const("Hello");
  29. SiWire * wired = siwire_dup(wire);
  30. TEST_NOTNULL(wire);
  31. TEST_TRUE(siwire_const_p(wire));
  32. TEST_FALSE(siwire_const_p(wired));
  33. TEST_NOTNULL(siwire_cstr(wire));
  34. TEST_NOTNULL(siwire_cstr(wired));
  35. TEST_INTEQ(siwire_size(wire), 5);
  36. TEST_INTEQ(siwire_size(wired), 5);
  37. siwire_free(wire);
  38. siwire_free(wired);
  39. TEST_DONE();
  40. }
  41. struct Mixin_ {
  42. size_t refs;
  43. };
  44. typedef struct Mixin_ Mixin;
  45. #define mixin(NAME) NAME NAME
  46. struct Buf_ {
  47. char * data;
  48. size_t size;
  49. mixin(Mixin);
  50. size_t space;
  51. };
  52. struct Cnt_ {
  53. int cnt;
  54. mixin(Mixin);
  55. };
  56. typedef struct Buf_ Buf;
  57. #define container_of(DATA, TYPE, FIELD) \
  58. ((TYPE*)(((char *)(&(DATA))) - offsetof(TYPE, FIELD)))
  59. #define mixin_of(DATA, MIXTYPE) \
  60. ((MIXTYPE *)&((DATA)->MIXTYPE))
  61. int main(void) {
  62. Buf buf;
  63. Buf * cont;
  64. Mixin * mix;
  65. TEST_INIT();
  66. printf("Offsetof mix1: %u\n", offsetof(Buf, Mixin));
  67. cont = container_of((buf.Mixin), Buf, Mixin);
  68. mix = mixin_of(&buf, Mixin);
  69. //cont = (Buf*)(((char *)&buf.mix1) - offsetof(Buf, mix1));
  70. printf("&buf, cont, mix: %p %p %p %d\n", &buf, cont, mix, cont == &buf);
  71. puts(SI_OS);
  72. TEST_RUN(siwire_new);
  73. TEST_RUN(siwire_const);
  74. TEST_RUN(siwire_dup);
  75. TEST_REPORT();
  76. }