mem.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. char arr2[51];
  16. memset(arr2, 0, 51); // Compiler builtin, should work
  17. memccpy((void *)arr2, (void *)arr, 1, 100);
  18. if (arr[50] != 1) {
  19. puts("Incorrect memccpy");
  20. exit(EXIT_FAILURE);
  21. }
  22. puts("Correct memccpy");
  23. int res;
  24. if ((res = memcmp("hello world", "hello world", 11))) {
  25. printf("Incorrect memcmp (1), expected 0 found %d\n", res);
  26. exit(EXIT_FAILURE);
  27. }
  28. if ((res = memcmp("hello world", "hello worlt", 11)) >= 0) {
  29. printf("Incorrect memcmp (2), expected -, found %d\n", res);
  30. exit(EXIT_FAILURE);
  31. }
  32. if ((res = memcmp("hello world", "hallo world", 5)) <= 0) {
  33. printf("Incorrect memcmp (3), expected +, found %d\n", res);
  34. exit(EXIT_FAILURE);
  35. }
  36. if ((res = memcmp("hello world", "henlo world", 5)) >= 0) {
  37. printf("Incorrect memcmp (4), expected -, found %d\n", res);
  38. exit(EXIT_FAILURE);
  39. }
  40. puts("Correct memcmp");
  41. }