rewrite the integration tests into C

This commit is contained in:
Vlasta Vesely 2021-03-12 19:33:56 +01:00
parent 739c7ea462
commit e45479ea96
7 changed files with 115 additions and 37 deletions

View file

@ -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

97
tests/integration.c Normal file
View file

@ -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);
}

8
tests/integration.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef __TEST_INTEGRATION_H
#define __TEST_INTEGRATION_H
#include <check.h>
void register_integration_tests(TCase *test_case);
#endif /* __TEST_INTEGRATION_H */

View file

@ -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"

View file

@ -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;

View file

@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <check.h>
#endif /* __TEST_TEST_H */

View file

@ -1,10 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* https://en.wikipedia.org/wiki/Velthuis */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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) {