-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_infrastructure.cpp
More file actions
277 lines (233 loc) · 8.73 KB
/
Copy pathtest_infrastructure.cpp
File metadata and controls
277 lines (233 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* test_infrastructure.cpp: Unit tests for core FSA/FST infrastructure
*
* Purpose:
* Tests the fundamental building blocks of the morphological analyzer:
* - Symbol system (SymbolTable, Symbol)
* - Finite-state acceptors (FSA)
* - Finite-state transducers (FST)
* - Epsilon transitions
*
* These tests verify that the automata machinery works correctly
* before we build higher-level morphological components on top.
*
* Compilation:
* g++ -std=c++17 -I. test_infrastructure.cpp SYMBOLS/symbol.cpp -o test_infra
*
* Usage:
* ./test_infra
*
* Expected Output:
* All tests should pass with ✓ marks
*/
#include "SYMBOLS/symbol.h"
#include "AUTOMATA/automaton.h"
#include "AUTOMATA/fsa.h"
#include "AUTOMATA/fst.h"
#include <iostream>
#include <cassert>
/**
* test_symbol_table: Verify SymbolTable functionality
*
* Tests:
* 1. Epsilon symbol is created automatically
* 2. Surface symbols can be created and compared
* 3. Lexical symbols can be created
* 4. Symbol uniqueness is enforced (same repr → same Symbol*)
*
* Coverage:
* - SymbolTable constructor
* - epsilon() accessor
* - get_or_create() factory method
* - Symbol type queries (is_epsilon, is_surface, is_lexical)
* - Symbol equality operators
*/
void test_symbol_table() {
std::cout << "Testing SymbolTable..." << std::endl;
SymbolTable symbols;
// Test 1: Epsilon symbol
auto eps = symbols.epsilon();
assert(eps->is_epsilon());
std::cout << " ✓ Epsilon symbol created" << std::endl;
// Test 2: Surface symbols
auto sym_a = symbols.get_or_create("a", SymbolType::SURFACE);
auto sym_b = symbols.get_or_create("b", SymbolType::SURFACE);
assert(sym_a->is_surface());
assert(*sym_a != *sym_b); // Different symbols
std::cout << " ✓ Surface symbols created and comparable" << std::endl;
// Test 3: Lexical symbols
auto pl = symbols.get_or_create("+PL", SymbolType::LEXICAL);
auto n = symbols.get_or_create("+N", SymbolType::LEXICAL);
assert(pl->is_lexical());
assert(*pl != *n); // Different symbols
std::cout << " ✓ Lexical symbols created" << std::endl;
// Test 4: Uniqueness (idempotent creation)
auto sym_a2 = symbols.get_or_create("a", SymbolType::SURFACE);
assert(sym_a == sym_a2); // Same pointer (not just equal, but identical)
std::cout << " ✓ Symbol uniqueness enforced" << std::endl;
std::cout << "SymbolTable tests passed!\n" << std::endl;
}
/**
* test_fsa: Verify FSA (Finite-State Acceptor) functionality
*
* Builds a simple FSA and tests acceptance:
*
* FSA Structure:
* State 0 (start) --a--> State 1 --b--> State 2 (final)
*
* Language:
* L(FSA) = {"ab"} (exactly one string)
*
* Tests:
* - FSA accepts "ab" (valid string)
* - FSA rejects "ac" (wrong character)
* - FSA rejects "a" (incomplete string)
*
* Coverage:
* - FSA construction (create_state, set_start_state, create_transition)
* - FSA.accepts() method
* - Deterministic traversal
*/
void test_fsa() {
std::cout << "Testing FSA..." << std::endl;
SymbolTable symbols;
FSA fsa(&symbols);
// Build simple FSA: accepts "ab"
auto s0 = fsa.create_state(false); // Start, non-final
auto s1 = fsa.create_state(false); // Intermediate, non-final
auto s2 = fsa.create_state(true); // End, final/accepting
fsa.set_start_state(s0);
auto sym_a = symbols.get_or_create("a", SymbolType::SURFACE);
auto sym_b = symbols.get_or_create("b", SymbolType::SURFACE);
auto sym_c = symbols.get_or_create("c", SymbolType::SURFACE);
fsa.create_transition(s0, s1, sym_a); // 0 --a--> 1
fsa.create_transition(s1, s2, sym_b); // 1 --b--> 2
std::cout << " Created FSA with " << fsa.num_states() << " states, "
<< fsa.num_transitions() << " transitions" << std::endl;
// Test acceptance
std::vector<std::shared_ptr<Symbol>> input_ab = {sym_a, sym_b};
std::vector<std::shared_ptr<Symbol>> input_ac = {sym_a, sym_c};
std::vector<std::shared_ptr<Symbol>> input_a = {sym_a};
assert(fsa.accepts(input_ab)); // Should accept "ab"
std::cout << " ✓ Accepts 'ab'" << std::endl;
assert(!fsa.accepts(input_ac)); // Should reject "ac" (wrong char)
std::cout << " ✓ Rejects 'ac'" << std::endl;
assert(!fsa.accepts(input_a)); // Should reject "a" (incomplete)
std::cout << " ✓ Rejects incomplete 'a'" << std::endl;
std::cout << "FSA tests passed!\n" << std::endl;
}
/**
* test_fst: Verify FST (Finite-State Transducer) basic functionality
*
* Builds a simple transducer and tests transduction:
*
* FST Structure:
* State 0 (start) --a:b--> State 1 (final)
*
* Relation:
* R(FST) = {("a", "b")} (maps "a" to "b")
*
* Tests:
* - FST transduces "a" → "b"
* - Result has correct structure
* - Output matches expected symbol
*
* Coverage:
* - FST construction (create_fst_state, create_fst_transition)
* - FST.transduce() method
* - FSTTransition with input:output pairs
*/
void test_fst() {
std::cout << "Testing FST..." << std::endl;
SymbolTable symbols;
FST fst(&symbols);
// Build simple FST: maps "a" -> "b"
auto s0 = fst.create_fst_state(false); // Start
auto s1 = fst.create_fst_state(true); // Final
fst.set_start_state(s0);
auto sym_a = symbols.get_or_create("a", SymbolType::SURFACE);
auto sym_b = symbols.get_or_create("b", SymbolType::SURFACE);
fst.create_fst_transition(s0, s1, sym_a, sym_b); // a:b
std::cout << " Created FST with " << fst.num_states() << " states" << std::endl;
// Test transduction
std::vector<std::shared_ptr<Symbol>> input = {sym_a};
auto results = fst.transduce(input);
assert(results.size() == 1); // One transduction result
assert(results[0].success); // Transduction succeeded
assert(results[0].output.size() == 1); // Output has one symbol
assert(*results[0].output[0] == *sym_b); // Output is 'b'
std::cout << " ✓ Transduces 'a' -> 'b'" << std::endl;
std::cout << "FST tests passed!\n" << std::endl;
}
/**
* test_fst_with_epsilon: Verify epsilon transitions in FSTs
*
* Builds an FST with epsilon transition (insertion):
*
* FST Structure:
* State 0 (start) --a:a--> State 1 --ε:b--> State 2 (final)
*
* Relation:
* R(FST) = {("a", "ab")} (inserts 'b' after 'a')
*
* Key Concept:
* The ε:b transition writes 'b' WITHOUT consuming input.
* This demonstrates insertion in finite-state morphology.
*
* Tests:
* - FST transduces "a" → "ab"
* - Output contains both 'a' and 'b' in correct order
* - Epsilon transition doesn't consume input
*
* Coverage:
* - Epsilon symbols in FSTs
* - Multi-symbol output
* - Insertion operations (ε:x)
*/
void test_fst_with_epsilon() {
std::cout << "Testing FST with epsilon transitions..." << std::endl;
SymbolTable symbols;
FST fst(&symbols);
// Build FST: maps "a" -> "ab" (insertion)
auto s0 = fst.create_fst_state(false); // Start
auto s1 = fst.create_fst_state(false); // Intermediate
auto s2 = fst.create_fst_state(true); // Final
fst.set_start_state(s0);
auto sym_a = symbols.get_or_create("a", SymbolType::SURFACE);
auto sym_b = symbols.get_or_create("b", SymbolType::SURFACE);
auto eps = symbols.epsilon();
fst.create_fst_transition(s0, s1, sym_a, sym_a); // a:a (identity)
fst.create_fst_transition(s1, s2, eps, sym_b); // ε:b (insertion)
// Test transduction
std::vector<std::shared_ptr<Symbol>> input = {sym_a};
auto results = fst.transduce(input);
assert(results.size() == 1); // One result
assert(results[0].output.size() == 2); // Output: "ab" (2 symbols)
assert(*results[0].output[0] == *sym_a); // First symbol is 'a'
assert(*results[0].output[1] == *sym_b); // Second symbol is 'b'
std::cout << " ✓ Transduces 'a' -> 'ab' with epsilon insertion" << std::endl;
std::cout << "FST epsilon tests passed!\n" << std::endl;
}
/**
* main: Test runner
*
* Runs all infrastructure tests in sequence.
* If any test fails (assertion), program terminates with error.
*
* @return 0 if all tests pass, 1 if any test fails
*/
int main() {
std::cout << "\n=== Testing Morphological Analyzer Infrastructure ===\n" << std::endl;
try {
test_symbol_table();
test_fsa();
test_fst();
test_fst_with_epsilon();
std::cout << "=== All tests passed! ===\n" << std::endl;
return 0;
}
catch (const std::exception& e) {
std::cerr << "Test failed with exception: " << e.what() << std::endl;
return 1;
}
}