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

78 lines
2.3 KiB
C++
Raw Normal View History

2024-01-02 16:14:23 +00:00
#include "Bits.h"
2022-11-23 15:41:33 +00:00
#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
{
2023-12-21 09:18:44 +00:00
Vector<String> bytes{
2022-11-28 10:16:04 +00:00
"11101101",
"01011101",
"00001001",
"01111111"};
2022-11-23 15:41:33 +00:00
BufferBitStream stream;
for(const auto& byte : bytes)
{
2024-01-02 16:14:23 +00:00
stream.writeByte(Bits::getFromString(byte));
2022-11-23 15:41:33 +00:00
}
unsigned char buffer{0} ;
2022-11-28 10:16:04 +00:00
auto valid = stream.readNextNBits(1, buffer);
2024-01-02 16:14:23 +00:00
//std::cout << "Slice0 is " << Bits::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);
2024-01-02 16:14:23 +00:00
//std::cout << "Slice1 is " << Bits::toString(buffer) << std::endl;
2022-11-23 15:41:33 +00:00
valid = stream.readNextNBits(5, buffer);
2024-01-02 16:14:23 +00:00
//std::cout << "Slice2 is " << Bits::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);
2024-01-02 16:14:23 +00:00
//std::cout << "Slice3 is " << Bits::toString(buffer) << std::endl;
2022-11-28 10:16:04 +00:00
valid = stream.readNextNBits(4, buffer);
2024-01-02 16:14:23 +00:00
//std::cout << "Slice3 is " << Bits::toString(buffer) << " and int " << static_cast<int>(buffer) << std::endl;
2022-11-28 10:16:04 +00:00
valid = stream.readNextNBits(3, buffer);
2024-01-02 16:14:23 +00:00
//std::cout << "Slice3 is " << Bits::toString(buffer) << std::endl;
2023-01-23 11:06:30 +00:00
REQUIRE(valid);
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;
2024-01-02 16:14:23 +00:00
stream.writeByte(Bits::getFromString("01100000"));
2022-11-28 10:16:04 +00:00
2024-01-02 16:14:23 +00:00
auto bits0 = Bits::getFromString("00000111");
2022-11-28 10:16:04 +00:00
stream.writeNBits(bits0, 3);
2024-01-02 16:14:23 +00:00
stream.writeByte(Bits::getFromString("11110000"));
2022-11-28 10:16:04 +00:00
2024-01-02 16:14:23 +00:00
auto bits1 = Bits::getFromString("01001101");
2022-11-28 10:16:04 +00:00
stream.writeNBits(bits1, 7);
2024-01-02 16:14:23 +00:00
stream.writeByte(Bits::getFromString("11110000"));
2022-11-28 10:16:04 +00:00
2024-01-02 16:14:23 +00:00
auto bits2 = Bits::getFromString("00000001");
2022-11-28 10:16:04 +00:00
stream.writeNBits(bits2, 1);
stream.flushRemainingBits();
stream.resetOffsets();
2024-01-02 16:14:23 +00:00
auto byte0 = Bits::toString(*stream.readNextByte());
auto byte1 = Bits::toString(*stream.readNextByte());
auto byte2 = Bits::toString(*stream.readNextByte());
auto byte3 = Bits::toString(*stream.readNextByte());
auto byte4 = Bits::toString(*stream.readNextByte());
2022-11-28 10:16:04 +00:00
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
}