Commit df1c22fa authored by nagayama15's avatar nagayama15

add: Add unit test

parent e6723dd1
...@@ -25,3 +25,4 @@ include_directories( ...@@ -25,3 +25,4 @@ include_directories(
) )
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(test)
...@@ -11,6 +11,6 @@ add_executable(snpi ...@@ -11,6 +11,6 @@ add_executable(snpi
) )
target_link_libraries(snpi target_link_libraries(snpi
binaryen::binaryen
fmt::fmt fmt::fmt
binaryen::binaryen
) )
...@@ -40,9 +40,9 @@ namespace kyut { ...@@ -40,9 +40,9 @@ namespace kyut {
} }
const auto pos = pos_read_; const auto pos = pos_read_;
pos_read_ = (pos_read_ + 1) % data_.size(); pos_read_ = (pos_read_ + 1) % (data_.size() * 8);
return (data_[pos / 8] >> (pos % 8)) & 1; return ((data_[pos / 8] << (pos % 8)) & 0x80) != 0;
} }
std::uint64_t read(std::size_t countBits) { std::uint64_t read(std::size_t countBits) {
...@@ -50,7 +50,7 @@ namespace kyut { ...@@ -50,7 +50,7 @@ namespace kyut {
for (std::size_t i = 0; i < countBits; i++) { for (std::size_t i = 0; i < countBits; i++) {
value <<= 1; value <<= 1;
value |= readBit(); value |= readBit() ? 1 : 0;
} }
return value; return value;
......
add_executable(test_kyut
test_kyut.cpp
test_BitStreamReader.cpp
)
target_link_libraries(test_kyut
fmt::fmt
binaryen::binaryen
)
#include <kyut/BitStreamReader.hpp>
#include <boost/test/unit_test.hpp>
namespace {
constexpr std::size_t operator""_zu(unsigned long long x) noexcept {
return static_cast<std::size_t>(x);
}
constexpr std::uint64_t operator""_u64(unsigned long long x) noexcept {
return static_cast<std::uint64_t>(x);
}
} // namespace
BOOST_AUTO_TEST_SUITE(kyut)
BOOST_AUTO_TEST_SUITE(bit_stream_reader)
BOOST_AUTO_TEST_CASE(read_bit) {
constexpr std::uint8_t data[1] = {0x89};
BitStreamReader reader{std::begin(data), std::end(data)};
// 0x89 == 0b1000'1001
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 1_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 2_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 3_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 4_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 5_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 6_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 7_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), true);
BOOST_REQUIRE_EQUAL(reader.tell(), 1_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 2_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 3_zu);
BOOST_REQUIRE_EQUAL(reader.readBit(), false);
BOOST_REQUIRE_EQUAL(reader.tell(), 4_zu);
}
BOOST_AUTO_TEST_CASE(read) {
constexpr std::uint8_t data[4] = {0x89, 0xab, 0xcd, 0xef};
BitStreamReader reader{std::begin(data), std::end(data)};
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu);
BOOST_REQUIRE_EQUAL(reader.read(4), 0x8_u64);
BOOST_REQUIRE_EQUAL(reader.tell(), 4_zu);
BOOST_REQUIRE_EQUAL(reader.read(4), 0x9_u64);
BOOST_REQUIRE_EQUAL(reader.tell(), 8_zu);
BOOST_REQUIRE_EQUAL(reader.read(8), 0xab_u64);
BOOST_REQUIRE_EQUAL(reader.tell(), 16_zu);
BOOST_REQUIRE_EQUAL(reader.read(16), 0xcdef_u64);
BOOST_REQUIRE_EQUAL(reader.tell(), 0_zu);
BOOST_REQUIRE_EQUAL(reader.read(24), 0x89abcd_u64);
BOOST_REQUIRE_EQUAL(reader.tell(), 24_zu);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_TEST_MAIN // Define main function in Boost::UnitTest
#include <boost/test/included/unit_test.hpp>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment