This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_asciibytes.py
145 lines (118 loc) · 3.83 KB
/
test_asciibytes.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Test the miniencoding lib
A decent testing approach is to test the round trip with a random, valid
string of bytes. by taking this approach, the same error/ bug would have to be
present in both the 'from' and 'to' functions which whilst possible is unlikely
"""
# pylint: disable=invalid-name
import random
import string
from miniencoding.asciibytes import AsciiBytes
def generator(length):
"""Generate a random sequence of bytes
Args:
length (int): length
Returns:
bytes: random sequence of bytes
"""
letters = string.printable
return ''.join(random.choice(letters) for i in range(length)).encode()
def test_ascii8():
"""Ascii8 has no strange behaviours or edge cases so this should be pretty
straightforward
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii.fromAscii8(testString)
assert testAscii.toAscii8() == testString
def test_ascii8Len():
"""Ascii8 has a length of 1 byte per char
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii.fromAscii8(testString)
assert len(testAscii.toAscii8()) == 32
def test_ascii7Printable():
"""Ascii7 has no strange behaviours or edge cases for printable chars so
this should be pretty straightforward
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii2 = AsciiBytes()
testAscii.fromAscii8(testString)
testAscii2.fromAscii7(testAscii.toAscii7())
assert testAscii2.toAscii8() == testString
def test_ascii7NonPrintable():
"""ascii7 cannot process ascii values over 127, will replace chars with '?'
"""
testAscii = AsciiBytes()
testAscii2 = AsciiBytes()
testAscii.fromAscii8(bytes([127, 128]))
testAscii2.fromAscii7(testAscii.toAscii7())
assert testAscii2.toAscii8() == bytes([127]) + b"?"
def test_ascii7Len():
"""Ascii7 has a length of 7/8 byte per char
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii.fromAscii8(testString)
assert len(testAscii.toAscii7()) == 4 * 7
def test_decSixBit():
"""Dec Six Bit has edge cases and strange behaviours as shown by the
inline for loop for the variable 'expected'
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii2 = AsciiBytes()
testAscii.fromAscii8(testString)
testAscii2.fromDecSixBit(testAscii.toDecSixBit())
expected = b"".join([
bytes([char]) if 0x20 <= char < 0x60 else b'?'
for char in testString.upper()])
assert testAscii2.toAscii8() == expected
def test_decSixBitLen():
"""Dec Six Bit has a length of 6/8 byte per char
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii.fromAscii8(testString)
assert len(testAscii.toDecSixBit()) == 4 * 6
def test_ICL():
"""ICL has edge cases and strange behaviours as shown by the
inline for loop for the variable 'expected'
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii2 = AsciiBytes()
testAscii.fromAscii8(testString)
testAscii2.fromICL(testAscii.toICL())
expected = b"".join([
bytes([char]) if 0x20 <= char < 0x60 else b'?'
for char in testString.upper()])
assert testAscii2.toAscii8() == expected
def test_ICLLen():
"""ICL has a length of 6/8 byte per char
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii.fromAscii8(testString)
assert len(testAscii.toICL()) == 4 * 6
def test_sixBitAscii():
"""Six Bit Ascii has edge cases and strange behaviours as shown by the
inline for loop for the variable 'expected'
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii2 = AsciiBytes()
testAscii.fromAscii8(testString)
testAscii2.fromSixBitAscii(testAscii.toSixBitAscii())
expected = b"".join([
bytes([char]) if 0x20 <= char < 0x60 else b'?'
for char in testString.upper()])
assert testAscii2.toAscii8() == expected
def test_sixBitAsciiLen():
"""Six Bit Ascii has a length of 6/8 byte per char
"""
testString = generator(32)
testAscii = AsciiBytes()
testAscii.fromAscii8(testString)
assert len(testAscii.toSixBitAscii()) == 4 * 6