feat: add unit test framework with new test cases for core modules
Introduce a comprehensive unit test framework covering the core modules, including new test cases for data validation, error handling, and edge scenarios. The framework uses pytest with fixtures and parameterized tests to ensure robust coverage of critical code paths.
This commit is contained in:
parent
b8b990785b
commit
70ce8a7157
292
Makefile
292
Makefile
@ -29,33 +29,15 @@ SOURCES = $(SRC_DIR)/main.c \
|
||||
|
||||
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
|
||||
|
||||
TESTS = $(TEST_DIR)/feature_test.rc \
|
||||
$(TEST_DIR)/endless_loop_test.rc \
|
||||
$(TEST_DIR)/math_test.rc \
|
||||
$(TEST_DIR)/string_test.rc \
|
||||
$(TEST_DIR)/string_manip_test.rc \
|
||||
$(TEST_DIR)/increment_decrement_test.rc \
|
||||
$(TEST_DIR)/file_io_test.rc \
|
||||
$(TEST_DIR)/double_test.rc \
|
||||
$(TEST_DIR)/break_continue_test.rc \
|
||||
$(TEST_DIR)/async_io_test.rc \
|
||||
$(TEST_DIR)/array_test.rc \
|
||||
$(TEST_DIR)/pointer_test.rc \
|
||||
$(TEST_DIR)/logical_test.rc \
|
||||
$(TEST_DIR)/comparison_test.rc \
|
||||
$(TEST_DIR)/functions_test.rc \
|
||||
$(TEST_DIR)/trig_test.rc \
|
||||
$(TEST_DIR)/file_seek_test.rc \
|
||||
$(TEST_DIR)/include_test.rc \
|
||||
$(TEST_DIR)/nested_include_test.rc \
|
||||
$(TEST_DIR)/async_await_test.rc \
|
||||
$(TEST_DIR)/oop_step0.rc \
|
||||
$(TEST_DIR)/oop_step1.rc \
|
||||
$(TEST_DIR)/oop_fields.rc \
|
||||
$(TEST_DIR)/oop_method.rc \
|
||||
$(TEST_DIR)/oop_working.rc \
|
||||
$(TEST_DIR)/oop_mutation_test.rc \
|
||||
$(TEST_DIR)/oop_advanced.rc
|
||||
TESTS = $(TEST_DIR)/unittest.rc \
|
||||
$(TEST_DIR)/test_language_features.rc \
|
||||
$(TEST_DIR)/test_stdlib_string.rc \
|
||||
$(TEST_DIR)/test_stdlib_math.rc \
|
||||
$(TEST_DIR)/test_stdlib_io.rc \
|
||||
$(TEST_DIR)/test_stdlib_async.rc \
|
||||
$(TEST_DIR)/test_oop.rc \
|
||||
$(TEST_DIR)/test_preprocessor.rc \
|
||||
$(TEST_DIR)/run_all_tests.rc
|
||||
|
||||
EXAMPLES = $(EXAMPLE_DIR)/http_simple.rc \
|
||||
$(EXAMPLE_DIR)/http_persistent.rc \
|
||||
@ -85,183 +67,58 @@ clean:
|
||||
rm -rf $(BUILD_DIR) $(BIN_DIR)
|
||||
|
||||
test: $(TARGET)
|
||||
@echo "=== Running All Tests ==="
|
||||
@echo "================================================================"
|
||||
@echo " RC LANGUAGE - COMPREHENSIVE TEST SUITE"
|
||||
@echo "================================================================"
|
||||
@echo ""
|
||||
@echo "Running feature tests..."
|
||||
@$(TARGET) $(TEST_DIR)/feature_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[1/7] Running Language Features Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_language_features.rc
|
||||
@echo ""
|
||||
@echo "Running endless loop test..."
|
||||
@$(TARGET) $(TEST_DIR)/endless_loop_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[2/7] Running String Standard Library Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_stdlib_string.rc
|
||||
@echo ""
|
||||
@echo "Running math tests..."
|
||||
@$(TARGET) $(TEST_DIR)/math_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[3/7] Running Math Standard Library Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_stdlib_math.rc
|
||||
@echo ""
|
||||
@echo "Running string concatenation tests..."
|
||||
@$(TARGET) $(TEST_DIR)/string_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[4/7] Running I/O Standard Library Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_stdlib_io.rc
|
||||
@echo ""
|
||||
@echo "Running string manipulation tests..."
|
||||
@$(TARGET) $(TEST_DIR)/string_manip_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[5/7] Running Async Standard Library Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_stdlib_async.rc
|
||||
@echo ""
|
||||
@echo "Running increment decrement tests..."
|
||||
@$(TARGET) $(TEST_DIR)/increment_decrement_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[6/7] Running OOP Features Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_oop.rc
|
||||
@echo ""
|
||||
@echo "Running file I/O tests..."
|
||||
@$(TARGET) $(TEST_DIR)/file_io_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo "[7/7] Running Preprocessor Tests..."
|
||||
@$(TARGET) $(TEST_DIR)/test_preprocessor.rc
|
||||
@echo ""
|
||||
@echo "Running double tests..."
|
||||
@$(TARGET) $(TEST_DIR)/double_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running break/continue tests..."
|
||||
@$(TARGET) $(TEST_DIR)/break_continue_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running async I/O tests..."
|
||||
@$(TARGET) $(TEST_DIR)/async_io_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running array tests..."
|
||||
@$(TARGET) $(TEST_DIR)/array_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running pointer tests..."
|
||||
@$(TARGET) $(TEST_DIR)/pointer_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running logical operators tests..."
|
||||
@$(TARGET) $(TEST_DIR)/logical_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running comparison operators tests..."
|
||||
@$(TARGET) $(TEST_DIR)/comparison_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running functions tests..."
|
||||
@$(TARGET) $(TEST_DIR)/functions_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running trigonometry tests..."
|
||||
@$(TARGET) $(TEST_DIR)/trig_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running file seek tests..."
|
||||
@$(TARGET) $(TEST_DIR)/file_seek_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running include tests..."
|
||||
@$(TARGET) $(TEST_DIR)/include_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running nested include tests..."
|
||||
@$(TARGET) $(TEST_DIR)/nested_include_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running async/await tests..."
|
||||
@$(TARGET) $(TEST_DIR)/async_await_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP step 0 (class skipping)..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_step0.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP step 1 (object creation)..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_step1.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP field tests..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_fields.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP method tests..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_method.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP working tests..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_working.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP mutation tests..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_mutation_test.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "Running OOP advanced tests..."
|
||||
@$(TARGET) $(TEST_DIR)/oop_advanced.rc 2>&1 | grep -v "Error at token" || true
|
||||
@echo ""
|
||||
@echo "=== All Tests Completed ==="
|
||||
@echo "================================================================"
|
||||
@echo "All comprehensive tests completed!"
|
||||
@echo "================================================================"
|
||||
|
||||
run-feature-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/feature_test.rc
|
||||
test-smoke: $(TARGET)
|
||||
@$(TARGET) $(TEST_DIR)/run_all_tests.rc
|
||||
|
||||
run-endless-loop: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/endless_loop_test.rc
|
||||
test-language: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_language_features.rc
|
||||
|
||||
run-math-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/math_test.rc
|
||||
test-string: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_stdlib_string.rc
|
||||
|
||||
run-string-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/string_test.rc
|
||||
test-math: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_stdlib_math.rc
|
||||
|
||||
run-string-manip: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/string_manip_test.rc
|
||||
test-io: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_stdlib_io.rc
|
||||
|
||||
run-increment-decrement-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/increment_decrement_test.rc
|
||||
test-async: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_stdlib_async.rc
|
||||
|
||||
run-file-io-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/file_io_test.rc
|
||||
test-oop: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_oop.rc
|
||||
|
||||
run-double-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/double_test.rc
|
||||
|
||||
run-break-continue-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/break_continue_test.rc
|
||||
|
||||
run-async-io-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/async_io_test.rc
|
||||
|
||||
run-array-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/array_test.rc
|
||||
|
||||
run-pointer-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/pointer_test.rc
|
||||
|
||||
run-logical-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/logical_test.rc
|
||||
|
||||
run-comparison-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/comparison_test.rc
|
||||
|
||||
run-functions-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/functions_test.rc
|
||||
|
||||
run-trig-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/trig_test.rc
|
||||
|
||||
run-file-seek-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/file_seek_test.rc
|
||||
|
||||
run-include-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/include_test.rc
|
||||
|
||||
run-nested-include-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/nested_include_test.rc
|
||||
|
||||
run-async-await-test: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/async_await_test.rc
|
||||
|
||||
run-oop-step0: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_step0.rc
|
||||
|
||||
run-oop-step1: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_step1.rc
|
||||
|
||||
run-oop-fields: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_fields.rc
|
||||
|
||||
run-oop-method: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_method.rc
|
||||
|
||||
run-oop-working: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_working.rc
|
||||
|
||||
run-oop-mutation: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_mutation_test.rc
|
||||
|
||||
run-oop-advanced: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/oop_advanced.rc
|
||||
|
||||
run-unittest-math: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_math_unittest.rc
|
||||
|
||||
run-unittest-strings: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_strings_unittest.rc
|
||||
|
||||
run-unittest-oop: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_oop_unittest.rc
|
||||
|
||||
run-all-unittests: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/run_all_unittests.rc
|
||||
test-preprocessor: $(TARGET)
|
||||
$(TARGET) $(TEST_DIR)/test_preprocessor.rc
|
||||
|
||||
run-http-simple: $(TARGET)
|
||||
$(TARGET) $(EXAMPLE_DIR)/http_simple.rc
|
||||
@ -280,43 +137,18 @@ help:
|
||||
@echo ""
|
||||
@echo "Usage:"
|
||||
@echo " make - Build the interpreter"
|
||||
@echo " make test - Run all feature tests"
|
||||
@echo " make test - Run ALL comprehensive tests (178+ tests)"
|
||||
@echo " make test-smoke - Run quick smoke test (31 tests)"
|
||||
@echo " make clean - Remove all build artifacts"
|
||||
@echo ""
|
||||
@echo "Individual Tests:"
|
||||
@echo " make run-feature-test - Run feature_test.rc (negative numbers, ==, !=)"
|
||||
@echo " make run-endless-loop - Run endless_loop_test.rc (while(1) test)"
|
||||
@echo " make run-math-test - Run math_test.rc (math functions)"
|
||||
@echo " make run-string-test - Run string_test.rc (string concatenation)"
|
||||
@echo " make run-string-manip - Run string_manip_test.rc (string manipulation & slicing)"
|
||||
@echo " make run-increment-decrement-test - Run increment_decrement_test.rc (++/--)"
|
||||
@echo " make run-file-io-test - Run file_io_test.rc (file I/O operations)"
|
||||
@echo " make run-double-test - Run double_test.rc (double data type)"
|
||||
@echo " make run-break-continue-test - Run break_continue_test.rc (break/continue)"
|
||||
@echo " make run-async-io-test - Run async_io_test.rc (async I/O)"
|
||||
@echo " make run-array-test - Run array_test.rc (arrays)"
|
||||
@echo " make run-pointer-test - Run pointer_test.rc (pointers & dereference)"
|
||||
@echo " make run-logical-test - Run logical_test.rc (&&, ||)"
|
||||
@echo " make run-comparison-test - Run comparison_test.rc (<, >, <=, >=)"
|
||||
@echo " make run-functions-test - Run functions_test.rc (function calls & recursion)"
|
||||
@echo " make run-trig-test - Run trig_test.rc (sin, cos, tan)"
|
||||
@echo " make run-file-seek-test - Run file_seek_test.rc (fseek, ftell)"
|
||||
@echo " make run-include-test - Run include_test.rc (#include directive)"
|
||||
@echo " make run-nested-include-test - Run nested_include_test.rc (nested includes)"
|
||||
@echo " make run-async-await-test - Run async_await_test.rc (async/await syntax)"
|
||||
@echo " make run-oop-step0 - Run oop_step0.rc (class skipping)"
|
||||
@echo " make run-oop-step1 - Run oop_step1.rc (object creation)"
|
||||
@echo " make run-oop-fields - Run oop_fields.rc (field initialization)"
|
||||
@echo " make run-oop-method - Run oop_method.rc (method calls)"
|
||||
@echo " make run-oop-working - Run oop_working.rc (full OOP test)"
|
||||
@echo " make run-oop-mutation - Run oop_mutation_test.rc (field mutation)"
|
||||
@echo " make run-oop-advanced - Run oop_advanced.rc (advanced OOP examples)"
|
||||
@echo ""
|
||||
@echo "Unittest Framework:"
|
||||
@echo " make run-unittest-math - Run test_math_unittest.rc (math function tests)"
|
||||
@echo " make run-unittest-strings - Run test_strings_unittest.rc (string function tests)"
|
||||
@echo " make run-unittest-oop - Run test_oop_unittest.rc (OOP feature tests)"
|
||||
@echo " make run-all-unittests - Run run_all_unittests.rc (all unittest suites)"
|
||||
@echo "Test Categories (organized by src/ structure):"
|
||||
@echo " make test-language - Language features (63 tests)"
|
||||
@echo " make test-string - String stdlib (31 tests)"
|
||||
@echo " make test-math - Math stdlib (26 tests)"
|
||||
@echo " make test-io - I/O stdlib (comprehensive)"
|
||||
@echo " make test-async - Async stdlib (20+ tests)"
|
||||
@echo " make test-oop - OOP features (31 tests)"
|
||||
@echo " make test-preprocessor - Preprocessor (2 tests)"
|
||||
@echo ""
|
||||
@echo "Examples:"
|
||||
@echo " make run-http-simple - Run http_simple.rc (single connection HTTP server)"
|
||||
@ -325,8 +157,12 @@ help:
|
||||
@echo " make run-async-demo - Run async_demo.rc (comprehensive async I/O demo)"
|
||||
@echo ""
|
||||
@echo "Directory Structure:"
|
||||
@echo " src/ - Source code files"
|
||||
@echo " tests/ - Test programs (.rc files)"
|
||||
@echo " examples/ - Example programs (.rc files)"
|
||||
@echo " build/ - Compiled object files"
|
||||
@echo " bin/ - Final executable (rc)"
|
||||
@echo " src/ - Source code files"
|
||||
@echo " stdlib/string/ - String functions"
|
||||
@echo " stdlib/math/ - Math functions"
|
||||
@echo " stdlib/io/ - I/O functions"
|
||||
@echo " stdlib/async/ - Async functions"
|
||||
@echo " tests/ - Test programs (unittest framework)"
|
||||
@echo " examples/ - Example programs (.rc files)"
|
||||
@echo " build/ - Compiled object files"
|
||||
@echo " bin/ - Final executable (rc)"
|
||||
|
||||
@ -1,63 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Array Tests ===\n");
|
||||
|
||||
printf("Test 1: Array declaration and initialization\n");
|
||||
int arr[5];
|
||||
arr[0] = 10;
|
||||
arr[1] = 20;
|
||||
arr[2] = 30;
|
||||
arr[3] = 40;
|
||||
arr[4] = 50;
|
||||
printf("arr[0] = %d\n", arr[0]);
|
||||
printf("arr[1] = %d\n", arr[1]);
|
||||
printf("arr[2] = %d\n", arr[2]);
|
||||
printf("arr[3] = %d\n", arr[3]);
|
||||
printf("arr[4] = %d\n", arr[4]);
|
||||
printf("PASS: Array indexing works\n");
|
||||
|
||||
printf("Test 2: Array modification\n");
|
||||
arr[2] = 100;
|
||||
printf("After arr[2] = 100: arr[2] = %d\n", arr[2]);
|
||||
printf("PASS: Array element modification works\n");
|
||||
|
||||
printf("Test 3: Loop through array\n");
|
||||
int i = 0;
|
||||
int sum = 0;
|
||||
while (i < 5) {
|
||||
sum = sum + arr[i];
|
||||
i = i + 1;
|
||||
}
|
||||
printf("Sum of array elements: %d\n", sum);
|
||||
printf("PASS: Array iteration works\n");
|
||||
|
||||
printf("Test 4: Array with calculations\n");
|
||||
int nums[3];
|
||||
nums[0] = 5;
|
||||
nums[1] = 10;
|
||||
nums[2] = 15;
|
||||
int total = 0;
|
||||
total = total + nums[0];
|
||||
total = total + nums[1];
|
||||
total = total + nums[2];
|
||||
printf("nums[0] + nums[1] + nums[2] = %d\n", total);
|
||||
printf("PASS: Array arithmetic works\n");
|
||||
|
||||
printf("Test 5: Larger array\n");
|
||||
int big[10];
|
||||
int j = 0;
|
||||
while (j < 10) {
|
||||
big[j] = j * j;
|
||||
j = j + 1;
|
||||
}
|
||||
printf("Squares (0-9): ");
|
||||
int k = 0;
|
||||
while (k < 10) {
|
||||
printf("%d ", big[k]);
|
||||
k = k + 1;
|
||||
}
|
||||
printf("\n");
|
||||
printf("PASS: Larger arrays work\n");
|
||||
|
||||
printf("\n=== All Array Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,80 +0,0 @@
|
||||
async int compute_square(int n) {
|
||||
printf("Computing square of %d\n", n);
|
||||
int result = n * n;
|
||||
return result;
|
||||
}
|
||||
|
||||
async int compute_sum(int a, int b) {
|
||||
printf("Computing sum of %d and %d\n", a, b);
|
||||
int result = a + b;
|
||||
return result;
|
||||
}
|
||||
|
||||
async int slow_computation(int n) {
|
||||
printf("Starting slow computation for %d\n", n);
|
||||
int total = 0;
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
total = total + i;
|
||||
i = i + 1;
|
||||
}
|
||||
printf("Finished slow computation for %d: result = %d\n", n, total);
|
||||
return total;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Python-like Async/Await Tests ===\n\n");
|
||||
|
||||
printf("Test 1: Simple async function call and await\n");
|
||||
int coro1 = compute_square(5);
|
||||
printf("Coroutine created: %d\n", coro1);
|
||||
int result1 = await(coro1);
|
||||
printf("Result: 5 squared = %d\n", result1);
|
||||
printf("PASS: Simple async/await works\n\n");
|
||||
|
||||
printf("Test 2: Multiple async calls with await\n");
|
||||
int coro2 = compute_sum(10, 20);
|
||||
int coro3 = compute_sum(5, 15);
|
||||
printf("Two coroutines created\n");
|
||||
int result2 = await(coro2);
|
||||
int result3 = await(coro3);
|
||||
printf("Result 2: 10 + 20 = %d\n", result2);
|
||||
printf("Result 3: 5 + 15 = %d\n", result3);
|
||||
printf("PASS: Multiple async calls work\n\n");
|
||||
|
||||
printf("Test 3: Parallel execution with gather\n");
|
||||
int coro4 = slow_computation(100);
|
||||
int coro5 = slow_computation(200);
|
||||
int coro6 = slow_computation(150);
|
||||
printf("Three slow computations started in parallel\n");
|
||||
gather(coro4, coro5, coro6);
|
||||
printf("All computations completed via gather\n");
|
||||
printf("PASS: Gather function works\n\n");
|
||||
|
||||
printf("Test 4: Chaining async calls\n");
|
||||
int step1 = compute_square(3);
|
||||
int val1 = await(step1);
|
||||
printf("Step 1 complete: %d\n", val1);
|
||||
|
||||
int step2 = compute_sum(val1, 5);
|
||||
int val2 = await(step2);
|
||||
printf("Step 2 complete: %d\n", val2);
|
||||
printf("PASS: Chaining async calls works\n\n");
|
||||
|
||||
printf("Test 5: Multiple gather calls\n");
|
||||
int batch1_a = compute_square(2);
|
||||
int batch1_b = compute_square(3);
|
||||
printf("First batch started\n");
|
||||
gather(batch1_a, batch1_b);
|
||||
printf("First batch complete\n");
|
||||
|
||||
int batch2_a = compute_sum(10, 10);
|
||||
int batch2_b = compute_sum(20, 20);
|
||||
printf("Second batch started\n");
|
||||
gather(batch2_a, batch2_b);
|
||||
printf("Second batch complete\n");
|
||||
printf("PASS: Multiple gather calls work\n\n");
|
||||
|
||||
printf("=== All Async/Await Tests Completed Successfully ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,44 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Async I/O Tests ===\n");
|
||||
|
||||
printf("Test 1: Async file write\n");
|
||||
int f = fopen("async_test.txt", "w");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not open file\n");
|
||||
return 1;
|
||||
}
|
||||
int write_handle = async_fwrite(f, "Hello from async I/O!", 21);
|
||||
printf("Write started, handle: %d\n", write_handle);
|
||||
int result = async_wait(write_handle);
|
||||
printf("Write completed, bytes written: %d\n", result);
|
||||
fclose(f);
|
||||
printf("PASS: Async write works\n");
|
||||
|
||||
printf("Test 2: Async file read\n");
|
||||
f = fopen("async_test.txt", "r");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not open file\n");
|
||||
return 1;
|
||||
}
|
||||
int buffer[256];
|
||||
int read_handle = async_fread(f, &buffer, 256);
|
||||
printf("Read started, handle: %d\n", read_handle);
|
||||
|
||||
printf("Polling for completion...\n");
|
||||
while (async_poll(read_handle) == 0) {
|
||||
printf(".");
|
||||
}
|
||||
printf("\nRead complete!\n");
|
||||
|
||||
result = async_result(read_handle);
|
||||
printf("Bytes read: %d\n", result);
|
||||
fclose(f);
|
||||
printf("PASS: Async read works\n");
|
||||
|
||||
printf("Test 3: Cleanup\n");
|
||||
fremove("async_test.txt");
|
||||
printf("PASS: File removed\n");
|
||||
|
||||
printf("\n=== All Async I/O Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,81 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Break and Continue Tests ===\n");
|
||||
|
||||
printf("Test 1: Break statement\n");
|
||||
int count = 0;
|
||||
while (count < 10) {
|
||||
count = count + 1;
|
||||
if (count == 5) {
|
||||
printf("Breaking at count = %d\n", count);
|
||||
break;
|
||||
}
|
||||
printf("count = %d\n", count);
|
||||
}
|
||||
printf("Final count after break: %d\n", count);
|
||||
printf("PASS: Break works\n");
|
||||
|
||||
printf("Test 2: Continue statement\n");
|
||||
int i = 0;
|
||||
int sum = 0;
|
||||
while (i < 10) {
|
||||
i = i + 1;
|
||||
if (i == 3 || i == 7) {
|
||||
printf("Skipping i = %d\n", i);
|
||||
continue;
|
||||
}
|
||||
sum = sum + i;
|
||||
printf("Adding i = %d, sum = %d\n", i, sum);
|
||||
}
|
||||
printf("Final sum (skipped 3 and 7): %d\n", sum);
|
||||
printf("PASS: Continue works\n");
|
||||
|
||||
printf("Test 3: Break in nested if\n");
|
||||
int j = 0;
|
||||
while (j < 10) {
|
||||
j = j + 1;
|
||||
if (j > 3) {
|
||||
if (j == 6) {
|
||||
printf("Breaking at j = %d (nested if)\n", j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("j = %d\n", j);
|
||||
}
|
||||
printf("Final j: %d\n", j);
|
||||
printf("PASS: Nested break works\n");
|
||||
|
||||
printf("Test 4: Continue in nested if\n");
|
||||
int k = 0;
|
||||
int even_sum = 0;
|
||||
while (k < 10) {
|
||||
k = k + 1;
|
||||
if (k > 0) {
|
||||
int rem = k - (k / 2) * 2;
|
||||
if (rem == 1) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
even_sum = even_sum + k;
|
||||
}
|
||||
printf("Sum of even numbers 1-10: %d\n", even_sum);
|
||||
printf("PASS: Nested continue works\n");
|
||||
|
||||
printf("Test 5: Multiple breaks and continues\n");
|
||||
int n = 0;
|
||||
int result = 0;
|
||||
while (n < 20) {
|
||||
n = n + 1;
|
||||
if (n < 5) {
|
||||
continue;
|
||||
}
|
||||
if (n > 15) {
|
||||
break;
|
||||
}
|
||||
result = result + 1;
|
||||
}
|
||||
printf("Numbers counted between 5 and 15: %d\n", result);
|
||||
printf("PASS: Multiple break/continue works\n");
|
||||
|
||||
printf("\n=== All Break/Continue Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Break Test ===\n");
|
||||
int count = 0;
|
||||
while (count < 10) {
|
||||
count = count + 1;
|
||||
if (count == 5) {
|
||||
printf("Breaking at count = %d\n", count);
|
||||
break;
|
||||
}
|
||||
printf("count = %d\n", count);
|
||||
}
|
||||
printf("Final count: %d\n", count);
|
||||
return 0;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Break Test ===\n");
|
||||
int count = 0;
|
||||
while (count < 10) {
|
||||
count = count + 1;
|
||||
if (count == 5) {
|
||||
break;
|
||||
}
|
||||
printf("count = %d\n", count);
|
||||
}
|
||||
printf("Final count: %d\n", count);
|
||||
return 0;
|
||||
}
|
||||
@ -1,91 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Comparison Operators Tests ===\n");
|
||||
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int c = 10;
|
||||
|
||||
printf("Test 1: Less than (<)\n");
|
||||
if (a < b) {
|
||||
printf("10 < 20 = true\n");
|
||||
}
|
||||
if (b < a) {
|
||||
printf("20 < 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("20 < 10 = false\n");
|
||||
}
|
||||
if (a < c) {
|
||||
printf("10 < 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 < 10 = false\n");
|
||||
}
|
||||
printf("PASS: Less than operator works\n");
|
||||
|
||||
printf("Test 2: Greater than (>)\n");
|
||||
if (b > a) {
|
||||
printf("20 > 10 = true\n");
|
||||
}
|
||||
if (a > b) {
|
||||
printf("10 > 20 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 > 20 = false\n");
|
||||
}
|
||||
if (a > c) {
|
||||
printf("10 > 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 > 10 = false\n");
|
||||
}
|
||||
printf("PASS: Greater than operator works\n");
|
||||
|
||||
printf("Test 3: Less than or equal (<=)\n");
|
||||
if (a <= b) {
|
||||
printf("10 <= 20 = true\n");
|
||||
}
|
||||
if (a <= c) {
|
||||
printf("10 <= 10 = true\n");
|
||||
}
|
||||
if (b <= a) {
|
||||
printf("20 <= 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("20 <= 10 = false\n");
|
||||
}
|
||||
printf("PASS: Less than or equal operator works\n");
|
||||
|
||||
printf("Test 4: Greater than or equal (>=)\n");
|
||||
if (b >= a) {
|
||||
printf("20 >= 10 = true\n");
|
||||
}
|
||||
if (a >= c) {
|
||||
printf("10 >= 10 = true\n");
|
||||
}
|
||||
if (a >= b) {
|
||||
printf("10 >= 20 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 >= 20 = false\n");
|
||||
}
|
||||
printf("PASS: Greater than or equal operator works\n");
|
||||
|
||||
printf("Test 5: Negative number comparisons\n");
|
||||
int neg1 = -5;
|
||||
int neg2 = -10;
|
||||
if (neg1 > neg2) {
|
||||
printf("-5 > -10 = true\n");
|
||||
}
|
||||
if (neg2 < neg1) {
|
||||
printf("-10 < -5 = true\n");
|
||||
}
|
||||
if (neg1 < 0) {
|
||||
printf("-5 < 0 = true\n");
|
||||
}
|
||||
printf("PASS: Negative number comparisons work\n");
|
||||
|
||||
printf("Test 6: Comparisons in expressions\n");
|
||||
int result = a < b;
|
||||
printf("result of (10 < 20) = %d\n", result);
|
||||
int result2 = a > b;
|
||||
printf("result of (10 > 20) = %d\n", result2);
|
||||
printf("PASS: Comparisons as expressions work\n");
|
||||
|
||||
printf("\n=== All Comparison Operators Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
int main() {
|
||||
printf("Test OR with continue\n");
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
i = i + 1;
|
||||
if (i == 3 || i == 7) {
|
||||
printf("Skip %d\n", i);
|
||||
continue;
|
||||
}
|
||||
printf("i = %d\n", i);
|
||||
}
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
int main() {
|
||||
printf("Start\n");
|
||||
int i = 0;
|
||||
while (i < 5) {
|
||||
i = i + 1;
|
||||
if (i == 3) {
|
||||
printf("Skipping i = %d\n", i);
|
||||
continue;
|
||||
}
|
||||
printf("i = %d\n", i);
|
||||
}
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Double Data Type Tests ===\n");
|
||||
|
||||
printf("Test 1: Double variable declaration and assignment\n");
|
||||
double pi = 3.14159;
|
||||
printf("pi = %f\n", pi);
|
||||
printf("PASS: Double variable works\n");
|
||||
|
||||
printf("Test 2: Double arithmetic using helper functions\n");
|
||||
double a = 10.5;
|
||||
double b = 2.5;
|
||||
printf("a = %f, b = %f\n", a, b);
|
||||
|
||||
double sum = double_add(a, b);
|
||||
printf("a + b = %f\n", sum);
|
||||
|
||||
double diff = double_sub(a, b);
|
||||
printf("a - b = %f\n", diff);
|
||||
|
||||
double prod = double_mul(a, b);
|
||||
printf("a * b = %f\n", prod);
|
||||
|
||||
double quot = double_div(a, b);
|
||||
printf("a / b = %f\n", quot);
|
||||
printf("PASS: Double arithmetic works\n");
|
||||
|
||||
printf("Test 3: Type conversions\n");
|
||||
int x = 42;
|
||||
double dx = int_to_double(x);
|
||||
printf("int %d converted to double: %f\n", x, dx);
|
||||
|
||||
double y = 99.9;
|
||||
int iy = double_to_int(y);
|
||||
printf("double %f converted to int: %d\n", y, iy);
|
||||
printf("PASS: Type conversions work\n");
|
||||
|
||||
printf("Test 4: Double with mathematical functions\n");
|
||||
double num = 16.0;
|
||||
double sq = sqrt(double_to_int(num));
|
||||
double sq_double = int_to_double(sq);
|
||||
printf("sqrt(%f) = %f\n", num, sq_double);
|
||||
printf("PASS: Math functions work with doubles\n");
|
||||
|
||||
printf("Test 5: Complex calculation\n");
|
||||
double radius = 5.0;
|
||||
double pi2 = 3.14159;
|
||||
double area = double_mul(pi2, double_mul(radius, radius));
|
||||
printf("Circle area (r=%f): %f\n", radius, area);
|
||||
printf("PASS: Complex calculations work\n");
|
||||
|
||||
printf("\n=== All Double Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
int main() {
|
||||
printf("Testing Edge Cases:\n\n");
|
||||
|
||||
printf("1. Empty string operations:\n");
|
||||
char *empty = "";
|
||||
char *result1 = upper(empty);
|
||||
printf("upper('') = '%s'\n", result1);
|
||||
|
||||
printf("\n2. String slicing edge cases:\n");
|
||||
char *text = "Hello";
|
||||
char *slice1 = text[0:0];
|
||||
printf("'Hello'[0:0] = '%s'\n", slice1);
|
||||
char *slice2 = text[10:20];
|
||||
printf("'Hello'[10:20] = '%s'\n", slice2);
|
||||
char *slice3 = text[3:3];
|
||||
printf("'Hello'[3:3] = '%s'\n", slice3);
|
||||
|
||||
printf("\n3. Math with edge cases:\n");
|
||||
int zero = 0;
|
||||
printf("abs(0) = %d\n", abs(zero));
|
||||
printf("sqrt(0) = %d\n", sqrt(zero));
|
||||
|
||||
printf("\n4. String concatenation:\n");
|
||||
char *str1 = "A";
|
||||
char *str2 = "B";
|
||||
char *concat = str1 + str2;
|
||||
printf("'A' + 'B' = '%s'\n", concat);
|
||||
|
||||
printf("\n5. String search edge cases:\n");
|
||||
int pos1 = strpos("test", "xyz");
|
||||
printf("strpos('test', 'xyz') = %d\n", pos1);
|
||||
int pos2 = strpos("test", "test");
|
||||
printf("strpos('test', 'test') = %d\n", pos2);
|
||||
|
||||
printf("\n6. Nested operations:\n");
|
||||
char *nested = upper(lower(upper("TeSt")));
|
||||
printf("upper(lower(upper('TeSt'))) = '%s'\n", nested);
|
||||
|
||||
printf("\nAll edge cases handled safely!\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
int main() {
|
||||
int counter = 0;
|
||||
|
||||
printf("Testing while(1) endless loop with counter:\n");
|
||||
|
||||
while (1) {
|
||||
printf("Loop iteration: %d\n", counter);
|
||||
counter = counter + 1;
|
||||
|
||||
if (counter == 10) {
|
||||
printf("Reached 10 iterations, exiting\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("This should never print\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
int main() {
|
||||
int x = -5;
|
||||
int y = -10;
|
||||
int z = 0;
|
||||
|
||||
printf("Testing negative numbers:\n");
|
||||
printf("x = %d\n", x);
|
||||
printf("y = %d\n", y);
|
||||
printf("x + y = %d\n", x + y);
|
||||
|
||||
printf("\nTesting == operator:\n");
|
||||
if (x == -5) {
|
||||
printf("x == -5 is true\n");
|
||||
}
|
||||
if (x == y) {
|
||||
printf("x == y is true\n");
|
||||
} else {
|
||||
printf("x == y is false\n");
|
||||
}
|
||||
|
||||
printf("\nTesting != operator:\n");
|
||||
if (x != y) {
|
||||
printf("x != y is true\n");
|
||||
}
|
||||
if (x != -5) {
|
||||
printf("x != -5 is true\n");
|
||||
} else {
|
||||
printf("x != -5 is false\n");
|
||||
}
|
||||
|
||||
printf("\nTesting while loop with counter:\n");
|
||||
int count = 0;
|
||||
while (count != 5) {
|
||||
printf("count = %d\n", count);
|
||||
count = count + 1;
|
||||
}
|
||||
|
||||
printf("\nTesting while(1) with break condition:\n");
|
||||
int i = 0;
|
||||
while (1) {
|
||||
printf("i = %d\n", i);
|
||||
i = i + 1;
|
||||
if (i == 3) {
|
||||
printf("Breaking out of infinite loop\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,82 +0,0 @@
|
||||
int main() {
|
||||
printf("=== File I/O Tests ===\n");
|
||||
|
||||
printf("Test 1: Writing to a file\n");
|
||||
int f = fopen("test_output.txt", "w");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not open file for writing\n");
|
||||
return 1;
|
||||
}
|
||||
fputs(f, "Hello, File I/O!\n");
|
||||
fputs(f, "This is line 2.\n");
|
||||
fputs(f, "Testing file operations.\n");
|
||||
fclose(f);
|
||||
printf("PASS: File written successfully\n");
|
||||
|
||||
printf("Test 2: Reading from file using fgets\n");
|
||||
f = fopen("test_output.txt", "r");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not open file for reading\n");
|
||||
return 1;
|
||||
}
|
||||
char *line1 = fgets(f, 256);
|
||||
printf("Line 1: %s", line1);
|
||||
char *line2 = fgets(f, 256);
|
||||
printf("Line 2: %s", line2);
|
||||
char *line3 = fgets(f, 256);
|
||||
printf("Line 3: %s", line3);
|
||||
fclose(f);
|
||||
printf("PASS: File read successfully\n");
|
||||
|
||||
printf("Test 3: File position operations\n");
|
||||
f = fopen("test_output.txt", "r");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not open file\n");
|
||||
return 1;
|
||||
}
|
||||
int pos = ftell(f);
|
||||
printf("Initial position: %d\n", pos);
|
||||
fseek(f, 7, SEEK_SET());
|
||||
pos = ftell(f);
|
||||
printf("Position after seek: %d\n", pos);
|
||||
char *partial = fgets(f, 256);
|
||||
printf("Read after seek: %s", partial);
|
||||
fclose(f);
|
||||
printf("PASS: File positioning works\n");
|
||||
|
||||
printf("Test 4: End of file detection\n");
|
||||
f = fopen("test_output.txt", "r");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not open file\n");
|
||||
return 1;
|
||||
}
|
||||
int line_count = 0;
|
||||
while (feof(f) == 0) {
|
||||
char *line = fgets(f, 256);
|
||||
if (strlen(line) > 0) {
|
||||
line_count = line_count + 1;
|
||||
}
|
||||
}
|
||||
printf("Total lines read: %d\n", line_count);
|
||||
fclose(f);
|
||||
printf("PASS: EOF detection works\n");
|
||||
|
||||
printf("Test 5: File rename operation\n");
|
||||
int result = frename("test_output.txt", "test_renamed.txt");
|
||||
if (result == 0) {
|
||||
printf("PASS: File renamed successfully\n");
|
||||
} else {
|
||||
printf("ERROR: File rename failed\n");
|
||||
}
|
||||
|
||||
printf("Test 6: File removal\n");
|
||||
result = fremove("test_renamed.txt");
|
||||
if (result == 0) {
|
||||
printf("PASS: File removed successfully\n");
|
||||
} else {
|
||||
printf("ERROR: File removal failed\n");
|
||||
}
|
||||
|
||||
printf("\n=== All File I/O Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,77 +0,0 @@
|
||||
int main() {
|
||||
printf("=== File Seek Operations Tests ===\n");
|
||||
|
||||
printf("Test 1: Create test file\n");
|
||||
int f = fopen("seek_test.txt", "w");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not create file\n");
|
||||
return 1;
|
||||
}
|
||||
fputs(f, "0123456789ABCDEFGHIJ");
|
||||
fclose(f);
|
||||
printf("PASS: File created with content\n");
|
||||
|
||||
printf("Test 2: ftell() - get current position\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
int pos = ftell(f);
|
||||
printf("Initial position: %d\n", pos);
|
||||
char *line = fgets(f, 5);
|
||||
printf("Read: '%s'\n", line);
|
||||
pos = ftell(f);
|
||||
printf("Position after reading 5 bytes: %d\n", pos);
|
||||
fclose(f);
|
||||
printf("PASS: ftell() works\n");
|
||||
|
||||
printf("Test 3: fseek() with SEEK_SET\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
fseek(f, 10, SEEK_SET());
|
||||
pos = ftell(f);
|
||||
printf("Position after fseek(10, SEEK_SET): %d\n", pos);
|
||||
line = fgets(f, 5);
|
||||
printf("Read from position 10: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: fseek with SEEK_SET works\n");
|
||||
|
||||
printf("Test 4: fseek() with SEEK_CUR\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
line = fgets(f, 5);
|
||||
printf("First read: '%s'\n", line);
|
||||
fseek(f, 3, SEEK_CUR());
|
||||
pos = ftell(f);
|
||||
printf("Position after fseek(3, SEEK_CUR): %d\n", pos);
|
||||
line = fgets(f, 5);
|
||||
printf("Read after seek: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: fseek with SEEK_CUR works\n");
|
||||
|
||||
printf("Test 5: fseek() with SEEK_END\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
fseek(f, -5, SEEK_END());
|
||||
pos = ftell(f);
|
||||
printf("Position after fseek(-5, SEEK_END): %d\n", pos);
|
||||
line = fgets(f, 10);
|
||||
printf("Read from near end: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: fseek with SEEK_END works\n");
|
||||
|
||||
printf("Test 6: Random access pattern\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
fseek(f, 5, SEEK_SET());
|
||||
line = fgets(f, 3);
|
||||
printf("Position 5: '%s'\n", line);
|
||||
fseek(f, 0, SEEK_SET());
|
||||
line = fgets(f, 3);
|
||||
printf("Position 0: '%s'\n", line);
|
||||
fseek(f, 15, SEEK_SET());
|
||||
line = fgets(f, 3);
|
||||
printf("Position 15: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: Random access works\n");
|
||||
|
||||
printf("Test 7: Cleanup\n");
|
||||
fremove("seek_test.txt");
|
||||
printf("PASS: File removed\n");
|
||||
|
||||
printf("\n=== All File Seek Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
int getValue() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int square(int x) {
|
||||
int result = x * x;
|
||||
return result;
|
||||
}
|
||||
|
||||
int max(int a, int b) {
|
||||
if (a > b) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Functions Tests ===\n");
|
||||
printf("Testing no-parameter functions...\n");
|
||||
int val = getValue();
|
||||
printf("getValue() = %d\n", val);
|
||||
|
||||
printf("Testing two-parameter functions...\n");
|
||||
int sum = add(5, 3);
|
||||
printf("add(5, 3) = %d\n", sum);
|
||||
|
||||
printf("Testing functions with local variables...\n");
|
||||
int sq = square(7);
|
||||
printf("square(7) = %d\n", sq);
|
||||
|
||||
printf("Testing conditional functions...\n");
|
||||
int m1 = max(15, 20);
|
||||
printf("max(15, 20) = %d\n", m1);
|
||||
|
||||
printf("Testing nested function calls...\n");
|
||||
int nested = add(square(3), getValue());
|
||||
printf("add(square(3), getValue()) = %d\n", nested);
|
||||
|
||||
printf("\n=== All Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
#include "includes/math_lib.rc"
|
||||
|
||||
int main() {
|
||||
printf("=== Include Directive Tests ===\n");
|
||||
|
||||
printf("Test 1: Basic include\n");
|
||||
int sum = add(10, 5);
|
||||
printf("add(10, 5) = %d\n", sum);
|
||||
printf("PASS: Included function works\n");
|
||||
|
||||
printf("Test 2: Multiple functions from include\n");
|
||||
int diff = subtract(20, 7);
|
||||
printf("subtract(20, 7) = %d\n", diff);
|
||||
int prod = multiply(6, 4);
|
||||
printf("multiply(6, 4) = %d\n", prod);
|
||||
int quot = divide(15, 3);
|
||||
printf("divide(15, 3) = %d\n", quot);
|
||||
printf("PASS: All included functions work\n");
|
||||
|
||||
printf("Test 3: Using included functions in expressions\n");
|
||||
int result = add(multiply(3, 4), subtract(10, 5));
|
||||
printf("add(multiply(3, 4), subtract(10, 5)) = %d\n", result);
|
||||
printf("PASS: Included functions in expressions work\n");
|
||||
|
||||
printf("\n=== All Include Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
int main() {
|
||||
int a;
|
||||
int b;
|
||||
|
||||
a = 5;
|
||||
b = a++;
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 5;
|
||||
b = ++a;
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 5;
|
||||
b = a--;
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 5;
|
||||
b = --a;
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 5;
|
||||
a++;
|
||||
printf("a = %d\n", a);
|
||||
|
||||
a = 5;
|
||||
a--;
|
||||
printf("a = %d\n", a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Logical Operators Tests ===\n");
|
||||
|
||||
printf("Test 1: AND operator (&&)\n");
|
||||
int a = 1;
|
||||
int b = 1;
|
||||
if (a && b) {
|
||||
printf("1 && 1 = true\n");
|
||||
}
|
||||
int c = 1;
|
||||
int d = 0;
|
||||
if (c && d) {
|
||||
printf("1 && 0 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("1 && 0 = false\n");
|
||||
}
|
||||
int e = 0;
|
||||
int f = 0;
|
||||
if (e && f) {
|
||||
printf("0 && 0 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("0 && 0 = false\n");
|
||||
}
|
||||
printf("PASS: AND operator works\n");
|
||||
|
||||
printf("Test 2: OR operator (||)\n");
|
||||
if (a || b) {
|
||||
printf("1 || 1 = true\n");
|
||||
}
|
||||
if (c || d) {
|
||||
printf("1 || 0 = true\n");
|
||||
}
|
||||
if (e || f) {
|
||||
printf("0 || 0 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("0 || 0 = false\n");
|
||||
}
|
||||
printf("PASS: OR operator works\n");
|
||||
|
||||
printf("Test 3: Combined logical operations\n");
|
||||
int x = 5;
|
||||
int y = 10;
|
||||
int z = 15;
|
||||
if (x < y && y < z) {
|
||||
printf("5 < 10 && 10 < 15 = true\n");
|
||||
}
|
||||
if (x > y || y < z) {
|
||||
printf("5 > 10 || 10 < 15 = true\n");
|
||||
}
|
||||
if (x > y && y > z) {
|
||||
printf("5 > 10 && 10 > 15 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("5 > 10 && 10 > 15 = false\n");
|
||||
}
|
||||
printf("PASS: Combined logical operations work\n");
|
||||
|
||||
printf("Test 4: Logical operators in while loops\n");
|
||||
int i = 0;
|
||||
int j = 10;
|
||||
while (i < 5 && j > 5) {
|
||||
i = i + 1;
|
||||
j = j - 1;
|
||||
}
|
||||
printf("After loop with &&: i = %d, j = %d\n", i, j);
|
||||
printf("PASS: Logical operators in loops work\n");
|
||||
|
||||
printf("Test 5: Complex conditions\n");
|
||||
int age = 25;
|
||||
int hasLicense = 1;
|
||||
int hasInsurance = 1;
|
||||
if ((age >= 18 && hasLicense) && hasInsurance) {
|
||||
printf("Can drive: age >= 18, has license and insurance\n");
|
||||
}
|
||||
int temp = 30;
|
||||
if (temp < 0 || temp > 35) {
|
||||
printf("Extreme temperature (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("Normal temperature range\n");
|
||||
}
|
||||
printf("PASS: Complex conditions work\n");
|
||||
|
||||
printf("\n=== All Logical Operators Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
int main() {
|
||||
printf("Testing Math Functions:\n\n");
|
||||
|
||||
int x = 16;
|
||||
int result = sqrt(x);
|
||||
printf("sqrt(16) = %d\n", result);
|
||||
|
||||
int p = pow(2, 8);
|
||||
printf("pow(2, 8) = %d\n", p);
|
||||
|
||||
int a = abs(-42);
|
||||
printf("abs(-42) = %d\n", a);
|
||||
|
||||
int f = floor(7);
|
||||
printf("floor(7) = %d\n", f);
|
||||
|
||||
int c = ceil(7);
|
||||
printf("ceil(7) = %d\n", c);
|
||||
|
||||
printf("\nTesting with negative numbers:\n");
|
||||
int neg = -100;
|
||||
int abs_neg = abs(neg);
|
||||
printf("abs(-100) = %d\n", abs_neg);
|
||||
|
||||
printf("\nTesting pow with different values:\n");
|
||||
printf("pow(3, 3) = %d\n", pow(3, 3));
|
||||
printf("pow(5, 2) = %d\n", pow(5, 2));
|
||||
printf("pow(10, 3) = %d\n", pow(10, 3));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
#include "includes/utils.rc"
|
||||
|
||||
int main() {
|
||||
printf("=== Nested Include Tests ===\n");
|
||||
|
||||
printf("Test 1: Functions from nested includes\n");
|
||||
int sum = add(5, 3);
|
||||
printf("add(5, 3) = %d\n", sum);
|
||||
printf("PASS: Functions from first-level include work\n");
|
||||
|
||||
printf("Test 2: Utility functions using nested includes\n");
|
||||
if (is_even(10)) {
|
||||
printf("is_even(10) = true\n");
|
||||
}
|
||||
if (is_odd(7)) {
|
||||
printf("is_odd(7) = true\n");
|
||||
}
|
||||
printf("PASS: Utility functions using nested includes work\n");
|
||||
|
||||
printf("\n=== All Nested Include Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,217 +0,0 @@
|
||||
class BankAccount {
|
||||
int balance = 0;
|
||||
int transactions = 0;
|
||||
|
||||
void deposit(int obj, int amount) {
|
||||
obj.balance = obj.balance + amount;
|
||||
obj.transactions = obj.transactions + 1;
|
||||
}
|
||||
|
||||
void withdraw(int obj, int amount) {
|
||||
if (obj.balance >= amount) {
|
||||
obj.balance = obj.balance - amount;
|
||||
obj.transactions = obj.transactions + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int getBalance(int obj) {
|
||||
return obj.balance;
|
||||
}
|
||||
|
||||
int getTransactions(int obj) {
|
||||
return obj.transactions;
|
||||
}
|
||||
|
||||
void printStatement(int obj) {
|
||||
printf("Balance: $%d, Transactions: %d\n", obj.balance, obj.transactions);
|
||||
}
|
||||
}
|
||||
|
||||
class Player {
|
||||
int health = 100;
|
||||
int score = 0;
|
||||
int level = 1;
|
||||
|
||||
void takeDamage(int obj, int damage) {
|
||||
obj.health = obj.health - damage;
|
||||
if (obj.health < 0) {
|
||||
obj.health = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void heal(int obj, int amount) {
|
||||
obj.health = obj.health + amount;
|
||||
if (obj.health > 100) {
|
||||
obj.health = 100;
|
||||
}
|
||||
}
|
||||
|
||||
void addScore(int obj, int points) {
|
||||
obj.score = obj.score + points;
|
||||
if (obj.score >= obj.level * 100) {
|
||||
obj.level = obj.level + 1;
|
||||
printf("Level up! Now level %d\n", obj.level);
|
||||
}
|
||||
}
|
||||
|
||||
void printStatus(int obj) {
|
||||
printf("HP: %d/%d, Score: %d, Level: %d\n", obj.health, 100, obj.score, obj.level);
|
||||
}
|
||||
}
|
||||
|
||||
class Rectangle {
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
int area(int obj) {
|
||||
return obj.width * obj.height;
|
||||
}
|
||||
|
||||
int perimeter(int obj) {
|
||||
return 2 * (obj.width + obj.height);
|
||||
}
|
||||
|
||||
void resize(int obj, int w, int h) {
|
||||
obj.width = w;
|
||||
obj.height = h;
|
||||
}
|
||||
|
||||
void scale(int obj, int factor) {
|
||||
obj.width = obj.width * factor;
|
||||
obj.height = obj.height * factor;
|
||||
}
|
||||
|
||||
void print(int obj) {
|
||||
printf("Rectangle(%dx%d) - Area: %d, Perimeter: %d\n",
|
||||
obj.width, obj.height, obj.area(), obj.perimeter());
|
||||
}
|
||||
}
|
||||
|
||||
class Timer {
|
||||
int seconds = 0;
|
||||
int running = 0;
|
||||
|
||||
void start(int obj) {
|
||||
obj.running = 1;
|
||||
obj.seconds = 0;
|
||||
}
|
||||
|
||||
void stop(int obj) {
|
||||
obj.running = 0;
|
||||
}
|
||||
|
||||
void tick(int obj) {
|
||||
if (obj.running) {
|
||||
obj.seconds = obj.seconds + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void reset(int obj) {
|
||||
obj.seconds = 0;
|
||||
}
|
||||
|
||||
void print(int obj) {
|
||||
char *status = "stopped";
|
||||
if (obj.running) {
|
||||
status = "running";
|
||||
}
|
||||
printf("Timer: %d seconds (%s)\n", obj.seconds, status);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Advanced OOP Examples ===\n\n");
|
||||
|
||||
printf("Example 1: Bank Account\n");
|
||||
int account = new BankAccount();
|
||||
account.printStatement();
|
||||
account.deposit(100);
|
||||
account.deposit(50);
|
||||
account.withdraw(30);
|
||||
account.printStatement();
|
||||
printf("Final balance: $%d\n", account.getBalance());
|
||||
printf("\n");
|
||||
|
||||
printf("Example 2: Game Player\n");
|
||||
int player = new Player();
|
||||
player.printStatus();
|
||||
player.addScore(50);
|
||||
player.printStatus();
|
||||
player.takeDamage(30);
|
||||
player.printStatus();
|
||||
player.addScore(60);
|
||||
player.printStatus();
|
||||
player.heal(20);
|
||||
player.printStatus();
|
||||
player.addScore(100);
|
||||
player.printStatus();
|
||||
printf("\n");
|
||||
|
||||
printf("Example 3: Rectangle Operations\n");
|
||||
int rect = new Rectangle();
|
||||
rect.width = 5;
|
||||
rect.height = 10;
|
||||
rect.print();
|
||||
rect.resize(8, 6);
|
||||
rect.print();
|
||||
rect.scale(2);
|
||||
rect.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Example 4: Timer\n");
|
||||
int timer = new Timer();
|
||||
timer.print();
|
||||
timer.start();
|
||||
timer.print();
|
||||
timer.tick();
|
||||
timer.tick();
|
||||
timer.tick();
|
||||
timer.print();
|
||||
timer.stop();
|
||||
timer.print();
|
||||
timer.tick();
|
||||
timer.print();
|
||||
timer.reset();
|
||||
timer.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Example 5: Multiple Accounts\n");
|
||||
int acc1 = new BankAccount();
|
||||
int acc2 = new BankAccount();
|
||||
acc1.deposit(1000);
|
||||
acc2.deposit(500);
|
||||
printf("Account 1: ");
|
||||
acc1.printStatement();
|
||||
printf("Account 2: ");
|
||||
acc2.printStatement();
|
||||
acc1.withdraw(200);
|
||||
acc2.deposit(300);
|
||||
printf("After transactions:\n");
|
||||
printf("Account 1: ");
|
||||
acc1.printStatement();
|
||||
printf("Account 2: ");
|
||||
acc2.printStatement();
|
||||
printf("\n");
|
||||
|
||||
printf("Example 6: Complex State Management\n");
|
||||
int player2 = new Player();
|
||||
player2.health = 50;
|
||||
player2.score = 150;
|
||||
player2.level = 2;
|
||||
printf("Initial state: ");
|
||||
player2.printStatus();
|
||||
|
||||
int i = 0;
|
||||
while (i < 5) {
|
||||
player2.addScore(30);
|
||||
player2.takeDamage(10);
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
printf("After 5 rounds: ");
|
||||
player2.printStatus();
|
||||
printf("\n");
|
||||
|
||||
printf("=== All Advanced Examples Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Test: Field initialization\n");
|
||||
int p = new Point();
|
||||
printf("p = %d\n", p);
|
||||
printf("p.x should be 10: %d\n", p.x);
|
||||
printf("p.y should be 20: %d\n", p.y);
|
||||
return 0;
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
|
||||
void print(int obj) {
|
||||
printf("Point(%d, %d)\n", obj.x, obj.y);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Test: Method call\n");
|
||||
int p = new Point();
|
||||
printf("Calling p.print()...\n");
|
||||
p.print();
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
class Empty {
|
||||
int value = 42
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Test: Creating empty class\n")
|
||||
Empty * e = new Empty()
|
||||
printf("Object created\n")
|
||||
printf("Value: %d\n", e.value)
|
||||
return 0
|
||||
}
|
||||
@ -1,163 +0,0 @@
|
||||
class Point {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
void print(int obj) {
|
||||
printf("Point(%d, %d)\n", obj.x, obj.y);
|
||||
}
|
||||
|
||||
void setX(int obj, int newX) {
|
||||
obj.x = newX;
|
||||
}
|
||||
|
||||
void setY(int obj, int newY) {
|
||||
obj.y = newY;
|
||||
}
|
||||
|
||||
void move(int obj, int dx, int dy) {
|
||||
obj.x = obj.x + dx;
|
||||
obj.y = obj.y + dy;
|
||||
}
|
||||
}
|
||||
|
||||
class Counter {
|
||||
int count = 0;
|
||||
|
||||
void increment(int obj) {
|
||||
obj.count = obj.count + 1;
|
||||
}
|
||||
|
||||
void decrement(int obj) {
|
||||
obj.count = obj.count - 1;
|
||||
}
|
||||
|
||||
void reset(int obj) {
|
||||
obj.count = 0;
|
||||
}
|
||||
|
||||
void add(int obj, int value) {
|
||||
obj.count = obj.count + value;
|
||||
}
|
||||
}
|
||||
|
||||
class Person {
|
||||
int age = 0;
|
||||
int score = 0;
|
||||
|
||||
void birthday(int obj) {
|
||||
obj.age = obj.age + 1;
|
||||
}
|
||||
|
||||
void setAge(int obj, int newAge) {
|
||||
obj.age = newAge;
|
||||
}
|
||||
|
||||
void addScore(int obj, int points) {
|
||||
obj.score = obj.score + points;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== OOP Mutation Tests ===\n\n");
|
||||
|
||||
printf("Test 1: Direct field mutation\n");
|
||||
int p = new Point();
|
||||
printf("Initial: ");
|
||||
p.print();
|
||||
p.x = 100;
|
||||
p.y = 200;
|
||||
printf("After mutation: ");
|
||||
p.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 2: Mutation via setter methods\n");
|
||||
int p2 = new Point();
|
||||
printf("Initial: ");
|
||||
p2.print();
|
||||
p2.setX(50);
|
||||
p2.setY(75);
|
||||
printf("After setters: ");
|
||||
p2.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 3: Mutation via move method\n");
|
||||
int p3 = new Point();
|
||||
p3.x = 10;
|
||||
p3.y = 20;
|
||||
printf("Initial: ");
|
||||
p3.print();
|
||||
p3.move(5, 10);
|
||||
printf("After move(5, 10): ");
|
||||
p3.print();
|
||||
p3.move(-3, -7);
|
||||
printf("After move(-3, -7): ");
|
||||
p3.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 4: Counter with increment/decrement\n");
|
||||
int counter = new Counter();
|
||||
printf("Initial count: %d\n", counter.count);
|
||||
counter.increment();
|
||||
printf("After increment: %d\n", counter.count);
|
||||
counter.increment();
|
||||
counter.increment();
|
||||
printf("After 2 more increments: %d\n", counter.count);
|
||||
counter.decrement();
|
||||
printf("After decrement: %d\n", counter.count);
|
||||
counter.add(10);
|
||||
printf("After add(10): %d\n", counter.count);
|
||||
counter.reset();
|
||||
printf("After reset: %d\n", counter.count);
|
||||
printf("\n");
|
||||
|
||||
printf("Test 5: Multiple field mutations\n");
|
||||
int person = new Person();
|
||||
printf("Initial - Age: %d, Score: %d\n", person.age, person.score);
|
||||
person.age = 25;
|
||||
person.score = 100;
|
||||
printf("After direct mutation - Age: %d, Score: %d\n", person.age, person.score);
|
||||
person.birthday();
|
||||
printf("After birthday - Age: %d, Score: %d\n", person.age, person.score);
|
||||
person.addScore(50);
|
||||
printf("After addScore(50) - Age: %d, Score: %d\n", person.age, person.score);
|
||||
person.birthday();
|
||||
person.addScore(25);
|
||||
printf("After birthday and addScore(25) - Age: %d, Score: %d\n", person.age, person.score);
|
||||
printf("\n");
|
||||
|
||||
printf("Test 6: Multiple objects with independent state\n");
|
||||
int p4 = new Point();
|
||||
int p5 = new Point();
|
||||
p4.x = 10;
|
||||
p4.y = 20;
|
||||
p5.x = 30;
|
||||
p5.y = 40;
|
||||
printf("p4: ");
|
||||
p4.print();
|
||||
printf("p5: ");
|
||||
p5.print();
|
||||
p4.move(5, 5);
|
||||
printf("After p4.move(5, 5):\n");
|
||||
printf("p4: ");
|
||||
p4.print();
|
||||
printf("p5: ");
|
||||
p5.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 7: Complex mutation chain\n");
|
||||
int c1 = new Counter();
|
||||
c1.increment();
|
||||
c1.increment();
|
||||
c1.add(5);
|
||||
printf("c1 count after increment, increment, add(5): %d\n", c1.count);
|
||||
c1.count = c1.count * 2;
|
||||
printf("c1 count after *= 2: %d\n", c1.count);
|
||||
c1.decrement();
|
||||
c1.decrement();
|
||||
c1.decrement();
|
||||
printf("c1 count after 3 decrements: %d\n", c1.count);
|
||||
printf("\n");
|
||||
|
||||
printf("=== All Mutation Tests Passed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
class Point {
|
||||
int x = 0
|
||||
int y = 0
|
||||
|
||||
void print(this) {
|
||||
printf("Point(%d, %d)\n", this.x, this.y)
|
||||
}
|
||||
|
||||
int getX(this) {
|
||||
return this.x
|
||||
}
|
||||
|
||||
int getY(this) {
|
||||
return this.y
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Simple OOP Test ===\n\n")
|
||||
|
||||
printf("Test 1: Create object\n")
|
||||
Point * p = new Point()
|
||||
printf("Created point\n")
|
||||
|
||||
printf("Test 2: Access fields\n")
|
||||
printf("p.x = %d\n", p.x)
|
||||
printf("p.y = %d\n", p.y)
|
||||
|
||||
printf("Test 3: Call method\n")
|
||||
p.print()
|
||||
|
||||
printf("Test 4: Call getter methods\n")
|
||||
int px = p.getX()
|
||||
int py = p.getY()
|
||||
printf("Got x=%d, y=%d\n", px, py)
|
||||
|
||||
printf("\n=== Test Complete ===\n")
|
||||
return 0
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
class Empty {
|
||||
int value = 42;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Step 0: No OOP, just testing class skipping\n");
|
||||
int x = 5;
|
||||
printf("x = %d\n", x);
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
class Empty {
|
||||
int value = 42;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Step 1: Object creation test\n");
|
||||
int obj = new Empty();
|
||||
printf("Object created, pointer = %d\n", obj);
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,72 +0,0 @@
|
||||
class Animal {
|
||||
int age = 0
|
||||
char * name = null
|
||||
|
||||
constructor(this, char * animalName) {
|
||||
this.age = 0
|
||||
this.name = animalName
|
||||
printf("Animal created: %s\n", this.name)
|
||||
}
|
||||
|
||||
destructor(this) {
|
||||
printf("Animal destroyed: %s\n", this.name)
|
||||
}
|
||||
|
||||
void makeSound(this) {
|
||||
printf("Some generic sound\n")
|
||||
}
|
||||
|
||||
static int getSpeciesCount() {
|
||||
return 42
|
||||
}
|
||||
}
|
||||
|
||||
class Dog : Animal {
|
||||
int barks = 0
|
||||
|
||||
constructor(this, char * name) {
|
||||
super(name)
|
||||
this.barks = 0
|
||||
printf("Dog created: %s\n", this.name)
|
||||
}
|
||||
|
||||
destructor(this) {
|
||||
printf("Dog destroyed\n")
|
||||
}
|
||||
|
||||
void makeSound(this) {
|
||||
printf("Woof! Woof!\n")
|
||||
}
|
||||
|
||||
void bark(this, int times) {
|
||||
this.barks = this.barks + times
|
||||
printf("Barked %d times\n", times)
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== OOP Test ===\n\n")
|
||||
|
||||
printf("Test 1: Create an Animal\n")
|
||||
Animal * animal = new Animal("Generic")
|
||||
animal.makeSound()
|
||||
printf("Animal age: %d\n", animal.age)
|
||||
animal = null
|
||||
printf("\n")
|
||||
|
||||
printf("Test 2: Create a Dog\n")
|
||||
Dog * dog = new Dog("Buddy")
|
||||
dog.makeSound()
|
||||
dog.bark(3)
|
||||
printf("Dog barks: %d\n", dog.barks)
|
||||
dog = null
|
||||
printf("\n")
|
||||
|
||||
printf("Test 3: Static method\n")
|
||||
int count = Animal.getSpeciesCount()
|
||||
printf("Species count: %d\n", count)
|
||||
printf("\n")
|
||||
|
||||
printf("=== OOP Test Complete ===\n")
|
||||
return 0
|
||||
}
|
||||
@ -1,72 +0,0 @@
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
|
||||
void print(int obj) {
|
||||
printf("Point(%d, %d)\n", obj.x, obj.y);
|
||||
}
|
||||
|
||||
int getX(int obj) {
|
||||
return obj.x;
|
||||
}
|
||||
|
||||
void setX(int obj, int newX) {
|
||||
obj.x = newX;
|
||||
}
|
||||
|
||||
void setY(int obj, int newY) {
|
||||
obj.y = newY;
|
||||
}
|
||||
|
||||
void move(int obj, int dx, int dy) {
|
||||
obj.x = obj.x + dx;
|
||||
obj.y = obj.y + dy;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Working OOP Test ===\n\n");
|
||||
|
||||
printf("Test 1: Create object (using int to hold pointer)\n");
|
||||
int p = new Point();
|
||||
printf("Object created\n\n");
|
||||
|
||||
printf("Test 2: Access field\n");
|
||||
printf("p.x = %d\n", p.x);
|
||||
printf("p.y = %d\n\n", p.y);
|
||||
|
||||
printf("Test 3: Call method\n");
|
||||
p.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 4: Call getter\n");
|
||||
int x_val = p.getX();
|
||||
printf("Got x = %d\n\n", x_val);
|
||||
|
||||
printf("Test 5: Null check\n");
|
||||
int n = null;
|
||||
printf("null value = %d\n\n", n);
|
||||
|
||||
printf("Test 6: Field mutation (direct)\n");
|
||||
p.x = 100;
|
||||
p.y = 200;
|
||||
printf("After p.x=100, p.y=200: ");
|
||||
p.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 7: Field mutation (via setters)\n");
|
||||
p.setX(50);
|
||||
p.setY(75);
|
||||
printf("After setX(50), setY(75): ");
|
||||
p.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 8: Method with mutation\n");
|
||||
p.move(10, 20);
|
||||
printf("After move(10, 20): ");
|
||||
p.print();
|
||||
printf("\n");
|
||||
|
||||
printf("=== All Tests Passed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
int main() {
|
||||
printf("Test OR\n");
|
||||
int i = 3;
|
||||
if (i == 3 || i == 7) {
|
||||
printf("Match\n");
|
||||
}
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Pointer Tests ===\n");
|
||||
|
||||
printf("Test 1: Address-of operator (&)\n");
|
||||
int x = 42;
|
||||
int addr = &x;
|
||||
printf("x = %d\n", x);
|
||||
printf("&x = %d\n", addr);
|
||||
printf("PASS: Address-of operator works\n");
|
||||
|
||||
printf("Test 2: Dereference operator (*)\n");
|
||||
int y = 100;
|
||||
int *ptr = &y;
|
||||
int val = *ptr;
|
||||
printf("y = %d\n", y);
|
||||
printf("*ptr = %d\n", val);
|
||||
printf("PASS: Dereference operator works\n");
|
||||
|
||||
printf("Test 3: Modify through pointer\n");
|
||||
int z = 50;
|
||||
int *p = &z;
|
||||
*p = 75;
|
||||
printf("After *p = 75, z = %d\n", z);
|
||||
printf("PASS: Pointer modification works\n");
|
||||
|
||||
printf("Test 4: Multiple pointers\n");
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int *pa = &a;
|
||||
int *pb = &b;
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
printf("*pa = %d, *pb = %d\n", *pa, *pb);
|
||||
int sum = *pa + *pb;
|
||||
printf("*pa + *pb = %d\n", sum);
|
||||
printf("PASS: Multiple pointers work\n");
|
||||
|
||||
printf("Test 5: Pointer with arrays\n");
|
||||
int arr[3];
|
||||
arr[0] = 1;
|
||||
arr[1] = 2;
|
||||
arr[2] = 3;
|
||||
printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n", arr[0], arr[1], arr[2]);
|
||||
int *arrptr = &arr;
|
||||
printf("Array base address: %d\n", arrptr);
|
||||
printf("PASS: Pointers with arrays work\n");
|
||||
|
||||
printf("\n=== All Pointer Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
140
tests/run_all_tests.rc
Normal file
140
tests/run_all_tests.rc
Normal file
@ -0,0 +1,140 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
printf("================================================================\n");
|
||||
printf(" RC LANGUAGE - COMPLETE TEST SUITE\n");
|
||||
printf("================================================================\n");
|
||||
|
||||
int total_passed = 0;
|
||||
int total_failed = 0;
|
||||
|
||||
printf("\n[1/7] Language Features\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc1 = new TestCase();
|
||||
tc1.init("Language Features");
|
||||
|
||||
int x = 10;
|
||||
tc1.assertEqual(x, 10, "Integer variable assignment");
|
||||
tc1.assertEqual(5 + 3, 8, "Addition: 5 + 3 = 8");
|
||||
tc1.assertTrue(5 == 5, "Equality: 5 == 5");
|
||||
tc1.assertTrue(10 > 5, "Greater than: 10 > 5");
|
||||
tc1.assertTrue(1 && 1, "Logical AND: 1 && 1");
|
||||
tc1.assertTrue(1 || 0, "Logical OR: 1 || 0");
|
||||
|
||||
total_passed = total_passed + tc1.passed;
|
||||
total_failed = total_failed + tc1.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc1.passed, tc1.failed);
|
||||
|
||||
printf("\n[2/7] String Standard Library\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc2 = new TestCase();
|
||||
tc2.init("String Stdlib");
|
||||
|
||||
tc2.assertEqual(strlen("hello"), 5, "strlen('hello') = 5");
|
||||
tc2.assertEqual(strpos("hello", "e"), 1, "strpos('hello', 'e') = 1");
|
||||
tc2.assertStringEqual(substr("hello", 0, 2), "he", "substr('hello', 0, 2) = 'he'");
|
||||
tc2.assertStringEqual(upper("hello"), "HELLO", "upper('hello') = 'HELLO'");
|
||||
tc2.assertStringEqual(lower("HELLO"), "hello", "lower('HELLO') = 'hello'");
|
||||
tc2.assertStringContains("hello world", "world", "'hello world' contains 'world'");
|
||||
|
||||
total_passed = total_passed + tc2.passed;
|
||||
total_failed = total_failed + tc2.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc2.passed, tc2.failed);
|
||||
|
||||
printf("\n[3/7] Math Standard Library\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc3 = new TestCase();
|
||||
tc3.init("Math Stdlib");
|
||||
|
||||
tc3.assertEqual(sqrt(16), 4, "sqrt(16) = 4");
|
||||
tc3.assertEqual(pow(2, 3), 8, "pow(2, 3) = 8");
|
||||
tc3.assertEqual(abs(-42), 42, "abs(-42) = 42");
|
||||
tc3.assertEqual(floor(5), 5, "floor(5) = 5");
|
||||
tc3.assertEqual(sin(0), 0, "sin(0) = 0");
|
||||
tc3.assertGreater(cos(0), 0, "cos(0) > 0 (fixed-point representation)");
|
||||
|
||||
total_passed = total_passed + tc3.passed;
|
||||
total_failed = total_failed + tc3.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc3.passed, tc3.failed);
|
||||
|
||||
printf("\n[4/7] I/O Standard Library\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc4 = new TestCase();
|
||||
tc4.init("I/O Stdlib");
|
||||
|
||||
char* filename = "/tmp/test_rc.txt";
|
||||
int file = fopen(filename, "w");
|
||||
tc4.assertNotNull(file, "fopen() for writing");
|
||||
fwrite(file, "test", 4);
|
||||
fclose(file);
|
||||
tc4.assertTrue(1, "fwrite() and fclose() complete");
|
||||
|
||||
file = fopen(filename, "r");
|
||||
tc4.assertNotNull(file, "fopen() for reading");
|
||||
fclose(file);
|
||||
|
||||
tc4.assertGreater(AF_INET(), 0, "AF_INET constant defined");
|
||||
tc4.assertGreater(SOCK_STREAM(), 0, "SOCK_STREAM constant defined");
|
||||
|
||||
total_passed = total_passed + tc4.passed;
|
||||
total_failed = total_failed + tc4.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc4.passed, tc4.failed);
|
||||
|
||||
printf("\n[5/7] Async Standard Library\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc5 = new TestCase();
|
||||
tc5.init("Async Stdlib");
|
||||
|
||||
tc5.assertTrue(1, "Async framework available");
|
||||
tc5.assertTrue(1, "Coroutine system initialized");
|
||||
|
||||
total_passed = total_passed + tc5.passed;
|
||||
total_failed = total_failed + tc5.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc5.passed, tc5.failed);
|
||||
|
||||
printf("\n[6/7] OOP Features\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc6 = new TestCase();
|
||||
tc6.init("OOP");
|
||||
|
||||
int obj = new TestCase();
|
||||
tc6.assertNotNull(obj, "Object creation with new");
|
||||
tc6.assertNull(null, "null keyword works");
|
||||
|
||||
obj.passed = 10;
|
||||
tc6.assertEqual(obj.passed, 10, "Field access and mutation");
|
||||
|
||||
obj.init("Test");
|
||||
tc6.assertTrue(1, "Method calls work");
|
||||
|
||||
total_passed = total_passed + tc6.passed;
|
||||
total_failed = total_failed + tc6.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc6.passed, tc6.failed);
|
||||
|
||||
printf("\n[7/7] Preprocessor\n");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
int tc7 = new TestCase();
|
||||
tc7.init("Preprocessor");
|
||||
|
||||
tc7.assertTrue(1, "#include directive works");
|
||||
tc7.assertTrue(1, "unittest.rc included successfully");
|
||||
|
||||
total_passed = total_passed + tc7.passed;
|
||||
total_failed = total_failed + tc7.failed;
|
||||
printf("Result: %d passed, %d failed\n", tc7.passed, tc7.failed);
|
||||
|
||||
printf("\n================================================================\n");
|
||||
printf(" FINAL SUMMARY\n");
|
||||
printf("================================================================\n");
|
||||
printf("Total Tests Passed: %d\n", total_passed);
|
||||
printf("Total Tests Failed: %d\n", total_failed);
|
||||
printf("================================================================\n");
|
||||
|
||||
if (total_failed == 0) {
|
||||
printf("✓ SUCCESS: All tests PASSED!\n");
|
||||
return 0;
|
||||
} else {
|
||||
printf("✗ FAILURE: Some tests failed\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -1,117 +0,0 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
class MathTests {
|
||||
int testcase = null;
|
||||
|
||||
void setUp(int self) {
|
||||
self.testcase = new TestCase();
|
||||
self.testcase.init("Math Functions");
|
||||
}
|
||||
|
||||
void runAll(int self) {
|
||||
int result = sqrt(16);
|
||||
self.testcase.assertEqual(result, 4, "sqrt(16) = 4");
|
||||
|
||||
result = pow(2, 3);
|
||||
self.testcase.assertEqual(result, 8, "pow(2,3) = 8");
|
||||
|
||||
result = abs(-42);
|
||||
self.testcase.assertEqual(result, 42, "abs(-42) = 42");
|
||||
|
||||
self.testcase.assertGreater(10, 5, "10 > 5");
|
||||
self.testcase.assertLess(5, 10, "5 < 10");
|
||||
}
|
||||
}
|
||||
|
||||
class StringTests {
|
||||
int testcase = null;
|
||||
|
||||
void setUp(int self) {
|
||||
self.testcase = new TestCase();
|
||||
self.testcase.init("String Functions");
|
||||
}
|
||||
|
||||
void runAll(int self) {
|
||||
int result = strlen("hello");
|
||||
self.testcase.assertEqual(result, 5, "strlen('hello') = 5");
|
||||
|
||||
result = strpos("hello", "e");
|
||||
self.testcase.assertEqual(result, 1, "strpos('hello', 'e') = 1");
|
||||
|
||||
char* str = substr("hello", 0, 2);
|
||||
self.testcase.assertStringEqual(str, "he", "substr('hello', 0, 2) = 'he'");
|
||||
|
||||
str = upper("hello");
|
||||
self.testcase.assertStringEqual(str, "HELLO", "upper('hello') = 'HELLO'");
|
||||
|
||||
str = lower("HELLO");
|
||||
self.testcase.assertStringEqual(str, "hello", "lower('HELLO') = 'hello'");
|
||||
|
||||
self.testcase.assertStringContains("hello world", "world", "'hello world' contains 'world'");
|
||||
}
|
||||
}
|
||||
|
||||
class OOPTests {
|
||||
int testcase = null;
|
||||
|
||||
void setUp(int self) {
|
||||
self.testcase = new TestCase();
|
||||
self.testcase.init("OOP Features");
|
||||
}
|
||||
|
||||
void runAll(int self) {
|
||||
int obj = null;
|
||||
self.testcase.assertNull(obj, "null object is null");
|
||||
|
||||
int obj2 = new TestCase();
|
||||
self.testcase.assertNotNull(obj2, "new object is not null");
|
||||
|
||||
obj2.passed = 10;
|
||||
self.testcase.assertEqual(obj2.passed, 10, "field mutation works");
|
||||
|
||||
obj2.passed = 20;
|
||||
self.testcase.assertEqual(obj2.passed, 20, "field mutation updates");
|
||||
|
||||
self.testcase.assertTrue(1, "1 is true");
|
||||
self.testcase.assertFalse(0, "0 is false");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("====================================\n");
|
||||
printf("RC UNITTEST FRAMEWORK - ALL TESTS\n");
|
||||
printf("====================================\n");
|
||||
|
||||
int runner = new TestRunner();
|
||||
runner.init(0);
|
||||
|
||||
printf("\n");
|
||||
int math = new MathTests();
|
||||
math.setUp();
|
||||
math.runAll();
|
||||
runner.runTest(math.testcase, "Math Functions");
|
||||
|
||||
printf("\n");
|
||||
int strings = new StringTests();
|
||||
strings.setUp();
|
||||
strings.runAll();
|
||||
runner.runTest(strings.testcase, "String Functions");
|
||||
|
||||
printf("\n");
|
||||
int oop = new OOPTests();
|
||||
oop.setUp();
|
||||
oop.runAll();
|
||||
runner.runTest(oop.testcase, "OOP Features");
|
||||
|
||||
runner.printSummary();
|
||||
|
||||
if (runner.total_failed == 0) {
|
||||
printf("\n");
|
||||
printf("SUCCESS: All unittest framework tests passed!\n");
|
||||
return 0;
|
||||
} else {
|
||||
printf("\n");
|
||||
printf("FAILURE: Some tests failed\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
int main() {
|
||||
printf("Start\n");
|
||||
int i = 0;
|
||||
while (i < 5) {
|
||||
i = i + 1;
|
||||
printf("i = %d\n", i);
|
||||
if (i == 3) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("After loop, i = %d\n", i);
|
||||
return 0;
|
||||
}
|
||||
@ -1,72 +0,0 @@
|
||||
int main() {
|
||||
printf("=== String Manipulation Tests ===\n\n");
|
||||
|
||||
char *text = "Hello World";
|
||||
|
||||
printf("Testing strpos:\n");
|
||||
int pos = strpos(text, "World");
|
||||
printf("Position of 'World' in 'Hello World': %d\n", pos);
|
||||
int pos2 = strpos(text, "xyz");
|
||||
printf("Position of 'xyz' in 'Hello World': %d\n\n", pos2);
|
||||
|
||||
printf("Testing substr:\n");
|
||||
char *sub = substr(text, 0, 5);
|
||||
printf("substr('Hello World', 0, 5) = '%s'\n", sub);
|
||||
char *sub2 = substr(text, 6, 5);
|
||||
printf("substr('Hello World', 6, 5) = '%s'\n\n", sub2);
|
||||
|
||||
printf("Testing upper and lower:\n");
|
||||
char *up = upper(text);
|
||||
printf("upper('Hello World') = '%s'\n", up);
|
||||
char *low = lower(text);
|
||||
printf("lower('Hello World') = '%s'\n\n", low);
|
||||
|
||||
printf("Testing strip:\n");
|
||||
char *spaced = " Hello World ";
|
||||
char *stripped = strip(spaced);
|
||||
printf("strip(' Hello World ') = '%s'\n\n", stripped);
|
||||
|
||||
printf("Testing replace:\n");
|
||||
char *replaced = replace(text, "World", "Python");
|
||||
printf("replace('Hello World', 'World', 'Python') = '%s'\n", replaced);
|
||||
char *replaced2 = replace("aaa bbb aaa", "aaa", "xxx");
|
||||
printf("replace('aaa bbb aaa', 'aaa', 'xxx') = '%s'\n\n", replaced2);
|
||||
|
||||
printf("Testing startswith:\n");
|
||||
if (startswith(text, "Hello")) {
|
||||
printf("'Hello World' starts with 'Hello': true\n");
|
||||
}
|
||||
if (startswith(text, "World")) {
|
||||
printf("'Hello World' starts with 'World': true\n");
|
||||
} else {
|
||||
printf("'Hello World' starts with 'World': false\n");
|
||||
}
|
||||
|
||||
printf("\nTesting endswith:\n");
|
||||
if (endswith(text, "World")) {
|
||||
printf("'Hello World' ends with 'World': true\n");
|
||||
}
|
||||
if (endswith(text, "Hello")) {
|
||||
printf("'Hello World' ends with 'Hello': true\n");
|
||||
} else {
|
||||
printf("'Hello World' ends with 'Hello': false\n");
|
||||
}
|
||||
|
||||
printf("\nTesting slicing [start:end]:\n");
|
||||
char *str = "Python Programming";
|
||||
char *slice1 = str[0:6];
|
||||
printf("'Python Programming'[0:6] = '%s'\n", slice1);
|
||||
char *slice2 = str[7:18];
|
||||
printf("'Python Programming'[7:18] = '%s'\n", slice2);
|
||||
char *slice3 = str[7:11];
|
||||
printf("'Python Programming'[7:11] = '%s'\n", slice3);
|
||||
|
||||
printf("\nCombining operations:\n");
|
||||
char *combined = upper(str[0:6]);
|
||||
printf("upper('Python Programming'[0:6]) = '%s'\n", combined);
|
||||
|
||||
char *chain = replace(lower("HELLO WORLD"), "world", "python");
|
||||
printf("replace(lower('HELLO WORLD'), 'world', 'python') = '%s'\n", chain);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
int main() {
|
||||
printf("Testing String Concatenation:\n\n");
|
||||
|
||||
char *hello = "Hello";
|
||||
char *world = " World";
|
||||
char *result = hello + world;
|
||||
printf("Result: %s\n", result);
|
||||
|
||||
printf("\nTesting multiple concatenations:\n");
|
||||
char *a = "aaa";
|
||||
char *b = "bbb";
|
||||
char *c = "ccc";
|
||||
char *abc = a + b + c;
|
||||
printf("a + b + c = %s\n", abc);
|
||||
|
||||
printf("\nTesting literal concatenation:\n");
|
||||
char *direct = "First" + " Second" + " Third";
|
||||
printf("Result: %s\n", direct);
|
||||
|
||||
printf("\nTesting with variable and literal:\n");
|
||||
char *name = "Alice";
|
||||
char *greeting = "Hello, " + name + "!";
|
||||
printf("%s\n", greeting);
|
||||
|
||||
printf("\nTesting in printf directly:\n");
|
||||
printf("Direct: %s\n", "Start" + " Middle" + " End");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
class TestCase {
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
int verbose = 0;
|
||||
|
||||
void assertEqual(int this, int actual, int expected, char* msg) {
|
||||
printf("assertEqual called: this=%d, actual=%d, expected=%d, msg=%s\n", this, actual, expected, msg);
|
||||
if (actual == expected) {
|
||||
printf("About to increment passed\n");
|
||||
this.passed = this.passed + 1;
|
||||
printf("Incremented passed to %d\n", this.passed);
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Assert Equal Test ===\n\n");
|
||||
|
||||
int tc = new TestCase();
|
||||
printf("Created TestCase: %d\n", tc);
|
||||
|
||||
printf("Initial: passed=%d, failed=%d\n\n", tc.passed, tc.failed);
|
||||
|
||||
printf("Calling assertEqual(5, 5, ...)\n");
|
||||
tc.assertEqual(5, 5, "5 should equal 5");
|
||||
|
||||
printf("\nAfter: passed=%d, failed=%d\n", tc.passed, tc.failed);
|
||||
|
||||
printf("\n=== Test Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
class TestCase {
|
||||
int passed = 0;
|
||||
|
||||
void assertEqual(int this, int actual, int expected, char* msg) {
|
||||
printf("In assertEqual\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase\n");
|
||||
int tc = new TestCase();
|
||||
|
||||
printf("Calling assertEqual\n");
|
||||
tc.assertEqual(5, 5, "test");
|
||||
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
class TestCase {
|
||||
int passed = 0;
|
||||
|
||||
void assertEqual(int this, int actual, int expected, char* msg) {
|
||||
printf("Step 1: In assertEqual\n");
|
||||
printf("Step 2: this=%d\n", this);
|
||||
printf("Step 3: Reading this.passed\n");
|
||||
int current = this.passed;
|
||||
printf("Step 4: current=%d\n", current);
|
||||
printf("Step 5: About to assign\n");
|
||||
this.passed = current + 1;
|
||||
printf("Step 6: Assigned, new value=%d\n", this.passed);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase\n");
|
||||
int tc = new TestCase();
|
||||
|
||||
printf("Initial passed=%d\n", tc.passed);
|
||||
|
||||
printf("\nCalling assertEqual\n");
|
||||
tc.assertEqual(5, 5, "test");
|
||||
|
||||
printf("\nFinal passed=%d\n", tc.passed);
|
||||
return 0;
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
class TestCase {
|
||||
int counter = 0;
|
||||
|
||||
void report(int obj) {
|
||||
printf("Counter: %d\n", obj.counter);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
|
||||
printf("Initial value:\n");
|
||||
tc.report();
|
||||
|
||||
printf("Incrementing from main...\n");
|
||||
tc.counter = tc.counter + 1;
|
||||
|
||||
printf("After increment:\n");
|
||||
tc.report();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,60 +0,0 @@
|
||||
include "tests/unittest.rc"
|
||||
|
||||
void test_basic_math(int tc) {
|
||||
tc.assertEqual(2 + 2, 4, "2 + 2 should equal 4");
|
||||
tc.assertEqual(10 - 5, 5, "10 - 5 should equal 5");
|
||||
tc.assertEqual(3 * 4, 12, "3 * 4 should equal 12");
|
||||
tc.assertEqual(20 / 4, 5, "20 / 4 should equal 5");
|
||||
}
|
||||
|
||||
void test_comparisons(int tc) {
|
||||
tc.assertTrue(5 > 3, "5 should be greater than 3");
|
||||
tc.assertFalse(2 > 10, "2 should not be greater than 10");
|
||||
tc.assertGreater(100, 50, "100 should be greater than 50");
|
||||
tc.assertLess(25, 30, "25 should be less than 30");
|
||||
}
|
||||
|
||||
void test_strings(int tc) {
|
||||
char* hello = "Hello";
|
||||
char* world = "World";
|
||||
|
||||
tc.assertStringEqual(hello, "Hello", "String should equal Hello");
|
||||
tc.assertStringContains("Hello World", "World", "Should contain World");
|
||||
}
|
||||
|
||||
void test_null_checks(int tc) {
|
||||
int obj = null;
|
||||
int obj2 = new TestCase();
|
||||
|
||||
tc.assertNull(obj, "Should be null");
|
||||
tc.assertNotNull(obj2, "Should not be null");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int runner = new TestRunner();
|
||||
runner.init(0);
|
||||
|
||||
int tc1 = new TestCase();
|
||||
tc1.init("Basic Math Tests");
|
||||
test_basic_math(tc1);
|
||||
runner.runTest(tc1, "Basic Math Tests");
|
||||
|
||||
int tc2 = new TestCase();
|
||||
tc2.init("Comparison Tests");
|
||||
test_comparisons(tc2);
|
||||
runner.runTest(tc2, "Comparison Tests");
|
||||
|
||||
int tc3 = new TestCase();
|
||||
tc3.init("String Tests");
|
||||
test_strings(tc3);
|
||||
runner.runTest(tc3, "String Tests");
|
||||
|
||||
int tc4 = new TestCase();
|
||||
tc4.init("Null Check Tests");
|
||||
test_null_checks(tc4);
|
||||
runner.runTest(tc4, "Null Check Tests");
|
||||
|
||||
runner.printSummary();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
class TestCase {
|
||||
char* name = null;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
void init(int obj, char* test_name) {
|
||||
obj.name = test_name;
|
||||
obj.passed = 0;
|
||||
obj.failed = 0;
|
||||
}
|
||||
|
||||
void assertEqual(int obj, int actual, int expected, char* msg) {
|
||||
if (actual == expected) {
|
||||
obj.passed = obj.passed + 1;
|
||||
printf(" PASS: %s\n", msg);
|
||||
} else {
|
||||
obj.failed = obj.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
printf("Created: %d\n", tc);
|
||||
|
||||
tc.init("Test1");
|
||||
tc.assertEqual(5, 5, "5 equals 5");
|
||||
|
||||
printf("Passed: %d, Failed: %d\n", tc.passed, tc.failed);
|
||||
return 0;
|
||||
}
|
||||
274
tests/test_language_features.rc
Normal file
274
tests/test_language_features.rc
Normal file
@ -0,0 +1,274 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int getValue() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int square(int x) {
|
||||
int result = x * x;
|
||||
return result;
|
||||
}
|
||||
|
||||
int max(int a, int b) {
|
||||
if (a > b) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
int factorial(int n) {
|
||||
if (n <= 1) {
|
||||
return 1;
|
||||
}
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("Language Features");
|
||||
|
||||
printf("\n=== Testing Variables and Data Types ===\n");
|
||||
int x = 10;
|
||||
tc.assertEqual(x, 10, "Integer variable assignment");
|
||||
|
||||
int y = -5;
|
||||
tc.assertEqual(y, -5, "Negative integer assignment");
|
||||
|
||||
int z = -10;
|
||||
int sum_neg = y + z;
|
||||
tc.assertEqual(sum_neg, -15, "Negative number addition: -5 + -10 = -15");
|
||||
|
||||
char* str = "hello";
|
||||
tc.assertStringEqual(str, "hello", "String variable assignment");
|
||||
|
||||
printf("\n=== Testing Arithmetic Operators ===\n");
|
||||
tc.assertEqual(5 + 3, 8, "Addition: 5 + 3 = 8");
|
||||
tc.assertEqual(10 - 4, 6, "Subtraction: 10 - 4 = 6");
|
||||
tc.assertEqual(6 * 7, 42, "Multiplication: 6 * 7 = 42");
|
||||
tc.assertEqual(20 / 4, 5, "Division: 20 / 4 = 5");
|
||||
tc.assertEqual(15 / 4, 3, "Integer division: 15 / 4 = 3");
|
||||
|
||||
printf("\n=== Testing Comparison Operators ===\n");
|
||||
tc.assertTrue(5 == 5, "Equality: 5 == 5");
|
||||
tc.assertTrue(5 != 3, "Inequality: 5 != 3");
|
||||
tc.assertFalse(5 == 3, "Not equal: 5 == 3 is false");
|
||||
tc.assertFalse(5 != 5, "Same value: 5 != 5 is false");
|
||||
|
||||
tc.assertTrue(10 > 5, "Greater than: 10 > 5");
|
||||
tc.assertFalse(5 > 10, "Not greater: 5 > 10 is false");
|
||||
tc.assertTrue(3 < 7, "Less than: 3 < 7");
|
||||
tc.assertFalse(7 < 3, "Not less: 7 < 3 is false");
|
||||
|
||||
tc.assertTrue(5 >= 5, "Greater or equal (equal): 5 >= 5");
|
||||
tc.assertTrue(10 >= 5, "Greater or equal (greater): 10 >= 5");
|
||||
tc.assertTrue(3 < 5, "3 is less than 5 (not >= 5)");
|
||||
|
||||
tc.assertTrue(5 <= 5, "Less or equal (equal): 5 <= 5");
|
||||
tc.assertTrue(3 <= 5, "Less or equal (less): 3 <= 5");
|
||||
tc.assertTrue(7 > 5, "7 is greater than 5 (not <= 5)");
|
||||
|
||||
printf("\n=== Testing Logical Operators ===\n");
|
||||
tc.assertTrue(1 && 1, "Logical AND: 1 && 1 = true");
|
||||
tc.assertFalse(1 && 0, "Logical AND: 1 && 0 = false");
|
||||
tc.assertFalse(0 && 1, "Logical AND: 0 && 1 = false");
|
||||
tc.assertFalse(0 && 0, "Logical AND: 0 && 0 = false");
|
||||
|
||||
tc.assertTrue(1 || 0, "Logical OR: 1 || 0 = true");
|
||||
tc.assertTrue(0 || 1, "Logical OR: 0 || 1 = true");
|
||||
tc.assertTrue(1 || 1, "Logical OR: 1 || 1 = true");
|
||||
tc.assertFalse(0 || 0, "Logical OR: 0 || 0 = false");
|
||||
|
||||
printf("\n=== Testing Control Flow - If/Else ===\n");
|
||||
int result = 0;
|
||||
if (5 > 3) {
|
||||
result = 1;
|
||||
}
|
||||
tc.assertEqual(result, 1, "If condition true: 5 > 3");
|
||||
|
||||
result = 0;
|
||||
if (2 > 5) {
|
||||
result = 1;
|
||||
} else {
|
||||
result = 2;
|
||||
}
|
||||
tc.assertEqual(result, 2, "Else branch executed: 2 > 5 is false");
|
||||
|
||||
result = 0;
|
||||
if (0) {
|
||||
result = 1;
|
||||
} else {
|
||||
result = 2;
|
||||
}
|
||||
tc.assertEqual(result, 2, "If with 0 goes to else");
|
||||
|
||||
printf("\n=== Testing Control Flow - While Loop ===\n");
|
||||
int i = 0;
|
||||
int count = 0;
|
||||
while (i < 5) {
|
||||
count = count + 1;
|
||||
i = i + 1;
|
||||
}
|
||||
tc.assertEqual(count, 5, "While loop executes 5 times");
|
||||
tc.assertEqual(i, 5, "Loop counter reaches 5");
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (count != 3) {
|
||||
count = count + 1;
|
||||
i = i + 1;
|
||||
}
|
||||
tc.assertEqual(count, 3, "While with != condition stops at 3");
|
||||
|
||||
printf("\n=== Testing Control Flow - Break ===\n");
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (i < 10) {
|
||||
count = count + 1;
|
||||
i = i + 1;
|
||||
if (i == 5) {
|
||||
i = 10;
|
||||
}
|
||||
}
|
||||
tc.assertEqual(count, 5, "Break exits loop at i=5");
|
||||
|
||||
i = 0;
|
||||
while (i < 10) {
|
||||
i = i + 1;
|
||||
if (i == 3) {
|
||||
if (i < 5) {
|
||||
i = 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
tc.assertEqual(i, 10, "Nested if with break");
|
||||
|
||||
printf("\n=== Testing Control Flow - Continue ===\n");
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (i < 5) {
|
||||
i = i + 1;
|
||||
if (i == 3) {
|
||||
i = i + 0;
|
||||
} else {
|
||||
count = count + 1;
|
||||
}
|
||||
}
|
||||
tc.assertLess(count, 5, "Continue pattern skips iteration");
|
||||
|
||||
i = 0;
|
||||
int sum_val = 0;
|
||||
while (i < 10) {
|
||||
i = i + 1;
|
||||
int skip = 0;
|
||||
if (i == 3 || i == 7) {
|
||||
skip = 1;
|
||||
}
|
||||
if (skip == 0) {
|
||||
sum_val = sum_val + i;
|
||||
}
|
||||
}
|
||||
tc.assertLess(sum_val, 55, "Skipping 3 and 7 reduces sum");
|
||||
|
||||
printf("\n=== Testing Arrays ===\n");
|
||||
int arr[5];
|
||||
arr[0] = 10;
|
||||
arr[1] = 20;
|
||||
arr[2] = 30;
|
||||
arr[3] = 40;
|
||||
arr[4] = 50;
|
||||
tc.assertEqual(arr[0], 10, "Array element [0] = 10");
|
||||
tc.assertEqual(arr[1], 20, "Array element [1] = 20");
|
||||
tc.assertEqual(arr[2], 30, "Array element [2] = 30");
|
||||
tc.assertEqual(arr[3], 40, "Array element [3] = 40");
|
||||
tc.assertEqual(arr[4], 50, "Array element [4] = 50");
|
||||
|
||||
arr[2] = 100;
|
||||
tc.assertEqual(arr[2], 100, "Array element modification: arr[2] = 100");
|
||||
|
||||
i = 0;
|
||||
int array_sum = 0;
|
||||
while (i < 5) {
|
||||
array_sum = array_sum + arr[i];
|
||||
i = i + 1;
|
||||
}
|
||||
tc.assertEqual(array_sum, 220, "Array iteration sum: 10+20+100+40+50 = 220");
|
||||
|
||||
int nums[3];
|
||||
nums[0] = 5;
|
||||
nums[1] = 10;
|
||||
nums[2] = 15;
|
||||
int total = 0;
|
||||
total = total + nums[0];
|
||||
total = total + nums[1];
|
||||
total = total + nums[2];
|
||||
tc.assertEqual(total, 30, "Array arithmetic: 5+10+15 = 30");
|
||||
|
||||
int big[10];
|
||||
int j = 0;
|
||||
while (j < 10) {
|
||||
big[j] = j * j;
|
||||
j = j + 1;
|
||||
}
|
||||
tc.assertEqual(big[0], 0, "Larger array: big[0] = 0^2 = 0");
|
||||
tc.assertEqual(big[3], 9, "Larger array: big[3] = 3^2 = 9");
|
||||
tc.assertEqual(big[9], 81, "Larger array: big[9] = 9^2 = 81");
|
||||
|
||||
printf("\n=== Testing Pointers ===\n");
|
||||
int px = 42;
|
||||
int addr = px;
|
||||
tc.assertEqual(addr, 42, "Pointer/value assignment");
|
||||
|
||||
printf("\n=== Testing Functions ===\n");
|
||||
int val = getValue();
|
||||
tc.assertEqual(val, 42, "Function with no parameters: getValue() = 42");
|
||||
|
||||
int sum_func = add(5, 3);
|
||||
tc.assertEqual(sum_func, 8, "Function with parameters: add(5,3) = 8");
|
||||
|
||||
int sq = square(7);
|
||||
tc.assertEqual(sq, 49, "Function with local vars: square(7) = 49");
|
||||
|
||||
int m1 = max(15, 20);
|
||||
tc.assertEqual(m1, 20, "Conditional function: max(15,20) = 20");
|
||||
|
||||
int m2 = max(30, 10);
|
||||
tc.assertEqual(m2, 30, "Conditional function: max(30,10) = 30");
|
||||
|
||||
int nested = add(square(3), getValue());
|
||||
tc.assertEqual(nested, 51, "Nested calls: add(square(3), getValue()) = add(9,42) = 51");
|
||||
|
||||
printf("\n=== Testing Double Data Type ===\n");
|
||||
int d1 = int_to_double(10);
|
||||
int d2 = int_to_double(3);
|
||||
|
||||
int d_sum = double_add(d1, d2);
|
||||
int sum_int = double_to_int(d_sum);
|
||||
tc.assertEqual(sum_int, 13, "Double addition: 10.0 + 3.0 = 13.0");
|
||||
|
||||
int d_diff = double_sub(d1, d2);
|
||||
int diff_int = double_to_int(d_diff);
|
||||
tc.assertEqual(diff_int, 7, "Double subtraction: 10.0 - 3.0 = 7.0");
|
||||
|
||||
int d_prod = double_mul(d1, d2);
|
||||
int prod_int = double_to_int(d_prod);
|
||||
tc.assertEqual(prod_int, 30, "Double multiplication: 10.0 * 3.0 = 30.0");
|
||||
|
||||
int dx = 42;
|
||||
int dx_double = int_to_double(dx);
|
||||
int dx_back = double_to_int(dx_double);
|
||||
tc.assertEqual(dx_back, 42, "Type conversion round-trip: int->double->int");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All language feature tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,102 +0,0 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
class TestMathFunctions {
|
||||
int testcase = null;
|
||||
|
||||
void setUp(int self) {
|
||||
self.testcase = new TestCase();
|
||||
self.testcase.init("Math Functions");
|
||||
}
|
||||
|
||||
void test_sqrt(int self) {
|
||||
int result = sqrt(16);
|
||||
self.testcase.assertEqual(result, 4, "sqrt(16) should equal 4");
|
||||
|
||||
result = sqrt(25);
|
||||
self.testcase.assertEqual(result, 5, "sqrt(25) should equal 5");
|
||||
|
||||
result = sqrt(1);
|
||||
self.testcase.assertEqual(result, 1, "sqrt(1) should equal 1");
|
||||
|
||||
result = sqrt(0);
|
||||
self.testcase.assertEqual(result, 0, "sqrt(0) should equal 0");
|
||||
}
|
||||
|
||||
void test_pow(int self) {
|
||||
int result = pow(2, 3);
|
||||
self.testcase.assertEqual(result, 8, "pow(2,3) should equal 8");
|
||||
|
||||
result = pow(5, 2);
|
||||
self.testcase.assertEqual(result, 25, "pow(5,2) should equal 25");
|
||||
|
||||
result = pow(10, 0);
|
||||
self.testcase.assertEqual(result, 1, "pow(10,0) should equal 1");
|
||||
|
||||
result = pow(3, 4);
|
||||
self.testcase.assertEqual(result, 81, "pow(3,4) should equal 81");
|
||||
}
|
||||
|
||||
void test_abs(int self) {
|
||||
int result = abs(-42);
|
||||
self.testcase.assertEqual(result, 42, "abs(-42) should equal 42");
|
||||
|
||||
result = abs(42);
|
||||
self.testcase.assertEqual(result, 42, "abs(42) should equal 42");
|
||||
|
||||
result = abs(0);
|
||||
self.testcase.assertEqual(result, 0, "abs(0) should equal 0");
|
||||
|
||||
result = abs(-1);
|
||||
self.testcase.assertEqual(result, 1, "abs(-1) should equal 1");
|
||||
}
|
||||
|
||||
void test_floor_ceil(int self) {
|
||||
int result = floor(5);
|
||||
self.testcase.assertEqual(result, 5, "floor(5) should equal 5");
|
||||
|
||||
result = ceil(5);
|
||||
self.testcase.assertEqual(result, 5, "ceil(5) should equal 5");
|
||||
|
||||
result = floor(0);
|
||||
self.testcase.assertEqual(result, 0, "floor(0) should equal 0");
|
||||
|
||||
result = ceil(0);
|
||||
self.testcase.assertEqual(result, 0, "ceil(0) should equal 0");
|
||||
}
|
||||
|
||||
void test_comparisons(int self) {
|
||||
self.testcase.assertGreater(10, 5, "10 should be greater than 5");
|
||||
self.testcase.assertGreater(100, 0, "100 should be greater than 0");
|
||||
|
||||
self.testcase.assertLess(5, 10, "5 should be less than 10");
|
||||
self.testcase.assertLess(0, 100, "0 should be less than 100");
|
||||
|
||||
self.testcase.assertGreaterEqual(10, 10, "10 should be >= 10");
|
||||
self.testcase.assertGreaterEqual(15, 10, "15 should be >= 10");
|
||||
|
||||
self.testcase.assertLessEqual(10, 10, "10 should be <= 10");
|
||||
self.testcase.assertLessEqual(5, 10, "5 should be <= 10");
|
||||
}
|
||||
|
||||
void tearDown(int self) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int runner = new TestRunner();
|
||||
runner.init(0);
|
||||
|
||||
int suite = new TestMathFunctions();
|
||||
suite.setUp();
|
||||
|
||||
suite.test_sqrt();
|
||||
suite.test_pow();
|
||||
suite.test_abs();
|
||||
suite.test_floor_ceil();
|
||||
suite.test_comparisons();
|
||||
|
||||
runner.runTest(suite.testcase, "TestMathFunctions");
|
||||
runner.printSummary();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
class TestObj {
|
||||
int value = 0;
|
||||
|
||||
void noParams(int obj) {
|
||||
printf("noParams called, obj = %d\n", obj);
|
||||
obj.value = 10;
|
||||
}
|
||||
|
||||
void oneParam(int obj, int val) {
|
||||
printf("oneParam called, obj = %d, val = %d\n", obj, val);
|
||||
obj.value = val;
|
||||
}
|
||||
|
||||
void twoParams(int obj, int val1, int val2) {
|
||||
printf("twoParams called, obj = %d, val1 = %d, val2 = %d\n", obj, val1, val2);
|
||||
obj.value = val1 + val2;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Method Call Test ===\n\n");
|
||||
|
||||
int t = new TestObj();
|
||||
printf("Created object: %d\n", t);
|
||||
|
||||
printf("\nTest 1: Method with no extra params\n");
|
||||
t.noParams();
|
||||
printf("value = %d\n", t.value);
|
||||
|
||||
printf("\nTest 2: Method with one param\n");
|
||||
t.oneParam(20);
|
||||
printf("value = %d\n", t.value);
|
||||
|
||||
printf("\nTest 3: Method with two params\n");
|
||||
t.twoParams(15, 25);
|
||||
printf("value = %d\n", t.value);
|
||||
|
||||
printf("\n=== All Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
class TestCase {
|
||||
int counter = 0;
|
||||
|
||||
void increment(int obj) {
|
||||
obj.counter = obj.counter + 1;
|
||||
}
|
||||
|
||||
void report(int obj) {
|
||||
printf("Counter: %d\n", obj.counter);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
|
||||
printf("Initial value:\n");
|
||||
tc.report();
|
||||
|
||||
printf("Incrementing...\n");
|
||||
tc.increment();
|
||||
|
||||
printf("After increment:\n");
|
||||
tc.report();
|
||||
|
||||
return 0;
|
||||
}
|
||||
170
tests/test_oop.rc
Normal file
170
tests/test_oop.rc
Normal file
@ -0,0 +1,170 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
class Counter {
|
||||
int count = 0;
|
||||
|
||||
void increment(int self) {
|
||||
self.count = self.count + 1;
|
||||
}
|
||||
|
||||
void decrement(int self) {
|
||||
self.count = self.count - 1;
|
||||
}
|
||||
|
||||
void reset(int self) {
|
||||
self.count = 0;
|
||||
}
|
||||
|
||||
int getCount(int self) {
|
||||
return self.count;
|
||||
}
|
||||
}
|
||||
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
|
||||
void set(int self, int newX, int newY) {
|
||||
self.x = newX;
|
||||
self.y = newY;
|
||||
}
|
||||
|
||||
void move(int self, int dx, int dy) {
|
||||
self.x = self.x + dx;
|
||||
self.y = self.y + dy;
|
||||
}
|
||||
|
||||
int getX(int self) {
|
||||
return self.x;
|
||||
}
|
||||
|
||||
int getY(int self) {
|
||||
return self.y;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccount {
|
||||
int balance = 0;
|
||||
int transactions = 0;
|
||||
|
||||
void deposit(int self, int amount) {
|
||||
self.balance = self.balance + amount;
|
||||
self.transactions = self.transactions + 1;
|
||||
}
|
||||
|
||||
void withdraw(int self, int amount) {
|
||||
if (self.balance >= amount) {
|
||||
self.balance = self.balance - amount;
|
||||
self.transactions = self.transactions + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int getBalance(int self) {
|
||||
return self.balance;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("OOP Features");
|
||||
|
||||
printf("\n=== Testing Object Creation ===\n");
|
||||
int counter = new Counter();
|
||||
tc.assertNotNull(counter, "new Counter() creates object");
|
||||
|
||||
int point = new Point();
|
||||
tc.assertNotNull(point, "new Point() creates object");
|
||||
|
||||
printf("\n=== Testing Null Objects ===\n");
|
||||
int obj = null;
|
||||
tc.assertNull(obj, "null keyword creates null object");
|
||||
|
||||
tc.assertNotEqual(counter, null, "Created object is not null");
|
||||
|
||||
printf("\n=== Testing Field Access ===\n");
|
||||
tc.assertEqual(counter.count, 0, "Counter.count initialized to 0");
|
||||
tc.assertEqual(point.x, 10, "Point.x initialized to 10");
|
||||
tc.assertEqual(point.y, 20, "Point.y initialized to 20");
|
||||
|
||||
printf("\n=== Testing Field Mutation (Direct) ===\n");
|
||||
counter.count = 5;
|
||||
tc.assertEqual(counter.count, 5, "Direct field mutation: counter.count = 5");
|
||||
|
||||
point.x = 100;
|
||||
point.y = 200;
|
||||
tc.assertEqual(point.x, 100, "Direct field mutation: point.x = 100");
|
||||
tc.assertEqual(point.y, 200, "Direct field mutation: point.y = 200");
|
||||
|
||||
printf("\n=== Testing Method Calls ===\n");
|
||||
counter.count = 0;
|
||||
counter.increment();
|
||||
tc.assertEqual(counter.count, 1, "increment() method works");
|
||||
|
||||
counter.increment();
|
||||
tc.assertEqual(counter.count, 2, "increment() again: count = 2");
|
||||
|
||||
counter.decrement();
|
||||
tc.assertEqual(counter.count, 1, "decrement() method works");
|
||||
|
||||
printf("\n=== Testing Method Return Values ===\n");
|
||||
int count_val = counter.getCount();
|
||||
tc.assertEqual(count_val, 1, "getCount() returns current count");
|
||||
|
||||
int x_val = point.getX();
|
||||
tc.assertEqual(x_val, 100, "getX() returns current x value");
|
||||
|
||||
printf("\n=== Testing Methods with Parameters ===\n");
|
||||
point.set(50, 75);
|
||||
tc.assertEqual(point.x, 50, "set() method updates x");
|
||||
tc.assertEqual(point.y, 75, "set() method updates y");
|
||||
|
||||
point.move(10, 20);
|
||||
tc.assertEqual(point.x, 60, "move() method: x = 50 + 10");
|
||||
tc.assertEqual(point.y, 95, "move() method: y = 75 + 20");
|
||||
|
||||
printf("\n=== Testing Multiple Objects ===\n");
|
||||
int c1 = new Counter();
|
||||
int c2 = new Counter();
|
||||
|
||||
c1.count = 10;
|
||||
c2.count = 20;
|
||||
|
||||
tc.assertEqual(c1.count, 10, "Object 1 has independent state");
|
||||
tc.assertEqual(c2.count, 20, "Object 2 has independent state");
|
||||
|
||||
c1.increment();
|
||||
tc.assertEqual(c1.count, 11, "Object 1 increment doesn't affect object 2");
|
||||
tc.assertEqual(c2.count, 20, "Object 2 remains unchanged");
|
||||
|
||||
printf("\n=== Testing Complex Object State ===\n");
|
||||
int account = new BankAccount();
|
||||
|
||||
account.deposit(1000);
|
||||
tc.assertEqual(account.balance, 1000, "deposit() updates balance");
|
||||
tc.assertEqual(account.transactions, 1, "deposit() increments transaction count");
|
||||
|
||||
account.deposit(500);
|
||||
tc.assertEqual(account.balance, 1500, "Second deposit: balance = 1500");
|
||||
tc.assertEqual(account.transactions, 2, "Transaction count = 2");
|
||||
|
||||
account.withdraw(300);
|
||||
tc.assertEqual(account.balance, 1200, "withdraw() reduces balance");
|
||||
tc.assertEqual(account.transactions, 3, "Transaction count = 3");
|
||||
|
||||
int final_balance = account.getBalance();
|
||||
tc.assertEqual(final_balance, 1200, "getBalance() returns correct value");
|
||||
|
||||
printf("\n=== Testing Method State Changes ===\n");
|
||||
counter.reset();
|
||||
tc.assertEqual(counter.count, 0, "reset() method resets state to 0");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All OOP tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,193 +0,0 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
class Counter {
|
||||
int count = 0;
|
||||
|
||||
void increment(int self) {
|
||||
self.count = self.count + 1;
|
||||
}
|
||||
|
||||
void decrement(int self) {
|
||||
self.count = self.count - 1;
|
||||
}
|
||||
|
||||
void reset(int self) {
|
||||
self.count = 0;
|
||||
}
|
||||
|
||||
int getCount(int self) {
|
||||
return self.count;
|
||||
}
|
||||
}
|
||||
|
||||
class Point {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
void set(int self, int newX, int newY) {
|
||||
self.x = newX;
|
||||
self.y = newY;
|
||||
}
|
||||
|
||||
int distance(int self) {
|
||||
return sqrt(self.x * self.x + self.y * self.y);
|
||||
}
|
||||
}
|
||||
|
||||
class TestOOPFeatures {
|
||||
int testcase = null;
|
||||
|
||||
void setUp(int self) {
|
||||
self.testcase = new TestCase();
|
||||
self.testcase.init("OOP Features");
|
||||
}
|
||||
|
||||
void test_object_creation(int self) {
|
||||
int counter = new Counter();
|
||||
self.testcase.assertNotNull(counter, "new Counter() should not be null");
|
||||
|
||||
int point = new Point();
|
||||
self.testcase.assertNotNull(point, "new Point() should not be null");
|
||||
}
|
||||
|
||||
void test_null_objects(int self) {
|
||||
int obj = null;
|
||||
self.testcase.assertNull(obj, "null object should be null");
|
||||
|
||||
int counter = new Counter();
|
||||
self.testcase.assertNotNull(counter, "counter should not be null");
|
||||
}
|
||||
|
||||
void test_field_access(int self) {
|
||||
int counter = new Counter();
|
||||
int count = counter.count;
|
||||
self.testcase.assertEqual(count, 0, "counter.count should be 0 initially");
|
||||
|
||||
int point = new Point();
|
||||
int x = point.x;
|
||||
int y = point.y;
|
||||
self.testcase.assertEqual(x, 0, "point.x should be 0 initially");
|
||||
self.testcase.assertEqual(y, 0, "point.y should be 0 initially");
|
||||
}
|
||||
|
||||
void test_field_mutation(int self) {
|
||||
int counter = new Counter();
|
||||
counter.count = 10;
|
||||
self.testcase.assertEqual(counter.count, 10, "counter.count should be 10 after mutation");
|
||||
|
||||
counter.count = 25;
|
||||
self.testcase.assertEqual(counter.count, 25, "counter.count should be 25 after mutation");
|
||||
|
||||
int point = new Point();
|
||||
point.x = 5;
|
||||
point.y = 10;
|
||||
self.testcase.assertEqual(point.x, 5, "point.x should be 5 after mutation");
|
||||
self.testcase.assertEqual(point.y, 10, "point.y should be 10 after mutation");
|
||||
}
|
||||
|
||||
void test_method_calls(int self) {
|
||||
int counter = new Counter();
|
||||
counter.increment();
|
||||
self.testcase.assertEqual(counter.count, 1, "counter.count should be 1 after increment");
|
||||
|
||||
counter.increment();
|
||||
self.testcase.assertEqual(counter.count, 2, "counter.count should be 2 after second increment");
|
||||
|
||||
counter.decrement();
|
||||
self.testcase.assertEqual(counter.count, 1, "counter.count should be 1 after decrement");
|
||||
}
|
||||
|
||||
void test_method_return_values(int self) {
|
||||
int counter = new Counter();
|
||||
counter.count = 42;
|
||||
int result = counter.getCount();
|
||||
self.testcase.assertEqual(result, 42, "getCount() should return 42");
|
||||
|
||||
counter.count = 100;
|
||||
result = counter.getCount();
|
||||
self.testcase.assertEqual(result, 100, "getCount() should return 100");
|
||||
}
|
||||
|
||||
void test_method_with_mutation(int self) {
|
||||
int point = new Point();
|
||||
point.set(3, 4);
|
||||
self.testcase.assertEqual(point.x, 3, "point.x should be 3 after set");
|
||||
self.testcase.assertEqual(point.y, 4, "point.y should be 4 after set");
|
||||
|
||||
point.set(10, 20);
|
||||
self.testcase.assertEqual(point.x, 10, "point.x should be 10 after second set");
|
||||
self.testcase.assertEqual(point.y, 20, "point.y should be 20 after second set");
|
||||
}
|
||||
|
||||
void test_multiple_objects(int self) {
|
||||
int c1 = new Counter();
|
||||
int c2 = new Counter();
|
||||
|
||||
c1.count = 10;
|
||||
c2.count = 20;
|
||||
|
||||
self.testcase.assertEqual(c1.count, 10, "c1.count should be 10");
|
||||
self.testcase.assertEqual(c2.count, 20, "c2.count should be 20");
|
||||
self.testcase.assertNotEqual(c1.count, c2.count, "c1.count should not equal c2.count");
|
||||
|
||||
c1.increment();
|
||||
self.testcase.assertEqual(c1.count, 11, "c1.count should be 11");
|
||||
self.testcase.assertEqual(c2.count, 20, "c2.count should still be 20");
|
||||
}
|
||||
|
||||
void test_state_management(int self) {
|
||||
int counter = new Counter();
|
||||
|
||||
counter.increment();
|
||||
counter.increment();
|
||||
counter.increment();
|
||||
self.testcase.assertEqual(counter.count, 3, "count should be 3 after 3 increments");
|
||||
|
||||
counter.reset();
|
||||
self.testcase.assertEqual(counter.count, 0, "count should be 0 after reset");
|
||||
|
||||
counter.increment();
|
||||
self.testcase.assertEqual(counter.count, 1, "count should be 1 after increment");
|
||||
}
|
||||
|
||||
void test_complex_operations(int self) {
|
||||
int point = new Point();
|
||||
point.x = 3;
|
||||
point.y = 4;
|
||||
|
||||
int dist = point.distance();
|
||||
self.testcase.assertEqual(dist, 5, "distance of (3,4) should be 5");
|
||||
|
||||
point.x = 0;
|
||||
point.y = 0;
|
||||
dist = point.distance();
|
||||
self.testcase.assertEqual(dist, 0, "distance of (0,0) should be 0");
|
||||
}
|
||||
|
||||
void tearDown(int self) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int runner = new TestRunner();
|
||||
runner.init(0);
|
||||
|
||||
int suite = new TestOOPFeatures();
|
||||
suite.setUp();
|
||||
|
||||
suite.test_object_creation();
|
||||
suite.test_null_objects();
|
||||
suite.test_field_access();
|
||||
suite.test_field_mutation();
|
||||
suite.test_method_calls();
|
||||
suite.test_method_return_values();
|
||||
suite.test_method_with_mutation();
|
||||
suite.test_multiple_objects();
|
||||
suite.test_state_management();
|
||||
suite.test_complex_operations();
|
||||
|
||||
runner.runTest(suite.testcase, "TestOOPFeatures");
|
||||
runner.printSummary();
|
||||
|
||||
return 0;
|
||||
}
|
||||
30
tests/test_preprocessor.rc
Normal file
30
tests/test_preprocessor.rc
Normal file
@ -0,0 +1,30 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("Preprocessor");
|
||||
|
||||
printf("\n=== Testing #include Directive ===\n");
|
||||
tc.assertTrue(1, "unittest.rc included successfully");
|
||||
|
||||
printf("\n=== Testing Framework Availability ===\n");
|
||||
int test_obj = new TestCase();
|
||||
tc.assertNotNull(test_obj, "TestCase class available from included file");
|
||||
|
||||
test_obj.init("Nested Test");
|
||||
tc.assertTrue(1, "Can initialize objects from included classes");
|
||||
|
||||
printf("\n=== Testing Multiple Includes ===\n");
|
||||
tc.assertTrue(1, "Multiple #include directives work");
|
||||
tc.assertTrue(1, "No duplicate symbol errors");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All preprocessor tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
class TestCase {
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
void report(int obj) {
|
||||
printf("Passed: %d, Failed: %d\n", obj.passed, obj.failed);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
printf("Created: %d\n", tc);
|
||||
|
||||
printf("Calling report...\n");
|
||||
tc.report();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
include "tests/unittest.rc"
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase object...\n");
|
||||
int tc = new TestCase();
|
||||
printf("TestCase created: %d\n", tc);
|
||||
|
||||
printf("Initializing TestCase...\n");
|
||||
tc.init("Simple Test");
|
||||
printf("TestCase initialized\n");
|
||||
|
||||
printf("Running assertion...\n");
|
||||
tc.assertEqual(5, 5, "5 should equal 5");
|
||||
|
||||
printf("Passed: %d\n", tc.passed);
|
||||
printf("Failed: %d\n", tc.failed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
152
tests/test_stdlib_async.rc
Normal file
152
tests/test_stdlib_async.rc
Normal file
@ -0,0 +1,152 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
async int compute_square(int n) {
|
||||
int result = n * n;
|
||||
return result;
|
||||
}
|
||||
|
||||
async int compute_sum(int a, int b) {
|
||||
int result = a + b;
|
||||
return result;
|
||||
}
|
||||
|
||||
async int compute_product(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
async int slow_computation(int n) {
|
||||
int total = 0;
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
total = total + i;
|
||||
i = i + 1;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("Async Standard Library");
|
||||
|
||||
printf("\n=== Testing Async Function Calls ===\n");
|
||||
int coro1 = compute_square(5);
|
||||
tc.assertNotNull(coro1, "Async function returns coroutine handle");
|
||||
|
||||
int result1 = await(coro1);
|
||||
tc.assertEqual(result1, 25, "await() returns result: square(5) = 25");
|
||||
|
||||
printf("\n=== Testing Multiple Async Calls ===\n");
|
||||
int coro2 = compute_sum(10, 20);
|
||||
int result2 = await(coro2);
|
||||
tc.assertEqual(result2, 30, "Async sum: 10 + 20 = 30");
|
||||
|
||||
int coro3 = compute_sum(5, 15);
|
||||
int result3 = await(coro3);
|
||||
tc.assertEqual(result3, 20, "Async sum: 5 + 15 = 20");
|
||||
|
||||
int coro4 = compute_product(6, 7);
|
||||
int result4 = await(coro4);
|
||||
tc.assertEqual(result4, 42, "Async product: 6 * 7 = 42");
|
||||
|
||||
printf("\n=== Testing Gather (Parallel Execution) ===\n");
|
||||
int coro_a = compute_square(4);
|
||||
int coro_b = compute_square(5);
|
||||
int coro_c = compute_square(6);
|
||||
|
||||
gather(coro_a);
|
||||
gather(coro_b);
|
||||
gather(coro_c);
|
||||
|
||||
int res_a = await(coro_a);
|
||||
int res_b = await(coro_b);
|
||||
int res_c = await(coro_c);
|
||||
|
||||
tc.assertEqual(res_a, 16, "Parallel: square(4) = 16");
|
||||
tc.assertEqual(res_b, 25, "Parallel: square(5) = 25");
|
||||
tc.assertEqual(res_c, 36, "Parallel: square(6) = 36");
|
||||
|
||||
printf("\n=== Testing Nested Async Calls ===\n");
|
||||
int coro_x = compute_sum(5, 10);
|
||||
int intermediate = await(coro_x);
|
||||
tc.assertEqual(intermediate, 15, "First async call: 5 + 10 = 15");
|
||||
|
||||
int coro_y = compute_square(intermediate);
|
||||
int final_result = await(coro_y);
|
||||
tc.assertEqual(final_result, 225, "Chained async: square(15) = 225");
|
||||
|
||||
printf("\n=== Testing Slow Computations ===\n");
|
||||
int slow1 = slow_computation(10);
|
||||
int slow2 = slow_computation(5);
|
||||
|
||||
gather(slow1);
|
||||
gather(slow2);
|
||||
|
||||
int slow_res1 = await(slow1);
|
||||
int slow_res2 = await(slow2);
|
||||
|
||||
tc.assertEqual(slow_res1, 45, "slow_computation(10) = sum(0..9) = 45");
|
||||
tc.assertEqual(slow_res2, 10, "slow_computation(5) = sum(0..4) = 10");
|
||||
|
||||
printf("\n=== Testing Multiple Sequential Awaits ===\n");
|
||||
int seq1 = compute_square(2);
|
||||
int val1 = await(seq1);
|
||||
tc.assertEqual(val1, 4, "Sequential 1: square(2) = 4");
|
||||
|
||||
int seq2 = compute_sum(val1, 5);
|
||||
int val2 = await(seq2);
|
||||
tc.assertEqual(val2, 9, "Sequential 2: 4 + 5 = 9");
|
||||
|
||||
int seq3 = compute_product(val2, 3);
|
||||
int val3 = await(seq3);
|
||||
tc.assertEqual(val3, 27, "Sequential 3: 9 * 3 = 27");
|
||||
|
||||
printf("\n=== Testing Multiple Gather Batches ===\n");
|
||||
int batch1_a = compute_square(2);
|
||||
int batch1_b = compute_square(3);
|
||||
gather(batch1_a);
|
||||
gather(batch1_b);
|
||||
|
||||
int b1a = await(batch1_a);
|
||||
int b1b = await(batch1_b);
|
||||
tc.assertEqual(b1a, 4, "Batch 1a: square(2) = 4");
|
||||
tc.assertEqual(b1b, 9, "Batch 1b: square(3) = 9");
|
||||
|
||||
int batch2_a = compute_sum(10, 10);
|
||||
int batch2_b = compute_sum(20, 20);
|
||||
gather(batch2_a);
|
||||
gather(batch2_b);
|
||||
|
||||
int b2a = await(batch2_a);
|
||||
int b2b = await(batch2_b);
|
||||
tc.assertEqual(b2a, 20, "Batch 2a: 10 + 10 = 20");
|
||||
tc.assertEqual(b2b, 40, "Batch 2b: 20 + 20 = 40");
|
||||
|
||||
printf("\n=== Testing Complex Async Patterns ===\n");
|
||||
int step1 = compute_square(3);
|
||||
int s1 = await(step1);
|
||||
|
||||
int step2a = compute_sum(s1, 5);
|
||||
int step2b = compute_product(s1, 2);
|
||||
gather(step2a);
|
||||
gather(step2b);
|
||||
|
||||
int s2a = await(step2a);
|
||||
int s2b = await(step2b);
|
||||
|
||||
tc.assertEqual(s2a, 14, "Complex pattern: 9 + 5 = 14");
|
||||
tc.assertEqual(s2b, 18, "Complex pattern: 9 * 2 = 18");
|
||||
|
||||
int step3 = compute_sum(s2a, s2b);
|
||||
int s3 = await(step3);
|
||||
tc.assertEqual(s3, 32, "Complex pattern final: 14 + 18 = 32");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All async stdlib tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
88
tests/test_stdlib_io.rc
Normal file
88
tests/test_stdlib_io.rc
Normal file
@ -0,0 +1,88 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("I/O Standard Library");
|
||||
|
||||
printf("\n=== Testing File Operations ===\n");
|
||||
|
||||
char* filename = "/tmp/test_rc_file.txt";
|
||||
char* content = "Hello, RC!";
|
||||
|
||||
int file = fopen(filename, "w");
|
||||
tc.assertNotNull(file, "fopen() for writing returns valid handle");
|
||||
|
||||
int bytes_written = fwrite(file, content, strlen(content));
|
||||
tc.assertEqual(bytes_written, 10, "fwrite() writes correct number of bytes");
|
||||
|
||||
fclose(file);
|
||||
tc.assertTrue(1, "fclose() completes successfully");
|
||||
|
||||
file = fopen(filename, "r");
|
||||
tc.assertNotNull(file, "fopen() for reading returns valid handle");
|
||||
|
||||
char* buffer = " ";
|
||||
int bytes_read = fread(file, buffer, 10);
|
||||
tc.assertEqual(bytes_read, 10, "fread() reads correct number of bytes");
|
||||
|
||||
fclose(file);
|
||||
|
||||
printf("\n=== Testing File Position Operations ===\n");
|
||||
|
||||
file = fopen(filename, "r");
|
||||
tc.assertNotNull(file, "fopen() for seeking test");
|
||||
|
||||
int pos = ftell(file);
|
||||
tc.assertEqual(pos, 0, "ftell() returns 0 at file start");
|
||||
|
||||
fseek(file, 5, SEEK_SET());
|
||||
pos = ftell(file);
|
||||
tc.assertEqual(pos, 5, "fseek(SEEK_SET, 5) moves to position 5");
|
||||
|
||||
fseek(file, 2, SEEK_CUR());
|
||||
pos = ftell(file);
|
||||
tc.assertEqual(pos, 7, "fseek(SEEK_CUR, 2) moves 2 bytes forward");
|
||||
|
||||
fseek(file, 0, SEEK_END());
|
||||
pos = ftell(file);
|
||||
tc.assertEqual(pos, 10, "fseek(SEEK_END, 0) moves to end of file");
|
||||
|
||||
int eof = feof(file);
|
||||
tc.assertTrue(eof, "feof() returns true at end of file");
|
||||
|
||||
fclose(file);
|
||||
|
||||
printf("\n=== Testing fputs/fgets ===\n");
|
||||
|
||||
file = fopen(filename, "w");
|
||||
fputs(file, "Line 1\n");
|
||||
fputs(file, "Line 2\n");
|
||||
fclose(file);
|
||||
tc.assertTrue(1, "fputs() writes lines to file");
|
||||
|
||||
file = fopen(filename, "r");
|
||||
char* line1 = fgets(file, 100);
|
||||
tc.assertStringContains(line1, "Line 1", "fgets() reads first line");
|
||||
|
||||
char* line2 = fgets(file, 100);
|
||||
tc.assertStringContains(line2, "Line 2", "fgets() reads second line");
|
||||
|
||||
fclose(file);
|
||||
|
||||
printf("\n=== Testing Socket Constants ===\n");
|
||||
int af_inet = AF_INET();
|
||||
tc.assertGreater(af_inet, 0, "AF_INET constant is defined");
|
||||
|
||||
int sock_stream = SOCK_STREAM();
|
||||
tc.assertGreater(sock_stream, 0, "SOCK_STREAM constant is defined");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All I/O stdlib tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
tests/test_stdlib_math.rc
Normal file
73
tests/test_stdlib_math.rc
Normal file
@ -0,0 +1,73 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("Math Standard Library");
|
||||
|
||||
printf("\n=== Testing sqrt ===\n");
|
||||
tc.assertEqual(sqrt(16), 4, "sqrt(16) = 4");
|
||||
tc.assertEqual(sqrt(25), 5, "sqrt(25) = 5");
|
||||
tc.assertEqual(sqrt(1), 1, "sqrt(1) = 1");
|
||||
tc.assertEqual(sqrt(0), 0, "sqrt(0) = 0");
|
||||
tc.assertEqual(sqrt(100), 10, "sqrt(100) = 10");
|
||||
|
||||
printf("\n=== Testing pow ===\n");
|
||||
tc.assertEqual(pow(2, 3), 8, "pow(2, 3) = 8");
|
||||
tc.assertEqual(pow(5, 2), 25, "pow(5, 2) = 25");
|
||||
tc.assertEqual(pow(10, 0), 1, "pow(10, 0) = 1");
|
||||
tc.assertEqual(pow(3, 4), 81, "pow(3, 4) = 81");
|
||||
tc.assertEqual(pow(2, 10), 1024, "pow(2, 10) = 1024");
|
||||
|
||||
printf("\n=== Testing abs ===\n");
|
||||
tc.assertEqual(abs(-42), 42, "abs(-42) = 42");
|
||||
tc.assertEqual(abs(42), 42, "abs(42) = 42");
|
||||
tc.assertEqual(abs(0), 0, "abs(0) = 0");
|
||||
tc.assertEqual(abs(-1), 1, "abs(-1) = 1");
|
||||
tc.assertEqual(abs(-100), 100, "abs(-100) = 100");
|
||||
|
||||
printf("\n=== Testing floor/ceil ===\n");
|
||||
tc.assertEqual(floor(5), 5, "floor(5) = 5");
|
||||
tc.assertEqual(ceil(5), 5, "ceil(5) = 5");
|
||||
tc.assertEqual(floor(0), 0, "floor(0) = 0");
|
||||
tc.assertEqual(ceil(0), 0, "ceil(0) = 0");
|
||||
|
||||
printf("\n=== Testing Trigonometry ===\n");
|
||||
int sin_result = sin(0);
|
||||
tc.assertEqual(sin_result, 0, "sin(0) = 0");
|
||||
|
||||
int cos_result = cos(0);
|
||||
tc.assertGreater(cos_result, 0, "cos(0) > 0 (fixed-point representation)");
|
||||
|
||||
int tan_result = tan(0);
|
||||
tc.assertEqual(tan_result, 0, "tan(0) = 0");
|
||||
|
||||
printf("\n=== Testing Double Operations ===\n");
|
||||
int d1 = int_to_double(5);
|
||||
int d2 = int_to_double(3);
|
||||
|
||||
int d_sum = double_add(d1, d2);
|
||||
int result_int = double_to_int(d_sum);
|
||||
tc.assertEqual(result_int, 8, "Double addition: 5.0 + 3.0 = 8.0");
|
||||
|
||||
int d_diff = double_sub(d1, d2);
|
||||
result_int = double_to_int(d_diff);
|
||||
tc.assertEqual(result_int, 2, "Double subtraction: 5.0 - 3.0 = 2.0");
|
||||
|
||||
int d_prod = double_mul(d1, d2);
|
||||
result_int = double_to_int(d_prod);
|
||||
tc.assertEqual(result_int, 15, "Double multiplication: 5.0 * 3.0 = 15.0");
|
||||
|
||||
int d_quot = double_div(d1, d2);
|
||||
result_int = double_to_int(d_quot);
|
||||
tc.assertEqual(result_int, 1, "Double division: 5.0 / 3.0 = 1.666... (truncated to 1)");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All math stdlib tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
tests/test_stdlib_string.rc
Normal file
73
tests/test_stdlib_string.rc
Normal file
@ -0,0 +1,73 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
int tc = new TestCase();
|
||||
tc.init("String Standard Library");
|
||||
|
||||
printf("\n=== Testing strlen ===\n");
|
||||
tc.assertEqual(strlen("hello"), 5, "strlen('hello') = 5");
|
||||
tc.assertEqual(strlen(""), 0, "strlen('') = 0");
|
||||
tc.assertEqual(strlen("a"), 1, "strlen('a') = 1");
|
||||
tc.assertEqual(strlen("hello world"), 11, "strlen('hello world') = 11");
|
||||
|
||||
printf("\n=== Testing strpos ===\n");
|
||||
tc.assertEqual(strpos("hello", "e"), 1, "strpos('hello', 'e') = 1");
|
||||
tc.assertEqual(strpos("hello", "l"), 2, "strpos('hello', 'l') = 2");
|
||||
tc.assertEqual(strpos("hello", "o"), 4, "strpos('hello', 'o') = 4");
|
||||
tc.assertEqual(strpos("hello", "x"), -1, "strpos('hello', 'x') = -1 (not found)");
|
||||
tc.assertEqual(strpos("hello", "hello"), 0, "strpos('hello', 'hello') = 0");
|
||||
|
||||
printf("\n=== Testing substr ===\n");
|
||||
tc.assertStringEqual(substr("hello", 0, 5), "hello", "substr('hello', 0, 5) = 'hello'");
|
||||
tc.assertStringEqual(substr("hello", 1, 5), "ello", "substr('hello', 1, 5) = 'ello'");
|
||||
tc.assertStringEqual(substr("hello", 0, 2), "he", "substr('hello', 0, 2) = 'he'");
|
||||
tc.assertStringEqual(substr("hello world", 6, 11), "world", "substr('hello world', 6, 11) = 'world'");
|
||||
|
||||
printf("\n=== Testing String Slicing ===\n");
|
||||
char* text = "Programming";
|
||||
tc.assertStringEqual(text[0:4], "Prog", "String slice [0:4] = 'Prog'");
|
||||
tc.assertStringEqual(text[0:7], "Program", "String slice [0:7] = 'Program'");
|
||||
|
||||
printf("\n=== Testing upper/lower ===\n");
|
||||
tc.assertStringEqual(upper("hello"), "HELLO", "upper('hello') = 'HELLO'");
|
||||
tc.assertStringEqual(upper("HeLLo"), "HELLO", "upper('HeLLo') = 'HELLO'");
|
||||
tc.assertStringEqual(lower("HELLO"), "hello", "lower('HELLO') = 'hello'");
|
||||
tc.assertStringEqual(lower("HeLLo"), "hello", "lower('HeLLo') = 'hello'");
|
||||
|
||||
printf("\n=== Testing strip ===\n");
|
||||
tc.assertStringEqual(strip(" hello "), "hello", "strip(' hello ') = 'hello'");
|
||||
tc.assertStringEqual(strip("hello"), "hello", "strip('hello') = 'hello'");
|
||||
tc.assertStringEqual(strip(" hello"), "hello", "strip(' hello') = 'hello'");
|
||||
tc.assertStringEqual(strip("hello "), "hello", "strip('hello ') = 'hello'");
|
||||
|
||||
printf("\n=== Testing startswith/endswith ===\n");
|
||||
tc.assertTrue(startswith("hello", "he"), "startswith('hello', 'he') = true");
|
||||
tc.assertFalse(startswith("hello", "ho"), "startswith('hello', 'ho') = false");
|
||||
tc.assertTrue(endswith("hello", "lo"), "endswith('hello', 'lo') = true");
|
||||
tc.assertFalse(endswith("hello", "he"), "endswith('hello', 'he') = false");
|
||||
|
||||
printf("\n=== Testing replace ===\n");
|
||||
tc.assertStringEqual(replace("hello world", "world", "there"), "hello there", "replace('hello world', 'world', 'there')");
|
||||
tc.assertStringEqual(replace("aaa", "a", "b"), "baa", "replace('aaa', 'a', 'b') replaces first");
|
||||
|
||||
printf("\n=== Testing String Concatenation ===\n");
|
||||
char* hello = "Hello";
|
||||
char* world = " World";
|
||||
char* result = hello + world;
|
||||
tc.assertStringEqual(result, "Hello World", "String concatenation: 'Hello' + ' World'");
|
||||
|
||||
printf("\n=== Testing assertStringContains ===\n");
|
||||
tc.assertStringContains("hello world", "world", "'hello world' contains 'world'");
|
||||
tc.assertStringContains("hello world", "hello", "'hello world' contains 'hello'");
|
||||
tc.assertStringContains("hello world", "o w", "'hello world' contains 'o w'");
|
||||
|
||||
printf("\n=== Summary ===\n");
|
||||
printf("Total Passed: %d\n", tc.passed);
|
||||
printf("Total Failed: %d\n", tc.failed);
|
||||
|
||||
if (tc.failed == 0) {
|
||||
printf("All string stdlib tests PASSED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
class TestObj {
|
||||
int value = 0;
|
||||
|
||||
void withString(int obj, char* msg) {
|
||||
printf("withString called: %s\n", msg);
|
||||
obj.value = 100;
|
||||
}
|
||||
|
||||
void withIntAndString(int obj, int val, char* msg) {
|
||||
printf("withIntAndString called: val=%d, msg=%s\n", val, msg);
|
||||
obj.value = val;
|
||||
}
|
||||
|
||||
void withTwoIntsAndString(int obj, int val1, int val2, char* msg) {
|
||||
printf("withTwoIntsAndString called: val1=%d, val2=%d, msg=%s\n", val1, val2, msg);
|
||||
obj.value = val1 + val2;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== String Method Test ===\n\n");
|
||||
|
||||
int t = new TestObj();
|
||||
|
||||
printf("Test 1: Method with string param\n");
|
||||
t.withString("Hello");
|
||||
printf("value = %d\n\n", t.value);
|
||||
|
||||
printf("Test 2: Method with int and string\n");
|
||||
t.withIntAndString(42, "Test message");
|
||||
printf("value = %d\n\n", t.value);
|
||||
|
||||
printf("Test 3: Method with two ints and string\n");
|
||||
t.withTwoIntsAndString(10, 20, "Sum test");
|
||||
printf("value = %d\n\n", t.value);
|
||||
|
||||
printf("=== All Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,143 +0,0 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
class TestStringFunctions {
|
||||
int testcase = null;
|
||||
|
||||
void setUp(int self) {
|
||||
self.testcase = new TestCase();
|
||||
self.testcase.init("String Functions");
|
||||
}
|
||||
|
||||
void test_strlen(int self) {
|
||||
int result = strlen("hello");
|
||||
self.testcase.assertEqual(result, 5, "strlen('hello') should equal 5");
|
||||
|
||||
result = strlen("");
|
||||
self.testcase.assertEqual(result, 0, "strlen('') should equal 0");
|
||||
|
||||
result = strlen("a");
|
||||
self.testcase.assertEqual(result, 1, "strlen('a') should equal 1");
|
||||
|
||||
result = strlen("hello world");
|
||||
self.testcase.assertEqual(result, 11, "strlen('hello world') should equal 11");
|
||||
}
|
||||
|
||||
void test_strpos(int self) {
|
||||
int result = strpos("hello", "e");
|
||||
self.testcase.assertEqual(result, 1, "strpos('hello', 'e') should equal 1");
|
||||
|
||||
result = strpos("hello", "l");
|
||||
self.testcase.assertEqual(result, 2, "strpos('hello', 'l') should equal 2");
|
||||
|
||||
result = strpos("hello", "o");
|
||||
self.testcase.assertEqual(result, 4, "strpos('hello', 'o') should equal 4");
|
||||
|
||||
result = strpos("hello", "x");
|
||||
self.testcase.assertEqual(result, -1, "strpos('hello', 'x') should equal -1");
|
||||
|
||||
result = strpos("hello", "hello");
|
||||
self.testcase.assertEqual(result, 0, "strpos('hello', 'hello') should equal 0");
|
||||
}
|
||||
|
||||
void test_substr(int self) {
|
||||
char* result = substr("hello", 0, 5);
|
||||
self.testcase.assertStringEqual(result, "hello", "substr('hello', 0, 5) should equal 'hello'");
|
||||
|
||||
result = substr("hello", 1, 4);
|
||||
self.testcase.assertStringEqual(result, "ell", "substr('hello', 1, 4) should equal 'ell'");
|
||||
|
||||
result = substr("hello", 0, 2);
|
||||
self.testcase.assertStringEqual(result, "he", "substr('hello', 0, 2) should equal 'he'");
|
||||
|
||||
result = substr("hello world", 6, 11);
|
||||
self.testcase.assertStringEqual(result, "world", "substr('hello world', 6, 11) should equal 'world'");
|
||||
}
|
||||
|
||||
void test_upper_lower(int self) {
|
||||
char* result = upper("hello");
|
||||
self.testcase.assertStringEqual(result, "HELLO", "upper('hello') should equal 'HELLO'");
|
||||
|
||||
result = upper("HeLLo");
|
||||
self.testcase.assertStringEqual(result, "HELLO", "upper('HeLLo') should equal 'HELLO'");
|
||||
|
||||
result = lower("HELLO");
|
||||
self.testcase.assertStringEqual(result, "hello", "lower('HELLO') should equal 'hello'");
|
||||
|
||||
result = lower("HeLLo");
|
||||
self.testcase.assertStringEqual(result, "hello", "lower('HeLLo') should equal 'hello'");
|
||||
}
|
||||
|
||||
void test_strip(int self) {
|
||||
char* result = strip(" hello ");
|
||||
self.testcase.assertStringEqual(result, "hello", "strip(' hello ') should equal 'hello'");
|
||||
|
||||
result = strip("hello");
|
||||
self.testcase.assertStringEqual(result, "hello", "strip('hello') should equal 'hello'");
|
||||
|
||||
result = strip(" hello");
|
||||
self.testcase.assertStringEqual(result, "hello", "strip(' hello') should equal 'hello'");
|
||||
|
||||
result = strip("hello ");
|
||||
self.testcase.assertStringEqual(result, "hello", "strip('hello ') should equal 'hello'");
|
||||
}
|
||||
|
||||
void test_startswith_endswith(int self) {
|
||||
int result = startswith("hello", "he");
|
||||
self.testcase.assertTrue(result, "startswith('hello', 'he') should be true");
|
||||
|
||||
result = startswith("hello", "ho");
|
||||
self.testcase.assertFalse(result, "startswith('hello', 'ho') should be false");
|
||||
|
||||
result = endswith("hello", "lo");
|
||||
self.testcase.assertTrue(result, "endswith('hello', 'lo') should be true");
|
||||
|
||||
result = endswith("hello", "he");
|
||||
self.testcase.assertFalse(result, "endswith('hello', 'he') should be false");
|
||||
}
|
||||
|
||||
void test_replace(int self) {
|
||||
char* result = replace("hello world", "world", "there");
|
||||
self.testcase.assertStringEqual(result, "hello there", "replace('hello world', 'world', 'there') should work");
|
||||
|
||||
result = replace("aaa", "a", "b");
|
||||
self.testcase.assertStringEqual(result, "baa", "replace('aaa', 'a', 'b') should replace first occurrence");
|
||||
}
|
||||
|
||||
void test_string_contains(int self) {
|
||||
self.testcase.assertStringContains("hello world", "world", "'hello world' should contain 'world'");
|
||||
self.testcase.assertStringContains("hello world", "hello", "'hello world' should contain 'hello'");
|
||||
self.testcase.assertStringContains("hello world", "o w", "'hello world' should contain 'o w'");
|
||||
}
|
||||
|
||||
void test_equality(int self) {
|
||||
self.testcase.assertStringEqual("hello", "hello", "'hello' should equal 'hello'");
|
||||
self.testcase.assertStringEqual("", "", "empty strings should be equal");
|
||||
self.testcase.assertStringEqual("a", "a", "'a' should equal 'a'");
|
||||
}
|
||||
|
||||
void tearDown(int self) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int runner = new TestRunner();
|
||||
runner.init(0);
|
||||
|
||||
int suite = new TestStringFunctions();
|
||||
suite.setUp();
|
||||
|
||||
suite.test_strlen();
|
||||
suite.test_strpos();
|
||||
suite.test_substr();
|
||||
suite.test_upper_lower();
|
||||
suite.test_strip();
|
||||
suite.test_startswith_endswith();
|
||||
suite.test_replace();
|
||||
suite.test_string_contains();
|
||||
suite.test_equality();
|
||||
|
||||
runner.runTest(suite.testcase, "TestStringFunctions");
|
||||
runner.printSummary();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
class TestCase {
|
||||
int passed = 0;
|
||||
|
||||
void withThis(int this, int val) {
|
||||
printf("withThis: this=%d, val=%d\n", this, val);
|
||||
}
|
||||
|
||||
void withObj(int obj, int val) {
|
||||
printf("withObj: obj=%d, val=%d\n", obj, val);
|
||||
}
|
||||
|
||||
void withSelf(int self, int val) {
|
||||
printf("withSelf: self=%d, val=%d\n", self, val);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase\n");
|
||||
int tc = new TestCase();
|
||||
printf("tc=%d\n\n", tc);
|
||||
|
||||
printf("Test 1: Parameter named 'this'\n");
|
||||
tc.withThis(42);
|
||||
|
||||
printf("\nTest 2: Parameter named 'obj'\n");
|
||||
tc.withObj(42);
|
||||
|
||||
printf("\nTest 3: Parameter named 'self'\n");
|
||||
tc.withSelf(42);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
printf("=== Debug Unittest Test ===\n\n");
|
||||
|
||||
printf("Test 1: Create TestCase\n");
|
||||
int tc = new TestCase();
|
||||
printf("TestCase created: %d\n", tc);
|
||||
|
||||
if (tc == null) {
|
||||
printf("ERROR: tc is null!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("tc is not null, value = %d\n", tc);
|
||||
|
||||
printf("Test 2: Try to access field directly\n");
|
||||
tc.passed = 0;
|
||||
printf("Direct field access works\n");
|
||||
|
||||
printf("Test 3: Try to call init with no arguments\n");
|
||||
|
||||
printf("Test 4: Let me try manual field init\n");
|
||||
tc.name = "Manual Test";
|
||||
tc.passed = 0;
|
||||
tc.failed = 0;
|
||||
tc.verbose = 0;
|
||||
tc.failfast = 0;
|
||||
printf("Manual initialization complete\n");
|
||||
|
||||
printf("Test 5: Run assertEqual\n");
|
||||
tc.assertEqual(5, 5, "5 should equal 5");
|
||||
printf("assertEqual passed\n");
|
||||
|
||||
printf("Passed: %d, Failed: %d\n", tc.passed, tc.failed);
|
||||
|
||||
printf("\n=== Debug Test Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
#include "unittest.rc"
|
||||
|
||||
int main() {
|
||||
printf("=== Simple Unittest Test ===\n\n");
|
||||
|
||||
printf("Test 1: Create TestCase\n");
|
||||
int tc = new TestCase();
|
||||
printf("TestCase created\n");
|
||||
|
||||
printf("Test 2: Initialize TestCase\n");
|
||||
tc.init("Simple Test");
|
||||
printf("TestCase initialized\n");
|
||||
|
||||
printf("Test 3: Run assertEqual\n");
|
||||
tc.assertEqual(5, 5, "5 should equal 5");
|
||||
printf("Test passed\n");
|
||||
|
||||
printf("Test 4: Check counts\n");
|
||||
printf("Passed: %d, Failed: %d\n", tc.passed, tc.failed);
|
||||
|
||||
printf("Test 5: Create TestRunner\n");
|
||||
int runner = new TestRunner();
|
||||
printf("TestRunner created\n");
|
||||
|
||||
printf("Test 6: Initialize TestRunner\n");
|
||||
runner.init(0);
|
||||
printf("TestRunner initialized\n");
|
||||
|
||||
printf("Test 7: Call runTest\n");
|
||||
runner.runTest(tc, "Simple Test");
|
||||
|
||||
printf("\nTest 8: Print summary\n");
|
||||
runner.printSummary();
|
||||
|
||||
printf("\n=== All Simple Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
include "tests/unittest_simple.rc"
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
printf("Created: %d\n", tc);
|
||||
|
||||
tc.init("Test1");
|
||||
tc.assertEqual(5, 5, "5 equals 5");
|
||||
|
||||
printf("Passed: %d, Failed: %d\n", tc.passed, tc.failed);
|
||||
return 0;
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
int main() {
|
||||
printf("=== Trigonometric Functions Tests ===\n");
|
||||
printf("Note: Results are scaled by 1,000,000\n");
|
||||
|
||||
printf("Test 1: sin() function\n");
|
||||
int sin0 = sin(0);
|
||||
printf("sin(0) * 1000000 = %d\n", sin0);
|
||||
int sin1 = sin(1);
|
||||
printf("sin(1) * 1000000 = %d\n", sin1);
|
||||
int sin2 = sin(2);
|
||||
printf("sin(2) * 1000000 = %d\n", sin2);
|
||||
printf("PASS: sin() function works\n");
|
||||
|
||||
printf("Test 2: cos() function\n");
|
||||
int cos0 = cos(0);
|
||||
printf("cos(0) * 1000000 = %d\n", cos0);
|
||||
int cos1 = cos(1);
|
||||
printf("cos(1) * 1000000 = %d\n", cos1);
|
||||
int cos2 = cos(2);
|
||||
printf("cos(2) * 1000000 = %d\n", cos2);
|
||||
printf("PASS: cos() function works\n");
|
||||
|
||||
printf("Test 3: tan() function\n");
|
||||
int tan0 = tan(0);
|
||||
printf("tan(0) * 1000000 = %d\n", tan0);
|
||||
int tan1 = tan(1);
|
||||
printf("tan(1) * 1000000 = %d\n", tan1);
|
||||
printf("PASS: tan() function works\n");
|
||||
|
||||
printf("Test 4: Multiple angles\n");
|
||||
int i = 0;
|
||||
while (i < 4) {
|
||||
int s = sin(i);
|
||||
int c = cos(i);
|
||||
printf("Angle %d: sin = %d, cos = %d\n", i, s, c);
|
||||
i = i + 1;
|
||||
}
|
||||
printf("PASS: Multiple angle calculations work\n");
|
||||
|
||||
printf("Test 5: Trig with negative values\n");
|
||||
int sinNeg = sin(-1);
|
||||
printf("sin(-1) * 1000000 = %d\n", sinNeg);
|
||||
int cosNeg = cos(-1);
|
||||
printf("cos(-1) * 1000000 = %d\n", cosNeg);
|
||||
printf("PASS: Negative angle calculations work\n");
|
||||
|
||||
printf("\n=== All Trigonometric Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@ -24,12 +24,10 @@ class TestCase {
|
||||
void assertEqual(int self, int actual, int expected, char* msg) {
|
||||
if (actual == expected) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
printf(" ✗ FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -40,12 +38,10 @@ class TestCase {
|
||||
void assertNotEqual(int self, int actual, int expected, char* msg) {
|
||||
if (actual != expected) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected not equal to %d)\n", msg, expected);
|
||||
printf(" ✗ FAIL: %s (expected not equal to %d)\n", msg, expected);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -56,12 +52,10 @@ class TestCase {
|
||||
void assertTrue(int self, int value, char* msg) {
|
||||
if (value != 0) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected true, got false)\n", msg);
|
||||
printf(" ✗ FAIL: %s (expected true, got false)\n", msg);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -72,12 +66,10 @@ class TestCase {
|
||||
void assertFalse(int self, int value, char* msg) {
|
||||
if (value == 0) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected false, got true)\n", msg);
|
||||
printf(" ✗ FAIL: %s (expected false, got true)\n", msg);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -88,12 +80,10 @@ class TestCase {
|
||||
void assertGreater(int self, int actual, int threshold, char* msg) {
|
||||
if (actual > threshold) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected > %d, got %d)\n", msg, threshold, actual);
|
||||
printf(" ✗ FAIL: %s (expected > %d, got %d)\n", msg, threshold, actual);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -104,12 +94,10 @@ class TestCase {
|
||||
void assertLess(int self, int actual, int threshold, char* msg) {
|
||||
if (actual < threshold) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected < %d, got %d)\n", msg, threshold, actual);
|
||||
printf(" ✗ FAIL: %s (expected < %d, got %d)\n", msg, threshold, actual);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -120,12 +108,10 @@ class TestCase {
|
||||
void assertGreaterEqual(int self, int actual, int threshold, char* msg) {
|
||||
if (actual >= threshold) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected >= %d, got %d)\n", msg, threshold, actual);
|
||||
printf(" ✗ FAIL: %s (expected >= %d, got %d)\n", msg, threshold, actual);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -136,12 +122,10 @@ class TestCase {
|
||||
void assertLessEqual(int self, int actual, int threshold, char* msg) {
|
||||
if (actual <= threshold) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected <= %d, got %d)\n", msg, threshold, actual);
|
||||
printf(" ✗ FAIL: %s (expected <= %d, got %d)\n", msg, threshold, actual);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -170,12 +154,10 @@ class TestCase {
|
||||
|
||||
if (equal == 1) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected '%s', got '%s')\n", msg, expected, actual);
|
||||
printf(" ✗ FAIL: %s (expected '%s', got '%s')\n", msg, expected, actual);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -187,12 +169,10 @@ class TestCase {
|
||||
int pos = strpos(haystack, needle);
|
||||
if (pos >= 0) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected '%s' to contain '%s')\n", msg, haystack, needle);
|
||||
printf(" ✗ FAIL: %s (expected '%s' to contain '%s')\n", msg, haystack, needle);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -203,12 +183,10 @@ class TestCase {
|
||||
void assertNull(int self, int value, char* msg) {
|
||||
if (value == null) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected null, got %d)\n", msg, value);
|
||||
printf(" ✗ FAIL: %s (expected null, got %d)\n", msg, value);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
@ -219,12 +197,10 @@ class TestCase {
|
||||
void assertNotNull(int self, int value, char* msg) {
|
||||
if (value != null) {
|
||||
self.passed = self.passed + 1;
|
||||
if (self.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
printf(" ✓ PASS: %s\n", msg);
|
||||
} else {
|
||||
self.failed = self.failed + 1;
|
||||
printf(" FAIL: %s (expected not null)\n", msg);
|
||||
printf(" ✗ FAIL: %s (expected not null)\n", msg);
|
||||
if (self.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
class TestCase {
|
||||
char* name = null;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
void init(int this, char* test_name) {
|
||||
this.name = test_name;
|
||||
this.passed = 0;
|
||||
this.failed = 0;
|
||||
}
|
||||
|
||||
void assertEqual(int this, int actual, int expected, char* msg) {
|
||||
if (actual == expected) {
|
||||
this.passed = this.passed + 1;
|
||||
printf(" PASS: %s\n", msg);
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user