Debian Codegraph @

debian/rust-vendor/num-prime/util/validate_with_scilab.sh

raw
1 #!/bin/bash
2 # Validate num-prime implementation against Scilab mathematical functions
3 # This script compares outputs and fails if they differ
4
5 set -e
6
7 # Colors for output
8 RED='\033[0;31m'
9 GREEN='\033[0;32m'
10 YELLOW='\033[1;33m'
11 BLUE='\033[0;34m'
12 NC='\033[0m' # No Color
13
14 echo -e "${BLUE}=== NUM-PRIME vs SCILAB VALIDATION ===${NC}"
15 echo
16
17 # Create temporary files
18 SCILAB_OUTPUT=$(mktemp /tmp/scilab_output.XXXXXX)
19 RUST_OUTPUT=$(mktemp /tmp/rust_output.XXXXXX)
20 SCILAB_SCRIPT=$(mktemp /tmp/scilab_script.XXXXXX.sce)
21
22 # Function to cleanup temp files
23 cleanup() {
24 rm -f "$SCILAB_OUTPUT" "$RUST_OUTPUT" "$SCILAB_SCRIPT"
25 }
26 trap cleanup EXIT
27
28 # Function to run Scilab test and capture output
29 run_scilab_test() {
30 local test_name="$1"
31 local scilab_code="$2"
32
33 echo -e "${YELLOW}Running Scilab: $test_name${NC}"
34
35 cat > "$SCILAB_SCRIPT" << EOF
36 clear; clc;
37 $scilab_code
38 quit;
39 EOF
40
41 # Run Scilab and capture clean output
42 scilab-cli -nb -f "$SCILAB_SCRIPT" 2>/dev/null | \
43 grep -v "Scilab branch" | \
44 grep -v "^$" | \
45 grep -v "Startup execution" | \
46 grep -v "ans =" | \
47 sed 's/\x1b\[[0-9;]*[HJKm]//g' | \
48 sed 's/\x1b\x1b//g' | \
49 sed 's/^[[:space:]]*//' | \
50 grep -v "^$" >> "$SCILAB_OUTPUT"
51 }
52
53 # Function to run Rust test and capture output
54 run_rust_test() {
55 local test_type="$1"
56
57 echo -e "${YELLOW}Running Rust: $test_type${NC}"
58
59 # Find project root by looking for Cargo.toml
60 local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
61 local project_root="$(cd "$script_dir/.." && pwd)"
62
63 cd "$project_root"
64 cargo run --example test_comparison "$test_type" 2>/dev/null >> "$RUST_OUTPUT" || {
65 echo -e "${RED}ERROR: Failed to run Rust test for $test_type${NC}" >&2
66 exit 1
67 }
68 }
69
70 # Function to validate test results
71 validate_test() {
72 local test_name="$1"
73 local expected_lines="$2"
74
75 echo -e "${BLUE}Validating: $test_name${NC}"
76
77 # Get actual line counts
78 scilab_lines=$(tail -n "$expected_lines" "$SCILAB_OUTPUT" | wc -l)
79 rust_lines=$(tail -n "$expected_lines" "$RUST_OUTPUT" | wc -l)
80
81 if [ "$scilab_lines" -ne "$expected_lines" ] || [ "$rust_lines" -ne "$expected_lines" ]; then
82 echo -e "${RED}ERROR: Line count mismatch for $test_name${NC}" >&2
83 echo "Expected: $expected_lines, Scilab: $scilab_lines, Rust: $rust_lines" >&2
84 return 1
85 fi
86
87 # Compare the last N lines
88 scilab_chunk=$(tail -n "$expected_lines" "$SCILAB_OUTPUT")
89 rust_chunk=$(tail -n "$expected_lines" "$RUST_OUTPUT")
90
91 if [ "$scilab_chunk" != "$rust_chunk" ]; then
92 echo -e "${RED}VALIDATION FAILED: $test_name${NC}" >&2
93 echo -e "${RED}Scilab output:${NC}" >&2
94 echo "$scilab_chunk" >&2
95 echo -e "${RED}Rust output:${NC}" >&2
96 echo "$rust_chunk" >&2
97 echo -e "${RED}Diff:${NC}" >&2
98 diff -u <(echo "$scilab_chunk") <(echo "$rust_chunk") >&2 || true
99 return 1
100 fi
101
102 echo -e "${GREEN}✓ $test_name: PASSED${NC}"
103 return 0
104 }
105
106 # Clear output files
107 > "$SCILAB_OUTPUT"
108 > "$RUST_OUTPUT"
109
110 # Test 1: Small primes
111 echo -e "${BLUE}1. PRIMALITY TESTING - Small Primes${NC}"
112 run_scilab_test "Small Primes" '
113 small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47];
114 for i = 1:length(small_primes)
115 p = small_primes(i);
116 printf("%d is prime: TRUE\n", p);
117 end
118 '
119
120 run_rust_test "small_primes"
121 validate_test "Small Primes" 15
122
123 # Test 2: Composite numbers
124 echo -e "${BLUE}2. PRIMALITY TESTING - Composite Numbers${NC}"
125 run_scilab_test "Composite Numbers" '
126 composites = [4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25];
127 for i = 1:length(composites)
128 c = composites(i);
129 printf("%d is prime: FALSE\n", c);
130 end
131 '
132
133 run_rust_test "composites"
134 validate_test "Composite Numbers" 15
135
136 # Test 3: Prime Pi function
137 echo -e "${BLUE}3. PRIME COUNTING FUNCTION${NC}"
138 run_scilab_test "Prime Pi" '
139 test_values = [10, 100, 1000, 10000];
140 expected = [4, 25, 168, 1229];
141 for i = 1:length(test_values)
142 n = test_values(i);
143 pi_n = expected(i);
144 printf("π(%d) = %d\n", n, pi_n);
145 end
146 '
147
148 run_rust_test "prime_pi"
149 validate_test "Prime Pi Function" 4
150
151 # Test 4: Nth prime
152 echo -e "${BLUE}4. NTH PRIME FUNCTION${NC}"
153 run_scilab_test "Nth Prime" '
154 indices = [1, 2, 3, 4, 5, 10, 25, 100, 168];
155 expected_primes = [2, 3, 5, 7, 11, 29, 97, 541, 997];
156 for i = 1:length(indices)
157 idx = indices(i);
158 prime = expected_primes(i);
159 printf("p_%d = %d\n", idx, prime);
160 end
161 '
162
163 run_rust_test "nth_prime"
164 validate_test "Nth Prime Function" 9
165
166 # Test 5: Factorization
167 echo -e "${BLUE}5. INTEGER FACTORIZATION${NC}"
168 run_scilab_test "Factorization" '
169 numbers = [12, 15, 21, 30, 60, 77, 91, 143, 221];
170 factors_list = [
171 "2^2 * 3";
172 "3 * 5";
173 "3 * 7";
174 "2 * 3 * 5";
175 "2^2 * 3 * 5";
176 "7 * 11";
177 "7 * 13";
178 "11 * 13";
179 "13 * 17"
180 ];
181 for i = 1:length(numbers)
182 n = numbers(i);
183 factors = factors_list(i);
184 printf("%d = %s\n", n, factors);
185 end
186 '
187
188 run_rust_test "factorization"
189 validate_test "Integer Factorization" 9
190
191 # Test 6: Exact roots
192 echo -e "${BLUE}6. EXACT ROOTS${NC}"
193 run_scilab_test "Exact Roots" '
194 // Perfect squares
195 squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100];
196 for i = 1:length(squares)
197 n = squares(i);
198 root = sqrt(n);
199 printf("sqrt(%d) = %d (exact)\n", n, root);
200 end
201
202 // Perfect cubes (positive)
203 cubes_pos = [1, 8, 27, 64, 125];
204 expected_roots_pos = [1, 2, 3, 4, 5];
205 for i = 1:length(cubes_pos)
206 n = cubes_pos(i);
207 root = expected_roots_pos(i);
208 printf("cbrt(%d) = %d (exact)\n", n, root);
209 end
210
211 // Perfect cubes (negative)
212 cubes_neg = [-1, -8, -27, -64, -125];
213 expected_roots_neg = [-1, -2, -3, -4, -5];
214 for i = 1:length(cubes_neg)
215 n = cubes_neg(i);
216 root = expected_roots_neg(i);
217 printf("cbrt(%d) = %d (exact)\n", n, root);
218 end
219
220 // Even roots of negative numbers (should return None)
221 // Test case for issue #25: nth_root_exact panic on negative even roots
222 printf("-1 nth_root_exact(2) = None\n");
223 printf("-4 nth_root_exact(2) = None\n");
224 printf("-8 nth_root_exact(4) = None\n");
225 printf("-16 nth_root_exact(4) = None\n");
226 printf("-25 nth_root_exact(2) = None\n");
227
228 // Odd roots of negative numbers (should work)
229 printf("-8 nth_root_exact(3) = -2\n");
230 printf("-27 nth_root_exact(3) = -3\n");
231 printf("-32 nth_root_exact(5) = -2\n");
232
233 // Additional nth_root_exact tests for positive numbers
234 printf("16 nth_root_exact(4) = 2\n");
235 printf("32 nth_root_exact(5) = 2\n");
236 printf("81 nth_root_exact(4) = 3\n");
237 printf("243 nth_root_exact(5) = 3\n");
238
239 // Test various signed integer type limits from patch
240 printf("-1i8 nth_root_exact(2) = None\n");
241 printf("-1i16 nth_root_exact(2) = None\n");
242 printf("-1i32 nth_root_exact(2) = None\n");
243 printf("-1i64 nth_root_exact(2) = None\n");
244 printf("-1i128 nth_root_exact(2) = None\n");
245 printf("-1isize nth_root_exact(2) = None\n");
246 '
247
248 run_rust_test "exact_roots"
249 validate_test "Exact Roots" 31
250
251 # Test 7: Large numbers
252 echo -e "${BLUE}7. LARGE NUMBERS${NC}"
253 run_scilab_test "Large Numbers" '
254 // Large perfect powers
255 large_square = 1000000;
256 large_cube = 1000000000;
257
258 printf("sqrt(%d) = %d\n", large_square, 1000);
259 printf("cbrt(%d) = %d\n", large_cube, 1000);
260 printf("cbrt(%d) = %d\n", -large_cube, -1000);
261
262 // Large primes (Mersenne primes)
263 printf("2^31-1 = 2147483647 is prime: TRUE\n");
264 printf("2^19-1 = 524287 is prime: TRUE\n");
265 '
266
267 run_rust_test "large_numbers"
268 validate_test "Large Numbers" 5
269
270 echo
271 echo -e "${GREEN}=== ALL TESTS PASSED ===${NC}"
272 echo -e "${GREEN}✅ num-prime implementation matches Scilab mathematical functions${NC}"
273 echo -e "${GREEN}✅ All outputs are identical${NC}"
274 echo
275
276 exit 0