# rawk Test Suite # This file tests all major features of the rawk language using assertions # Basic function definitions for testing $add = (x, y) -> x + y; $multiply = (a, b) -> a * b; $greet = (name) -> "Hello, " name; $square = (x) -> x * x; $double = (x) -> x * 2; # Multi-line function for testing $calculate_area = (width, height) -> { area = width * height return area }; # Function that calls other functions $complex_calc = (x, y) -> { doubled = double(x) squared = square(y) result = add(doubled, squared) return result }; # Test runner BEGIN { print "=== rawk Test Suite ===" test_count = 0 passed_count = 0 # Test 1: Basic single-line functions test_count++ result = add(5, 3) expect_equal(result, 8, "add(5, 3) should return 8") passed_count++ print "✓ Test " test_count ": Basic addition" test_count++ result = multiply(4, 7) expect_equal(result, 28, "multiply(4, 7) should return 28") passed_count++ print "✓ Test " test_count ": Basic multiplication" test_count++ result = greet("World") expect_equal(result, "Hello, World", "greet(\"World\") should return 'Hello, World'") passed_count++ print "✓ Test " test_count ": String concatenation" # Test 2: Multi-line functions test_count++ result = calculate_area(5, 3) expect_equal(result, 15, "calculate_area(5, 3) should return 15") passed_count++ print "✓ Test " test_count ": Multi-line function" # Test 3: Nested function calls test_count++ result = double(square(3)) expect_equal(result, 18, "double(square(3)) should return 18") passed_count++ print "✓ Test " test_count ": Nested function calls" test_count++ result = square(double(3)) expect_equal(result, 36, "square(double(3)) should return 36") passed_count++ print "✓ Test " test_count ": Different nested function order" # Test 4: Function calls within function bodies test_count++ result = complex_calc(3, 4) expect_equal(result, 22, "complex_calc(3, 4) should return 22") passed_count++ print "✓ Test " test_count ": Function calls within function bodies" # Test 5: Edge cases test_count++ result = add(0, 0) expect_equal(result, 0, "add(0, 0) should return 0") passed_count++ print "✓ Test " test_count ": Zero values" test_count++ result = multiply(-2, 3) expect_equal(result, -6, "multiply(-2, 3) should return -6") passed_count++ print "✓ Test " test_count ": Negative numbers" # Test 6: String operations test_count++ result = greet("") expect_equal(result, "Hello, ", "greet(\"\") should return 'Hello, '") passed_count++ print "✓ Test " test_count ": Empty string" # Test 7: Boolean assertions test_count++ expect_true(add(2, 2) == 4, "2 + 2 should equal 4") passed_count++ print "✓ Test " test_count ": Boolean true assertion" test_count++ expect_false(add(2, 2) == 5, "2 + 2 should not equal 5") passed_count++ print "✓ Test " test_count ": Boolean false assertion" # Test 8: Array operations (basic) test_count++ data[1] = 10 data[2] = 20 data[3] = 30 expect_equal(data[1], 10, "data[1] should be 10") expect_equal(data[2], 20, "data[2] should be 20") expect_equal(data[3], 30, "data[3] should be 30") passed_count++ print "✓ Test " test_count ": Basic array operations" # Test 9: Conditional expressions test_count++ result = 5 > 3 ? "greater" : "less" expect_equal(result, "greater", "5 > 3 should be 'greater'") passed_count++ print "✓ Test " test_count ": Conditional expressions" # Test 10: Complex expressions test_count++ result = (2 + 3) * 4 expect_equal(result, 20, "(2 + 3) * 4 should be 20") passed_count++ print "✓ Test " test_count ": Complex expressions" # Summary print "\n=== Test Summary ===" print "Total tests: " test_count print "Passed: " passed_count print "Failed: " (test_count - passed_count) if (passed_count == test_count) { print "🎉 All tests passed!" } else { print "❌ Some tests failed!" exit 1 } }