@@ -101,3 +101,111 @@ test "@math.probable_prime" {
101101 content = "true" ,
102102 )
103103}
104+
105+ ///|
106+ #cfg (false )
107+ test "is_probable_prime edge cases" {
108+ let r = @random .Rand ::new ()
109+
110+ // Test with negative iterations (should panic)
111+ try {
112+ ignore (is_probable_prime (17N , r , iters = - 1 ))
113+ assert_false (true ) // Should not reach here
114+ } catch {
115+ _ => () // Expected to panic
116+ }
117+
118+ // Test with zero iterations (should panic)
119+ try {
120+ ignore (is_probable_prime (17N , r , iters = 0 ))
121+ assert_false (true ) // Should not reach here
122+ } catch {
123+ _ => () // Expected to panic
124+ }
125+
126+ // Test with minimum positive iterations
127+ assert_true (is_probable_prime (17N , r , iters = 1 ))
128+
129+ // Test with 4 (even number larger than 3)
130+ assert_false (is_probable_prime (4N , r ))
131+
132+ // Test larger even numbers
133+ assert_false (is_probable_prime (100N , r ))
134+ assert_false (is_probable_prime (1000N , r ))
135+ }
136+
137+ ///|
138+ #cfg (false )
139+ test "trial_divisions boundary conditions" {
140+ let r = @random .Rand ::new ()
141+
142+ // Test numbers that equal small primes (edge case in trial_divisions)
143+ assert_true (is_probable_prime (2N , r ))
144+ assert_true (is_probable_prime (3N , r ))
145+ assert_true (is_probable_prime (5N , r ))
146+ assert_true (is_probable_prime (7N , r ))
147+ assert_true (is_probable_prime (11N , r ))
148+
149+ // Test numbers that are multiples of small primes but not the primes themselves
150+ assert_false (is_probable_prime (4N , r )) // 2*2
151+ assert_false (is_probable_prime (6N , r )) // 2*3
152+ assert_false (is_probable_prime (9N , r )) // 3*3
153+ assert_false (is_probable_prime (15N , r )) // 3*5
154+ assert_false (is_probable_prime (21N , r )) // 3*7
155+ assert_false (is_probable_prime (25N , r )) // 5*5
156+ }
157+
158+ ///|
159+ #cfg (false )
160+ test "miller_rabin edge cases" {
161+ let r = @random .Rand ::new ()
162+
163+ // Test with small numbers that would trigger edge case handling in miller_rabin_test
164+ // (these won't actually call miller_rabin_test due to early returns,
165+ // but we test the full path)
166+ assert_true (is_probable_prime (5N , r ))
167+ assert_false (is_probable_prime (8N , r ))
168+ assert_false (is_probable_prime (10N , r ))
169+
170+ // Test numbers that exercise different bit length ranges for trial divisions
171+ // 512 bit boundary
172+ let mid_size = @bigint .BigInt ::from_string (
173+ "12999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" ,
174+ )
175+ ignore (is_probable_prime (mid_size , r ))
176+ }
177+
178+ ///|
179+ #cfg (false )
180+ test "probable_prime different bit sizes" {
181+ let rand = @random .Rand ::new ()
182+
183+ // Test various bit sizes
184+ for bit_size in [8 , 16 , 24 , 48 , 72 , 128 ] {
185+ let prime = probable_prime (bit_size , rand )
186+ assert_true (is_probable_prime (prime , rand ))
187+ // Verify the bit length is approximately correct
188+ assert_true (prime .bit_length () <= bit_size + 1 )
189+ assert_true (prime .bit_length () >= bit_size - 1 )
190+ }
191+ }
192+
193+ ///|
194+ #cfg (false )
195+ test "comprehensive primality tests" {
196+ let r = @random .Rand ::new ()
197+
198+ // Test a range of small numbers to ensure comprehensive coverage
199+ let known_primes = [
200+ 2N , 3N , 5N , 7N , 11N , 13N , 17N , 19N , 23N , 29N , 31N , 37N , 41N , 43N , 47N ,
201+ ]
202+ let known_composites = [
203+ 4N , 6N , 8N , 9N , 10N , 12N , 14N , 15N , 16N , 18N , 20N , 21N , 22N , 24N , 25N ,
204+ ]
205+ for prime in known_primes {
206+ assert_true (is_probable_prime (prime , r ))
207+ }
208+ for composite in known_composites {
209+ assert_false (is_probable_prime (composite , r ))
210+ }
211+ }
0 commit comments