40 |
46 |
41 def test_get_compression_parameters(self): |
47 def test_get_compression_parameters(self): |
42 p = zstd.get_compression_parameters(1) |
48 p = zstd.get_compression_parameters(1) |
43 self.assertIsInstance(p, zstd.CompressionParameters) |
49 self.assertIsInstance(p, zstd.CompressionParameters) |
44 |
50 |
45 self.assertEqual(p[0], 19) |
51 self.assertEqual(p.window_log, 19) |
|
52 |
|
53 def test_members(self): |
|
54 p = zstd.CompressionParameters(10, 6, 7, 4, 5, 8, 1) |
|
55 self.assertEqual(p.window_log, 10) |
|
56 self.assertEqual(p.chain_log, 6) |
|
57 self.assertEqual(p.hash_log, 7) |
|
58 self.assertEqual(p.search_log, 4) |
|
59 self.assertEqual(p.search_length, 5) |
|
60 self.assertEqual(p.target_length, 8) |
|
61 self.assertEqual(p.strategy, 1) |
|
62 |
|
63 |
|
64 @make_cffi |
|
65 class TestFrameParameters(unittest.TestCase): |
|
66 def test_invalid_type(self): |
|
67 with self.assertRaises(TypeError): |
|
68 zstd.get_frame_parameters(None) |
|
69 |
|
70 with self.assertRaises(TypeError): |
|
71 zstd.get_frame_parameters(u'foobarbaz') |
|
72 |
|
73 def test_invalid_input_sizes(self): |
|
74 with self.assertRaisesRegexp(zstd.ZstdError, 'not enough data for frame'): |
|
75 zstd.get_frame_parameters(b'') |
|
76 |
|
77 with self.assertRaisesRegexp(zstd.ZstdError, 'not enough data for frame'): |
|
78 zstd.get_frame_parameters(zstd.FRAME_HEADER) |
|
79 |
|
80 def test_invalid_frame(self): |
|
81 with self.assertRaisesRegexp(zstd.ZstdError, 'Unknown frame descriptor'): |
|
82 zstd.get_frame_parameters(b'foobarbaz') |
|
83 |
|
84 def test_attributes(self): |
|
85 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x00\x00') |
|
86 self.assertEqual(params.content_size, 0) |
|
87 self.assertEqual(params.window_size, 1024) |
|
88 self.assertEqual(params.dict_id, 0) |
|
89 self.assertFalse(params.has_checksum) |
|
90 |
|
91 # Lowest 2 bits indicate a dictionary and length. Here, the dict id is 1 byte. |
|
92 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x01\x00\xff') |
|
93 self.assertEqual(params.content_size, 0) |
|
94 self.assertEqual(params.window_size, 1024) |
|
95 self.assertEqual(params.dict_id, 255) |
|
96 self.assertFalse(params.has_checksum) |
|
97 |
|
98 # Lowest 3rd bit indicates if checksum is present. |
|
99 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x04\x00') |
|
100 self.assertEqual(params.content_size, 0) |
|
101 self.assertEqual(params.window_size, 1024) |
|
102 self.assertEqual(params.dict_id, 0) |
|
103 self.assertTrue(params.has_checksum) |
|
104 |
|
105 # Upper 2 bits indicate content size. |
|
106 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x40\x00\xff\x00') |
|
107 self.assertEqual(params.content_size, 511) |
|
108 self.assertEqual(params.window_size, 1024) |
|
109 self.assertEqual(params.dict_id, 0) |
|
110 self.assertFalse(params.has_checksum) |
|
111 |
|
112 # Window descriptor is 2nd byte after frame header. |
|
113 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x00\x40') |
|
114 self.assertEqual(params.content_size, 0) |
|
115 self.assertEqual(params.window_size, 262144) |
|
116 self.assertEqual(params.dict_id, 0) |
|
117 self.assertFalse(params.has_checksum) |
|
118 |
|
119 # Set multiple things. |
|
120 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x45\x40\x0f\x10\x00') |
|
121 self.assertEqual(params.content_size, 272) |
|
122 self.assertEqual(params.window_size, 262144) |
|
123 self.assertEqual(params.dict_id, 15) |
|
124 self.assertTrue(params.has_checksum) |
|
125 |
46 |
126 |
47 if hypothesis: |
127 if hypothesis: |
48 s_windowlog = strategies.integers(min_value=zstd.WINDOWLOG_MIN, |
128 s_windowlog = strategies.integers(min_value=zstd.WINDOWLOG_MIN, |
49 max_value=zstd.WINDOWLOG_MAX) |
129 max_value=zstd.WINDOWLOG_MAX) |
50 s_chainlog = strategies.integers(min_value=zstd.CHAINLOG_MIN, |
130 s_chainlog = strategies.integers(min_value=zstd.CHAINLOG_MIN, |
63 zstd.STRATEGY_LAZY, |
143 zstd.STRATEGY_LAZY, |
64 zstd.STRATEGY_LAZY2, |
144 zstd.STRATEGY_LAZY2, |
65 zstd.STRATEGY_BTLAZY2, |
145 zstd.STRATEGY_BTLAZY2, |
66 zstd.STRATEGY_BTOPT)) |
146 zstd.STRATEGY_BTOPT)) |
67 |
147 |
|
148 |
|
149 @make_cffi |
68 class TestCompressionParametersHypothesis(unittest.TestCase): |
150 class TestCompressionParametersHypothesis(unittest.TestCase): |
69 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, |
151 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, |
70 s_searchlength, s_targetlength, s_strategy) |
152 s_searchlength, s_targetlength, s_strategy) |
71 def test_valid_init(self, windowlog, chainlog, hashlog, searchlog, |
153 def test_valid_init(self, windowlog, chainlog, hashlog, searchlog, |
72 searchlength, targetlength, strategy): |
154 searchlength, targetlength, strategy): |
73 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, |
155 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, |
74 searchlog, searchlength, |
156 searchlog, searchlength, |
75 targetlength, strategy) |
157 targetlength, strategy) |
76 self.assertEqual(tuple(p), |
|
77 (windowlog, chainlog, hashlog, searchlog, |
|
78 searchlength, targetlength, strategy)) |
|
79 |
158 |
80 # Verify we can instantiate a compressor with the supplied values. |
159 # Verify we can instantiate a compressor with the supplied values. |
81 # ZSTD_checkCParams moves the goal posts on us from what's advertised |
160 # ZSTD_checkCParams moves the goal posts on us from what's advertised |
82 # in the constants. So move along with them. |
161 # in the constants. So move along with them. |
83 if searchlength == zstd.SEARCHLENGTH_MIN and strategy in (zstd.STRATEGY_FAST, zstd.STRATEGY_GREEDY): |
162 if searchlength == zstd.SEARCHLENGTH_MIN and strategy in (zstd.STRATEGY_FAST, zstd.STRATEGY_GREEDY): |