blob: a47c3f2978ca0ccf4537937f5a9f4e23d91a6d5f [file] [log] [blame]
adamantiked0f19252016-01-28 14:06:13 -03001# -*- 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
19import unittest
20
21import rsa.prime
Sybren A. Stüvelcfff5bf2016-03-17 12:01:23 +010022
23
24class 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))