strings.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <strings.h>
  5. #include "test_helpers.h"
  6. int main(void) {
  7. assert(!bcmp("hello", "hehe", 2));
  8. assert(bcmp("hello", "haha", 2));
  9. char* new = malloc(3);
  10. bcopy("hi", new, 3); // include nul byte
  11. assert(!strcasecmp("hi", new));
  12. assert(strcasecmp("he", new));
  13. assert(strcasecmp("hello", "HEllO") == 0);
  14. assert(strcasecmp("hello", "HEllOo") < 0);
  15. assert(strcasecmp("5", "5") == 0);
  16. assert(strcasecmp("5", "4") > 0);
  17. assert(strcasecmp("5", "6") < 0);
  18. assert(strncasecmp("hello", "Hello World", 5) == 0);
  19. assert(strncasecmp("FLOOR0_1", "FLOOR0_1FLOOR4_1", 8) == 0);
  20. assert(strncasecmp("FL00RO_1", "FLOOR0_1FLOOR4_1", 8) < 0);
  21. // Ensure we aren't relying on the 5th (lowercase) bit on non-alpha characters
  22. assert(strcasecmp("{[", "[{") > 0);
  23. bzero(new, 1);
  24. assert(*new == 0);
  25. assert(*(new+1) == 'i');
  26. assert(*(new+2) == 0);
  27. assert(ffs(1) == 1);
  28. assert(ffs(2) == 2);
  29. assert(ffs(3) == 1);
  30. assert(ffs(10) == 2);
  31. char* str = "hihih";
  32. assert(index(str, 'i') == str + 1);
  33. assert(rindex(str, 'i') == str + 3);
  34. char buf[] = "password";
  35. explicit_bzero(buf, sizeof(buf));
  36. assert(buf[0] == 0);
  37. }