about summary refs log tree commit diff
path: root/tests.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-11-05 18:52:11 +0100
committerLeah Neukirchen <leah@vuxu.org>2021-11-05 18:52:46 +0100
commit75ea20675d012e692996bc6beae9149b0deb08b0 (patch)
treeab1569403e1c5f8e76f5f93f969a3335275c54a5 /tests.c
downloadlibste-75ea20675d012e692996bc6beae9149b0deb08b0.tar.gz
libste-75ea20675d012e692996bc6beae9149b0deb08b0.tar.xz
libste-75ea20675d012e692996bc6beae9149b0deb08b0.zip
initial commit
Diffstat (limited to 'tests.c')
-rw-r--r--tests.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/tests.c b/tests.c
new file mode 100644
index 0000000..9255a70
--- /dev/null
+++ b/tests.c
@@ -0,0 +1,125 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "ste.h"
+
+static int status;
+
+void
+is(char *desc, int ok)
+{
+	if (!ok)
+		status = 1;
+	printf("%s - %s\n", ok ? "ok" : "not ok", desc);
+}
+
+int
+main()
+{
+	printf("1..39\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);
+
+
+	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);
+
+
+	return status;
+}