]> git.kaiwu.me - klib.git/commitdiff
Add kputw() and kputl() tests
authorJohn Marshall <jm18@sanger.ac.uk>
Mon, 22 Jul 2013 15:07:45 +0000 (16:07 +0100)
committerJohn Marshall <jm18@sanger.ac.uk>
Mon, 22 Jul 2013 15:07:45 +0000 (16:07 +0100)
.gitignore
test/Makefile
test/kstring_test.c [new file with mode: 0644]

index d6ead37eebce0af90c27254378ac2ff2d9950bc6..010a8ebe69efc398f498f28cc8c9b1b6b050bed4 100644 (file)
@@ -36,4 +36,5 @@ test/ksort_test
 test/ksort_test-stl
 test/kstring_bench
 test/kstring_bench2
+test/kstring_test
 test/kvec_test
index cc52bb90c952f0ba806ed3e59b21131452e12cb5..22c726122147fd22b5ae19561b14f1fbbc6febb8 100644 (file)
@@ -3,7 +3,7 @@ CXX=g++
 CFLAGS=-g -Wall -O2 -I..
 CXXFLAGS=$(CFLAGS)
 PROGS=kbtree_test khash_keith khash_keith2 khash_test klist_test kseq_test kseq_bench \
-               kseq_bench2 ksort_test ksort_test-stl kvec_test kmin_test kstring_bench kstring_bench2
+               kseq_bench2 ksort_test ksort_test-stl kvec_test kmin_test kstring_bench kstring_bench2 kstring_test
 
 all:$(PROGS)
 
@@ -52,3 +52,5 @@ kstring_bench:kstring_bench.c ../kstring.h ../kstring.c
 kstring_bench2:kstring_bench2.c ../kstring.h ../kstring.c
                $(CC) $(CFLAGS) -o $@ kstring_bench2.c ../kstring.c
 
+kstring_test:kstring_test.c ../kstring.h ../kstring.c
+               $(CC) $(CFLAGS) -o $@ kstring_test.c ../kstring.c
diff --git a/test/kstring_test.c b/test/kstring_test.c
new file mode 100644 (file)
index 0000000..76f9532
--- /dev/null
@@ -0,0 +1,76 @@
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "kstring.h"
+
+int nfail = 0;
+
+void check(const char *what, const kstring_t *ks, const char *correct)
+{
+       if (ks->l != strlen(correct) || strcmp(ks->s, correct) != 0) {
+               fprintf(stderr, "%s produced \"%.*s\" (\"%s\" is correct)\tFAIL\n", what, (int)(ks->l), ks->s, correct);
+               nfail++;
+       }
+}
+
+void test_kputw(kstring_t *ks, int n)
+{
+       char buf[16];
+
+       ks->l = 0;
+       kputw(n, ks);
+
+       sprintf(buf, "%d", n);
+       check("kputw()", ks, buf);
+}
+
+void test_kputl(kstring_t *ks, long n)
+{
+       char buf[24];
+
+       ks->l = 0;
+       kputl(n, ks);
+
+       sprintf(buf, "%ld", n);
+       check("kputl()", ks, buf);
+}
+
+int main()
+{
+       kstring_t ks;
+
+       ks.l = ks.m = 0;
+       ks.s = NULL;
+
+       test_kputw(&ks, 0);
+       test_kputw(&ks, 1);
+       test_kputw(&ks, 37);
+       test_kputw(&ks, 12345);
+       test_kputw(&ks, -12345);
+       test_kputw(&ks, INT_MAX);
+       test_kputw(&ks, -INT_MAX);
+       test_kputw(&ks, INT_MIN);
+
+       test_kputl(&ks, 0);
+       test_kputl(&ks, 1);
+       test_kputl(&ks, 37);
+       test_kputl(&ks, 12345);
+       test_kputl(&ks, -12345);
+       test_kputl(&ks, INT_MAX);
+       test_kputl(&ks, -INT_MAX);
+       test_kputl(&ks, INT_MIN);
+       test_kputl(&ks, LONG_MAX);
+       test_kputl(&ks, -LONG_MAX);
+       test_kputl(&ks, LONG_MIN);
+
+       free(ks.s);
+
+       if (nfail > 0) {
+               fprintf(stderr, "Total failures: %d\n", nfail);
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_SUCCESS;
+}