view TestBitDecoder.cpp @ 44:7348d4efa4f6

More testing for BitDecoder.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Mon, 10 Sep 2012 19:18:30 +0200
parents bf3dce7fedcb
children 41cc0d8ac77f
line wrap: on
line source

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

#include "Exception/InvalidDataException.hpp"


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");
}

BOOST_AUTO_TEST_CASE( TestLong )
{
  BitDecoder* up = new BitDecoder("b");
  BitDecoder* down = new BitDecoder("a");
  for (int i = 0; i < 12; ++i) {
    down = BitDecoder::merge(down, up);
    up = new BitDecoder(QString('c' + i));
  }

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

  BOOST_REQUIRE(full->data().isNull());
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1")), "n");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("01")), "m");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("001")), "l");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0001")), "k");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("00001")), "j");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("000001")), "i");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000001")), "h");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("00000001")), "g");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("000000001")), "f");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000000001")), "e");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("00000000001")), "d");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("000000000001")), "c");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000000000001")), "b");
  BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000000000000")), "a");

}