test_mem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "si_mem.h"
  2. #include "si_test.h"
  3. TEST_FUNC(si_malloc) {
  4. void * aid = si_malloc(10);
  5. TEST_NOTNULL(aid);
  6. TEST_NULL(si_free(aid));
  7. TEST_DONE();
  8. }
  9. TEST_FUNC(si_memcpy) {
  10. char src[10] = "banana";
  11. char dst[10];
  12. char * res = si_memcpy(dst, src, 7);
  13. TEST_NOTNULL(res);
  14. TEST_INTEQ(res[0], 'b');
  15. TEST_INTEQ(res[5], 'a');
  16. TEST_DONE();
  17. }
  18. TEST_FUNC(si_copyalloc) {
  19. char src[10] = "banana";
  20. char * res = si_copyalloc(5, src, 3);
  21. TEST_NOTNULL(res);
  22. TEST_INTEQ(res[0], 'b');
  23. TEST_INTEQ(res[1], 'a');
  24. TEST_INTEQ(res[2], 'n');
  25. TEST_INTEQ(res[3], 0 );
  26. si_free(res);
  27. TEST_DONE();
  28. }
  29. TEST_FUNC(si_smemcpy) {
  30. char src[8] = "banana";
  31. char dst[10] = { 0, 1, 2, 3, 4, 5, 6 , 7 , 8, 9 };
  32. char * res = si_smemcpy(dst, 14, 7, src, 8, 1, 77);
  33. TEST_NOTNULL(res);
  34. TEST_INTEQ(res[0], 0);
  35. TEST_INTEQ(res[6], 6);
  36. TEST_INTEQ(res[7], 'a');
  37. TEST_INTEQ(res[8], 'n');
  38. TEST_INTEQ(res[9], 'a');
  39. TEST_DONE();
  40. }
  41. TEST_FUNC(si_realloc) {
  42. char * aid = si_malloc(10);
  43. char * new = NULL;
  44. TEST_NOTNULL(aid);
  45. TEST_NULL(new);
  46. aid[2] = 10;
  47. TEST_INTEQ(aid[2], 10);
  48. new = si_realloc(aid, 20);
  49. TEST_NOTNULL(new);
  50. TEST_INTEQ(new[2], 10);
  51. TEST_NULL(si_free(new));
  52. TEST_DONE();
  53. }
  54. struct TestMem1_ {
  55. int i;
  56. char c;
  57. };
  58. typedef struct TestMem1_ TestMem1;
  59. TEST_FUNC(si_mem) {
  60. char * aid = "Hi!";
  61. char buf[32];
  62. SiMem * mem, *me2, *me3, *me4;
  63. TestMem1 tm1 = { 1, 2 } ;
  64. TestMem1 *tmptr;
  65. mem = simem_new(16);
  66. simem_putc(mem, 0 , 'x');
  67. TEST_INTEQ(simem_getc(mem, 0), 'x');
  68. TEST_INTEQ(simem_room(mem), 16);
  69. TEST_ZERO(simem_cmpc(mem, 0, 'x'));
  70. TEST_INTEQ(simem_putdata(mem, 1 , aid, 4), 4);
  71. TEST_ZERO(simem_cmpc(mem, 0, 'x'));
  72. TEST_ZERO(simem_cmpc(mem, 1, 'H'));
  73. TEST_ZERO(simem_cmpc(mem, 2, 'i'));
  74. TEST_ZERO(simem_cmpc(mem, 3, '!'));
  75. TEST_ZERO(simem_cmpc(mem, 4, '\0'));
  76. TEST_INTEQ(simem_putdata(mem, 16, aid, 4), 0);
  77. TEST_INTEQ(simem_putdata(mem, 1 , aid, 0), 0);
  78. TEST_INTEQ(simem_putdata(mem, 15, aid, 4), 1);
  79. TEST_INTEQ(simem_getdata(mem, 1 , buf, 4), 4);
  80. TEST_STREQ(buf, aid);
  81. TEST_INTEQ(simem_getdata(mem, 16, buf, 4), 0);
  82. TEST_INTEQ(simem_getdata(mem, 1 , buf, 0), 0);
  83. TEST_STREQ(buf, aid);
  84. TEST_INTEQ(simem_getdata(mem, 15, buf, 4), 1);
  85. TEST_INTEQ(buf[0], 'H');
  86. TEST_ZERO(simem_cmpdata(mem, 0, "xHi!\0", 5));
  87. TEST_NOTNULL(simem_realloc(mem, 32));
  88. TEST_INTEQ(simem_room(mem), 32);
  89. TEST_NULL(simem_cmpdata(mem, 0, "xHi!\0", 5));
  90. TEST_ZERO(simem_cmpc(mem, 15, 'H'));
  91. me2 = simem_dup(mem);
  92. simem_fillall(mem, 0);
  93. TEST_ZERO(simem_cmpc(mem, 0, 0));
  94. TEST_NULL(simem_cmpdata(me2, 0, "xHi!\0", 5));
  95. TEST_NULL(simem_cmpdata(me2, 15, "H", 1));
  96. me3 = simem_newptr(64);
  97. TEST_NOTNULL(simem_putptr(me3, 6, me2));
  98. TEST_NOTNULL(simem_putptr(me3, 7, me3));
  99. TEST_NULL(simem_putptr(me3, 88, mem));
  100. TEST_PTREQ(me2, simem_getptr(me3, 6));
  101. TEST_PTREQ(me3, simem_getptr(me3, 7));
  102. TEST_NULL(simem_getptr(me3, 88));
  103. TEST_NULL(simem_getptr(me3, 1));
  104. me4 = simem_newelement(10, sizeof(TestMem1));
  105. TEST_NOTNULL(simem_putelement(me4, 7, &tm1, sizeof(TestMem1)));
  106. TEST_NULL(simem_putelement(me4, 10, &tm1, sizeof(TestMem1)));
  107. tmptr = (TestMem1 *) simem_getelement(me4, 7, sizeof(TestMem1));
  108. TEST_NULL(simem_getelement(me4, 10, sizeof(TestMem1)));
  109. TEST_NOTNULL(tmptr);
  110. TEST_INTEQ(tmptr->i, 1);
  111. TEST_INTEQ(tmptr->c, 2);
  112. TEST_PTRNEQ(&tm1, tmptr);
  113. simem_free(mem);
  114. simem_free(me2);
  115. simem_free(me3);
  116. simem_free(me4);
  117. TEST_DONE();
  118. }
  119. int main(void) {
  120. TEST_INIT();
  121. TEST_RUN(si_malloc);
  122. TEST_RUN(si_memcpy);
  123. TEST_RUN(si_smemcpy);
  124. TEST_RUN(si_copyalloc);
  125. TEST_RUN(si_realloc);
  126. TEST_RUN(si_mem);
  127. TEST_REPORT();
  128. }