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