作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

  1. #include "gtest/gtest.h"
  2. #include "leveldb/env.h"
  3. #include "leveldb/db.h"
  4. using namespace leveldb;
  5. constexpr int value_size = 2048;
  6. constexpr int data_size = 128 << 20;
  7. std::map<std::string, std::string> data;
  8. Status OpenDB(std::string dbName, DB **db) {
  9. Options options;
  10. options.create_if_missing = true;
  11. return DB::Open(options, dbName, db);
  12. }
  13. void InsertData(DB *db) {
  14. WriteOptions writeOptions;
  15. int key_num = data_size / value_size;
  16. srand(0);
  17. for (int i = 0; i < key_num; i++) {
  18. int key_ = rand() % key_num+1;
  19. std::string key = std::to_string(key_);
  20. std::string value(value_size, 'a');
  21. db->Put(writeOptions, key, value);
  22. data[key] = value;
  23. }
  24. }
  25. TEST(TestBasicIO, PointGet) {
  26. DB *db;
  27. if(OpenDB("testdb", &db).ok() == false) {
  28. std::cerr << "open db failed" << std::endl;
  29. abort();
  30. }
  31. data.clear();
  32. InsertData(db);
  33. ReadOptions readOptions;
  34. for (auto iter = data.begin(); iter != data.end(); ++iter) {
  35. std::string value;
  36. db->Get(readOptions, iter->first, &value);
  37. ASSERT_TRUE(value == iter->second);
  38. }
  39. delete db;
  40. }
  41. TEST(TestBasicIO, RangeQuery) {
  42. DB *db;
  43. if(OpenDB("testdb", &db).ok() == false) {
  44. std::cerr << "open db failed" << std::endl;
  45. abort();
  46. }
  47. data.clear();
  48. InsertData(db);
  49. ReadOptions readOptions;
  50. auto iter = db->NewIterator(readOptions);
  51. iter->SeekToFirst();
  52. while (iter->Valid()) {
  53. ASSERT_TRUE(data[iter->key().ToString()] == iter->fields()["1"]);
  54. iter->Next();
  55. }
  56. }
  57. int main(int argc, char **argv) {
  58. testing::InitGoogleTest(&argc, argv);
  59. return RUN_ALL_TESTS();
  60. }