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
16  # @pytest.mark.parametrize(
17  # "",[
18  # ("", "")],
19  # )
20 
22  with pytest.raises(InvalidCustomCharsetLengthException):
23  b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXY") # noqa
24 
26  with pytest.raises(InvalidCustomCharsetLengthException):
27  b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZZ") # noqa
28 
30  with pytest.raises(InvalidCustomCharsetException):
31  b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYY") # noqa
32 
34  b36c = Base36Coder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") # noqa
35  assert b36c is not None
36 
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  )
49  b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
50  bresult = b36c.to_standard_charset(input)
51  assert bresult == output
52 
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  )
65  b36c = Base36Coder("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") # noqa
66  bresult = b36c.to_custom_charset(input)
67  assert bresult == output
68 
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  )
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  )
114  b36c = Base36Coder() # noqa
115  bresult = b36c.int_to_b36(input)
116  assert bresult == output
117 
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 
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  )
166  b36c = Base36Coder() # noqa
167  bresult = b36c.b36_to_bytes(input)
168  assert bresult == output
169 
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 
This class provides methods allowing conversion between integer, hexadecimal integer and base36 strin...
Definition: base36.py:46
def test_encoding_base36_to_std_charset_valid_input(self, input, output)
def test_encoding_base36_int_to_b36_default_charset_valid_input(self, input, output)
def test_encoding_base36_bytes_to_b36_default_charset_valid_input(self, input, output)
def test_encoding_base36_to_custom_charset_valid_input(self, input, output)
This module provides utlilities related to converting to/from base36 encoding.
Definition: base36.py:1