Skip to content

Commit b4bc89a

Browse files
pyropycmccandless
andauthored
diffie-hellman: add test template (exercism#2203)
Co-authored-by: Corey McCandless <cmccandless@users.noreply.github.com>
1 parent 7829011 commit b4bc89a

3 files changed

Lines changed: 65 additions & 33 deletions

File tree

bin/generate_tests.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ def replace_all(string, chars, rep):
5959
)
6060

6161

62-
def to_snake(string):
62+
def to_snake(string, wordchars_only=False):
6363
"""
6464
Convert pretty much anything to to_snake.
65+
66+
By default whitespace and punctuation will be converted
67+
to underscores as well, pass wordchars_only=True to preserve these as is.
6568
"""
6669
clean = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", string)
6770
clean = re.sub("([a-z0-9])([A-Z])", r"\1_\2", clean).lower()
68-
return replace_all(clean, whitespace + punctuation, "_")
71+
return clean if wordchars_only else replace_all(clean, whitespace + punctuation, "_")
6972

7073

7174
def camel_case(string):
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{% set class = exercise | camel_case -%}
3+
{{ macros.header(["private_key", "public_key", "secret"]) }}
4+
5+
class {{ exercise | camel_case }}Test(unittest.TestCase):
6+
{% for case in cases -%}
7+
{% set property = case["property"] | to_snake -%}
8+
{% set description = case["description"] | to_snake -%}
9+
{% set expected = case['expected'] %}
10+
{% set input = case['input'] %}
11+
def test_{{ description }}(self):
12+
{%- if property == "private_key_is_in_range" %}
13+
primes = [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
14+
for p in primes:
15+
self.assertTrue(1 < private_key(p) < p)
16+
{%- elif property == "private_key_is_random" %}
17+
"""
18+
Can fail due to randomness, but most likely will not,
19+
due to pseudo-randomness and the large number chosen
20+
"""
21+
p = 2147483647
22+
private_keys = [private_key(p) for _ in range(5)]
23+
self.assertEqual(len(set(private_keys)), len(private_keys))
24+
{%- elif property == "key_exchange" %}
25+
{% for key, val in input.items() -%}
26+
{{ key | to_snake }} = {{ val | string | to_snake(wordchars_only=True) }}
27+
{% endfor -%}
28+
self.assertTrue({{ expected | to_snake(wordchars_only=True) }})
29+
{%- else %}
30+
{% for key, val in input.items() -%}
31+
{{ key | to_snake }} = {{ val }}
32+
{% endfor -%}
33+
self.assertEqual({{ expected }}, {{ property }}({% for key in input.keys() %}{{ key | to_snake }},{% endfor %}))
34+
{%- endif -%}
35+
{% endfor %}
36+
37+
{{ macros.footer() }}
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,48 @@
11
import unittest
22

3-
import diffie_hellman
4-
3+
from diffie_hellman import private_key, public_key, secret
54

65
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
76

8-
class DiffieHellmanTest(unittest.TestCase):
97

10-
def test_private_key_is_in_range(self):
8+
class DiffieHellmanTest(unittest.TestCase):
9+
def test_private_key_is_in_range_1_p(self):
1110
primes = [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
12-
for i in primes:
13-
self.assertTrue(1 < diffie_hellman.private_key(i) < i)
11+
for p in primes:
12+
self.assertTrue(1 < private_key(p) < p)
1413

15-
# Can fail due to randomness, but most likely will not,
16-
# due to pseudo-randomness and the large number chosen
1714
def test_private_key_is_random(self):
15+
"""
16+
Can fail due to randomness, but most likely will not,
17+
due to pseudo-randomness and the large number chosen
18+
"""
1819
p = 2147483647
19-
private_keys = []
20-
for i in range(5):
21-
private_keys.append(diffie_hellman.private_key(p))
20+
private_keys = [private_key(p) for _ in range(5)]
2221
self.assertEqual(len(set(private_keys)), len(private_keys))
2322

2423
def test_can_calculate_public_key_using_private_key(self):
2524
p = 23
2625
g = 5
27-
private = 6
28-
expected = 8
29-
30-
actual = diffie_hellman.public_key(p, g, private)
31-
self.assertEqual(actual, expected)
26+
private_key = 6
27+
self.assertEqual(8, public_key(p, g, private_key))
3228

3329
def test_can_calculate_secret_using_other_party_s_public_key(self):
3430
p = 23
35-
public = 19
36-
private = 6
37-
expected = 2
38-
39-
actual = diffie_hellman.secret(p, public, private)
40-
self.assertEqual(actual, expected)
31+
their_public_key = 19
32+
my_private_key = 6
33+
self.assertEqual(2, secret(p, their_public_key, my_private_key))
4134

4235
def test_key_exchange(self):
4336
p = 23
4437
g = 5
45-
alice_private_key = diffie_hellman.private_key(p)
46-
bob_private_key = diffie_hellman.private_key(p)
47-
alice_public_key = diffie_hellman.public_key(p, g, alice_private_key)
48-
bob_public_key = diffie_hellman.public_key(p, g, bob_private_key)
49-
secret_a = diffie_hellman.secret(p, bob_public_key, alice_private_key)
50-
secret_b = diffie_hellman.secret(p, alice_public_key, bob_private_key)
51-
52-
self.assertEqual(secret_a, secret_b)
38+
alice_private_key = private_key(p)
39+
bob_private_key = private_key(p)
40+
alice_public_key = public_key(p, g, alice_private_key)
41+
bob_public_key = public_key(p, g, bob_private_key)
42+
secret_a = secret(p, bob_public_key, alice_private_key)
43+
secret_b = secret(p, alice_public_key, bob_private_key)
44+
self.assertTrue(secret_a == secret_b)
5345

5446

55-
if __name__ == '__main__':
47+
if __name__ == "__main__":
5648
unittest.main()

0 commit comments

Comments
 (0)