Overview
Testing Push Swap is crucial to ensure correctness and efficiency. The program must:- Correctly sort all valid inputs
- Minimize operations to achieve good performance scores
- Handle edge cases without errors or crashes
- Manage memory properly with no leaks
Basic Testing
Manual Testing
Test the program with simple inputs:Verifying Output
To verify that the operations actually sort the stack, you’ll need a checker program. The typical approach:The checker program is typically provided separately by 42 School or needs to be implemented as a bonus. It reads operations from stdin and verifies they sort the stack correctly.
Test Categories
Small Stack Tests (2-3 elements)
Medium Stack Tests (5-100 elements)
For 5 elements, you should aim for ≤12 operations. The algorithm uses the turk algorithm for stacks larger than 3.
Large Stack Tests (100-500 elements)
- 100 elements: Less than 700 operations (5 points), less than 900 (4 points), less than 1100 (3 points)
- 500 elements: Less than 5500 operations (5 points), less than 7000 (4 points), less than 8500 (3 points)
Edge Cases
Empty and Invalid Input
Already Sorted Input
is_stack_sorted() before attempting to sort.
Reverse Sorted Input
Duplicate Numbers
error_repetition() in ft_error_exit.c:72.
Invalid Syntax
error_syntax() in ft_error_exit.c:54.
Automated Testing
Performance Testing Script
Create a script to test operation counts:test_performance.sh
Correctness Testing Script
test_correctness.sh
Memory Leak Testing
Use Valgrind to check for memory leaks:Expected Valgrind Output
The program properly frees memory in
free_stack() (ft_error_exit.c:16) and free_split() (ft_error_exit.c:33), ensuring no leaks even on error conditions.Test with Different Ranges
Visual Testing Tools
Consider using visualizers to watch the sorting process:- push_swap_visualizer: GUI tool showing stack operations
- push_swap_tester: Comprehensive testing framework
Testing Checklist
Correctness
- Program sorts all test cases correctly
- Output is validated by checker
- Already sorted stacks produce no output
Error Handling
- Duplicates trigger “Error”
- Non-numeric input triggers “Error”
- Integer overflow triggers “Error”
- Empty input exits cleanly
Performance
- 3 elements: ≤3 operations
- 5 elements: ≤12 operations
- 100 elements: Less than 700 operations (target)
- 500 elements: Less than 5500 operations (target)
Debugging Tips
Add Debug Output
Modify operations functions to print stack state:Test Specific Cases
If a test fails, isolate the exact input:Next Steps
- Review the algorithm implementation
- Learn about contributing improvements
- Explore optimization strategies