mem.c 1.2 KB

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