view TestBitDecoder.cpp @ 20:754e12c927b3

Class for decoding binary datarepresentations, eg. Huffman trees.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Wed, 05 Sep 2012 21:54:18 +0200
parents
children b2c2c2bf2bbd
line wrap: on
line source

#include "BitDecoder.hpp"
#include "TestFramework.hpp"

#include "InvalidDataException.hpp"
#include <QtCore/QDebug>

QBitArray bitsFromString(const QString& str)
{
  QBitArray bits(str.size());
  for (int i = 0; i < str.size(); ++i) {
    if (str[i] == '1')
      bits[i] = true;
    else if (str[i] == '0')
      bits[i] = false;
    else
      throw InvalidDataException();
  }
  return bits;
}

BOOST_AUTO_TEST_CASE( TestSimple )
{
  BitDecoder* up = new BitDecoder("a");
  BitDecoder* down = new BitDecoder("b");

  BitDecoder* full = BitDecoder::merge(down, up);

  BOOST_REQUIRE(full->data().isNull());
  BOOST_REQUIRE_EQUAL(up->data(), "a");
  BOOST_REQUIRE_EQUAL(down->data(), "b");

  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1")), "a");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0")), "b");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1111")), "aaaa");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000")), "bbbb");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1101")), "aaba");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1111")), "aaaa");
}