summaryrefslogtreecommitdiffstats
path: root/usr/klibc/tests/strsearch.c
blob: 01fd55d835fb06f9c57a56f0c35c3e9aee7b603d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <string.h>

int main(void)
{
	const char haystack[] = "haystack";
	const char empty[] = "";
	const char *p;
	size_t len;

	p = strchr(haystack, 'a');
	printf("found 'a' at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 1)
		goto error;
	p = strchr(haystack, 'b');
	printf("found 'b' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strchr(haystack, 0);
	printf("found 0 at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 8)
		goto error;

	p = strrchr(haystack, 'a');
	printf("found 'a' at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 5)
		goto error;
	p = strrchr(haystack, 'b');
	printf("found 'b' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strrchr(haystack, 0);
	printf("found 0 at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 8)
		goto error;

	len = strspn(haystack, "hasty");
	printf("found %zu chars from 'hasty'\n", len);
	if (len != 6)
		goto error;
	len = strspn(haystack, "haystack");
	printf("found %zu chars from 'haystack'\n", len);
	if (len != 8)
		goto error;
	len = strspn(haystack, "");
	printf("found %zu chars from ''\n", len);
	if (len != 0)
		goto error;

	len = strcspn(haystack, "stick");
	printf("found %zu chars not from 'stick'\n", len);
	if (len != 3)
		goto error;
	len = strcspn(haystack, "needle");
	printf("found %zu chars not from 'needle'\n", len);
	if (len != 8)
		goto error;
	len = strcspn(haystack, "");
	printf("found %zu chars not from ''\n", len);
	if (len != 8)
		goto error;

	p = strpbrk(haystack, "stick");
	printf("found char from 'stick' at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 3)
		goto error;
	p = strpbrk(haystack, "needle");
	printf("found char from 'needle' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strpbrk(haystack, "");
	printf("found char from '' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;

	p = strstr(haystack, "stack");
	printf("found 'stack' at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 3)
		goto error;
	p = strstr(haystack, "tacks");
	printf("found 'tacks' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strstr(haystack, "needle");
	printf("found 'needle' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strstr(haystack, "k");
	printf("found 'k' at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack + 7)
		goto error;
	p = strstr(haystack, "b");
	printf("found 'b' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strstr(haystack, "kk");
	printf("found 'kk' at offset %zd\n", p ? p - haystack : -1);
	if (p != NULL)
		goto error;
	p = strstr(haystack, "");
	printf("found '' at offset %zd\n", p ? p - haystack : -1);
	if (p != haystack)
		goto error;
	p = strstr(empty, "");
	printf("found '' at offset %zd\n", p ? p - empty : -1);
	if (p != empty)
		goto error;

	return 0;
error:
	printf("unexpected result\n");
	return 1;
}