about summary refs log tree commit diff
path: root/tests.c
blob: 14829a8fe7b3584c741915db4e544c31a928e179 (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <string.h>

#include "ste.h"

static int status;

void
is(const char *desc, int ok)
{
	if (!ok)
		status = 1;
	printf("%s - %s\n", ok ? "ok" : "not ok", desc);
}

int
main()
{
	printf("1..49\n");

	printf("# stecpy\n");

	char buf[16];
	char *end = buf + sizeof buf;
	char buf2[32] = "stringxyzxyzxyzxyzxyz";
	char *pos, *prevpos;

	pos = buf;
	pos = stecpy(pos, end, "abc");
	is("1x3 = 3", strlen(buf) == 3);
	pos = stecpy(pos, end, "def");
	is("2x3 = 6", strlen(buf) == 6);
	pos = stecpy(pos, end, "ghi");
	is("3x3 = 9", strlen(buf) == 9);
	pos = stecpy(pos, end, "jkl");
	is("4x3 = 12", strlen(buf) == 12);
	pos = stecpy(pos, end, "mno");
	is("5x3 = 15", strlen(buf) == 15);
	pos = stecpy(pos, end, "full");
	is("buffer is full", strlen(buf) == 15);
	is("return value is end", pos == end);
	pos = stecpy(pos, end, "fuller");
	is("buffer doesn't get fuller", strlen(buf) == 15);
	is("return value is end", pos == end);

	pos = buf;
	pos = stecpy(pos, end, "abcdefghijklmnopq");
	is("truncation", strlen(buf) == 15);
	is("return value is end", pos == end);

	pos = buf;
	pos = stecpy(pos, end, "xyz");
	pos = stecpy(prevpos=pos, end, "");
	is("empty append works", strlen(buf) == 3);
	is("return value is unchanged", pos == prevpos);

	pos = buf;
	pos = stecpy(pos, end, "xyz");
	pos = stecpy(pos, pos, "foo");
	is("final append works", strlen(buf) == 3);

	printf("# steprn\n");

	pos = buf;
	pos = steprn(pos, end, "%d", 123);
	is("1x3 = 3", strlen(buf) == 3);
	pos = steprn(pos, end, "%d", 456);
	is("2x3 = 6", strlen(buf) == 6);
	pos = steprn(pos, end, "%d", 789);
	is("3x3 = 9", strlen(buf) == 9);
	pos = steprn(pos, end, "%03d", 007);
	is("4x3 = 12", strlen(buf) == 12);
	pos = steprn(pos, end, "%d", -42);
	is("5x3 = 15", strlen(buf) == 15);
	pos = steprn(pos, end, "%d", 7890);
	is("buffer is full", strlen(buf) == 15);
	is("return value is end", pos == end);
	pos = steprn(pos, end, "%d", 67890);
	is("buffer doesn't get fuller", strlen(buf) == 15);
	is("return value is end", pos == end);

	pos = buf;
	pos = steprn(pos, end, "%s", "abcdefghijklmnopq");
	is("truncation", strlen(buf) == 15);
	is("return value is end", pos == end);

	pos = buf;
	pos = steprn(pos, end, "%s%s", "x", "yz");
	pos = steprn(prevpos=pos, end, "");
	is("empty append works", strlen(buf) == 3);
	is("return value is unchanged", pos == prevpos);


	printf("# stecpe\n");

	pos = buf;
	pos = stecpe(pos, end, buf2, buf2 + 6);
	is("1x6 = 6", strlen(buf) == 6);
	pos = stecpe(pos, end, buf2, buf2 + 6);
	is("2x6 = 12", strlen(buf) == 12);
	pos = stecpe(pos, end, buf2, buf2 + 6);
	is("buffer is full", strlen(buf) == 15);
	is("return value is end", pos == end);

	pos = buf;
	pos = steprn(pos, end, buf2, buf2 + sizeof buf2);
	is("truncation", strlen(buf) == 15);
	is("return value is end", pos == end);

	pos = buf;
	pos = stecpe(pos, end, buf2, buf2 + 3);
	pos = stecpe(prevpos=pos, end, buf2, buf2);
	is("empty append works", strlen(buf) == 3);
	is("return value is unchanged", pos == prevpos);


	printf("# stechr\n");
	char *x = stechr(buf2, buf2 + sizeof buf2, 'x');
	is("x found", *x == 'x');
	is("x is at buf2[6]", x == buf2 + 6);
	char *w = stechr(buf2, buf2 + sizeof buf2, 'w');
	is("w not found", *w == 0);
	is("returned end of string instead", w == buf2 + strlen(buf2));
	char *y = stechr(buf2, buf2 + 6, 'y');
	is("y not found in first 6 chars", y == buf2 + 6);


	printf("# steccpy\n");
	pos = buf;
	pos = steccpy(pos, end, "abc,def", ',');
	is("1x3 = 3", strlen(buf) == 3);
	pos = steccpy(pos, end, "def:ghijkl", ':');
	is("2x3 = 6", strlen(buf) == 6);
	pos = steccpy(pos, end, ":ghi", ':');
	pos = steccpy(pos, end, "ghi", ':');
	is("3x3 = 9", strlen(buf) == 9);
	pos = steccpy(pos, end, "jkl", '\0');
	is("4x3 = 12", strlen(buf) == 12);
	pos = steccpy(pos, end, "mnopqst", '!');
	is("5x3 = 15", strlen(buf) == 15);
	pos = steccpy(pos, end, "full", '!');
	is("buffer is full", strlen(buf) == 15);
	is("return value is end", pos == end);
	pos = steccpy(pos, end, "fuller", 'r');
	is("buffer doesn't get fuller", strlen(buf) == 15);
	is("return value is end", pos == end);


	return status;
}