Skip to content

Latest commit

 

History

History
261 lines (200 loc) · 7.54 KB

File metadata and controls

261 lines (200 loc) · 7.54 KB

ZIP Compiler - Comprehensive Test Suite

Overview

The ZIP compiler now includes a comprehensive test suite covering 46 test cases across multiple categories:

  • Basic functionality (variables, operators, functions)
  • Control flow (if/else, while, for, break, continue)
  • Edge cases and complex scenarios
  • Error detection and validation

Test Categories

1. Basic Functionality Tests (10 tests)

Tests for core language features:

Test Description
empty_main Minimal program with empty main function
simple_variable Single variable declaration and assignment
multiple_variables Multiple variable declarations
variable_reassignment Reassigning variables multiple times
arithmetic_operations All arithmetic operators (+, -, *, /, %)
unary_operators Unary minus (-) and logical NOT (!)
boolean_literals Boolean true/false literals
string_literal String literal support
comparison_operators All comparison operators (==, !=, <, >, <=, >=)
logical_operators Logical operators (&&, ||)

File: test_suite.py (BASIC_TESTS)

2. Function Tests (6 tests)

Tests for function definition and calling:

Test Description
function_no_params Functions with no parameters
function_single_param Single parameter function
function_multiple_params Multiple parameters (up to 6 supported)
function_six_params Maximum parameters test
recursive_factorial Simple recursion (factorial)
mutual_recursion Mutual recursion (is_even/is_odd)

File: test_suite.py (FUNCTION_TESTS)

3. Control Flow Tests (12 tests)

Tests for conditional and loop statements:

Test Description
if_statement Simple if statement
if_else_statement If/else branching
if_else_if_else Chain of if/else if/else
while_loop Basic while loop
while_with_break While loop with break statement
while_with_continue While loop with continue statement
for_loop Basic for loop
for_with_break For loop with break
for_with_continue For loop with continue
nested_loops Nested while loops
nested_for_loops Nested for loops
break_in_nested_loop Break statement in nested loops

File: test_suite.py (CONTROL_FLOW_TESTS)

4. Edge Cases & Complex Scenarios (12 tests)

Tests for advanced features and edge cases:

Test Description
large_numbers Large integer values (999999+)
negative_numbers Negative number handling
zero_values Zero and operations with zero
complex_expressions Complex arithmetic expressions with precedence
deeply_nested_expressions Deeply nested parenthesized expressions
long_variable_names Long descriptive variable names
multiple_functions Program with 5+ functions
fibonacci Fibonacci recursive sequence
gcd_algorithm GCD calculation with recursion
nested_if_else Deeply nested if/else statements
complex_boolean_logic Complex boolean expressions
string_operations String handling and printing

File: test_suite.py (EDGE_CASE_TESTS)

5. Error Detection Tests (6 tests)

Tests that verify error handling:

Test Description Expected Result
undeclared_variable Using undefined variable Compilation fails
reassign_undefined Assigning to undefined variable Compilation fails
break_outside_loop Break statement outside loop Compilation fails
continue_outside_loop Continue statement outside loop Compilation fails
undefined_function Calling non-existent function Compilation fails
too_many_function_args Too many arguments to function Compilation fails

File: test_suite.py (ERROR_TESTS)

Individual Test Case Files

Example programs are provided in examples/:

File Purpose
test_arithmetic.zip Arithmetic operations demonstration
test_recursion.zip Factorial and recursion testing
test_loops.zip Loop control flow (break/continue)
test_nested_loops.zip Nested loops and complex control flow
test_strings.zip String literals and printing
test_booleans.zip Boolean operations and comparisons
test_functions.zip Function calls with multiple parameters
test_mutual_recursion.zip Mutual recursion pattern

Running the Tests

Run Full Test Suite

python test_suite.py

This runs all 46 tests and reports:

  • Pass/fail status for each test
  • Error details for failed tests
  • Summary statistics

Expected output:

Passed: 46/46
✅ ALL TESTS PASSED! Compiler is working correctly!

Run Individual Test Programs

# Compile and examine a specific test
python src/main.py examples/test_arithmetic.zip -o test_arithmetic
./test_arithmetic

# Or just run through compiler
python src/main.py examples/test_functions.zip -o functions_test

Run Optimization Benchmarks

python test_optimizations.py

Test Coverage

Language Features Covered

Variables

  • Declaration with type annotation
  • Reassignment
  • Scope handling

Data Types

  • int (integers)
  • bool (boolean true/false)
  • string (string literals)

Operators

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: &&, ||, !
  • Unary: -, !

Functions

  • Definition and calling
  • Parameters (up to 6)
  • Return statements
  • Recursion (simple and mutual)

Control Flow

  • if/else statements
  • if/else if/else chains
  • while loops
  • for loops
  • break statements
  • continue statements
  • Nested control structures

Built-in Functions

  • print() for integers, booleans, strings

Comments

  • Single-line comments with //

Test Results Summary

BASIC FUNCTIONALITY:        10/10 ✓
FUNCTION TESTS:              6/6  ✓
CONTROL FLOW TESTS:         12/12 ✓
EDGE CASES & COMPLEX:       12/12 ✓
ERROR DETECTION:             6/6  ✓
────────────────────────────────────
TOTAL:                      46/46 ✓

Performance Metrics

When running with optimized compiler:

  • Lexer: ~0.1-0.3ms per test
  • Parser: ~0.04-0.1ms per test
  • Analyzer: ~0.08-0.2ms per test
  • CodeGen: ~0.02-0.05ms per test
  • Average total: 0.3ms per compilation

Adding New Tests

To add new tests to the suite:

  1. Define test program code as a string
  2. Add to appropriate test category list:
    BASIC_TESTS.append(("test_name", """code here"""))
  3. Run python test_suite.py to execute

Or create a new .zip file in examples/ directory.

Error Scenarios

The compiler correctly detects and rejects:

  • ✓ Undeclared variables
  • ✓ Undefined functions
  • ✓ break/continue outside loops
  • ✓ Too many function arguments
  • ✓ Invalid syntax
  • ✓ Type mismatches

Future Test Enhancements

Potential additions:

  1. Integration tests with actual execution
  2. Assembly code validation tests
  3. Optimization validation tests
  4. Memory safety tests
  5. Performance regression tests
  6. Stress tests with very large programs

Notes

  • All tests validate compilation success/failure
  • Actual program execution requires assembling and linking with as and ld
  • Tests are Python-based and work on all platforms
  • Error detection tests verify compile-time checking works correctly

Status: ✅ All 46 tests passing Last Updated: 2026-04-22 Coverage: 46 test cases across 5 categories