From f86955244a947bfd10b0ef7a5ae6a900fca141e3 Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Mon, 6 May 2024 15:13:24 -0300 Subject: [PATCH] Removed underscore in test --- src/primitives/block_header.hpp | 10 +- .../block_header_repository_test.cpp | 72 +++---- test/src/blockchain/block_storage_test.cpp | 4 +- .../crypto/crypto_store/crypto_store_test.cpp | 135 ++++++------ test/src/storage/rocksdb/rocksdb_fs_test.cpp | 9 +- .../rocksdb/rocksdb_integration_test.cpp | 200 +++++++++--------- .../trie/trie_storage/trie_batch_test.cpp | 40 ++-- test/testutil/storage/base_crdt_test.cpp | 8 +- test/testutil/storage/base_crdt_test.hpp | 5 +- test/testutil/storage/base_fs_test.cpp | 8 +- test/testutil/storage/base_fs_test.hpp | 6 +- test/testutil/storage/base_rocksdb_test.cpp | 39 ++-- test/testutil/storage/base_rocksdb_test.hpp | 17 +- 13 files changed, 281 insertions(+), 272 deletions(-) diff --git a/src/primitives/block_header.hpp b/src/primitives/block_header.hpp index 394319fd..a07dd980 100644 --- a/src/primitives/block_header.hpp +++ b/src/primitives/block_header.hpp @@ -17,11 +17,11 @@ namespace sgns::primitives */ struct BlockHeader { - BlockHash parent_hash{}; ///< 32-byte Blake2s hash of parent header - BlockNumber number = 0u; ///< index of current block in the chain - base::Hash256 state_root{}; ///< root of the Merkle tree - base::Hash256 extrinsics_root{}; ///< field for validation integrity - Digest digest{}; ///< chain-specific auxiliary data + BlockHash parent_hash; ///< 32-byte Blake2s hash of parent header + BlockNumber number = 0U; ///< index of current block in the chain + base::Hash256 state_root; ///< root of the Merkle tree + base::Hash256 extrinsics_root; ///< field for validation integrity + Digest digest; ///< chain-specific auxiliary data bool operator==( const BlockHeader &rhs ) const { diff --git a/test/src/blockchain/block_header_repository_test.cpp b/test/src/blockchain/block_header_repository_test.cpp index fc1ef652..2719fdb7 100644 --- a/test/src/blockchain/block_header_repository_test.cpp +++ b/test/src/blockchain/block_header_repository_test.cpp @@ -28,10 +28,10 @@ using sgns::primitives::BlockHeader; using sgns::primitives::BlockId; using sgns::primitives::BlockNumber; -class BlockHeaderRepository_Test : public test::BaseCRDT_Test +class BlockHeaderRepositoryFixture : public test::CRDTFixture { public: - BlockHeaderRepository_Test() : BaseCRDT_Test( fs::path( "blockheaderrepotest.lvldb" ) ) + BlockHeaderRepositoryFixture() : CRDTFixture( fs::path( "blockheaderrepotest.lvldb" ) ) { } @@ -43,11 +43,11 @@ class BlockHeaderRepository_Test : public test::BaseCRDT_Test header_repo_ = std::make_shared( db_, hasher, db_path_ ); } - outcome::result storeHeader( BlockNumber num, BlockHeader h ) + outcome::result storeHeader( BlockNumber num, BlockHeader &&header ) { - BlockHeader header = std::move( h ); - header.number = num; + header.number = num; OUTCOME_TRY( ( auto &&, enc_header ), sgns::scale::encode( header ) ); + auto hash = hasher->blake2b_256( enc_header ); BOOST_OUTCOME_TRYV2( auto &&, putWithPrefix( *db_, Prefix::HEADER, header.number, hash, Buffer{ enc_header } ) ); @@ -56,20 +56,18 @@ class BlockHeaderRepository_Test : public test::BaseCRDT_Test static BlockHeader getDefaultHeader() { - BlockHeader h{}; - h.number = 42; - h.extrinsics_root = "DEADBEEF"_hash256; - h.parent_hash = "ABCDEF"_hash256; - h.state_root = "010203"_hash256; - return h; + return { .parent_hash = "ABCDEF"_hash256, + .number = 42, + .state_root = "010203"_hash256, + .extrinsics_root = "DEADBEEF"_hash256 }; } std::shared_ptr hasher; std::shared_ptr header_repo_; }; -class BlockHeaderRepository_NumberParametrized_Test : public BlockHeaderRepository_Test, - public testing::WithParamInterface +class BlockHeaderRepositoryNumberParametrizedFixture : public BlockHeaderRepositoryFixture, + public testing::WithParamInterface { }; @@ -80,26 +78,28 @@ const std::vector ParamValues = { 1, 42, 12345, 0, 0xFFFFFFFF }; * @when accessing a header that wasn't put into storage * @then result is error */ -TEST_F( BlockHeaderRepository_Test, UnexistingHeader ) +TEST_F( BlockHeaderRepositoryFixture, UnexistingHeader ) { - //auto chosen_number = ParamValues[0]; - //for(auto& c: ParamValues) { - // if(c != chosen_number) { - // EXPECT_OUTCOME_TRUE_1(storeHeader(c, getDefaultHeader())); - // } - //} - //BlockHeader not_in_storage = getDefaultHeader(); - //not_in_storage.number = chosen_number; - //EXPECT_OUTCOME_TRUE(enc_header, sgns::scale::encode(not_in_storage)); - //auto hash = hasher->blake2b_256(enc_header); - //EXPECT_OUTCOME_FALSE_1(header_repo_->getBlockHeader(chosen_number)); - //EXPECT_OUTCOME_FALSE_1(header_repo_->getBlockHeader(hash)); - //EXPECT_OUTCOME_FALSE_1(header_repo_->getHashById(chosen_number)); - //EXPECT_OUTCOME_FALSE_1(header_repo_->getNumberById(hash)); - // - //// doesn't require access to storage, as it basically returns its argument - //EXPECT_OUTCOME_TRUE_1(header_repo_->getHashById(hash)); - //EXPECT_OUTCOME_TRUE_1(header_repo_->getNumberById(chosen_number)); + auto chosen_number = ParamValues[0]; + for ( auto c : ParamValues ) + { + if ( c != chosen_number ) + { + EXPECT_OUTCOME_TRUE_1( storeHeader( c, getDefaultHeader() ) ); + } + } + BlockHeader not_in_storage = getDefaultHeader(); + not_in_storage.number = chosen_number; + EXPECT_OUTCOME_TRUE( enc_header, sgns::scale::encode( not_in_storage ) ); + auto hash = hasher->blake2b_256( enc_header ); + EXPECT_OUTCOME_FALSE_1( header_repo_->getBlockHeader( chosen_number ) ); + EXPECT_OUTCOME_FALSE_1( header_repo_->getBlockHeader( hash ) ); + EXPECT_OUTCOME_FALSE_1( header_repo_->getHashById( chosen_number ) ); + EXPECT_OUTCOME_FALSE_1( header_repo_->getNumberById( hash ) ); + + // doesn't require access to storage, as it basically returns its argument + EXPECT_OUTCOME_TRUE_1( header_repo_->getHashById( hash ) ); + EXPECT_OUTCOME_TRUE_1( header_repo_->getNumberById( chosen_number ) ); } /** @@ -108,7 +108,7 @@ TEST_F( BlockHeaderRepository_Test, UnexistingHeader ) * @then resulting hash is equal to the original hash of the block for both * retrieval through getHashByNumber and getHashById */ -TEST_P( BlockHeaderRepository_NumberParametrized_Test, GetHashByNumber ) +TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHashByNumber ) { //EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader())); //EXPECT_OUTCOME_TRUE(maybe_hash, header_repo_->getHashByNumber(GetParam())); @@ -124,7 +124,7 @@ TEST_P( BlockHeaderRepository_NumberParametrized_Test, GetHashByNumber ) * @then resulting number is equal to the original block number for both * retrieval through getNumberByHash and getNumberById */ -TEST_P( BlockHeaderRepository_NumberParametrized_Test, GetNumberByHash ) +TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetNumberByHash ) { //EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader())); //EXPECT_OUTCOME_TRUE(maybe_number, header_repo_->getNumberByHash(hash)); @@ -140,7 +140,7 @@ TEST_P( BlockHeaderRepository_NumberParametrized_Test, GetNumberByHash ) * @then the same header that was put into the storage is returned, regardless * of whether the id contained a number or a hash */ -TEST_P( BlockHeaderRepository_NumberParametrized_Test, GetHeader ) +TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHeader ) { //EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader())); //EXPECT_OUTCOME_TRUE(header_by_num, header_repo_->getBlockHeader(GetParam())); @@ -151,4 +151,4 @@ TEST_P( BlockHeaderRepository_NumberParametrized_Test, GetHeader ) //ASSERT_EQ(header_by_num, header_should_be); } -INSTANTIATE_TEST_SUITE_P( Numbers, BlockHeaderRepository_NumberParametrized_Test, testing::ValuesIn( ParamValues ) ); +INSTANTIATE_TEST_SUITE_P( Numbers, BlockHeaderRepositoryNumberParametrizedFixture, testing::ValuesIn( ParamValues ) ); diff --git a/test/src/blockchain/block_storage_test.cpp b/test/src/blockchain/block_storage_test.cpp index 59064ad9..a813be52 100644 --- a/test/src/blockchain/block_storage_test.cpp +++ b/test/src/blockchain/block_storage_test.cpp @@ -28,10 +28,10 @@ using sgns::storage::face::GenericStorageMock; using testing::_; using testing::Return; -class BlockStorageTest : public test::BaseCRDT_Test +class BlockStorageTest : public test::CRDTFixture { public: - BlockStorageTest() : BaseCRDT_Test( fs::path( "blockstoragetest.lvldb" ) ) + BlockStorageTest() : CRDTFixture( fs::path( "blockstoragetest.lvldb" ) ) { } diff --git a/test/src/crypto/crypto_store/crypto_store_test.cpp b/test/src/crypto/crypto_store/crypto_store_test.cpp index 7bc5f9f8..499bf679 100644 --- a/test/src/crypto/crypto_store/crypto_store_test.cpp +++ b/test/src/crypto/crypto_store/crypto_store_test.cpp @@ -41,78 +41,69 @@ using namespace sgns::crypto::key_types; static CryptoStoreImpl::Path crypto_store_test_directory = boost::filesystem::temp_directory_path() / "crypto_store_test"; -struct CryptoStoreTest : public test::BaseFS_Test { - CryptoStoreTest() : BaseFS_Test(crypto_store_test_directory) {} - - void SetUp() override { - auto ed25519_provider = std::make_shared(); - auto csprng = std::make_shared(); - auto sr25519_provider = std::make_shared(csprng); - auto secp256k1_provider = std::make_shared(); - - auto pbkdf2_provider = std::make_shared(); - bip39_provider = - std::make_shared(std::move(pbkdf2_provider)); - crypto_store = - std::make_shared(std::move(ed25519_provider), - std::move(sr25519_provider), - std::move(secp256k1_provider), - bip39_provider, - std::move(csprng)); - - mnemonic = - "ozone drill grab fiber curtain grace pudding thank cruise elder eight " - "picnic"; - EXPECT_OUTCOME_TRUE(e, Buffer::fromHex("9e885d952ad362caeb4efe34a8e91bd2")); - entropy = std::move(e); - EXPECT_OUTCOME_TRUE(s, - Blob<32>::fromHex("a4681403ba5b6a3f3bd0b0604ce439a78244" - "c7d43b127ec35cd8325602dd47fd")); - seed = s; - key_type = kProd; - - EXPECT_OUTCOME_TRUE( - ed_publ, -// ED25519PublicKey::fromHex("3e765f2bde3daadd443097b3145abf1f71f99f0aa946" -// "960990fe02aa26b7fc72")); - ED25519PublicKey::fromHex("3086e3f8cdc1e69f855a1b1907331b7594500c0fc40e" - "91e25e734513df85289f")); - EXPECT_OUTCOME_TRUE( - ed_priv, - ED25519PrivateKey::fromHex("a4681403ba5b6a3f3bd0b0604ce439a78244c7d43b1" - "27ec35cd8325602dd47fd")); - ed_pair = {ed_priv, ed_publ}; - - EXPECT_OUTCOME_TRUE( - sr_publ, - SR25519PublicKey::fromHex("7e68abebffcfe3409af87fbb0b5254b7add6ec3efc1f5b882f3621df45afec15")); - EXPECT_OUTCOME_TRUE( - sr_secr, - SR25519SecretKey::fromHex( - "d0c92c34038c17ea76208b04623203cbd9ce0901e82486da0053c0c2c4488b69" - "19f52f334750e98500ba84fd528a6ff810b91b43d11e3d3eb1c2176c9dda0b57")); - sr_pair = {sr_secr, sr_publ}; - - EXPECT_OUTCOME_TRUE_MSG_1( - crypto_store->initialize(crypto_store_test_directory), - "initialization failed"); - } - - bool isStoredOnDisk(KeyTypeId kt, const Blob<32> &public_key) { - auto file_name = sgns::crypto::decodeKeyTypeId(kt) + public_key.toHex(); - auto file_path = crypto_store_test_directory / file_name; - return boost::filesystem::exists(file_path); - } - - std::shared_ptr bip39_provider; - std::shared_ptr crypto_store; - std::string mnemonic; - Buffer entropy; - Blob<32> seed; - KeyTypeId key_type; - - ED25519Keypair ed_pair; - SR25519Keypair sr_pair; +struct CryptoStoreTest : public test::FSFixture +{ + CryptoStoreTest() : FSFixture( crypto_store_test_directory ) + { + } + + void SetUp() override + { + auto ed25519_provider = std::make_shared(); + auto csprng = std::make_shared(); + auto sr25519_provider = std::make_shared( csprng ); + auto secp256k1_provider = std::make_shared(); + + auto pbkdf2_provider = std::make_shared(); + bip39_provider = std::make_shared( std::move( pbkdf2_provider ) ); + crypto_store = + std::make_shared( std::move( ed25519_provider ), std::move( sr25519_provider ), + std::move( secp256k1_provider ), bip39_provider, std::move( csprng ) ); + + mnemonic = "ozone drill grab fiber curtain grace pudding thank cruise elder eight " + "picnic"; + EXPECT_OUTCOME_TRUE( e, Buffer::fromHex( "9e885d952ad362caeb4efe34a8e91bd2" ) ); + entropy = std::move( e ); + EXPECT_OUTCOME_TRUE( s, Blob<32>::fromHex( "a4681403ba5b6a3f3bd0b0604ce439a78244" + "c7d43b127ec35cd8325602dd47fd" ) ); + seed = s; + key_type = kProd; + + EXPECT_OUTCOME_TRUE( ed_publ, + // ED25519PublicKey::fromHex("3e765f2bde3daadd443097b3145abf1f71f99f0aa946" + // "960990fe02aa26b7fc72")); + ED25519PublicKey::fromHex( "3086e3f8cdc1e69f855a1b1907331b7594500c0fc40e" + "91e25e734513df85289f" ) ); + EXPECT_OUTCOME_TRUE( ed_priv, ED25519PrivateKey::fromHex( "a4681403ba5b6a3f3bd0b0604ce439a78244c7d43b1" + "27ec35cd8325602dd47fd" ) ); + ed_pair = { ed_priv, ed_publ }; + + EXPECT_OUTCOME_TRUE( + sr_publ, SR25519PublicKey::fromHex( "7e68abebffcfe3409af87fbb0b5254b7add6ec3efc1f5b882f3621df45afec15" ) ); + EXPECT_OUTCOME_TRUE( + sr_secr, SR25519SecretKey::fromHex( "d0c92c34038c17ea76208b04623203cbd9ce0901e82486da0053c0c2c4488b69" + "19f52f334750e98500ba84fd528a6ff810b91b43d11e3d3eb1c2176c9dda0b57" ) ); + sr_pair = { sr_secr, sr_publ }; + + EXPECT_OUTCOME_TRUE_MSG_1( crypto_store->initialize( crypto_store_test_directory ), "initialization failed" ); + } + + bool isStoredOnDisk( KeyTypeId kt, const Blob<32> &public_key ) + { + auto file_name = sgns::crypto::decodeKeyTypeId( kt ) + public_key.toHex(); + auto file_path = crypto_store_test_directory / file_name; + return boost::filesystem::exists( file_path ); + } + + std::shared_ptr bip39_provider; + std::shared_ptr crypto_store; + std::string mnemonic; + Buffer entropy; + Blob<32> seed; + KeyTypeId key_type; + + ED25519Keypair ed_pair; + SR25519Keypair sr_pair; }; /** diff --git a/test/src/storage/rocksdb/rocksdb_fs_test.cpp b/test/src/storage/rocksdb/rocksdb_fs_test.cpp index 6536738d..938150cd 100644 --- a/test/src/storage/rocksdb/rocksdb_fs_test.cpp +++ b/test/src/storage/rocksdb/rocksdb_fs_test.cpp @@ -12,9 +12,12 @@ namespace fs = boost::filesystem; namespace sgns::storage { - struct rocksdb_Open : public test::BaseFS_Test { - rocksdb_Open() : test::BaseFS_Test("supergenius_rocksdb_open") {} - }; + struct rocksdb_Open : public test::FSFixture + { + rocksdb_Open() : test::FSFixture( "supergenius_rocksdb_open" ) + { + } + }; /** * @given options with disabled option `create_if_missing` diff --git a/test/src/storage/rocksdb/rocksdb_integration_test.cpp b/test/src/storage/rocksdb/rocksdb_integration_test.cpp index 26bc25da..103117c6 100644 --- a/test/src/storage/rocksdb/rocksdb_integration_test.cpp +++ b/test/src/storage/rocksdb/rocksdb_integration_test.cpp @@ -3,23 +3,22 @@ #include "testutil/storage/base_rocksdb_test.hpp" #include -#include #include #include -#include "storage/rocksdb/rocksdb.hpp" #include "storage/database_error.hpp" #include "testutil/outcome.hpp" using namespace sgns::storage; -namespace fs = boost::filesystem; -struct RocksDB_Integration_Test : public test::BaseRocksDB_Test { - RocksDB_Integration_Test() - : test::BaseRocksDB_Test("supergenius_rocksdb_integration_test") {} +struct RocksDBIntegrationFixture : public test::RocksDBFixture +{ + RocksDBIntegrationFixture() : test::RocksDBFixture( "supergenius_rocksdb_integration_test" ) + { + } - Buffer key_{1, 3, 3, 7}; - Buffer value_{1, 2, 3}; + Buffer key_{ 1, 3, 3, 7 }; + Buffer value_{ 1, 2, 3 }; }; /** @@ -27,11 +26,12 @@ struct RocksDB_Integration_Test : public test::BaseRocksDB_Test { * @when read {key} * @then {value} is correct */ -TEST_F(RocksDB_Integration_Test, Put_Get) { - EXPECT_OUTCOME_TRUE_1(db_->put(key_, value_)); - EXPECT_TRUE(db_->contains(key_)); - EXPECT_OUTCOME_TRUE_2(val, db_->get(key_)); - EXPECT_EQ(val, value_); +TEST_F( RocksDBIntegrationFixture, PutGet ) +{ + EXPECT_OUTCOME_TRUE_1( db_->put( key_, value_ ) ); + EXPECT_TRUE( db_->contains( key_ ) ); + EXPECT_OUTCOME_TRUE_2( val, db_->get( key_ ) ); + EXPECT_EQ( val, value_ ); } /** @@ -39,12 +39,13 @@ TEST_F(RocksDB_Integration_Test, Put_Get) { * @when read {key} * @then get "not found" */ -TEST_F(RocksDB_Integration_Test, Get_NonExistent) { - EXPECT_FALSE(db_->contains(key_)); - EXPECT_OUTCOME_TRUE_1(db_->remove(key_)); - auto r = db_->get(key_); - EXPECT_FALSE(r); - EXPECT_EQ(r.error().value(), (int)DatabaseError::NOT_FOUND); +TEST_F( RocksDBIntegrationFixture, GetNonExistent ) +{ + EXPECT_FALSE( db_->contains( key_ ) ); + EXPECT_OUTCOME_TRUE_1( db_->remove( key_ ) ); + auto r = db_->get( key_ ); + EXPECT_FALSE( r ); + EXPECT_EQ( r.error().value(), (int)DatabaseError::NOT_FOUND ); } /** @@ -52,28 +53,31 @@ TEST_F(RocksDB_Integration_Test, Get_NonExistent) { * @when create batch and write KVs * @then data is written only after commit */ -TEST_F(RocksDB_Integration_Test, WriteBatch) { - std::list keys{{0}, {1}, {2}, {3}, {4}, {5}}; - Buffer toBeRemoved = {3}; - std::list expected{{0}, {1}, {2}, {4}, {5}}; - - auto batch = db_->batch(); - ASSERT_TRUE(batch); - - for (const auto &item : keys) { - EXPECT_OUTCOME_TRUE_1(batch->put(item, item)); - EXPECT_FALSE(db_->contains(item)); - } - EXPECT_OUTCOME_TRUE_1(batch->remove(toBeRemoved)); - EXPECT_OUTCOME_TRUE_1(batch->commit()); - - for (const auto &item : expected) { - EXPECT_TRUE(db_->contains(item)); - EXPECT_OUTCOME_TRUE_2(val, db_->get(item)); - EXPECT_EQ(val, item); - } - - EXPECT_FALSE(db_->contains(toBeRemoved)); +TEST_F( RocksDBIntegrationFixture, WriteBatch ) +{ + std::list keys{ { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 } }; + Buffer toBeRemoved = { 3 }; + std::list expected{ { 0 }, { 1 }, { 2 }, { 4 }, { 5 } }; + + auto batch = db_->batch(); + ASSERT_TRUE( batch ); + + for ( const auto &item : keys ) + { + EXPECT_OUTCOME_TRUE_1( batch->put( item, item ) ); + EXPECT_FALSE( db_->contains( item ) ); + } + EXPECT_OUTCOME_TRUE_1( batch->remove( toBeRemoved ) ); + EXPECT_OUTCOME_TRUE_1( batch->commit() ); + + for ( const auto &item : expected ) + { + EXPECT_TRUE( db_->contains( item ) ); + EXPECT_OUTCOME_TRUE_2( val, db_->get( item ) ); + EXPECT_EQ( val, item ); + } + + EXPECT_FALSE( db_->contains( toBeRemoved ) ); } /** @@ -81,58 +85,64 @@ TEST_F(RocksDB_Integration_Test, WriteBatch) { * @when iterate over kv pairs forward and backward * @then we iterate over all items */ -TEST_F(RocksDB_Integration_Test, Iterator) { - const size_t size = 100; - // 100 buffers of size 1 each; 0..99 - std::list keys; - for (size_t i = 0; i < size; i++) { - keys.emplace_back(1, i); - } - - for (const auto &item : keys) { - EXPECT_OUTCOME_TRUE_1(db_->put(item, item)); - } - - std::array counter{}; - - logger->warn("forward iteration"); - auto it = db_->cursor(); - EXPECT_OUTCOME_TRUE_1(it->seekToFirst()); - for (; it->isValid(); it->next().assume_value()) { - auto k = it->key().value(); - auto v = it->value().value(); - EXPECT_EQ(k, v); - - logger->info("key: {}, value: {}", k.toHex(), v.toHex()); - - EXPECT_GE(k[0], 0); - EXPECT_LT(k[0], size); - EXPECT_GT(k.size(), 0); - - counter[k[0]]++; - } - - for (size_t i = 0; i < size; i++) { - EXPECT_EQ(counter[i], 1); - } - - logger->warn("backward iteration"); - size_t c = 0; - uint8_t index = 0xf; - Buffer seekTo{index}; - // seek to `index` element - EXPECT_OUTCOME_TRUE_1(it->seek(seekTo)); - while (it->isValid()) { - auto k = it->key().value(); - auto v = it->value().value(); - EXPECT_EQ(k, v); - - logger->info("key: {}, value: {}", k.toHex(), v.toHex()); - - c++; - EXPECT_OUTCOME_TRUE_1(it->prev()); - } - - EXPECT_FALSE(it->isValid()); - EXPECT_EQ(c, index + 1); +TEST_F( RocksDBIntegrationFixture, Iterator ) +{ + const size_t size = 100; + // 100 buffers of size 1 each; 0..99 + std::list keys; + for ( size_t i = 0; i < size; i++ ) + { + keys.emplace_back( 1, i ); + } + + for ( const auto &item : keys ) + { + EXPECT_OUTCOME_TRUE_1( db_->put( item, item ) ); + } + + std::array counter{}; + + logger->warn( "forward iteration" ); + auto it = db_->cursor(); + EXPECT_OUTCOME_TRUE_1( it->seekToFirst() ); + for ( ; it->isValid(); it->next().assume_value() ) + { + auto k = it->key().value(); + auto v = it->value().value(); + EXPECT_EQ( k, v ); + + logger->info( "key: {}, value: {}", k.toHex(), v.toHex() ); + + EXPECT_GE( k[0], 0 ); + EXPECT_LT( k[0], size ); + EXPECT_GT( k.size(), 0 ); + + counter[k[0]]++; + } + + for ( size_t i = 0; i < size; i++ ) + { + EXPECT_EQ( counter[i], 1 ); + } + + logger->warn( "backward iteration" ); + size_t c = 0; + uint8_t index = 0xf; + Buffer seekTo{ index }; + // seek to `index` element + EXPECT_OUTCOME_TRUE_1( it->seek( seekTo ) ); + while ( it->isValid() ) + { + auto k = it->key().value(); + auto v = it->value().value(); + EXPECT_EQ( k, v ); + + logger->info( "key: {}, value: {}", k.toHex(), v.toHex() ); + + c++; + EXPECT_OUTCOME_TRUE_1( it->prev() ); + } + + EXPECT_FALSE( it->isValid() ); + EXPECT_EQ( c, index + 1 ); } diff --git a/test/src/storage/trie/trie_storage/trie_batch_test.cpp b/test/src/storage/trie/trie_storage/trie_batch_test.cpp index 5cdc3c90..d52541a4 100644 --- a/test/src/storage/trie/trie_storage/trie_batch_test.cpp +++ b/test/src/storage/trie/trie_storage/trie_batch_test.cpp @@ -24,29 +24,29 @@ using testing::_; using testing::Invoke; using testing::Return; -class TrieBatchTest : public test::BaseRocksDB_Test { - public: - TrieBatchTest() : BaseRocksDB_Test("rocksdbtest") {} - - void SetUp() override { - open(); - auto factory = std::make_shared(); - auto codec = std::make_shared(); - auto serializer = std::make_shared( - factory, - codec, - std::make_shared(std::move(db_), kNodePrefix)); - - trie = TrieStorageImpl::createEmpty(factory, codec, serializer, boost::none) - .value(); - } +class TrieBatchTest : public test::RocksDBFixture +{ +public: + TrieBatchTest() : RocksDBFixture( "rocksdbtest" ) + { + } + + void SetUp() override + { + open(); + auto factory = std::make_shared(); + auto codec = std::make_shared(); + auto serializer = std::make_shared( + factory, codec, std::make_shared( std::move( db_ ), kNodePrefix ) ); + + trie = TrieStorageImpl::createEmpty( factory, codec, serializer, boost::none ).value(); + } - static const std::vector> data; + static const std::vector> data; - std::unique_ptr trie; + std::unique_ptr trie; - static const Buffer kNodePrefix; - + static const Buffer kNodePrefix; }; const Buffer TrieBatchTest::kNodePrefix{1}; diff --git a/test/testutil/storage/base_crdt_test.cpp b/test/testutil/storage/base_crdt_test.cpp index 0ff1efa0..61b46eac 100644 --- a/test/testutil/storage/base_crdt_test.cpp +++ b/test/testutil/storage/base_crdt_test.cpp @@ -31,7 +31,7 @@ const std::string logger_config(R"( namespace test { - void BaseCRDT_Test::open() + void CRDTFixture::open() { auto logging_system = std::make_shared( std::make_shared( @@ -70,16 +70,16 @@ namespace test ASSERT_TRUE(db_) << "BaseCRDT_Test: db is nullptr"; } - BaseCRDT_Test::BaseCRDT_Test( fs::path path ) : BaseFS_Test( std::move( path ) ) + CRDTFixture::CRDTFixture( fs::path path ) : FSFixture( std::move( path ) ) { } - void BaseCRDT_Test::SetUp() + void CRDTFixture::SetUp() { open(); } - void BaseCRDT_Test::TearDown() + void CRDTFixture::TearDown() { // clear(); } diff --git a/test/testutil/storage/base_crdt_test.hpp b/test/testutil/storage/base_crdt_test.hpp index 424c9ab1..483d9795 100644 --- a/test/testutil/storage/base_crdt_test.hpp +++ b/test/testutil/storage/base_crdt_test.hpp @@ -15,10 +15,9 @@ namespace test { - struct BaseCRDT_Test : public BaseFS_Test + struct CRDTFixture : public FSFixture { - - BaseCRDT_Test( fs::path path ); + CRDTFixture( fs::path path ); void open(); diff --git a/test/testutil/storage/base_fs_test.cpp b/test/testutil/storage/base_fs_test.cpp index 55d2fd05..559724de 100644 --- a/test/testutil/storage/base_fs_test.cpp +++ b/test/testutil/storage/base_fs_test.cpp @@ -3,7 +3,7 @@ namespace test { - void BaseFS_Test::clear() + void FSFixture::clear() { if ( fs::exists( base_path ) ) { @@ -11,7 +11,7 @@ namespace test } } - BaseFS_Test::BaseFS_Test( fs::path path ) : base_path( std::move( path ) ) + FSFixture::FSFixture( fs::path path ) : base_path( std::move( path ) ) { clear(); mkdir(); @@ -20,13 +20,13 @@ namespace test logger->set_level( spdlog::level::debug ); } - void BaseFS_Test::SetUp() + void FSFixture::SetUp() { clear(); mkdir(); } - void BaseFS_Test::TearDown() + void FSFixture::TearDown() { } } diff --git a/test/testutil/storage/base_fs_test.hpp b/test/testutil/storage/base_fs_test.hpp index 6b16508f..a5d7eb87 100644 --- a/test/testutil/storage/base_fs_test.hpp +++ b/test/testutil/storage/base_fs_test.hpp @@ -15,12 +15,12 @@ namespace test * @brief Base test, which involves filesystem. Can be created with given * path. Clears path before test and after test. */ - struct BaseFS_Test : public ::testing::Test + struct FSFixture : public ::testing::Test { // not explicit, intentionally - BaseFS_Test( fs::path path ); + FSFixture( fs::path path ); - ~BaseFS_Test() override + ~FSFixture() override { clear(); } diff --git a/test/testutil/storage/base_rocksdb_test.cpp b/test/testutil/storage/base_rocksdb_test.cpp index 02332d50..c3b81ce9 100644 --- a/test/testutil/storage/base_rocksdb_test.cpp +++ b/test/testutil/storage/base_rocksdb_test.cpp @@ -4,27 +4,32 @@ namespace test { - void BaseRocksDB_Test::open() { - rocksdb::Options options; - options.create_if_missing = true; - - auto r = rocksdb::create(getPathString(), options); - if (!r) { - throw std::invalid_argument(r.error().message()); + void RocksDBFixture::open() + { + rocksdb::Options options; + options.create_if_missing = true; + + auto r = rocksdb::create( getPathString(), options ); + if ( !r ) + { + throw std::invalid_argument( r.error().message() ); + } + + db_ = std::move( r.value() ); + ASSERT_TRUE( db_ ) << "BaseRocksDB_Test: db is nullptr"; } - db_ = std::move(r.value()); - ASSERT_TRUE(db_) << "BaseRocksDB_Test: db is nullptr"; - } - - BaseRocksDB_Test::BaseRocksDB_Test(fs::path path) - : BaseFS_Test(std::move(path)) {} + RocksDBFixture::RocksDBFixture( fs::path path ) : FSFixture( std::move( path ) ) + { + } - void BaseRocksDB_Test::SetUp() { - open(); + void RocksDBFixture::SetUp() + { + open(); } - void BaseRocksDB_Test::TearDown() { - // clear(); + void RocksDBFixture::TearDown() + { + // clear(); } } // namespace test diff --git a/test/testutil/storage/base_rocksdb_test.hpp b/test/testutil/storage/base_rocksdb_test.hpp index 4b72caec..ff2837ea 100644 --- a/test/testutil/storage/base_rocksdb_test.hpp +++ b/test/testutil/storage/base_rocksdb_test.hpp @@ -9,19 +9,20 @@ namespace test { - struct BaseRocksDB_Test : public BaseFS_Test { - using rocksdb = sgns::storage::rocksdb; + struct RocksDBFixture : public FSFixture + { + using rocksdb = sgns::storage::rocksdb; - BaseRocksDB_Test(fs::path path); + RocksDBFixture( fs::path path ); - void open(); + void open(); - void SetUp() override; + void SetUp() override; - void TearDown() override; + void TearDown() override; - std::shared_ptr db_; - }; + std::shared_ptr db_; + }; } // namespace test