-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_examples.py
More file actions
393 lines (315 loc) · 11.1 KB
/
Copy pathtest_examples.py
File metadata and controls
393 lines (315 loc) · 11.1 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
#!/usr/bin/env python3
"""
Comprehensive testing of the Agentic Code Generator with various Python code examples.
"""
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
from agentic_codegen.core.main import CodeGenerator
from agentic_codegen.utils.logger import setup_logger
logger = setup_logger(__name__)
def test_fibonacci():
"""Test with recursive Fibonacci function."""
print("\n" + "=" * 60)
print("🧪 TESTING: Recursive Fibonacci Function")
print("=" * 60)
code = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# Test the function
result = fibonacci(10)
print(f"Fibonacci(10) = {result}")
"""
generator = CodeGenerator()
cpp_code, metadata = generator.convert(code)
print("📝 Input Python Code:")
print(code)
print("\n" + "-" * 50)
print("📄 Generated C++ Code:")
print(cpp_code)
print("\n" + "-" * 50)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Verification: {metadata.get('verification_result', 'N/A')}")
print(f"✓ Validation Issues: {len(metadata.get('validation_issues', []))}")
print(f"✓ Validation Warnings: {len(metadata.get('validation_warnings', []))}")
def test_mathematical_computation():
"""Test with mathematical computation (Pi calculation)."""
print("\n" + "=" * 60)
print("🧪 TESTING: Mathematical Computation (Pi Calculation)")
print("=" * 60)
code = """
import math
def calculate_pi_leibniz(iterations):
\"\"\"Calculate pi using Leibniz formula\"\"\"
result = 1.0
for i in range(1, iterations + 1):
if i % 2 == 1:
result -= 1.0 / (2 * i + 1)
else:
result += 1.0 / (2 * i + 1)
return result * 4
def calculate_pi_monte_carlo(points):
\"\"\"Calculate pi using Monte Carlo method\"\"\"
inside_circle = 0
for _ in range(points):
x = random.random()
y = random.random()
if x*x + y*y <= 1:
inside_circle += 1
return 4 * inside_circle / points
# Test calculations
import random
random.seed(42) # For reproducible results
pi_leibniz = calculate_pi_leibniz(10000)
pi_monte_carlo = calculate_pi_monte_carlo(10000)
print(f"Pi (Leibniz): {pi_leibniz:.6f}")
print(f"Pi (Monte Carlo): {pi_monte_carlo:.6f}")
print(f"Actual Pi: {math.pi:.6f}")
"""
generator = CodeGenerator()
cpp_code, metadata = generator.convert(code)
print("📝 Input Python Code:")
print(code)
print("\n" + "-" * 50)
print("📄 Generated C++ Code:")
print(cpp_code)
print("\n" + "-" * 50)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Verification: {metadata.get('verification_result', 'N/A')}")
print(f"✓ Validation Issues: {len(metadata.get('validation_issues', []))}")
print(f"✓ Validation Warnings: {len(metadata.get('validation_warnings', []))}")
def test_data_processing():
"""Test with data processing operations."""
print("\n" + "=" * 60)
print("🧪 TESTING: Data Processing Operations")
print("=" * 60)
code = """
def process_data(data):
\"\"\"Process a list of numbers\"\"\"
# Filter positive numbers
positive = [x for x in data if x > 0]
# Calculate statistics
if positive:
total = sum(positive)
count = len(positive)
average = total / count
# Find min and max
minimum = min(positive)
maximum = max(positive)
return {
'total': total,
'count': count,
'average': average,
'min': minimum,
'max': maximum
}
else:
return {'total': 0, 'count': 0, 'average': 0, 'min': 0, 'max': 0}
def sort_and_filter(data, threshold):
\"\"\"Sort data and filter by threshold\"\"\"
# Sort in descending order
sorted_data = sorted(data, reverse=True)
# Filter values above threshold
filtered = [x for x in sorted_data if x > threshold]
return filtered
# Test the functions
test_data = [-5, 10, -3, 25, 8, -12, 15, 0, 7]
stats = process_data(test_data)
filtered = sort_and_filter(test_data, 5)
print(f"Original data: {test_data}")
print(f"Statistics: {stats}")
print(f"Filtered (>5): {filtered}")
"""
generator = CodeGenerator()
cpp_code, metadata = generator.convert(code)
print("📝 Input Python Code:")
print(code)
print("\n" + "-" * 50)
print("📄 Generated C++ Code:")
print(cpp_code)
print("\n" + "-" * 50)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Verification: {metadata.get('verification_result', 'N/A')}")
print(f"✓ Validation Issues: {len(metadata.get('validation_issues', []))}")
print(f"✓ Validation Warnings: {len(metadata.get('validation_warnings', []))}")
def test_string_manipulation():
"""Test with string processing operations."""
print("\n" + "=" * 60)
print("🧪 TESTING: String Manipulation")
print("=" * 60)
code = """
def reverse_words(text):
\"\"\"Reverse the order of words in a string\"\"\"
words = text.split()
reversed_words = words[::-1]
return ' '.join(reversed_words)
def count_vowels(text):
\"\"\"Count vowels in a string\"\"\"
vowels = 'aeiouAEIOU'
count = 0
for char in text:
if char in vowels:
count += 1
return count
def palindrome_check(text):
\"\"\"Check if a string is a palindrome\"\"\"
# Remove spaces and convert to lowercase
clean_text = ''.join(text.split()).lower()
return clean_text == clean_text[::-1]
# Test the functions
test_string = "Hello World Python Programming"
reversed_str = reverse_words(test_string)
vowel_count = count_vowels(test_string)
is_palindrome = palindrome_check("A man a plan a canal Panama")
print(f"Original: '{test_string}'")
print(f"Reversed words: '{reversed_str}'")
print(f"Vowel count: {vowel_count}")
print(f"Is palindrome: {is_palindrome}")
"""
generator = CodeGenerator()
cpp_code, metadata = generator.convert(code)
print("📝 Input Python Code:")
print(code)
print("\n" + "-" * 50)
print("📄 Generated C++ Code:")
print(cpp_code)
print("\n" + "-" * 50)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Verification: {metadata.get('verification_result', 'N/A')}")
print(f"✓ Validation Issues: {len(metadata.get('validation_issues', []))}")
print(f"✓ Validation Warnings: {len(metadata.get('validation_warnings', []))}")
def test_complex_algorithm():
"""Test with a more complex algorithm."""
print("\n" + "=" * 60)
print("🧪 TESTING: Complex Algorithm (Sorting)")
print("=" * 60)
code = """
def bubble_sort(arr):
\"\"\"Sort array using bubble sort\"\"\"
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
# Swap elements
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
def quicksort(arr):
\"\"\"Sort array using quicksort\"\"\"
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
def binary_search(arr, target):
\"\"\"Binary search in sorted array\"\"\"
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Test the algorithms
test_array = [64, 34, 25, 12, 22, 11, 90, 5, 77, 30]
print(f"Original array: {test_array}")
# Bubble sort
bubble_sorted = bubble_sort(test_array.copy())
print(f"Bubble sorted: {bubble_sorted}")
# Quicksort
quick_sorted = quicksort(test_array.copy())
print(f"Quick sorted: {quick_sorted}")
# Binary search
search_target = 22
search_result = binary_search(quick_sorted, search_target)
print(f"Binary search for {search_target}: index {search_result}")
"""
generator = CodeGenerator()
cpp_code, metadata = generator.convert(code)
print("📝 Input Python Code:")
print(code)
print("\n" + "-" * 50)
print("📄 Generated C++ Code:")
print(cpp_code)
print("\n" + "-" * 50)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Verification: {metadata.get('verification_result', 'N/A')}")
print(f"✓ Validation Issues: {len(metadata.get('validation_issues', []))}")
print(f"✓ Validation Warnings: {len(metadata.get('validation_warnings', []))}")
def test_error_handling():
"""Test error handling with problematic code."""
print("\n" + "=" * 60)
print("🧪 TESTING: Error Handling")
print("=" * 60)
# Test with dangerous code
dangerous_code = """
import os
import subprocess
def dangerous_function():
# This should be blocked by validation
result = os.system('rm -rf /')
return subprocess.call(['ls', '-la'])
dangerous_function()
"""
print("Testing dangerous code (should be blocked):")
generator = CodeGenerator()
cpp_code, metadata = generator.convert(dangerous_code)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Validation Issues: {metadata.get('validation_issues', [])}")
print(f"✓ Validation Warnings: {metadata.get('validation_warnings', [])}")
# Test with syntax error
syntax_error_code = """
def broken function( # Missing colon
return "broken"
broken function()
"""
print("\\nTesting syntax error code:")
cpp_code, metadata = generator.convert(syntax_error_code)
print("📊 Results:")
print(f"✓ Success: {metadata.get('success', False)}")
print(f"✓ Validation Issues: {metadata.get('validation_issues', [])}")
def run_comprehensive_tests():
"""Run all comprehensive tests."""
print("🚀 STARTING COMPREHENSIVE TESTING OF AGENTIC CODE GENERATOR")
print("=" * 80)
# Check environment
groq_key = os.getenv("GROQ_API_KEY")
if not groq_key:
print("⚠️ WARNING: GROQ_API_KEY not set. Some tests may fail.")
print(" Set it with: export GROQ_API_KEY=your_key_here")
else:
print("✅ GROQ_API_KEY is set")
try:
# Run all tests
test_fibonacci()
test_mathematical_computation()
test_data_processing()
test_string_manipulation()
test_complex_algorithm()
test_error_handling()
print("\\n" + "=" * 80)
print("🎉 COMPREHENSIVE TESTING COMPLETED!")
print("=" * 80)
print("📊 Summary:")
print("• Tested 6 different types of Python code")
print("• Validated input sanitization and security")
print("• Verified C++ code generation capabilities")
print("• Tested error handling and validation")
except Exception as e:
print(f"\\n❌ Testing failed with error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
run_comprehensive_tests()