adamantike | d0f1925 | 2016-01-28 14:06:13 -0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # |
| 3 | # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu> |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Tests prime functions.""" |
| 18 | |
| 19 | import unittest |
| 20 | |
| 21 | import rsa.prime |
Sybren A. Stüvel | cfff5bf | 2016-03-17 12:01:23 +0100 | [diff] [blame^] | 22 | |
| 23 | |
| 24 | class PrimeTest(unittest.TestCase): |
| 25 | def test_is_prime(self): |
| 26 | """Test some common primes.""" |
| 27 | |
| 28 | # Test some trivial numbers |
| 29 | self.assertFalse(rsa.prime.is_prime(-1)) |
| 30 | self.assertFalse(rsa.prime.is_prime(0)) |
| 31 | self.assertFalse(rsa.prime.is_prime(1)) |
| 32 | self.assertTrue(rsa.prime.is_prime(2)) |
| 33 | self.assertFalse(rsa.prime.is_prime(42)) |
| 34 | self.assertTrue(rsa.prime.is_prime(41)) |
| 35 | |
| 36 | # Test some slightly larger numbers |
| 37 | self.assertEqual( |
| 38 | [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997], |
| 39 | [x for x in range(901, 1000) if rsa.prime.is_prime(x)] |
| 40 | ) |
| 41 | |
| 42 | # Test around the 50th millionth known prime. |
| 43 | self.assertTrue(rsa.prime.is_prime(982451653)) |
| 44 | self.assertFalse(rsa.prime.is_prime(982451653 * 961748941)) |