1570af302Sopenharmony_ci#define _DEFAULT_SOURCE 1
2570af302Sopenharmony_ci#define _BSD_SOURCE 1
3570af302Sopenharmony_ci#include <stdio.h>
4570af302Sopenharmony_ci#include <string.h>
5570af302Sopenharmony_ci#include "test.h"
6570af302Sopenharmony_ci
7570af302Sopenharmony_ci/* r = place to store result
8570af302Sopenharmony_ci * f = function call to test (or any expression)
9570af302Sopenharmony_ci * x = expected result
10570af302Sopenharmony_ci * m = message to print on failure (with formats for r & x)
11570af302Sopenharmony_ci**/
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \
14570af302Sopenharmony_ci	((r) = (f)) == (x) || \
15570af302Sopenharmony_ci	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
16570af302Sopenharmony_ci
17570af302Sopenharmony_ci#define TEST_S(s, x, m) ( \
18570af302Sopenharmony_ci	!strcmp((s),(x)) || \
19570af302Sopenharmony_ci	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
20570af302Sopenharmony_ci
21570af302Sopenharmony_ciint main(void)
22570af302Sopenharmony_ci{
23570af302Sopenharmony_ci	char b[32];
24570af302Sopenharmony_ci	char *s;
25570af302Sopenharmony_ci	int i;
26570af302Sopenharmony_ci
27570af302Sopenharmony_ci	b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0;
28570af302Sopenharmony_ci	TEST(s, strcpy(b, b+16), b, "wrong return %p != %p");
29570af302Sopenharmony_ci	TEST_S(s, "abc", "strcpy gave incorrect string");
30570af302Sopenharmony_ci	TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p");
31570af302Sopenharmony_ci	TEST_S(s, "abc", "strcpy gave incorrect string");
32570af302Sopenharmony_ci	TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p");
33570af302Sopenharmony_ci	TEST_S(s, "abc", "strcpy gave incorrect string");
34570af302Sopenharmony_ci	TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p");
35570af302Sopenharmony_ci	TEST_S(s, "abc", "strcpy gave incorrect string");
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci	TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p");
38570af302Sopenharmony_ci	TEST_S(s, "bc", "strcpy gave incorrect string");
39570af302Sopenharmony_ci	TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p");
40570af302Sopenharmony_ci	TEST_S(s, "c", "strcpy gave incorrect string");
41570af302Sopenharmony_ci	TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p");
42570af302Sopenharmony_ci	TEST_S(s, "", "strcpy gave incorrect string");
43570af302Sopenharmony_ci
44570af302Sopenharmony_ci	TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
45570af302Sopenharmony_ci	TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
46570af302Sopenharmony_ci	TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
47570af302Sopenharmony_ci	TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci	b[3] = 'x'; b[4] = 0;
50570af302Sopenharmony_ci	strncpy(b, "abc", 3);
51570af302Sopenharmony_ci	TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
52570af302Sopenharmony_ci	TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
53570af302Sopenharmony_ci
54570af302Sopenharmony_ci	TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
55570af302Sopenharmony_ci	TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci	strcpy(b, "abc");
58570af302Sopenharmony_ci	TEST(s, strncat(b, "123456", 3), b, "%p != %p");
59570af302Sopenharmony_ci	TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
60570af302Sopenharmony_ci	TEST_S(s, "abc123", "strncat gave incorrect string");
61570af302Sopenharmony_ci
62570af302Sopenharmony_ci	strcpy(b, "aaababccdd0001122223");
63570af302Sopenharmony_ci	TEST(s, strchr(b, 'b'), b+3, "%p != %p");
64570af302Sopenharmony_ci	TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
65570af302Sopenharmony_ci	TEST(i, strspn(b, "abcd"), 10, "%d != %d");
66570af302Sopenharmony_ci	TEST(i, strcspn(b, "0123"), 10, "%d != %d");
67570af302Sopenharmony_ci	TEST(s, strpbrk(b, "0123"), b+10, "%d != %d");
68570af302Sopenharmony_ci
69570af302Sopenharmony_ci	strcpy(b, "abc   123; xyz; foo");
70570af302Sopenharmony_ci	TEST(s, strtok(b, " "), b, "%p != %p");
71570af302Sopenharmony_ci	TEST_S(s, "abc", "strtok result");
72570af302Sopenharmony_ci
73570af302Sopenharmony_ci	TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
74570af302Sopenharmony_ci	TEST_S(s, "  123", "strtok result");
75570af302Sopenharmony_ci
76570af302Sopenharmony_ci	TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
77570af302Sopenharmony_ci	TEST_S(s, "xyz", "strtok result");
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci	TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
80570af302Sopenharmony_ci	TEST_S(s, "foo", "strtok result");
81570af302Sopenharmony_ci
82570af302Sopenharmony_ci	memset(b, 'x', sizeof b);
83570af302Sopenharmony_ci	TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
84570af302Sopenharmony_ci	TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
85570af302Sopenharmony_ci	TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
86570af302Sopenharmony_ci
87570af302Sopenharmony_ci	memset(b, 'x', sizeof b);
88570af302Sopenharmony_ci	TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
89570af302Sopenharmony_ci	TEST(i, b[0], 'a', "strlcpy did not copy character %d");
90570af302Sopenharmony_ci	TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
91570af302Sopenharmony_ci
92570af302Sopenharmony_ci	memset(b, 'x', sizeof b);
93570af302Sopenharmony_ci	TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
94570af302Sopenharmony_ci	TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
95570af302Sopenharmony_ci
96570af302Sopenharmony_ci	TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
97570af302Sopenharmony_ci
98570af302Sopenharmony_ci	memcpy(b, "abc\0\0\0x\0", 8);
99570af302Sopenharmony_ci	TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
100570af302Sopenharmony_ci	TEST_S(b, "abc123", "strlcat result");
101570af302Sopenharmony_ci
102570af302Sopenharmony_ci	memcpy(b, "abc\0\0\0x\0", 8);
103570af302Sopenharmony_ci	TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
104570af302Sopenharmony_ci	TEST_S(b, "abc12", "strlcat result");
105570af302Sopenharmony_ci	TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
106570af302Sopenharmony_ci
107570af302Sopenharmony_ci	memcpy(b, "abc\0\0\0x\0", 8);
108570af302Sopenharmony_ci	TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
109570af302Sopenharmony_ci	TEST_S(b, "abc", "strlcat result");
110570af302Sopenharmony_ci
111570af302Sopenharmony_ci	memcpy(b, "abc\0\0\0x\0", 8);
112570af302Sopenharmony_ci	TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
113570af302Sopenharmony_ci	TEST_S(b, "abc", "strlcat result");
114570af302Sopenharmony_ci
115570af302Sopenharmony_ci	return t_status;
116570af302Sopenharmony_ci}
117