作者: 韩晨旭 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.

125 lines
3.3 KiB

  1. #include <iostream>
  2. #include <gtest/gtest.h>
  3. #include "leveldb/db.h"
  4. #include "leveldb/env.h"
  5. using namespace std;
  6. using namespace leveldb;
  7. Status OpenDB(std::string dbName, DB **db) {
  8. Options options;
  9. options.create_if_missing = true;
  10. return DB::Open(options, dbName, db);
  11. }
  12. template<typename T>
  13. // Return true if v1 and v2 have same elements
  14. bool CompareVector(const std::vector<T>& v1, const std::vector<T>& v2) {
  15. if (v1.size() != v2.size()) {
  16. return false;
  17. } else {
  18. std::set<T> compare_set(v1.begin(), v1.end());
  19. for (const auto &ele : v2) {
  20. if (compare_set.insert(ele).second == true) {
  21. return false;
  22. }
  23. }
  24. }
  25. return true;
  26. }
  27. TEST(TestFields, GetPutIterator) {
  28. DB *db;
  29. if(OpenDB("testdb", &db).ok() == false) {
  30. cerr << "Open DB Failed" << endl;
  31. }
  32. std::string key_1 = "k_1";
  33. std::string key_2 = "k_2";
  34. FieldArray field_array_1 = {
  35. {"name", "Arcueid01"},
  36. {"address", "tYpeMuuN"},
  37. {"phone", "122-233-4455"}
  38. };
  39. FieldArray field_array_2 = {
  40. {"name", "Arcueid02"},
  41. {"address", "tYpeMuuN"},
  42. {"phone", "199-999-2004"}
  43. };
  44. const auto fields_1 = Fields(field_array_1);
  45. const auto fields_2 = Fields(field_array_2);
  46. db->Put(WriteOptions(), key_1, fields_1);
  47. db->Put(WriteOptions(), key_2, fields_2);
  48. Fields ret;
  49. db->Get(ReadOptions(), key_1, &ret);
  50. const auto fields_ret = ret.GetFieldArray();
  51. ASSERT_EQ(CompareVector<Field>(fields_ret, field_array_1), true);
  52. db->Get(ReadOptions(), key_2, &ret);
  53. ASSERT_EQ(ret["name"], "Arcueid02");
  54. ASSERT_EQ(ret["address"], "tYpeMuuN");
  55. ASSERT_EQ(ret["phone"], "199-999-2004");
  56. auto iter = db->NewIterator(ReadOptions());
  57. iter->SeekToFirst();
  58. while (iter->Valid()) {
  59. auto key = iter->key().ToString();
  60. auto fields = iter->fields();
  61. if (key == "k_1") {
  62. ASSERT_EQ(fields["name"], "Arcueid01");
  63. ASSERT_EQ(fields["address"], "tYpeMuuN");
  64. ASSERT_EQ(fields["phone"], "122-233-4455");
  65. }
  66. if (key == "k_2") {
  67. ASSERT_EQ(fields["name"], "Arcueid02");
  68. ASSERT_EQ(fields["address"], "tYpeMuuN");
  69. ASSERT_EQ(fields["phone"], "199-999-2004");
  70. }
  71. iter->Next();
  72. }
  73. delete iter;
  74. delete db;
  75. }
  76. TEST(TestFields, SearchKey) {
  77. DB *db;
  78. if(OpenDB("testdb", &db).ok() == false) {
  79. cerr << "Open DB Failed" << endl;
  80. }
  81. std::vector<std::string> keys_have_field = {"k_1", "k_3"};
  82. std::vector<std::string> keys_wo_field = {"k_2", "k_4"};
  83. Field field_test = {"test_name", "Harry"};
  84. FieldArray field_array_have_field = {
  85. {"name", "Arcueid"},
  86. {"address", "tYpeMuuN"},
  87. {"phone", "122-233-4455"},
  88. field_test
  89. };
  90. FieldArray field_array_wo_field = {
  91. {"name", "Arcueid"}, {"address", "tYpeMuuN"}, {"phone", "122-233-4455"}};
  92. const auto fields_have_field = Fields(field_array_have_field);
  93. const auto fields_wo_field = Fields(field_array_wo_field);
  94. for(const auto& key : keys_have_field){
  95. db->Put(WriteOptions(), key, fields_have_field);
  96. }
  97. for (const auto& key : keys_wo_field) {
  98. db->Put(WriteOptions(), key, fields_wo_field);
  99. }
  100. const std::vector<std::string> key_ret = db->FindKeysByField(field_test);
  101. ASSERT_EQ(CompareVector<std::string>(key_ret, keys_have_field), true);
  102. }
  103. int main(int argc, char **argv) {
  104. testing::InitGoogleTest(&argc, argv);
  105. return RUN_ALL_TESTS();
  106. }