summaryrefslogtreecommitdiffstats
path: root/usr/klibc/tests/strlcpycat.c
blob: 14df0cd4dbde2d633d98398d7bc5bfed4adbecce (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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	char temp[8];
	size_t len;

	printf("strlcpy:\n");
	len = strlcpy(temp, "123", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "123") != 0)
		goto error;

	len = strlcpy(temp, "", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "") != 0)
		goto error;

	len = strlcpy(temp, "1234567890", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "1234567") != 0)
		goto error;

	len = strlcpy(temp, "123", 1);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "") != 0)
		goto error;

	len = strlcpy(temp, "1234567890", 1);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "") != 0)
		goto error;

	len = strlcpy(temp, "123", 0);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "") != 0)
		goto error;

	len = strlcpy(temp, "1234567890", 0);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "") != 0)
		goto error;

	len = strlcpy(temp, "1234567", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "1234567") != 0)
		goto error;

	len = strlcpy(temp, "12345678", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "1234567") != 0)
		goto error;

	printf("\n");
	printf("strlcat:\n");
	strcpy(temp, "");
	len = strlcat(temp, "123", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "123") != 0)
		goto error;

	strcpy(temp, "ABC");
	len = strlcat(temp, "", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "ABC") != 0)
		goto error;

	strcpy(temp, "");
	len = strlcat(temp, "", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "") != 0)
		goto error;

	strcpy(temp, "ABC");
	len = strlcat(temp, "123", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "ABC123") != 0)
		goto error;

	strcpy(temp, "ABC");
	len = strlcat(temp, "1234567890", sizeof(temp));
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "ABC1234") != 0)
		goto error;

	strcpy(temp, "ABC");
	len = strlcat(temp, "123", 5);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "ABC1") != 0)
		goto error;

	strcpy(temp, "ABC");
	len = strlcat(temp, "123", 1);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "ABC") != 0)
		goto error;

	strcpy(temp, "ABC");
	len = strlcat(temp, "123", 0);
	printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
	if (strcmp(temp, "ABC") != 0)
		goto error;

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