2021-03-12 19:33:56 +01:00
|
|
|
#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);
|
2021-03-13 08:36:53 +01:00
|
|
|
|
2021-03-12 19:33:56 +01:00
|
|
|
ck_assert_str_eq(a, b);
|
2021-03-13 08:36:53 +01:00
|
|
|
|
2021-03-12 19:33:56 +01:00
|
|
|
free(a);
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:36:53 +01:00
|
|
|
START_TEST(test_transliterate_files)
|
2021-03-12 19:33:56 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2021-03-13 08:36:53 +01:00
|
|
|
static void test_output(const char *command, const char *expected)
|
2021-03-12 19:33:56 +01:00
|
|
|
{
|
2021-03-13 08:36:53 +01:00
|
|
|
char *out;
|
|
|
|
|
|
|
|
out = exec_command(command);
|
|
|
|
ck_assert_str_eq(expected, out);
|
2021-03-12 19:33:56 +01:00
|
|
|
free(out);
|
|
|
|
}
|
2021-03-13 08:36:53 +01:00
|
|
|
|
|
|
|
START_TEST(test_transliterate_arguments)
|
|
|
|
{
|
|
|
|
test_output("./iast \"भगवद्गीता\"", "bhagavadgītā\n");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-12-21 21:55:16 +01:00
|
|
|
START_TEST(test_transcript_czech)
|
2021-03-13 08:36:53 +01:00
|
|
|
{
|
|
|
|
test_output("./iast -c \"भगवद्गीता\"", "bhagavadgíta\n");
|
|
|
|
test_output("./iast --czech \"तन्त्रशास्त्रम्\"", "tantrašástra\n");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-12-21 21:55:16 +01:00
|
|
|
START_TEST(test_transcript_hindi)
|
|
|
|
{
|
|
|
|
test_output("./iast -H \"हिन्दी\"", "hindee\n");
|
|
|
|
test_output("./iast --hindi \"लड़की\"", "ladakee\n");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-03-13 08:36:53 +01:00
|
|
|
START_TEST(test_velthuis)
|
|
|
|
{
|
2021-04-16 12:14:08 +02:00
|
|
|
test_output("./iast \".rta.m ca satyam\" -e", "ṛtaṃ ca satyam\n");
|
|
|
|
test_output("./iast \".rta.m ca satyam\" --encode", "ṛtaṃ ca satyam\n");
|
|
|
|
test_output("./iast \".rta.m ca satyam\" --velthuis", "ṛtaṃ ca satyam\n");
|
|
|
|
|
|
|
|
/* reverse */
|
|
|
|
test_output("./iast \"ṛtaṃ ca satyam\" -er", ".rta.m ca satyam\n");
|
2021-12-21 13:45:46 +01:00
|
|
|
|
|
|
|
/* force Devanagari output */
|
|
|
|
test_output("./iast \"agnii.h\" -ed", "अग्नीः\n");
|
2021-03-13 08:36:53 +01:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-04-16 10:23:45 +02:00
|
|
|
START_TEST(test_ascii)
|
|
|
|
{
|
|
|
|
test_output("./iast \"अग्निमीळे पुरोहितं\" -a", "agnimii.le purohita.m\n");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-03-13 08:36:53 +01:00
|
|
|
START_TEST(test_version)
|
|
|
|
{
|
|
|
|
test_output("./iast -v", "iast v2.0.0\n");
|
|
|
|
}
|
2021-03-12 19:33:56 +01:00
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_usage)
|
|
|
|
{
|
|
|
|
char *out;
|
2021-03-13 08:36:53 +01:00
|
|
|
|
2021-03-12 19:33:56 +01:00
|
|
|
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);
|
2021-03-13 16:15:40 +01:00
|
|
|
|
|
|
|
out = exec_command("./iast -h");
|
|
|
|
out[44] = '\0';
|
|
|
|
ck_assert_str_eq("iast, a helper for Sanskrit transliteration.", out);
|
|
|
|
free(out);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_errors)
|
|
|
|
{
|
|
|
|
test_output("./iast -x 2>&1", "[iast] error: unrecognised option '-x'.\n");
|
|
|
|
test_output("./iast -f xxx 2>&1", "[iast] error: failed to read file 'xxx'.\n");
|
2021-12-30 14:12:16 +01:00
|
|
|
test_output("./iast -Hr 2>&1", "[iast] error: invalid combination of '-H' and '-r'.\n");
|
|
|
|
test_output("./iast -cr 2>&1", "[iast] error: invalid combination of '-c' and '-r'.\n");
|
2021-03-12 19:33:56 +01:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
void register_integration_tests(TCase *test_case)
|
|
|
|
{
|
2021-03-13 08:36:53 +01:00
|
|
|
tcase_add_test(test_case, test_transliterate_files);
|
|
|
|
tcase_add_test(test_case, test_transliterate_arguments);
|
2021-12-21 21:55:16 +01:00
|
|
|
tcase_add_test(test_case, test_transcript_czech);
|
|
|
|
tcase_add_test(test_case, test_transcript_hindi);
|
2021-03-13 08:36:53 +01:00
|
|
|
tcase_add_test(test_case, test_velthuis);
|
2021-04-16 10:23:45 +02:00
|
|
|
tcase_add_test(test_case, test_ascii);
|
2021-03-12 19:33:56 +01:00
|
|
|
tcase_add_test(test_case, test_version);
|
|
|
|
tcase_add_test(test_case, test_usage);
|
2021-03-13 16:15:40 +01:00
|
|
|
tcase_add_test(test_case, test_errors);
|
2021-03-12 19:33:56 +01:00
|
|
|
}
|