-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
370 lines (307 loc) · 8.9 KB
/
Copy pathconftest.py
File metadata and controls
370 lines (307 loc) · 8.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
"""
Pytest configuration and fixtures for the test suite.
"""
import pytest
from typing import List, Dict, Any
from ..parser.preprocessor import Preprocessor, PreprocessorConfig
from ..parser.parser import PseudocodeParser, ParserConfig
from ..schemas.complexity import ComplexityClass
# =============================================================================
# Parser Fixtures
# =============================================================================
@pytest.fixture
def preprocessor() -> Preprocessor:
"""Create a default preprocessor instance."""
return Preprocessor()
@pytest.fixture
def preprocessor_strict() -> Preprocessor:
"""Create a preprocessor with strict settings."""
config = PreprocessorConfig(
normalize_case=True,
normalize_operators=True,
normalize_whitespace=True,
fix_common_typos=False, # Strict: don't fix typos
preserve_strings=True,
)
return Preprocessor(config)
@pytest.fixture
def parser() -> PseudocodeParser:
"""Create a default parser instance."""
return PseudocodeParser()
@pytest.fixture
def parser_strict() -> PseudocodeParser:
"""Create a parser with strict mode enabled."""
config = ParserConfig(strict_mode=True)
return PseudocodeParser(config)
# =============================================================================
# Sample Pseudocode Fixtures
# =============================================================================
@pytest.fixture
def simple_for_loop() -> str:
"""Simple FOR loop pseudocode."""
return """FOR i = 1 TO n DO
print(i)
END FOR"""
@pytest.fixture
def nested_for_loops() -> str:
"""Nested FOR loops pseudocode."""
return """FOR i = 1 TO n DO
FOR j = 1 TO n DO
sum = sum + A[i][j]
END FOR
END FOR"""
@pytest.fixture
def triple_nested_loops() -> str:
"""Triple nested loops pseudocode."""
return """FOR i = 1 TO n DO
FOR j = 1 TO n DO
FOR k = 1 TO n DO
result = result + A[i][j][k]
END FOR
END FOR
END FOR"""
@pytest.fixture
def while_loop() -> str:
"""Simple WHILE loop pseudocode."""
return """WHILE i < n DO
i = i + 1
count = count + 1
END WHILE"""
@pytest.fixture
def binary_search() -> str:
"""Binary search algorithm (O(log n))."""
return """FUNCTION binarySearch(A, target, low, high)
WHILE low <= high DO
mid = (low + high) / 2
IF A[mid] == target THEN
RETURN mid
ELSE IF A[mid] < target THEN
low = mid + 1
ELSE
high = mid - 1
END IF
END WHILE
RETURN -1
END FUNCTION"""
@pytest.fixture
def bubble_sort() -> str:
"""Bubble sort algorithm (O(n²))."""
return """FUNCTION bubbleSort(A, n)
FOR i = 1 TO n-1 DO
FOR j = 1 TO n-i DO
IF A[j] > A[j+1] THEN
swap(A[j], A[j+1])
END IF
END FOR
END FOR
END FUNCTION"""
@pytest.fixture
def recursive_fibonacci() -> str:
"""Recursive Fibonacci (O(2^n))."""
return """FUNCTION fib(n)
IF n <= 1 THEN
RETURN n
END IF
RETURN fib(n-1) + fib(n-2)
END FUNCTION"""
@pytest.fixture
def recursive_factorial() -> str:
"""Recursive factorial (O(n))."""
return """FUNCTION factorial(n)
IF n <= 1 THEN
RETURN 1
END IF
RETURN n * factorial(n-1)
END FUNCTION"""
@pytest.fixture
def merge_sort() -> str:
"""Merge sort algorithm (O(n log n))."""
return """FUNCTION mergeSort(A, left, right)
IF left < right THEN
mid = (left + right) / 2
mergeSort(A, left, mid)
mergeSort(A, mid+1, right)
merge(A, left, mid, right)
END IF
END FUNCTION
FUNCTION merge(A, left, mid, right)
FOR i = left TO right DO
temp[i] = A[i]
END FOR
END FUNCTION"""
@pytest.fixture
def linear_search() -> str:
"""Linear search algorithm (O(n))."""
return """FUNCTION linearSearch(A, n, target)
FOR i = 1 TO n DO
IF A[i] == target THEN
RETURN i
END IF
END FOR
RETURN -1
END FUNCTION"""
@pytest.fixture
def matrix_multiplication() -> str:
"""Matrix multiplication (O(n³))."""
return """FUNCTION matrixMultiply(A, B, n)
FOR i = 1 TO n DO
FOR j = 1 TO n DO
C[i][j] = 0
FOR k = 1 TO n DO
C[i][j] = C[i][j] + A[i][k] * B[k][j]
END FOR
END FOR
END FOR
RETURN C
END FUNCTION"""
# =============================================================================
# Pseudocode Style Variations Fixtures
# =============================================================================
@pytest.fixture
def python_style_loop() -> str:
"""Python-style pseudocode."""
return """def bubble_sort(arr):
for i in range(len(arr)):
for j in range(len(arr) - i - 1):
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])"""
@pytest.fixture
def pascal_style_loop() -> str:
"""Pascal-style pseudocode."""
return """PROCEDURE BubbleSort(A: ARRAY; n: INTEGER);
BEGIN
FOR i := 1 TO n-1 DO
FOR j := 1 TO n-i DO
IF A[j] > A[j+1] THEN
SWAP(A[j], A[j+1])
END
END
END
END"""
@pytest.fixture
def c_style_loop() -> str:
"""C-style pseudocode."""
return """function bubbleSort(A[], n) {
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (A[j] > A[j+1]) {
swap(A[j], A[j+1]);
}
}
}
}"""
@pytest.fixture
def mixed_case_keywords() -> str:
"""Pseudocode with mixed case keywords."""
return """FOR i = 1 To n DO
While j < n Do
IF condition Then
j = j + 1
ELSE
j = j - 1
End If
End While
End For"""
@pytest.fixture
def unicode_operators() -> str:
"""Pseudocode with unicode operators."""
return """FOR i ← 1 TO n DO
IF A[i] ≤ max AND A[i] ≥ min THEN
IF A[i] ≠ target THEN
count ← count + 1
END IF
END IF
END FOR"""
@pytest.fixture
def typos_in_keywords() -> str:
"""Pseudocode with common typos."""
return """FUCNTION test(n)
WHLIE i < n DO
i = i + 1
END WHLIE
RETRUN result
END FUCNTION"""
# =============================================================================
# Edge Case Fixtures
# =============================================================================
@pytest.fixture
def empty_function() -> str:
"""Empty function body."""
return """FUNCTION emptyFunc()
END FUNCTION"""
@pytest.fixture
def deeply_nested() -> str:
"""Deeply nested structure."""
return """FOR i = 1 TO n DO
FOR j = 1 TO n DO
FOR k = 1 TO n DO
FOR l = 1 TO n DO
FOR m = 1 TO n DO
x = x + 1
END FOR
END FOR
END FOR
END FOR
END FOR"""
@pytest.fixture
def multiple_functions() -> str:
"""Multiple function definitions."""
return """FUNCTION helper(x)
RETURN x * 2
END FUNCTION
FUNCTION main(n)
FOR i = 1 TO n DO
result = helper(i)
END FOR
RETURN result
END FUNCTION"""
@pytest.fixture
def foreach_loop() -> str:
"""For-each loop pseudocode."""
return """FOR EACH item IN collection DO
process(item)
END FOR"""
@pytest.fixture
def repeat_until_loop() -> str:
"""Repeat-until loop pseudocode."""
return """REPEAT
x = x + 1
UNTIL x >= n"""
# =============================================================================
# Expected Complexity Fixtures
# =============================================================================
@pytest.fixture
def complexity_test_cases() -> List[Dict[str, Any]]:
"""Test cases with expected complexities."""
return [
{
"name": "constant",
"code": "x = 1\ny = 2\nz = x + y",
"expected_time": ComplexityClass.CONSTANT,
"expected_space": ComplexityClass.CONSTANT,
},
{
"name": "single_loop",
"code": "FOR i = 1 TO n DO\n print(i)\nEND FOR",
"expected_time": ComplexityClass.LINEAR,
"expected_space": ComplexityClass.CONSTANT,
},
{
"name": "nested_loops",
"code": "FOR i = 1 TO n DO\n FOR j = 1 TO n DO\n x = x + 1\n END FOR\nEND FOR",
"expected_time": ComplexityClass.QUADRATIC,
"expected_space": ComplexityClass.CONSTANT,
},
{
"name": "triple_nested",
"code": "FOR i = 1 TO n DO\n FOR j = 1 TO n DO\n FOR k = 1 TO n DO\n x = 1\n END FOR\n END FOR\nEND FOR",
"expected_time": ComplexityClass.CUBIC,
"expected_space": ComplexityClass.CONSTANT,
},
{
"name": "logarithmic",
"code": "WHILE n > 1 DO\n n = n / 2\nEND WHILE",
"expected_time": ComplexityClass.LOGARITHMIC,
"expected_space": ComplexityClass.CONSTANT,
},
]