caellion-python-commons: tests/test_encoding_base36.py Source File

caellion-python-commons

test_encoding_base36.py
Go to the documentation of this file.
1 # test framework
2 import unittest
3 import pytest
4
5 # package
6 from caellion.pycommons.encoding.base36 import Base36Coder
7 from caellion.pycommons.encoding.base36 import ValueTooLargeException
8 from caellion.pycommons.encoding.base36 import NumberNotPositiveOrZeroException
9 from caellion.pycommons.encoding.base36 import InvalidInputStringException
10 from caellion.pycommons.encoding.base36 import InvalidCustomCharsetException
11 from caellion.pycommons.encoding.base36 import InvalidCustomCharsetLengthException
12
13
14 # test cases
15 class TestEncodingBase36Coder():
16 # @pytest.mark.parametrize(
17 # "",[
18 # ("", "")],
19 # )
20
21 def test_encoding_base36_init_too_short(self):
22 with pytest.raises(InvalidCustomCharsetLengthException):
23 b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXY") # noqa
24
25 def test_encoding_base36_init_too_long(self):
26 with pytest.raises(InvalidCustomCharsetLengthException):
27 b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZZ") # noqa
28
29 def test_encoding_base36_init_length_ok_double_char(self):
30 with pytest.raises(InvalidCustomCharsetException):
31 b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYY") # noqa
32
33 def test_encoding_base36_init_ok(self):
34 b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") # noqa
35 assert b36c is not None
36
37 def test_encoding_base36_to_std_charset_invalid_input(self):
38 with pytest.raises(InvalidInputStringException):
39 b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
40 bresult = b36c.to_standard_charset("aa") # noqa
41
42 @pytest.mark.parametrize( "input,output", [("ABCD", "0123"),
43 ("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
44 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
45 ("AAAA", "0000"),
46 ("LINE", "B8D4"), ]
47 )
48 def test_encoding_base36_to_std_charset_valid_input(self, input, output):
49 b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
50 bresult = b36c.to_standard_charset(input)
51 assert bresult == output
52
53 def test_encoding_base36_to_custom_charset_invalid_input(self):
54 with pytest.raises(InvalidInputStringException):
55 b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
56 bresult = b36c.to_custom_charset("aa") # noqa
57
58 @pytest.mark.parametrize( "input,output", [ ("0123", "ABCD"),
59 ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
60 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),
61 ("0000", "AAAA"),
62 ("B8D4", "LINE"), ]
63 )
64 def test_encoding_base36_to_custom_charset_valid_input(self, input, output):
65 b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
66 bresult = b36c.to_custom_charset(input)
67 assert bresult == output
68
69 def test_encoding_base36_int_to_base36_negative(self):
70 with pytest.raises(NumberNotPositiveOrZeroException):
71 b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
72 bresult = b36c.int_to_b36(-1)
73
74 @pytest.mark.parametrize( "input,output", [ (b"\x00", "0"),
75 (b"\x00\x00\x00\x00", "0"),
76 (b"\x00\x00\x00\x01", "1"),
77 (b"\x00\x00\x00\x0a", "A"),
78 (b"\x00\x00\x00\x0f", "F"),
79 (b"\x00\x00\x00\x10", "G"),
80 (b"\x00\x00\x00\x23", "Z"),
81 (b"\x00\x00\x00\x24", "10"),
82 (b"\x00\x00\x00\xff", "73"),
83 (b"\x00\x00\xff\xff", "1EKF"),
84 (b"\x00\xff\xff\xff", "9ZLDR"),
85 (b"\xff\xff\xff\xff", "1Z141Z3"),
86 (b"\x01\x00\x00\x00\x00", "1Z141Z4"),
87 (b"\x00\x00\x00\x01\x00\x00\x00\x00", "1Z141Z4"),
88 (b"\xff\xff\xff\xff\xff\xff\xff\xff", "3W5E11264SGSF"),
89 ]
90 # 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
91 )
92 def test_encoding_base36_bytes_to_b36_default_charset_valid_input(self, input, output):
93 b36c = Base36Coder() # noqa
94 bresult = b36c.bytes_to_b36(input)
95 assert bresult == output
96
97 @pytest.mark.parametrize( "input,output", [ (0, "0"),
98 (1, "1"),
99 (10, "A"),
100 (15, "F"),
101 (16, "G"),
102 (35, "Z"),
103 (36, "10"),
104 (255, "73"),
105 (65535, "1EKF"),
106 (16777215, "9ZLDR"),
107 (4294967295, "1Z141Z3"),
108 (4294967296, "1Z141Z4"),
109 (18446744073709551615, "3W5E11264SGSF"),
110 ]
111 # 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
112 )
113 def test_encoding_base36_int_to_b36_default_charset_valid_input(self, input, output):
114 b36c = Base36Coder() # noqa
115 bresult = b36c.int_to_b36(input)
116 assert bresult == output
117
118 def test_encoding_base36_to_int_negative(self):
119 with pytest.raises(InvalidInputStringException):
120 b36c = Base36Coder() # noqa
121 bresult = b36c.b36_to_int("-A")
122
123 @pytest.mark.parametrize( "input,output", [ ("0", 0),
124 ("1", 1),
125 ("A", 10),
126 ("F", 15),
127 ("G", 16),
128 ("Z", 35),
129 ("10", 36),
130 ("73", 255),
131 ("1EKF", 65535),
132 ("9ZLDR", 16777215),
133 ("1Z141Z3", 4294967295),
134 ("1Z141Z4", 4294967296),
135 ("3W5E11264SGSF", 18446744073709551615),
136 ]
137 # 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
138 )
139 def test_encoding_base36_to_int_valid_input(self, input, output):
140 b36c = Base36Coder() # noqa
141 bresult = b36c.b36_to_int(input)
142 assert bresult == output
143
144 def test_encoding_base36_to_bytes_negative(self):
145 with pytest.raises(InvalidInputStringException):
146 b36c = Base36Coder() # noqa
147 bresult = b36c.b36_to_int("-A")
148
149 @pytest.mark.parametrize( "input,output", [ ("0", b"\x00"),
150 ("1", b"\x01"),
151 ("A", b"\x0a"),
152 ("F", b"\x0f"),
153 ("G", b"\x10"),
154 ("Z", b"\x23"),
155 ("10", b"\x24"),
156 ("73", b"\xff"),
157 ("1EKF", b"\xff\xff"),
158 ("9ZLDR", b"\xff\xff\xff"),
159 ("1Z141Z3", b"\xff\xff\xff\xff"),
160 ("1Z141Z4", b"\x01\x00\x00\x00\x00"),
161 ("3W5E11264SGSF", b"\xff\xff\xff\xff\xff\xff\xff\xff"),
162 ]
163 # 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
164 )
165 def test_encoding_base36_to_bytes_valid_input(self, input, output):
166 b36c = Base36Coder() # noqa
167 bresult = b36c.b36_to_bytes(input)
168 assert bresult == output
169
170 def test_encoding_base36_to_bytes_too_large(self):
171 with pytest.raises(ValueTooLargeException):
172 b36c = Base36Coder() # noqa
173 bresult = b36c.b36_to_bytes('2ZZZZZZZZZZZZZZZZZZZZZZZ')
174
175
176 if __name__ == "__main__":
177 unittest.main()
178
caellion.pycommons.encoding.base36.Base36Coder
This class provides methods allowing conversion between integer, hexadecimal integer and base36 strin...
Definition: base36.py:46
tests.test_encoding_base36.TestEncodingBase36Coder
Definition: test_encoding_base36.py:15
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_custom_charset_invalid_input
def test_encoding_base36_to_custom_charset_invalid_input(self)
Definition: test_encoding_base36.py:53
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_std_charset_valid_input
def test_encoding_base36_to_std_charset_valid_input(self, input, output)
Definition: test_encoding_base36.py:48
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_bytes_too_large
def test_encoding_base36_to_bytes_too_large(self)
Definition: test_encoding_base36.py:170
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_init_too_long
def test_encoding_base36_init_too_long(self)
Definition: test_encoding_base36.py:25
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_int_to_b36_default_charset_valid_input
def test_encoding_base36_int_to_b36_default_charset_valid_input(self, input, output)
Definition: test_encoding_base36.py:113
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_bytes_valid_input
def test_encoding_base36_to_bytes_valid_input(self, input, output)
Definition: test_encoding_base36.py:165
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_int_negative
def test_encoding_base36_to_int_negative(self)
Definition: test_encoding_base36.py:118
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_int_valid_input
def test_encoding_base36_to_int_valid_input(self, input, output)
Definition: test_encoding_base36.py:139
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_init_ok
def test_encoding_base36_init_ok(self)
Definition: test_encoding_base36.py:33
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_bytes_to_b36_default_charset_valid_input
def test_encoding_base36_bytes_to_b36_default_charset_valid_input(self, input, output)
Definition: test_encoding_base36.py:92
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_custom_charset_valid_input
def test_encoding_base36_to_custom_charset_valid_input(self, input, output)
Definition: test_encoding_base36.py:64
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_init_length_ok_double_char
def test_encoding_base36_init_length_ok_double_char(self)
Definition: test_encoding_base36.py:29
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_bytes_negative
def test_encoding_base36_to_bytes_negative(self)
Definition: test_encoding_base36.py:144
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_int_to_base36_negative
def test_encoding_base36_int_to_base36_negative(self)
Definition: test_encoding_base36.py:69
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_init_too_short
def test_encoding_base36_init_too_short(self)
Definition: test_encoding_base36.py:21
tests.test_encoding_base36.TestEncodingBase36Coder.test_encoding_base36_to_std_charset_invalid_input
def test_encoding_base36_to_std_charset_invalid_input(self)
Definition: test_encoding_base36.py:37
caellion.pycommons.encoding.base36
This module provides utlilities related to converting to/from base36 encoding.
Definition: base36.py:1