Skip to content

Commit

Permalink
[test] Add test for bzip2 RUNA, RUNB decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmlnkn committed Feb 11, 2024
1 parent ed03ab1 commit f0e5d89
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/tests/indexed_bzip2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ target_sources(testBZ2Reader PRIVATE ${CMAKE_CURRENT_LIST_DIR}/testBZ2Reader.cpp
target_link_libraries(testBZ2Reader PRIVATE indexed_bzip2_parallel)
add_test(NAME Test-BZ2-Reader COMMAND testBZ2Reader)
add_dependencies(all_tests testBZ2Reader)

add_executable(testRunAB)
target_sources(testRunAB PRIVATE ${CMAKE_CURRENT_LIST_DIR}/testRunAB.cpp)
target_link_libraries(testRunAB PRIVATE core)
add_test(NAME Test-Run-AB COMMAND testRunAB)
add_dependencies(all_tests testRunAB)
134 changes: 134 additions & 0 deletions src/tests/indexed_bzip2/testRunAB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <iostream>
#include <stdexcept>
#include <string_view>
#include <vector>

#include <BitManipulation.hpp>
#include <common.hpp>
#include <TestHelpers.hpp>


const std::vector<std::string_view> ENCODE_TABLE = {
"",
"A",
"B",
"AA",
"BA",
"AB",
"BB",
"AAA",
"BAA",
"ABA",
"BBA",
"AAB",
"BAB",
"ABB",
"BBB",
"AAAA",
"BAAA",
"ABAA",
"BBAA",
"AABA",
"BABA",
"ABBA",
"BBBA",
"AAAB",
"BAAB",
"ABAB",
"BBAB",
"AABB",
"BABB",
"ABBB",
"BBBB",
"AAAAA",
"BAAAA",
"ABAAA",
"BBAAA",
"AABAA",
"BABAA",
"ABBAA",
"BBBAA",
"AAABA",
"BAABA",
"ABABA",
"BBABA",
"AABBA",
"BABBA",
"ABBBA",
"BBBBA",
"AAAAB",
"BAAAB",
"ABAAB",
"BBAAB",
"AABAB",
"BABAB",
"ABBAB",
"BBBAB",
"AAABB",
"BAABB",
"ABABB",
"BBABB",
"AABBB",
"BABBB",
"ABBBB",
"BBBBB",
"AAAAAA",
};


class RunDecoder
{
public:
void
merge( char c )
{
switch ( c )
{
case 'A':
case 'B':
m_bits |= ( c - 'A' ) << m_bitCount;
++m_bitCount;
break;
default:
throw std::logic_error( "Only A or B allowed!" );
}
}

[[nodiscard]] uint32_t
value() const
{
//return ( ( 1U << m_bitCount ) | reverseBits( m_bits, m_bitCount ) ) - 1U;
return ( ( 1U << m_bitCount ) | m_bits ) - 1U;
}

private:
uint8_t m_bitCount{ 0 };
uint32_t m_bits{ 0 };
};


void
testAB()
{
for ( size_t length = 1; length < ENCODE_TABLE.size(); ++length ) {
const auto& sequence = ENCODE_TABLE[length];
RunDecoder decoder;
for ( const auto c : sequence ) {
decoder.merge( c );
}
std::cerr << "Decode: " << sequence << ", which should be " << length << ", and got: "
<< decoder.value() << "\n";
REQUIRE( decoder.value() == length );
}
}


int
main()
{
testAB();

std::cout << "Tests successful: " << ( gnTests - gnTestErrors ) << " / " << gnTests << "\n";

return gnTestErrors;
}

0 comments on commit f0e5d89

Please sign in to comment.