generated from lambda-feedback/evaluation-function-boilerplate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_local.py
More file actions
715 lines (660 loc) · 20.9 KB
/
test_local.py
File metadata and controls
715 lines (660 loc) · 20.9 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
"""
Local test script for FSA evaluation function.
Bypasses Lambda/API Gateway to test the evaluation function directly.
Tests cover: basic equivalence, NFAs, epsilon transitions, edge cases, and validation errors.
"""
from evaluation_function.evaluation import evaluation_function
from lf_toolkit.evaluation import Params
def run_test(test_num, test_name, response_data, answer_data, params=None):
"""Helper function to run a test case"""
print("=" * 70)
print(f"Test {test_num}: {test_name}")
print("=" * 70)
if params is None:
params = Params()
try:
result = evaluation_function(response_data, answer_data, params)
result_dict = result.to_dict()
status = "[PASS]" if result_dict['is_correct'] else "[FAIL]"
print(f"{status} Result: {result_dict['is_correct']}")
print(f" Feedback: {result_dict['feedback']}")
if 'command' in result_dict:
print(f" Command: {result_dict['command']}")
print()
return result_dict
except Exception as e:
print(f"[ERROR] {e}")
import traceback
traceback.print_exc()
print()
return None
# =============================================================================
# BASIC TESTS
# =============================================================================
# Test 1: Equivalent DFAs with different state names (should pass)
run_test(
1, "Equivalent DFAs - accepts a(a|b)*",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": ["q0|a|q1", "q1|a|q1", "q1|b|q1"],
"initial_state": "q0",
"accept_states": ["q1"]
},
answer_data={
"states": ["s0", "s1"],
"alphabet": ["a", "b"],
"transitions": ["s0|a|s1", "s1|a|s1", "s1|b|s1"],
"initial_state": "s0",
"accept_states": ["s1"]
}
)
# Test 2: Different Languages (should fail)
run_test(
2, "Different Languages - incomplete transitions",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": ["q0|a|q1", "q1|b|q1"], # Missing transitions
"initial_state": "q0",
"accept_states": ["q1"]
},
answer_data={
"states": ["s0", "s1"],
"alphabet": ["a", "b"],
"transitions": ["s0|a|s1", "s1|a|s1", "s1|b|s1"],
"initial_state": "s0",
"accept_states": ["s1"]
}
)
# Test 3: Simple FSA - accepts exactly 'a'
run_test(
3, "Simple DFA - accepts exactly 'a'",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1", "q0|b|q2",
"q1|a|q2", "q1|b|q2",
"q2|a|q2", "q2|b|q2"
],
"initial_state": "q0",
"accept_states": ["q1"]
},
answer_data={
"states": ["s0", "s1", "s2"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s1", "s0|b|s2",
"s1|a|s2", "s1|b|s2",
"s2|a|s2", "s2|b|s2"
],
"initial_state": "s0",
"accept_states": ["s1"]
}
)
# =============================================================================
# EPSILON TRANSITION TESTS (ε-NFA)
# =============================================================================
# Test 4: ε-NFA that accepts strings with 'ab'
run_test(
4, "ε-NFA - accepts strings containing 'ab'",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q0", "q0|b|q0", # Stay in q0
"q0|a|q1", # Go to q1 on 'a'
"q1|b|q2", # Go to q2 on 'b'
"q2|a|q2", "q2|b|q2" # Stay in q2 (accept)
],
"initial_state": "q0",
"accept_states": ["q2"]
},
answer_data={
"states": ["s0", "s1", "s2"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s0", "s0|b|s0",
"s0|a|s1",
"s1|b|s2",
"s2|a|s2", "s2|b|s2"
],
"initial_state": "s0",
"accept_states": ["s2"]
}
)
# Test 5: ε-NFA with epsilon transitions
run_test(
5, "ε-NFA with epsilon transitions - (a|b)*",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
"q0|ε|q1", # Epsilon to q1
"q1|a|q2", # a to q2
"q1|b|q2", # b to q2
"q2|ε|q1", # Epsilon back to q1
],
"initial_state": "q0",
"accept_states": ["q0", "q2"]
},
answer_data={
"states": ["s0"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s0",
"s0|b|s0"
],
"initial_state": "s0",
"accept_states": ["s0"]
}
)
# =============================================================================
# NFA TESTS (Non-deterministic)
# =============================================================================
# Test 6: NFA with multiple transitions on same symbol
run_test(
6, "NFA - multiple transitions on 'a'",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1", # Non-deterministic on 'a'
"q0|a|q2", # Two transitions on 'a'
"q1|b|q1",
"q2|b|q2"
],
"initial_state": "q0",
"accept_states": ["q1"]
},
answer_data={
"states": ["s0", "s1"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s1",
"s1|b|s1"
],
"initial_state": "s0",
"accept_states": ["s1"]
}
)
# =============================================================================
# EDGE CASES
# =============================================================================
# Test 7: Empty language (no accept states)
run_test(
7, "Empty Language - no accept states",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1",
"q1|a|q1",
"q1|b|q1"
],
"initial_state": "q0",
"accept_states": [] # No accept states
},
answer_data={
"states": ["s0", "s1"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s1",
"s1|a|s1",
"s1|b|s1"
],
"initial_state": "s0",
"accept_states": []
}
)
# Test 8: Accepts empty string (initial state is accept state)
run_test(
8, "Accepts empty string - ε ∈ L",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a"],
"transitions": [
"q0|a|q1",
"q1|a|q1"
],
"initial_state": "q0",
"accept_states": ["q0", "q1"] # Initial state is accepting
},
answer_data={
"states": ["s0", "s1"],
"alphabet": ["a"],
"transitions": [
"s0|a|s1",
"s1|a|s1"
],
"initial_state": "s0",
"accept_states": ["s0", "s1"]
}
)
# Test 9: Single state FSA (accepts everything)
run_test(
9, "Single State DFA - accepts all strings",
response_data={
"states": ["q0"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q0",
"q0|b|q0"
],
"initial_state": "q0",
"accept_states": ["q0"]
},
answer_data={
"states": ["s0"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s0",
"s0|b|s0"
],
"initial_state": "s0",
"accept_states": ["s0"]
}
)
# Test 10: Complex DFA - even number of 'a's
run_test(
10, "Complex DFA - even number of 'a's",
response_data={
"states": ["even", "odd"],
"alphabet": ["a", "b"],
"transitions": [
"even|a|odd",
"odd|a|even",
"even|b|even",
"odd|b|odd"
],
"initial_state": "even",
"accept_states": ["even"]
},
answer_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1",
"q1|a|q0",
"q0|b|q0",
"q1|b|q1"
],
"initial_state": "q0",
"accept_states": ["q0"]
}
)
# Test 11: Non-minimal DFA (has redundant states)
run_test(
11, "Non-minimal DFA - redundant states",
response_data={
"states": ["q0", "q1", "q2", "q3"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1",
"q0|b|q2",
"q1|a|q1", "q1|b|q1",
"q2|a|q3", "q2|b|q3",
"q3|a|q3", "q3|b|q3"
],
"initial_state": "q0",
"accept_states": ["q1", "q2"] # q2 and q3 behave similarly
},
answer_data={
"states": ["s0", "s1", "s2"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s1",
"s0|b|s1",
"s1|a|s1", "s1|b|s1",
"s2|a|s2", "s2|b|s2"
],
"initial_state": "s0",
"accept_states": ["s1"]
}
)
# Test 12: Unreachable states
run_test(
12, "DFA with unreachable states",
response_data={
"states": ["q0", "q1", "q2", "q3"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1",
"q0|b|q1",
"q1|a|q1", "q1|b|q1",
"q2|a|q3", # q2 and q3 are unreachable
"q3|a|q3", "q3|b|q3"
],
"initial_state": "q0",
"accept_states": ["q1"]
},
answer_data={
"states": ["s0", "s1"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s1",
"s0|b|s1",
"s1|a|s1", "s1|b|s1"
],
"initial_state": "s0",
"accept_states": ["s1"]
}
)
# =============================================================================
# COMPLEX PATTERN TESTS
# =============================================================================
# Test 13: Strings ending with 'ab'
run_test(
13, "Strings ending with 'ab'",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
"q0|a|q1", "q0|b|q0",
"q1|a|q1", "q1|b|q2",
"q2|a|q1", "q2|b|q0"
],
"initial_state": "q0",
"accept_states": ["q2"]
},
answer_data={
"states": ["s0", "s1", "s2"],
"alphabet": ["a", "b"],
"transitions": [
"s0|a|s1", "s0|b|s0",
"s1|a|s1", "s1|b|s2",
"s2|a|s1", "s2|b|s0"
],
"initial_state": "s0",
"accept_states": ["s2"]
}
)
# Test 14: Binary numbers divisible by 3
run_test(
14, "Binary numbers divisible by 3",
response_data={
"states": ["r0", "r1", "r2"],
"alphabet": ["0", "1"],
"transitions": [
"r0|0|r0", "r0|1|r1",
"r1|0|r2", "r1|1|r0",
"r2|0|r1", "r2|1|r2"
],
"initial_state": "r0",
"accept_states": ["r0"]
},
answer_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["0", "1"],
"transitions": [
"q0|0|q0", "q0|1|q1",
"q1|0|q2", "q1|1|q0",
"q2|0|q1", "q2|1|q2"
],
"initial_state": "q0",
"accept_states": ["q0"]
}
)
# Test 15: Wrong answer for divisibility by 3 (should fail)
run_test(
15, "Wrong divisibility by 3 (should fail)",
response_data={
"states": ["q0", "q1"],
"alphabet": ["0", "1"],
"transitions": [
"q0|0|q0", "q0|1|q1",
"q1|0|q1", "q1|1|q0"
],
"initial_state": "q0",
"accept_states": ["q0"] # This is wrong for div by 3
},
answer_data={
"states": ["r0", "r1", "r2"],
"alphabet": ["0", "1"],
"transitions": [
"r0|0|r0", "r0|1|r1",
"r1|0|r2", "r1|1|r0",
"r2|0|r1", "r2|1|r2"
],
"initial_state": "r0",
"accept_states": ["r0"]
}
)
# =============================================================================
# PREVIEW FUNCTION TESTS
# =============================================================================
print("\n")
print("#" * 70)
print("# PREVIEW FUNCTION TESTS (Pre-submission Validation)")
print("#" * 70)
print()
from evaluation_function.preview import preview_function
from lf_toolkit.preview import Params as PreviewParams
def run_preview_test(test_num, test_name, response_data, params=None):
"""Helper function to run a preview test case"""
print("=" * 70)
print(f"Preview Test {test_num}: {test_name}")
print("=" * 70)
if params is None:
params = PreviewParams()
try:
result = preview_function(response_data, params)
# Handle both Result object and dict
if hasattr(result, 'to_dict'):
result_dict = result.to_dict()
elif isinstance(result, dict):
result_dict = result
else:
result_dict = {'preview': result}
# Extract preview data
preview_data = result_dict.get('preview', {})
if hasattr(preview_data, 'model_dump'):
preview_data = preview_data.model_dump()
sympy_data = preview_data.get('sympy', {})
is_valid = sympy_data.get('valid', False) if sympy_data else False
status = "[VALID]" if is_valid else "[INVALID]"
print(f"{status} FSA is valid: {is_valid}")
feedback = preview_data.get('feedback', '')
if feedback:
print(f" Feedback:\n {str(feedback).replace(chr(10), chr(10) + ' ')}")
if sympy_data and sympy_data.get('errors'):
print(f" Errors: {len(sympy_data['errors'])}")
if sympy_data and sympy_data.get('warnings'):
print(f" Warnings: {len(sympy_data['warnings'])}")
print()
return result_dict
except Exception as e:
print(f"[ERROR] {e}")
import traceback
traceback.print_exc()
print()
return None
# Preview Test P1: Valid DFA
run_preview_test(
"P1", "Valid DFA - should pass",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "b"}
],
"initial_state": "q0",
"accept_states": ["q1"]
}
)
# Preview Test P2: Invalid initial state
run_preview_test(
"P2", "Invalid initial state - should fail",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"}
],
"initial_state": "q99", # Does not exist
"accept_states": ["q1"]
}
)
# Preview Test P3: Invalid accept state
run_preview_test(
"P3", "Invalid accept state - should fail",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"}
],
"initial_state": "q0",
"accept_states": ["q99"] # Does not exist
}
)
# Preview Test P4: Transition references non-existent state
run_preview_test(
"P4", "Transition to non-existent state - should fail",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q99", "symbol": "a"} # q99 doesn't exist
],
"initial_state": "q0",
"accept_states": ["q1"]
}
)
# Preview Test P5: Transition with invalid symbol
run_preview_test(
"P5", "Transition with symbol not in alphabet - should fail",
response_data={
"states": ["q0", "q1"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "c"} # 'c' not in alphabet
],
"initial_state": "q0",
"accept_states": ["q1"]
}
)
# Preview Test P6: FSA with unreachable states (warning)
run_preview_test(
"P6", "FSA with unreachable states - valid with warning",
response_data={
"states": ["q0", "q1", "q2", "q3"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "b"},
{"from_state": "q2", "to_state": "q3", "symbol": "a"} # q2, q3 unreachable
],
"initial_state": "q0",
"accept_states": ["q1"]
}
)
# Preview Test P7: FSA with dead states (warning)
run_preview_test(
"P7", "FSA with dead states - valid with warning",
response_data={
"states": ["q0", "q1", "dead"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "dead", "symbol": "b"},
{"from_state": "q1", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "b"},
{"from_state": "dead", "to_state": "dead", "symbol": "a"},
{"from_state": "dead", "to_state": "dead", "symbol": "b"}
],
"initial_state": "q0",
"accept_states": ["q1"] # "dead" can never reach accept
}
)
# Preview Test P8: Not a valid FSA structure (parse error)
run_preview_test(
"P8", "Invalid structure - missing required fields",
response_data={
"states": ["q0"],
# Missing alphabet, transitions, initial_state, accept_states
}
)
# Preview Test P9: Empty states list
run_preview_test(
"P9", "Empty states list - should fail",
response_data={
"states": [],
"alphabet": ["a"],
"transitions": [],
"initial_state": "q0",
"accept_states": []
}
)
# Preview Test P10: NFA (valid, non-deterministic)
run_preview_test(
"P10", "Valid NFA - non-deterministic allowed",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q0", "to_state": "q2", "symbol": "a"}, # Non-deterministic
{"from_state": "q1", "to_state": "q1", "symbol": "b"},
{"from_state": "q2", "to_state": "q2", "symbol": "b"}
],
"initial_state": "q0",
"accept_states": ["q1"]
}
)
# Preview Test P11: Epsilon transitions
run_preview_test(
"P11", "Epsilon NFA - epsilon transitions",
response_data={
"states": ["q0", "q1", "q2"],
"alphabet": ["a", "b"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q2", "symbol": "b"}
],
"initial_state": "q0",
"accept_states": ["q2"]
}
)
# Preview Test P12: Null/None response
run_preview_test(
"P12", "Null response - should fail gracefully",
response_data=None
)
# Preview Test P13: String response (JSON)
import json
run_preview_test(
"P13", "JSON string response - should parse",
response_data=json.dumps({
"states": ["q0", "q1"],
"alphabet": ["a"],
"transitions": [
{"from_state": "q0", "to_state": "q1", "symbol": "a"},
{"from_state": "q1", "to_state": "q1", "symbol": "a"}
],
"initial_state": "q0",
"accept_states": ["q1"]
})
)
print("=" * 70)
print("ALL EVALUATION + PREVIEW TESTS COMPLETED!")
print("=" * 70)
print("\nRun with: python test_local.py")
print("\nEvaluation tests cover:")
print(" - Basic DFA equivalence")
print(" - Non-deterministic FSAs (NFAs)")
print(" - Epsilon transitions")
print(" - Edge cases (empty language, single state, unreachable states)")
print(" - Complex patterns (ending with 'ab', divisibility by 3)")
print("\nPreview tests cover:")
print(" - Valid FSA validation")
print(" - Invalid initial/accept states")
print(" - Invalid transitions (state/symbol)")
print(" - Unreachable and dead states (warnings)")
print(" - Parse errors (invalid structure, null input)")
print(" - NFA and epsilon transition support")
print(" - JSON string input parsing")