From 1b2362085699791bd0719b2a6ae67e9e35e642c1 Mon Sep 17 00:00:00 2001 From: Arcueid <981354012@qq.com> Date: Wed, 11 Dec 2024 09:55:57 -0800 Subject: [PATCH] append basic io test and pass --- CMakeLists.txt | 5 ++++ test/test_basicio.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 test/test_basicio.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 6930e85..b281c75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -535,3 +535,8 @@ add_executable(test_vtable "${PROJECT_SOURCE_DIR}/test/test_vtable.cc" ) target_link_libraries(test_vtable PRIVATE leveldb gtest) + +add_executable(test_basicio + "${PROJECT_SOURCE_DIR}/test/test_basicio.cc" +) +target_link_libraries(test_basicio PRIVATE leveldb gtest) \ No newline at end of file diff --git a/test/test_basicio.cc b/test/test_basicio.cc new file mode 100644 index 0000000..2654077 --- /dev/null +++ b/test/test_basicio.cc @@ -0,0 +1,75 @@ +#include "gtest/gtest.h" +#include "leveldb/env.h" +#include "leveldb/db.h" +using namespace leveldb; + +constexpr int value_size = 2048; +constexpr int data_size = 128 << 20; + +std::map data; + +Status OpenDB(std::string dbName, DB **db) { + Options options; + options.create_if_missing = true; + return DB::Open(options, dbName, db); +} + +void InsertData(DB *db) { + WriteOptions writeOptions; + int key_num = data_size / value_size; + srand(0); + + for (int i = 0; i < key_num; i++) { + int key_ = rand() % key_num+1; + std::string key = std::to_string(key_); + std::string value(value_size, 'a'); + db->Put(writeOptions, key, value); + data[key] = value; + } +} + +TEST(TestBasicIO, PointGet) { + DB *db; + if(OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } + + data.clear(); + InsertData(db); + + ReadOptions readOptions; + + for (auto iter = data.begin(); iter != data.end(); ++iter) { + std::string value; + db->Get(readOptions, iter->first, &value); + ASSERT_TRUE(value == iter->second); + } + + delete db; +} + +TEST(TestBasicIO, RangeQuery) { + DB *db; + if(OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } + + data.clear(); + InsertData(db); + + ReadOptions readOptions; + + auto iter = db->NewIterator(readOptions); + iter->SeekToFirst(); + while (iter->Valid()) { + ASSERT_TRUE(data[iter->key().ToString()] == iter->fields()["1"]); + iter->Next(); + } +} + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}