Upload files to ''
This commit is contained in:
		
							
								
								
									
										124
									
								
								test_huffman.py
									
									
									
									
									
								
							
							
						
						
									
										124
									
								
								test_huffman.py
									
									
									
									
									
								
							@@ -17,88 +17,88 @@
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import unittest
 | 
					import unittest
 | 
				
			||||||
from huffman_py.Sommets import *
 | 
					from huffman_py.Node import *
 | 
				
			||||||
from huffman_py.Arbre import *
 | 
					from huffman_py.Tree import *
 | 
				
			||||||
from huffman_py.fonctions.encode import *
 | 
					from huffman_py.functions.encode import *
 | 
				
			||||||
from huffman_py.fonctions.decode import *
 | 
					from huffman_py.functions.decode import *
 | 
				
			||||||
from huffman_py.fonctions.occurence import *
 | 
					from huffman_py.functions.occurence import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# pour lancer les tests utilisez:
 | 
					# pour lancer les tests utilisez:
 | 
				
			||||||
# python -m unittest discover
 | 
					# python -m unittest discover
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestUtils(unittest.TestCase):
 | 
					class TestUtils(unittest.TestCase):
 | 
				
			||||||
    def test_Arbre_id(self):
 | 
					    def test_Tree_id(self):
 | 
				
			||||||
        a = Sommets(10, 'a')
 | 
					        a = Node(10, 'a')
 | 
				
			||||||
        b = Sommets(8,'b')
 | 
					        b = Node(8,'b')
 | 
				
			||||||
        r1 = Sommets(18,'',left=b,right=a)
 | 
					        r1 = Node(18,'',left=b,right=a)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        arbre1 = Arbre(r1)
 | 
					        tree1 = Tree(r1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # vérification affectation d'un identifiant unique en créant l'arbre
 | 
					        # vérification affectation d'un identifier unique en créant l'tree
 | 
				
			||||||
        liste_id = []
 | 
					        liste_id = []
 | 
				
			||||||
        verification_id = True
 | 
					        verification_id = True
 | 
				
			||||||
        for elem in arbre1.sommets:
 | 
					        for elem in tree1.nodes:
 | 
				
			||||||
            liste_id.append(elem.identifiant)
 | 
					            liste_id.append(elem.identifier)
 | 
				
			||||||
        if len(liste_id) > len(set(liste_id)):
 | 
					        if len(liste_id) > len(set(liste_id)):
 | 
				
			||||||
            verification_id  = False
 | 
					            verification_id  = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertTrue(verification_id)
 | 
					        self.assertTrue(verification_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_Arbre_fusion(self):
 | 
					    def test_Tree_fusion(self):
 | 
				
			||||||
        # fusion des arbres
 | 
					        # fusion des trees
 | 
				
			||||||
        # on doit retrouver les éléments des 2 arbres + une nouvelle racine
 | 
					        # on doit retrouver les éléments des 2 trees + une nouvelle root
 | 
				
			||||||
        # les identifiants doivent toujours être uniques
 | 
					        # les identifiers doivent toujours être uniques
 | 
				
			||||||
        a = Sommets(10, 'a')
 | 
					        a = Node(10, 'a')
 | 
				
			||||||
        b = Sommets(8,'b')
 | 
					        b = Node(8,'b')
 | 
				
			||||||
        r1 = Sommets(18,'',left=b,right=a)
 | 
					        r1 = Node(18,'',left=b,right=a)
 | 
				
			||||||
        c = Sommets(15, 'c')
 | 
					        c = Node(15, 'c')
 | 
				
			||||||
        d = Sommets(20,'d')
 | 
					        d = Node(20,'d')
 | 
				
			||||||
        r2 = Sommets(18,'',left=d,right=c)
 | 
					        r2 = Node(18,'',left=d,right=c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        arbre1 = Arbre(r1)
 | 
					        tree1 = Tree(r1)
 | 
				
			||||||
        arbre2 = Arbre(r2)
 | 
					        tree2 = Tree(r2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        l1 = len(arbre1.sommets)
 | 
					        l1 = len(tree1.nodes)
 | 
				
			||||||
        l2 = len(arbre2.sommets)
 | 
					        l2 = len(tree2.nodes)
 | 
				
			||||||
        arbre1 += arbre2
 | 
					        tree1 += tree2
 | 
				
			||||||
        verification_fusion = True
 | 
					        verification_fusion = True
 | 
				
			||||||
        if l1+l2+1 != len(arbre1.sommets):
 | 
					        if l1+l2+1 != len(tree1.nodes):
 | 
				
			||||||
            verification_fusion = False
 | 
					            verification_fusion = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        liste_id2 = []
 | 
					        liste_id2 = []
 | 
				
			||||||
        verification_id2 = True
 | 
					        verification_id2 = True
 | 
				
			||||||
        for elem in arbre1.sommets:
 | 
					        for elem in tree1.nodes:
 | 
				
			||||||
            liste_id2.append(elem.identifiant)
 | 
					            liste_id2.append(elem.identifier)
 | 
				
			||||||
        if len(liste_id2) > len(set(liste_id2)):
 | 
					        if len(liste_id2) > len(set(liste_id2)):
 | 
				
			||||||
            verification_id2  = False
 | 
					            verification_id2  = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertTrue(verification_fusion)
 | 
					        self.assertTrue(verification_fusion)
 | 
				
			||||||
        self.assertTrue(verification_id2)
 | 
					        self.assertTrue(verification_id2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_Arbre_recherche(self):
 | 
					    def test_Tree_seek(self):
 | 
				
			||||||
        # recherche de sommet
 | 
					        # seek de node
 | 
				
			||||||
        a = Sommets(10, 'a')
 | 
					        a = Node(10, 'a')
 | 
				
			||||||
        b = Sommets(8,'b')
 | 
					        b = Node(8,'b')
 | 
				
			||||||
        r1 = Sommets(18,'',left=b,right=a)
 | 
					        r1 = Node(18,'',left=b,right=a)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        arbre1 = Arbre(r1)
 | 
					        tree1 = Tree(r1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assertEqual(arbre1.recherche(a),a)
 | 
					        self.assertEqual(tree1.seek(a),a)
 | 
				
			||||||
        self.assertNotEqual(arbre1.recherche(b),a)
 | 
					        self.assertNotEqual(tree1.seek(b),a)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_Arbre_suppression(self):
 | 
					    def test_Tree_suppression(self):
 | 
				
			||||||
        a = Sommets(10, 'a')
 | 
					        a = Node(10, 'a')
 | 
				
			||||||
        b = Sommets(8,'b')
 | 
					        b = Node(8,'b')
 | 
				
			||||||
        r1 = Sommets(18,'',left=b,right=a)
 | 
					        r1 = Node(18,'',left=b,right=a)
 | 
				
			||||||
        r2 = Sommets(18,'',left=None,right=r1)
 | 
					        r2 = Node(18,'',left=None,right=r1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        arbre1 = Arbre(r2)
 | 
					        tree1 = Tree(r2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        arbre1 -= r1
 | 
					        tree1 -= r1
 | 
				
			||||||
        self.assertEqual(arbre1.recherche(a),None)
 | 
					        self.assertEqual(tree1.seek(a),None)
 | 
				
			||||||
        self.assertEqual(arbre1.recherche(b),None)
 | 
					        self.assertEqual(tree1.seek(b),None)
 | 
				
			||||||
        self.assertEqual(arbre1.recherche(r1),None)
 | 
					        self.assertEqual(tree1.seek(r1),None)
 | 
				
			||||||
        self.assertEqual(arbre1.recherche(r2),r2)
 | 
					        self.assertEqual(tree1.seek(r2),r2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_occurences(self):
 | 
					    def test_occurences(self):
 | 
				
			||||||
        o1 = calcul_occurence('aaabcc')
 | 
					        o1 = calcul_occurence('aaabcc')
 | 
				
			||||||
@@ -114,27 +114,27 @@ class TestUtils(unittest.TestCase):
 | 
				
			|||||||
    def test_encodage_huffman(self):
 | 
					    def test_encodage_huffman(self):
 | 
				
			||||||
        string = 'mouton'
 | 
					        string = 'mouton'
 | 
				
			||||||
        string2 = 'vache'
 | 
					        string2 = 'vache'
 | 
				
			||||||
        (encodedOutput, racine, huffmanEncoding) = huffman_encode(string)
 | 
					        (encodedOutput, root, huffmanEncoding) = huffman_encode(string)
 | 
				
			||||||
        (encodedOutput2, racine2, huffmanEncoding2) = huffman_encode(string2)
 | 
					        (encodedOutput2, root2, huffmanEncoding2) = huffman_encode(string2)
 | 
				
			||||||
        # l'encodage doit être différent (les dictionnaires et arbres aussi)
 | 
					        # l'encodage doit être différent (les dictionnaires et trees aussi)
 | 
				
			||||||
        self.assertNotEqual(huffmanEncoding, huffmanEncoding2)
 | 
					        self.assertNotEqual(huffmanEncoding, huffmanEncoding2)
 | 
				
			||||||
        self.assertNotEqual(encodedOutput, encodedOutput2)
 | 
					        self.assertNotEqual(encodedOutput, encodedOutput2)
 | 
				
			||||||
        self.assertNotEqual(Arbre(racine),Arbre(racine2))
 | 
					        self.assertNotEqual(Tree(root),Tree(root2))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_decodage_huffman(self):
 | 
					    def test_decodage_huffman(self):
 | 
				
			||||||
        string = 'chèvre'
 | 
					        string = 'chèvre'
 | 
				
			||||||
        (encodedOutput, racine, huffmanEncoding) = huffman_encode(string)
 | 
					        (encodedOutput, root, huffmanEncoding) = huffman_encode(string)
 | 
				
			||||||
        # on doit être capable de décoder avec la racine de l'arbre ou avec le dictionnaire
 | 
					        # on doit être capable de décoder avec la root de l'tree ou avec le dictionnaire
 | 
				
			||||||
        self.assertEqual(string,huffman_decode(encodedOutput,racine))
 | 
					        self.assertEqual(string,huffman_decode(encodedOutput,root))
 | 
				
			||||||
        self.assertEqual(string,decode_from_dico(encodedOutput,huffmanEncoding))
 | 
					        self.assertEqual(string,decode_from_dict(encodedOutput,huffmanEncoding))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # on doit être capable de détecter si le dictionnaire/arbre n'est  pas celui correspondant à un texte encodé
 | 
					        # on doit être capable de détecter si le dictionnaire/tree n'est  pas celui correspondant à un texte encodé
 | 
				
			||||||
        string2 = 'poule'
 | 
					        string2 = 'poule'
 | 
				
			||||||
        (encodedOutput2, racine2, huffmanEncoding2) = huffman_encode(string2)
 | 
					        (encodedOutput2, root2, huffmanEncoding2) = huffman_encode(string2)
 | 
				
			||||||
        with self.assertRaises(ValueError):
 | 
					        with self.assertRaises(ValueError):
 | 
				
			||||||
            decode_from_dico(encodedOutput2,huffmanEncoding)
 | 
					            decode_from_dict(encodedOutput2,huffmanEncoding)
 | 
				
			||||||
        with self.assertRaises(ValueError):
 | 
					        with self.assertRaises(ValueError):
 | 
				
			||||||
            huffman_decode(encodedOutput2,racine)
 | 
					            huffman_decode(encodedOutput2,root)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user