From e45479ea96fe20424d4b7f521be4e0ea27520be1 Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Fri, 12 Mar 2021 19:33:56 +0100 Subject: [PATCH] rewrite the integration tests into C --- Makefile.in | 4 +- tests/integration.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ tests/integration.h | 8 ++++ tests/integration.sh | 29 ------------- tests/main.c | 3 ++ tests/test.h | 2 + velthuis.c | 9 ++-- 7 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 tests/integration.c create mode 100644 tests/integration.h delete mode 100644 tests/integration.sh diff --git a/Makefile.in b/Makefile.in index 206cce2..76c3f2e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -24,7 +24,7 @@ OBJECTS = iast.o iast-czech.o transliteration.o transcription.o utf8.o \ velthuis.o TEST_OBJECTS = tests/main.o tests/translit.o tests/transcript.o \ - tests/velthuis.o tests/utf8.o + tests/velthuis.o tests/utf8.o tests/integration.o AUX_FILES = Makefile configure aclocal.m4 install-sh config.h* *.log \ *.status *.cache @@ -36,7 +36,7 @@ include $(wildcard *.d tests/*.d) $(PROGNAME): main.o $(OBJECTS) $(QUIET_LD) $(CC) $^ -o $@ $(LFLAGS) -test: tests/test +test: all tests/test %.o: %.c diff --git a/tests/integration.c b/tests/integration.c new file mode 100644 index 0000000..57f60aa --- /dev/null +++ b/tests/integration.c @@ -0,0 +1,97 @@ +#include "test.h" +#include "integration.h" +#include "../transliteration.h" + +static char *exec_command(const char *command) +{ + char buf[512], *ret = NULL; + FILE *proc; + int n, len = 0; + + proc = popen(command, "r"); + + while (1) { + n = fread(buf, 1, sizeof(buf), proc); + if (n == 0 || n == -1) + break; + ret = realloc(ret, len + n + 1); + memcpy(ret + len, buf, n); + len += n; + } + + if (ret) + ret[len] = '\0'; + + pclose(proc); + + return ret; +} + +static char *read_file(const char *filename) +{ + unsigned char buf[512]; + char *ret = NULL; + int fd, len = 0, n; + + fd = open(filename, O_RDONLY); + while (1) { + n = read(fd, buf, sizeof(buf)); + if (n == 0 || n == -1) + break; + ret = realloc(ret, len + n + 1); + memcpy(ret + len, buf, n); + len += n; + } + + if (ret) + ret[len] = '\0'; + + close(fd); + + return ret; +} + +static void test_file_transliteration(const char *filename) +{ + char cmd[512], *a, *b; + + sprintf(cmd, "./iast -f %s | ./iast -r -f -", filename); + a = exec_command(cmd); + b = read_file(filename); + ck_assert_str_eq(a, b); + free(a); + free(b); +} + +START_TEST(test_integration) +{ + test_file_transliteration("tests/texts/bhagavadgita-1.txt"); + test_file_transliteration("tests/texts/mandukya-upanishad.txt"); + test_file_transliteration("tests/texts/rg-01001.txt"); +} +END_TEST + +START_TEST(test_version) +{ + char *out = exec_command("./iast -v"); + ck_assert_str_eq("iast v2.0.0\n", out); + free(out); +} +END_TEST + +START_TEST(test_usage) +{ + char *out; + out = exec_command("./iast"); + out[44] = '\0'; /* the first line is enough here */ + ck_assert_str_eq("iast, a helper for Sanskrit transliteration.", out); + free(out); +} +END_TEST + +void register_integration_tests(TCase *test_case) +{ + tcase_add_test(test_case, test_integration); + tcase_add_test(test_case, test_version); + tcase_add_test(test_case, test_usage); +} diff --git a/tests/integration.h b/tests/integration.h new file mode 100644 index 0000000..46723cd --- /dev/null +++ b/tests/integration.h @@ -0,0 +1,8 @@ +#ifndef __TEST_INTEGRATION_H +#define __TEST_INTEGRATION_H + +#include + +void register_integration_tests(TCase *test_case); + +#endif /* __TEST_INTEGRATION_H */ diff --git a/tests/integration.sh b/tests/integration.sh deleted file mode 100644 index b9b50a1..0000000 --- a/tests/integration.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh -set -e - -usage=$(./iast -h) -test -n $(echo "$usage" | grep "iast [flags and text arguments in any order]") - -version=$(./iast -v) -test "$version" = "iast v2.0.0" - -./iast -f tests/texts/bhagavadgita-1.txt | - ./iast -r -f - >/tmp/iast-bhagavadgita-1.txt.out -./iast -f tests/texts/mandukya-upanishad.txt | - ./iast -r -f - >/tmp/iast-mandukya-upanishad.txt.out -./iast -f tests/texts/rg-01001.txt | - ./iast -r -f - >/tmp/rg-01001.txt.out - -expected=$(sha1sum tests/texts/bhagavadgita-1.txt | head -c40) -computed=$(sha1sum /tmp/iast-bhagavadgita-1.txt.out | head -c40) -test "$expected" = "$computed" - -expected=$(sha1sum tests/texts/mandukya-upanishad.txt | head -c40) -computed=$(sha1sum /tmp/iast-mandukya-upanishad.txt.out | head -c40) -test "$expected" = "$computed" - -expected=$(sha1sum tests/texts/rg-01001.txt | head -c40) -computed=$(sha1sum /tmp/rg-01001.txt.out | head -c40) -test "$expected" = "$computed" - -printf "\033[32mpassed\033[0m\n" diff --git a/tests/main.c b/tests/main.c index 3684828..e025e5c 100644 --- a/tests/main.c +++ b/tests/main.c @@ -3,6 +3,7 @@ #include "transcript.h" #include "velthuis.h" #include "utf8.h" +#include "integration.h" static Suite *create_test_suite() { @@ -17,6 +18,8 @@ static Suite *create_test_suite() register_velthuis_encoder_tests(test_case); register_utf8_tests(test_case); + register_integration_tests(test_case); + suite_add_tcase(suite, test_case); return suite; diff --git a/tests/test.h b/tests/test.h index 3bbe929..eeea412 100644 --- a/tests/test.h +++ b/tests/test.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include #endif /* __TEST_TEST_H */ diff --git a/velthuis.c b/velthuis.c index bb727db..5607d37 100644 --- a/velthuis.c +++ b/velthuis.c @@ -1,10 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* https://en.wikipedia.org/wiki/Velthuis */ -#include -#include -#include - +#include "compat.h" #include "velthuis.h" #include "utf8.h" @@ -61,9 +58,9 @@ int encode_velthuis_to_iast_punctation(const char *text, char **out) const struct encoder_tuple *tuple; char *buf, *dest; - buf = calloc(1, strlen(text) << 1); + buf = calloc(1, strlen(text) * 2); /* should be enough */ if (buf == NULL) - return NULL; + return ENOMEM; dest = buf; while (str < end) {