#!/usr/bin/python
#-*- coding: utf-8 -*-

from __future__ import division

import unittest

from os import remove
from os.path import isfile, getsize
from huffman import HuffmanFile as hfile


class HuffmanFileTest(unittest.TestCase):

    def setUp(self):
    	"""
    	This function sets all necessary variables for testing
    	"""
        self.hf = hfile()

	def test_isInstance(self):
		"""
		This function tests if self.hf is an instance of HuffmanFile
		"""
		self.assertIsInstance(self.hf, hfile)

    def test_fileCreated(self):
        """
        This function tests if output file 'compressHuffman.hf' exists
        """
        self.hf.encodingFile('huffmanDefinition.txt', 'compressHuffman')
        self.assertTrue('compressHuffman.hf')

    def test_rateCompression(self):
        """
        This function tests if compression rate is like 0.53 (in theory)
        """
        rate = getsize('compressHuffman.hf') /  getsize('huffmanDefinition.txt')
        self.assertTrue(rate > 0.52 and rate < 0.54)

    def test_decoding(self):
        """
        This function tests if decoded file is equal to original file
        """
        self.hf.encodingFile('huffmanDefinition.txt', 'compressHuffman')
        self.hf.decodingFile('compressHuffman', 'decompresse.txt', 'Hu.tr')
        self.assertEqual(getsize('decompresse.txt'), getsize('huffmanDefinition.txt'))





if __name__=="__main__":

    unittest.main()
