-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
101 lines (76 loc) · 2.84 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import hashlib
import unittest
from version_2 import sha_256
LENGTH = 32
EMPTY_MESSAGE = ""
SHORT_MESSAGE = "Hello, World!"
AVERAGE_MESSAGE = (
"Special cases aren't special enough to break the rules.\n"
"Although practicality beats purity."
)
BYTES_MESSAGE = b"jlfdsldqfbcalmqpiorancbcamw,,;!w?ijoanwnfvbw;xww"
UNICODE_MESSAGE = (
"Adds word-relevant emojis ❤ to text ✨ with sometimes hilarious 😂 results 😏."
"Read more about its history 📚 and how it works"
)
class TestFixedLength(unittest.TestCase):
ERROR_MSG = f"Result length should be {LENGTH}"
@classmethod
def setUpClass(cls):
cls.test_data = open("LONG_MESSAGE.txt")
cls.LONG_MESSAGE = cls.test_data.read()
@classmethod
def tearDownClass(cls):
cls.test_data.close()
def test_empty_message(self):
self.assertEqual(len(sha_256(EMPTY_MESSAGE)) , LENGTH, self.ERROR_MSG)
def test_short_message(self):
self.assertEqual(len(sha_256(SHORT_MESSAGE)) , LENGTH, self.ERROR_MSG)
def test_average_message(self):
self.assertEqual(len(sha_256(AVERAGE_MESSAGE)) , LENGTH, self.ERROR_MSG)
def test_bytes_message(self):
self.assertEqual(len(sha_256(BYTES_MESSAGE)) , LENGTH, self.ERROR_MSG)
def test_unicode_message(self):
self.assertEqual(len(sha_256(UNICODE_MESSAGE)) , LENGTH, self.ERROR_MSG)
def test_long_message(self):
self.assertEqual(len(sha_256(self.LONG_MESSAGE)) , LENGTH, self.ERROR_MSG)
class TestSHA256(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.test_data = open("LONG_MESSAGE.txt")
cls.LONG_MESSAGE = cls.test_data.read()
@classmethod
def tearDownClass(cls):
cls.test_data.close()
def test_empty_message(self):
self.assertEqual(
sha_256(EMPTY_MESSAGE),
hashlib.sha256(EMPTY_MESSAGE.encode("utf-8")).digest()
)
def test_short_message(self):
self.assertEqual(
sha_256(SHORT_MESSAGE),
hashlib.sha256(SHORT_MESSAGE.encode("utf-8")).digest()
)
def test_average_message(self):
self.assertEqual(
sha_256(AVERAGE_MESSAGE),
hashlib.sha256(AVERAGE_MESSAGE.encode("utf-8")).digest()
)
def test_bytes_message(self):
self.assertEqual(
sha_256(BYTES_MESSAGE),
hashlib.sha256(BYTES_MESSAGE).digest()
)
def test_unicode_message(self):
self.assertEqual(
sha_256(UNICODE_MESSAGE),
hashlib.sha256(UNICODE_MESSAGE.encode("utf-8")).digest()
)
def test_long_message(self):
self.assertEqual(
sha_256(self.LONG_MESSAGE),
hashlib.sha256(self.LONG_MESSAGE.encode("utf-8")).digest()
)
if __name__ == "__main__":
unittest.main()