Skip to content

Commit

Permalink
add missing forward declaration for packing/unpacking dynamic_bitset;…
Browse files Browse the repository at this point in the history
… add tests
  • Loading branch information
linh2931 committed Jan 2, 2024
1 parent 42f2cc4 commit b7c5028
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libraries/libfc/include/fc/io/raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace fc {
template<typename Stream> void unpack( Stream& s, Int<256>& n );
template<typename Stream, typename T> void pack( Stream& s, const boost::multiprecision::number<T>& n );
template<typename Stream, typename T> void unpack( Stream& s, boost::multiprecision::number<T>& n );
template<typename Stream, typename T> void pack( Stream& s, const boost::dynamic_bitset<T>& bs );
template<typename Stream, typename T> void unpack( Stream& s, boost::dynamic_bitset<T>& bs );

template<typename Stream, typename Arg0, typename... Args>
inline void pack( Stream& s, const Arg0& a0, const Args&... args ) {
Expand Down
1 change: 1 addition & 0 deletions libraries/libfc/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_executable( test_fc
crypto/test_webauthn.cpp
io/test_cfile.cpp
io/test_json.cpp
io/test_raw.cpp
io/test_tracked_storage.cpp
network/test_message_buffer.cpp
scoped_exit/test_scoped_exit.cpp
Expand Down
38 changes: 38 additions & 0 deletions libraries/libfc/test/io/test_raw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <fc/exception/exception.hpp>
#include <fc/io/raw.hpp>

#include <boost/test/unit_test.hpp>
#include <boost/dynamic_bitset.hpp>

using namespace fc;

BOOST_AUTO_TEST_SUITE(raw_test_suite)


BOOST_AUTO_TEST_CASE(dynamic_bitset_test)
{
constexpr uint8_t bits = 0b00011110;
boost::dynamic_bitset<uint8_t> bs1(8, bits); // bit set size 8

char buff[4];
datastream<char*> ds(buff, sizeof(buff));

fc::raw::pack( ds, bs1 );

boost::dynamic_bitset<uint8_t> bs2(8);
ds.seekp(0);
fc::raw::unpack( ds, bs2 );

// 0b00011110
BOOST_CHECK(!bs2.test(0));
BOOST_CHECK(bs2.test(1));
BOOST_CHECK(bs2.test(2));
BOOST_CHECK(bs2.test(2));
BOOST_CHECK(bs2.test(3));
BOOST_CHECK(bs2.test(4));
BOOST_CHECK(!bs2.test(5));
BOOST_CHECK(!bs2.test(6));
BOOST_CHECK(!bs2.test(7));
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit b7c5028

Please sign in to comment.