From ec045869d02d033f2614e7162b100bae9b2580ba Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 30 Nov 2018 12:33:09 +0100 Subject: add huffman decoder --- tests/thuffmandecoder.nim | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/thuffmandecoder.nim (limited to 'tests') diff --git a/tests/thuffmandecoder.nim b/tests/thuffmandecoder.nim new file mode 100644 index 0000000..d4d81b4 --- /dev/null +++ b/tests/thuffmandecoder.nim @@ -0,0 +1,43 @@ +# gzip-like LZSS compressor +# Copyright (C) 2018 Pacien TRAN-GIRARD +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import unittest, streams +import bitreader, bitwriter +import huffmantree, huffmandecoder + +suite "huffdecoder": + let tree = huffmanBranch( + huffmanLeaf(1'u), + huffmanBranch( + huffmanLeaf(2'u), + huffmanLeaf(3'u))) + + test "decode": + let stream = newStringStream() + defer: stream.close() + let bitWriter = stream.bitWriter() + bitWriter.writeBool(true) # 2 + bitWriter.writeBool(false) + bitWriter.writeBool(false) # 1 + bitWriter.writeBool(true) # 3 + bitWriter.writeBool(true) + bitWriter.flush() + stream.setPosition(0) + let bitReader = stream.bitReader() + let decoder = tree.decoder() + check decoder.decode(bitReader) == 2'u + check decoder.decode(bitReader) == 1'u + check decoder.decode(bitReader) == 3'u -- cgit v1.2.3