blob: 7f33b59625d5f1b7b5a8320ea421448d55f4a415 [file] [log] [blame]
Marc Kupietzb4ade0f2024-11-23 10:12:22 +01001#include <stddef.h>
Marc Kupietz6663f112021-03-14 09:20:59 +01002#include <stdio.h>
3#include <string.h>
4#define __USE_XOPEN_EXTENDED
5#include <ftw.h>
6#include "../src/collocatordb.h"
7#include "acutest.h"
Marc Kupietz023d4592026-07-31 09:46:34 +09008#include "config.h"
Marc Kupietz6663f112021-03-14 09:20:59 +01009
10char dbpath[] = "../tests/data/wpd19_10000";
11const int testword = 10; // ist
12
13void test_open_db() {
Marc Kupietz5ffc4742024-11-15 15:45:12 +010014 COLLOCATORDB* cdb = open_collocatordb(dbpath);
Marc Kupietz6663f112021-03-14 09:20:59 +010015 TEST_ASSERT(cdb != NULL);
16}
17
18void test_get_word() {
Marc Kupietz5ffc4742024-11-15 15:45:12 +010019 COLLOCATORDB* cdb = open_collocatordb(dbpath);
Marc Kupietz6663f112021-03-14 09:20:59 +010020 TEST_ASSERT(cdb != NULL);
21 char *word = get_word(cdb, testword);
22 char *expected = "ist";
23 TEST_CHECK(strcmp(word, expected) == 0);
24 TEST_MSG("Expected: %s", expected);
25 TEST_MSG("Produced: %s", word);
26}
27
28void test_collocation_scores() {
Marc Kupietz5ffc4742024-11-15 15:45:12 +010029 COLLOCATORDB* cdb = open_collocatordb(dbpath);
Marc Kupietz6663f112021-03-14 09:20:59 +010030 TEST_ASSERT(cdb != NULL);
Marc Kupietze889cec2024-11-23 12:08:42 +010031 char *expected = " { \"f1\": 217,\"w1\":\"Aluminium\", \"N\": 152743, \"collocates\": [{\"word\":\"Anwendungstechnologie\",\"f2\":16,\"f\":16,\"npmi\":0.594849,\"pmi\":8.4592,\"llr\":188.227,\"lfmd\":16.4592,\"md\":12.4592,\"md_nws\":10.1373,\"dice\":0.0711111,\"ld\":10.1862,\"ln_count\":16,\"rn_count\":0,\"ln_pmi\":9.4592,\"rn_pmi\":-1,\"ldaf\":11.1358,\"win\":32,\"afwin\":32}]}\n";
Marc Kupietz6663f112021-03-14 09:20:59 +010032 char *produced = get_collocation_scores_as_json(cdb, 62, 966);
33 TEST_CHECK(strcmp(produced, expected) == 0);
34 TEST_MSG("Expected: %s", expected);
35 TEST_MSG("Produced: %s", produced);
36}
37
38
39void test_collocation_analysis_as_json() {
Marc Kupietz5ffc4742024-11-15 15:45:12 +010040 COLLOCATORDB* cdb = open_collocatordb(dbpath);
Marc Kupietz6663f112021-03-14 09:20:59 +010041 TEST_ASSERT(cdb != NULL);
42 char *json = get_collocators_as_json(cdb, testword);
Marc Kupietze889cec2024-11-23 12:08:42 +010043 char *needle = "\"word\":\"um\",\"f2\":264,\"f\":5,\"npmi\":-0.0556349,\"pmi\":-0.958074,\"llr\":2.87723,\"lfmd\":3.68578,\"md\":1.36385,\"md_nws\":0.363854,\"dice\":0.00169952,\"ld\":4.79935,\"ln_count\":0,\"rn_count\":1,\"ln_pmi\":-1,\"rn_pmi\":-1,\"ldaf\":4.79935,\"win\":668,\"afwin\":668";
Marc Kupietz6663f112021-03-14 09:20:59 +010044 TEST_CHECK(strstr(json, needle) > 0);
45 TEST_MSG("Expected to contain: %s", needle);
Marc Kupietz6663f112021-03-14 09:20:59 +010046}
47
48void test_collocation_analysis() {
Marc Kupietz5ffc4742024-11-15 15:45:12 +010049 COLLOCATORDB* cdb = open_collocatordb(dbpath);
Marc Kupietz6663f112021-03-14 09:20:59 +010050 TEST_ASSERT(cdb != NULL);
51 char *expected = "Anwendungstechnologie";
52 const COLLOCATOR *c = get_collocators(cdb, 62);
53 char *produced = get_word(cdb,c[0].w2);
54 TEST_CHECK(strcmp(produced, expected) == 0);
55 TEST_MSG("Expected: %s", expected);
56 TEST_MSG("Produced: %s", produced);
Marc Kupietz6663f112021-03-14 09:20:59 +010057}
58
59int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
60 int rv = remove(fpath);
61 if (rv)
62 perror(fpath);
63 return rv;
64}
65
66int rmrf(char *path) {
67 return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
68}
69
70void test_writing() {
Marc Kupietzb4ade0f2024-11-23 10:12:22 +010071 char tmp_template[] = "/tmp/tmpfileXXXXXX";
72 int fd = mkstemp(tmp_template);
73 if (fd == -1) {
74 perror("mkstemp");
75 exit(EXIT_FAILURE);
76 }
77 close(fd);
78 char *tmp = strdup(tmp_template);
79
Marc Kupietz6663f112021-03-14 09:20:59 +010080 long size = 0;
Marc Kupietz673bd812021-03-14 17:27:44 +010081 int i;
Marc Kupietz6663f112021-03-14 09:20:59 +010082
Marc Kupietzb4ade0f2024-11-23 10:12:22 +010083 char *rocksdbfn = malloc(strlen(tmp) + strlen(".rocksdb") + 1);
84 strcpy(rocksdbfn, tmp);
Marc Kupietz6663f112021-03-14 09:20:59 +010085 strcat(rocksdbfn, ".rocksdb");
86 COLLOCATORDB *cdb = open_collocatordb_for_write(rocksdbfn);
87
Marc Kupietzb4ade0f2024-11-23 10:12:22 +010088 char *vocabfn = malloc(strlen(tmp) + strlen(".vocab") + 1);
Marc Kupietz6663f112021-03-14 09:20:59 +010089 strcpy(vocabfn, tmp);
90 strcat(vocabfn, ".vocab");
91 FILE *h = fopen(vocabfn, "w");
92 fprintf(h, "word0 2000\n");
93 fprintf(h, "word1 2000\n");
94 fprintf(h, "word2 2000\n");
95 fclose(h);
96 read_vocab(cdb, vocabfn);
97 inc_collocator(cdb, 0, 1, 4); size++;
Marc Kupietzb4ade0f2024-11-23 10:12:22 +010098 for (i = 0; i < 1000; i++) {
Marc Kupietz6663f112021-03-14 09:20:59 +010099 inc_collocator(cdb, 0, 1, i % 5); size++;
100 inc_collocator(cdb, 0, 1, -i % 5); size++;
101 inc_collocator(cdb, 1, 0, i % 5); size++;
102 inc_collocator(cdb, 1, 0, -i % 5); size++;
103 inc_collocator(cdb, 0, 2, i % 5); size++;
104 inc_collocator(cdb, 0, 2, -i % 5); size++;
105 }
106 inc_collocator(cdb, 1, 2, 4); size++;
107 COLLOCATOR *c = get_collocators(cdb, 0);
108 TEST_ASSERT(c != NULL);
Marc Kupietz1b09e4d2021-03-14 15:20:19 +0100109 TEST_CHECK(c[0].w2 == 1);
Marc Kupietz6663f112021-03-14 09:20:59 +0100110 TEST_CHECK(c[0].raw == 2001);
111 TEST_CHECK(c[0].left_raw == 200);
112 TEST_CHECK(c[0].right_raw == 200);
113
114 rmrf(rocksdbfn);
115}
Marc Kupietzb4ade0f2024-11-23 10:12:22 +0100116
Marc Kupietz6208fd72024-11-15 15:46:19 +0100117void test_version_function() {
118 char *version = get_version();
Marc Kupietz023d4592026-07-31 09:46:34 +0900119 /* against the version of the build, so that a version bump does not have to
120 be repeated here */
121 TEST_CHECK(strcmp(version, PROJECT_VERSION) == 0);
122 TEST_MSG("Unexpected version: %s, expected %s", version, PROJECT_VERSION);
Marc Kupietz6208fd72024-11-15 15:46:19 +0100123}
Marc Kupietz6663f112021-03-14 09:20:59 +0100124
Marc Kupietz6e2dbd12024-11-23 10:11:32 +0100125void test_get_word_id() {
126 COLLOCATORDB* cdb = open_collocatordb(dbpath);
127 TEST_ASSERT(cdb != NULL);
128 uint64_t id = get_word_id(cdb, "ist");
129 TEST_CHECK(id == 10);
130 TEST_MSG("Unexpected word id: %lu", id);
131}
132
Marc Kupietz94ea77b2024-11-23 12:32:19 +0100133void test_collocatordb_query_command_line_tool() {
134 int result = system("../build/collocatordb_query ../tests/data/wpd19_10000 ist > /dev/null 2>&1");
135 TEST_CHECK(result == 0);
136 TEST_MSG("collectordb_query command failed with result: %d", result);
137}
138
Marc Kupietzd26b1052024-12-10 16:56:39 +0100139void test_get_corpus_size() {
140 COLLOCATORDB* cdb = open_collocatordb(dbpath);
141 TEST_ASSERT(cdb != NULL);
142 uint64_t size = get_corpus_size(cdb);
143 TEST_CHECK(size == 152743);
144 TEST_MSG("Unexpected corpus size: %lu", size);
145}
146
Marc Kupietz21b964c2024-12-10 17:10:50 +0100147void test_get_word_frequency() {
148 COLLOCATORDB* cdb = open_collocatordb(dbpath);
149 TEST_ASSERT(cdb != NULL);
150 int w1 = get_word_id(cdb, "Test");
151 uint64_t freq = get_word_frequency(cdb, w1);
152 TEST_CHECK(freq == 3);
153 TEST_MSG("Unexpected word frequency: %lu", freq);
154}
155
Marc Kupietz6663f112021-03-14 09:20:59 +0100156TEST_LIST = {
157 { "open database for reading", test_open_db },
158 { "get word", test_get_word },
159 { "collocation scores", test_collocation_scores },
160 { "collocation analysis", test_collocation_analysis },
161 { "collocation analysis as json", test_collocation_analysis_as_json },
162 { "writing", test_writing },
Marc Kupietz6208fd72024-11-15 15:46:19 +0100163 { "version function", test_version_function },
Marc Kupietz6e2dbd12024-11-23 10:11:32 +0100164 { "get word id", test_get_word_id },
Marc Kupietzd26b1052024-12-10 16:56:39 +0100165 { "get corpus size", test_get_corpus_size},
Marc Kupietz21b964c2024-12-10 17:10:50 +0100166 { "get word frequency", test_get_word_frequency},
Marc Kupietz94ea77b2024-11-23 12:32:19 +0100167 { "collocatordb_query command line tool", test_collocatordb_query_command_line_tool},
Marc Kupietz6663f112021-03-14 09:20:59 +0100168 { NULL, NULL }
169};