huffman-py/test_huffman.py

142 lines
4.7 KiB
Python
Raw Normal View History

2023-05-25 00:38:48 +02:00
#
# Sam Hadow - Huffman-py
# Copyright (C) 2023
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import unittest
2023-05-25 02:03:08 +02:00
from huffman_py.Node import *
from huffman_py.Tree import *
from huffman_py.functions.encode import *
from huffman_py.functions.decode import *
from huffman_py.functions.occurence import *
2023-05-25 00:38:48 +02:00
# pour lancer les tests utilisez:
# python -m unittest discover
class TestUtils(unittest.TestCase):
2023-05-25 02:03:08 +02:00
def test_Tree_id(self):
a = Node(10, 'a')
b = Node(8,'b')
r1 = Node(18,'',left=b,right=a)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
tree1 = Tree(r1)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
# vérification affectation d'un identifier unique en créant l'tree
2023-05-25 00:38:48 +02:00
liste_id = []
verification_id = True
2023-05-25 02:03:08 +02:00
for elem in tree1.nodes:
liste_id.append(elem.identifier)
2023-05-25 00:38:48 +02:00
if len(liste_id) > len(set(liste_id)):
verification_id = False
self.assertTrue(verification_id)
2023-05-25 02:03:08 +02:00
def test_Tree_fusion(self):
# fusion des trees
# on doit retrouver les éléments des 2 trees + une nouvelle root
# les identifiers doivent toujours être uniques
a = Node(10, 'a')
b = Node(8,'b')
r1 = Node(18,'',left=b,right=a)
c = Node(15, 'c')
d = Node(20,'d')
r2 = Node(18,'',left=d,right=c)
tree1 = Tree(r1)
tree2 = Tree(r2)
l1 = len(tree1.nodes)
l2 = len(tree2.nodes)
tree1 += tree2
2023-05-25 00:38:48 +02:00
verification_fusion = True
2023-05-25 02:03:08 +02:00
if l1+l2+1 != len(tree1.nodes):
2023-05-25 00:38:48 +02:00
verification_fusion = False
liste_id2 = []
verification_id2 = True
2023-05-25 02:03:08 +02:00
for elem in tree1.nodes:
liste_id2.append(elem.identifier)
2023-05-25 00:38:48 +02:00
if len(liste_id2) > len(set(liste_id2)):
verification_id2 = False
self.assertTrue(verification_fusion)
self.assertTrue(verification_id2)
2023-05-25 02:03:08 +02:00
def test_Tree_seek(self):
# seek de node
a = Node(10, 'a')
b = Node(8,'b')
r1 = Node(18,'',left=b,right=a)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
tree1 = Tree(r1)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
self.assertEqual(tree1.seek(a),a)
self.assertNotEqual(tree1.seek(b),a)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
def test_Tree_suppression(self):
a = Node(10, 'a')
b = Node(8,'b')
r1 = Node(18,'',left=b,right=a)
r2 = Node(18,'',left=None,right=r1)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
tree1 = Tree(r2)
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
tree1 -= r1
self.assertEqual(tree1.seek(a),None)
self.assertEqual(tree1.seek(b),None)
self.assertEqual(tree1.seek(r1),None)
self.assertEqual(tree1.seek(r2),r2)
2023-05-25 00:38:48 +02:00
def test_occurences(self):
o1 = calcul_occurence('aaabcc')
o2 = calcul_occurence('bacaac')
# dans les 2 cas on doit avoir le même dictionnaire
# 3 pour a, 2 pour c, 1 pour b
self.assertEqual(o1,o2)
self.assertEqual(o1['c'],2)
self.assertEqual(o1['b'],1)
self.assertEqual(o1['a'],3)
def test_encodage_huffman(self):
string = 'mouton'
string2 = 'vache'
2023-05-25 02:03:08 +02:00
(encodedOutput, root, huffmanEncoding) = huffman_encode(string)
(encodedOutput2, root2, huffmanEncoding2) = huffman_encode(string2)
# l'encodage doit être différent (les dictionnaires et trees aussi)
2023-05-25 00:38:48 +02:00
self.assertNotEqual(huffmanEncoding, huffmanEncoding2)
self.assertNotEqual(encodedOutput, encodedOutput2)
2023-05-25 02:03:08 +02:00
self.assertNotEqual(Tree(root),Tree(root2))
2023-05-25 00:38:48 +02:00
def test_decodage_huffman(self):
string = 'chèvre'
2023-05-25 02:03:08 +02:00
(encodedOutput, root, huffmanEncoding) = huffman_encode(string)
# on doit être capable de décoder avec la root de l'tree ou avec le dictionnaire
self.assertEqual(string,huffman_decode(encodedOutput,root))
self.assertEqual(string,decode_from_dict(encodedOutput,huffmanEncoding))
2023-05-25 00:38:48 +02:00
2023-05-25 02:03:08 +02:00
# on doit être capable de détecter si le dictionnaire/tree n'est pas celui correspondant à un texte encodé
2023-05-25 00:38:48 +02:00
string2 = 'poule'
2023-05-25 02:03:08 +02:00
(encodedOutput2, root2, huffmanEncoding2) = huffman_encode(string2)
2023-05-25 00:38:48 +02:00
with self.assertRaises(ValueError):
2023-05-25 02:03:08 +02:00
decode_from_dict(encodedOutput2,huffmanEncoding)
2023-05-25 00:38:48 +02:00
with self.assertRaises(ValueError):
2023-05-25 02:03:08 +02:00
huffman_decode(encodedOutput2,root)
2023-05-25 00:38:48 +02:00
if __name__ == '__main__':
unittest.main()