| 1 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from pycompressor.huffman import HuffmanCompressor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | class HuffmanCompressionUnitTest(unittest.TestCase): | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |     def test_huffman_binary(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |         huffman = HuffmanCompressor() | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |         text = 'Hello World' | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |         bit_stream = huffman.compress_to_binary(text) | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |         self.assertFalse(bit_stream.is_empty()) | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         print(bit_stream.size()) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |         decompressed = huffman.decompress_from_binary(bit_stream) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         print(decompressed) | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         self.assertEqual(text, decompressed) | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     def test_huffman_string(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         huffman = HuffmanCompressor() | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         original = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been ' \ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |                    'the industry standard dummy text ever since the 1500s, when an unknown printer took a galley ' \ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                    'of type and scrambled it to make a type specimen book. It has survived not only five centuries, ' \ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |                    'but also the leap into electronic typesetting, remaining essentially unchanged. It was ' \ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |                    'popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, ' \ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                    'and more recently with desktop publishing software like Aldus PageMaker including versions of ' \ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                    'Lorem Ipsum. ' | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         print('before compression: ' + original) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         print('length: ' + str(len(original))) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         compressed = huffman.compress_to_string(original) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         print('after compression: ' + compressed) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         print('length: ' + str(len(compressed))) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         decompressed = huffman.decompress_from_string(compressed) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         print('after decompression: ' + decompressed) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         print('length: ' + str(len(decompressed))) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         self.assertEqual(original, decompressed) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | if __name__ == '__main__': | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |     unittest.main() | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |  |