test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "tralloc.h"
  5. void test_tralloc();
  6. void test_tralloc_wrap();
  7. void fail(const char *msg)
  8. {
  9. fprintf(stderr, msg);
  10. exit(EXIT_FAILURE);
  11. }
  12. int main ()
  13. {
  14. #ifdef TRALLOC_WRAP
  15. #define malloc(n) tr_malloc(n)
  16. #define calloc(num, n) tr_calloc(num, n)
  17. #define realloc(ptr, n) tr_realloc(ptr, n)
  18. #define free(ptr) tr_free(ptr)
  19. test_tralloc_wrap ();
  20. #else
  21. test_tralloc();
  22. #endif
  23. return EXIT_SUCCESS;
  24. }
  25. void test_tralloc()
  26. {
  27. size_t siz;
  28. char *str;
  29. printf("tr_siz: %lu, tr_limit: %lu\n", tr_siz(), tr_limit());
  30. if (tr_siz() != 0) fail("initial tr_siz not 0\n");
  31. if (tr_limit() != 0) fail("initial tr_setlimit not 0\n");
  32. siz = 6;
  33. str = tr_malloc(siz);
  34. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  35. if (str == NULL) fail("tr_malloc failed\n");
  36. if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
  37. strcpy(str, "foobar");
  38. if (strcmp(str, "foobar") != 0) fail("strcpy failed\n");
  39. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  40. siz = 12;
  41. str = tr_realloc(str, siz);
  42. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  43. if (str == NULL) fail("tr_realloc failed\n");
  44. if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
  45. strcpy(str, "foobarfoobar");
  46. if (strcmp(str, "foobarfoobar") != 0) fail("strcpy failed\n");
  47. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  48. tr_free(str);
  49. siz = 6;
  50. tr_setlimit(10);
  51. puts("the following should cause an assertion failure:");
  52. fflush(stdout);
  53. str = tr_calloc(2, siz); /* should cause assertion failure */
  54. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  55. tr_free(str);
  56. }
  57. void test_tralloc_wrap()
  58. {
  59. size_t siz;
  60. char *str;
  61. printf("tr_siz: %lu, tr_limit: %lu\n", tr_siz(), tr_limit());
  62. if (tr_siz() != 0) fail("initial tr_siz not 0\n");
  63. if (tr_limit() != 0) fail("initial tr_setlimit not 0\n");
  64. siz = 6;
  65. str = malloc(siz);
  66. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  67. if (str == NULL) fail("malloc failed\n");
  68. if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
  69. strcpy(str, "foobar");
  70. if (strcmp(str, "foobar") != 0) fail("strcpy failed\n");
  71. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  72. siz = 12;
  73. str = realloc(str, siz);
  74. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  75. if (str == NULL) fail("realloc failed\n");
  76. if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
  77. strcpy(str, "foobarfoobar");
  78. if (strcmp(str, "foobarfoobar") != 0) fail("strcpy failed\n");
  79. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  80. free(str);
  81. siz = 6;
  82. tr_setlimit(10);
  83. puts("the following should cause an assertion failure:");
  84. fflush(stdout);
  85. str = calloc(2, siz); /* should cause assertion failure */
  86. printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
  87. free(str);
  88. }