Guest User

Untitled

a guest
Feb 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <criterion/criterion.h>
  2. #include "ft_malloc.h"
  3. #include <limits.h>
  4. #include <stdio.h>
  5.  
  6. void *ft_malloc(size_t size);
  7.  
  8. Test(evaluation, test1)
  9. {
  10. int i;
  11. char *addr;
  12.  
  13. i = 0;
  14. while (i < 1024)
  15. {
  16. addr = (char *)ft_malloc(1024);
  17. addr[0] = 42;
  18. i += 1;
  19. }
  20. cr_assert(addr[0] == 42);
  21. }
  22.  
  23. Test(tiny_basic, simple_integer)
  24. {
  25. int *p = 0;
  26.  
  27. p = ft_malloc(sizeof(int));
  28. cr_assert_not(p == NULL);
  29. *p = 42;
  30. cr_assert_eq(*p, 42);
  31. }
  32.  
  33. Test(tiny_basic, two_integer)
  34. {
  35. int *p = 0;
  36. int *q = 0;
  37.  
  38. p = ft_malloc(sizeof(int));
  39. q = ft_malloc(sizeof(int));
  40. cr_assert_not(p == NULL);
  41. cr_assert_not(q == NULL);
  42.  
  43. cr_assert_not(p == q);
  44.  
  45. *p = 42;
  46. *q = 1337;
  47. cr_assert_eq(*p, 42);
  48. cr_assert_eq(*q, 1337);
  49. }
  50.  
  51. Test(tiny_basic, int_max)
  52. {
  53. int *p = 0;
  54.  
  55. p = ft_malloc(sizeof(int));
  56. cr_assert_not(p == NULL);
  57. *p = INT_MAX;
  58. cr_assert_eq(*p, INT_MAX);
  59. }
  60.  
  61. Test(tiny_basic, many)
  62. {
  63. int *p[500] = { NULL };
  64. int i;
  65.  
  66. i = 0;
  67. while (i < 500)
  68. {
  69. p[i] = ft_malloc(sizeof(int));
  70. *p[i] = 4242 + i;
  71. i += 1;
  72. }
  73. i = 0;
  74. while (i < 500)
  75. {
  76. cr_assert_eq(*p[i], 4242 + i);
  77. i += 1;
  78. }
  79. }
  80.  
  81. Test(tiny_basic, many_more)
  82. {
  83. int *p[5000] = { NULL };
  84. int i;
  85.  
  86. i = 0;
  87. while (i < 5000)
  88. {
  89. p[i] = ft_malloc(sizeof(int));
  90. *p[i] = 42 + i;
  91. i += 1;
  92. }
  93. i = 0;
  94. while (i < 5000)
  95. {
  96. cr_assert_eq(*p[i], 42 + i);
  97. i += 1;
  98. }
  99. }
  100.  
  101. Test(small_basic, medium_struct)
  102. {
  103. struct s_med {
  104. int a;
  105. int b;
  106. int c;
  107. int d;
  108. int e;
  109. int f;
  110. };
  111.  
  112. struct s_med *a = ft_malloc(sizeof(struct s_med));
  113.  
  114. cr_assert_not_null(a);
  115.  
  116. a->a = 42;
  117. a->b = 1337;
  118. a->c = 230598;
  119. a->d = 42;
  120. a->e = 1337;
  121. a->f = 230598;
  122.  
  123. cr_assert_eq(a->a, 42);
  124. cr_assert_eq(a->b, 1337);
  125. cr_assert_eq(a->c, 230598);
  126. cr_assert_eq(a->d, 42);
  127. cr_assert_eq(a->e, 1337);
  128. cr_assert_eq(a->f, 230598);
  129. }
  130.  
  131. Test(small_basic, overwrite_int)
  132. {
  133. struct s_test {
  134. int a;
  135. int b;
  136. int c;
  137. int d;
  138. int e;
  139. };
  140.  
  141. struct s_test *a;
  142.  
  143. a = ft_malloc(sizeof(struct s_test));
  144. a->a = 10;
  145. a->b = 42;
  146. a->c = 123134;
  147. a->d = 1337;
  148. a->e = 99999;
  149.  
  150. int *p = ft_malloc(sizeof(int));
  151. *p = 628380674;
  152.  
  153. cr_assert_eq(a->a, 10);
  154. cr_assert_eq(a->b, 42);
  155. cr_assert_eq(a->c, 123134);
  156. cr_assert_eq(a->d, 1337);
  157. cr_assert_eq(a->e, 99999);
  158. }
Add Comment
Please, Sign In to add comment