aboutsummaryrefslogtreecommitdiff
path: root/tests/huffman/thuffmandecoder.nim
blob: 44fad641302797317c8b158bf7cef4be14011249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 <https://www.gnu.org/licenses/>.

import unittest, streams
import bitio/bitreader, bitio/bitwriter
import huffman/huffmantree, huffman/huffmandecoder

suite "huffmandecoder":
  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