stuff-from-scratch/test/core/TestBitStream.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

2022-11-23 15:41:33 +00:00
#include "ByteUtils.h"
#include "BufferBitStream.h"
2022-11-29 18:00:19 +00:00
#include "TestFramework.h"
2022-11-23 15:41:33 +00:00
#include <iostream>
2022-11-29 18:00:19 +00:00
TEST_CASE(TestReadBitStream, "core")
2022-11-23 15:41:33 +00:00
{
2022-11-28 10:16:04 +00:00
std::vector<std::string> bytes{
"11101101",
"01011101",
"00001001",
"01111111"};
2022-11-23 15:41:33 +00:00
BufferBitStream stream;
for(const auto& byte : bytes)
{
stream.writeByte(ByteUtils::getFromString(byte));
}
unsigned char buffer{0} ;
2022-11-28 10:16:04 +00:00
auto valid = stream.readNextNBits(1, buffer);
2022-11-30 18:28:50 +00:00
//std::cout << "Slice0 is " << ByteUtils::toString(buffer) << std::endl;
2022-11-23 15:41:33 +00:00
2022-11-28 10:16:04 +00:00
valid = stream.readNextNBits(2, buffer);
2022-11-30 18:28:50 +00:00
//std::cout << "Slice1 is " << ByteUtils::toString(buffer) << std::endl;
2022-11-23 15:41:33 +00:00
valid = stream.readNextNBits(5, buffer);
2022-11-30 18:28:50 +00:00
//std::cout << "Slice2 is " << ByteUtils::toString(buffer) << std::endl;
2022-11-23 15:41:33 +00:00
2022-11-28 10:16:04 +00:00
valid = stream.readNextNBits(5, buffer);
2022-11-30 18:28:50 +00:00
//std::cout << "Slice3 is " << ByteUtils::toString(buffer) << std::endl;
2022-11-28 10:16:04 +00:00
valid = stream.readNextNBits(4, buffer);
2022-11-30 18:28:50 +00:00
//std::cout << "Slice3 is " << ByteUtils::toString(buffer) << " and int " << static_cast<int>(buffer) << std::endl;
2022-11-28 10:16:04 +00:00
valid = stream.readNextNBits(3, buffer);
2022-11-30 18:28:50 +00:00
//std::cout << "Slice3 is " << ByteUtils::toString(buffer) << std::endl;
2022-11-28 10:16:04 +00:00
}
2022-11-29 18:00:19 +00:00
TEST_CASE(TestWritingBitStream, "core")
2022-11-28 10:16:04 +00:00
{
BufferBitStream stream;
stream.writeByte(ByteUtils::getFromString("01100000"));
auto bits0 = ByteUtils::getFromString("00000111");
stream.writeNBits(bits0, 3);
stream.writeByte(ByteUtils::getFromString("11110000"));
auto bits1 = ByteUtils::getFromString("01001101");
stream.writeNBits(bits1, 7);
stream.writeByte(ByteUtils::getFromString("11110000"));
auto bits2 = ByteUtils::getFromString("00000001");
stream.writeNBits(bits2, 1);
stream.flushRemainingBits();
stream.resetOffsets();
auto byte0 = ByteUtils::toString(*stream.readNextByte());
auto byte1 = ByteUtils::toString(*stream.readNextByte());
auto byte2 = ByteUtils::toString(*stream.readNextByte());
auto byte3 = ByteUtils::toString(*stream.readNextByte());
auto byte4 = ByteUtils::toString(*stream.readNextByte());
2022-11-30 18:28:50 +00:00
//std::cout << "Got bytes 0 " << byte0 << std::endl;
//std::cout << "Got bytes 1 " << byte1 << std::endl;
//std::cout << "Got bytes 2 " << byte2 << std::endl;
//std::cout << "Got bytes 3 " << byte3 << std::endl;
//std::cout << "Got bytes 4 " << byte4 << std::endl;
2022-11-28 10:16:04 +00:00
}