mem.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "test_helpers.h"
  5. int main(void) {
  6. puts("# mem #");
  7. char arr[100];
  8. memset(arr, 0, 100); // Compiler builtin, should work
  9. arr[50] = 1;
  10. if ((size_t)memchr((void *)arr, 1, 100) - (size_t)arr != 50) {
  11. puts("Incorrect memchr");
  12. exit(EXIT_FAILURE);
  13. }
  14. puts("Correct memchr");
  15. if ((size_t)memrchr((void *)arr, 1, 100) - (size_t)arr != 50) {
  16. puts("Incorrect memrchr");
  17. exit(EXIT_FAILURE);
  18. }
  19. puts("Correct memrchr");
  20. char arr2[51];
  21. memset(arr2, 0, 51); // Compiler builtin, should work
  22. memccpy((void *)arr2, (void *)arr, 1, 100);
  23. if (arr[50] != 1) {
  24. puts("Incorrect memccpy");
  25. exit(EXIT_FAILURE);
  26. }
  27. puts("Correct memccpy");
  28. int res;
  29. if ((res = memcmp("hello world", "hello world", 11))) {
  30. printf("Incorrect memcmp (1), expected 0 found %d\n", res);
  31. exit(EXIT_FAILURE);
  32. }
  33. if ((res = memcmp("hello world", "hello worlt", 11)) >= 0) {
  34. printf("Incorrect memcmp (2), expected -, found %d\n", res);
  35. exit(EXIT_FAILURE);
  36. }
  37. if ((res = memcmp("hello world", "hallo world", 5)) <= 0) {
  38. printf("Incorrect memcmp (3), expected +, found %d\n", res);
  39. exit(EXIT_FAILURE);
  40. }
  41. if ((res = memcmp("hello world", "henlo world", 5)) >= 0) {
  42. printf("Incorrect memcmp (4), expected -, found %d\n", res);
  43. exit(EXIT_FAILURE);
  44. }
  45. puts("Correct memcmp");
  46. }