作者: 韩晨旭 10225101440 李畅 10225102463
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

124 righe
3.4 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "db/version_set.h"
  5. #include "util/logging.h"
  6. #include "util/testharness.h"
  7. #include "util/testutil.h"
  8. namespace leveldb {
  9. class FindFileTest {
  10. public:
  11. std::vector<FileMetaData*> files_;
  12. ~FindFileTest() {
  13. for (int i = 0; i < files_.size(); i++) {
  14. delete files_[i];
  15. }
  16. }
  17. void Add(const char* smallest, const char* largest,
  18. SequenceNumber smallest_seq = 100,
  19. SequenceNumber largest_seq = 100) {
  20. FileMetaData* f = new FileMetaData;
  21. f->number = files_.size() + 1;
  22. f->smallest = InternalKey(smallest, smallest_seq, kTypeValue);
  23. f->largest = InternalKey(largest, largest_seq, kTypeValue);
  24. files_.push_back(f);
  25. }
  26. int Find(const char* key) {
  27. InternalKey target(key, 100, kTypeValue);
  28. InternalKeyComparator cmp(BytewiseComparator());
  29. return FindFile(cmp, files_, target.Encode());
  30. }
  31. bool Overlaps(const char* smallest, const char* largest) {
  32. InternalKeyComparator cmp(BytewiseComparator());
  33. return SomeFileOverlapsRange(cmp, files_, smallest, largest);
  34. }
  35. };
  36. TEST(FindFileTest, Empty) {
  37. ASSERT_EQ(0, Find("foo"));
  38. ASSERT_TRUE(! Overlaps("a", "z"));
  39. }
  40. TEST(FindFileTest, Single) {
  41. Add("p", "q");
  42. ASSERT_EQ(0, Find("a"));
  43. ASSERT_EQ(0, Find("p"));
  44. ASSERT_EQ(0, Find("p1"));
  45. ASSERT_EQ(0, Find("q"));
  46. ASSERT_EQ(1, Find("q1"));
  47. ASSERT_EQ(1, Find("z"));
  48. ASSERT_TRUE(! Overlaps("a", "b"));
  49. ASSERT_TRUE(! Overlaps("z1", "z2"));
  50. ASSERT_TRUE(Overlaps("a", "p"));
  51. ASSERT_TRUE(Overlaps("a", "q"));
  52. ASSERT_TRUE(Overlaps("a", "z"));
  53. ASSERT_TRUE(Overlaps("p", "p1"));
  54. ASSERT_TRUE(Overlaps("p", "q"));
  55. ASSERT_TRUE(Overlaps("p", "z"));
  56. ASSERT_TRUE(Overlaps("p1", "p2"));
  57. ASSERT_TRUE(Overlaps("p1", "z"));
  58. ASSERT_TRUE(Overlaps("q", "q"));
  59. ASSERT_TRUE(Overlaps("q", "q1"));
  60. }
  61. TEST(FindFileTest, Multiple) {
  62. Add("150", "200");
  63. Add("200", "250");
  64. Add("300", "350");
  65. Add("400", "450");
  66. ASSERT_EQ(0, Find("100"));
  67. ASSERT_EQ(0, Find("150"));
  68. ASSERT_EQ(0, Find("151"));
  69. ASSERT_EQ(0, Find("199"));
  70. ASSERT_EQ(0, Find("200"));
  71. ASSERT_EQ(1, Find("201"));
  72. ASSERT_EQ(1, Find("249"));
  73. ASSERT_EQ(1, Find("250"));
  74. ASSERT_EQ(2, Find("251"));
  75. ASSERT_EQ(2, Find("299"));
  76. ASSERT_EQ(2, Find("300"));
  77. ASSERT_EQ(2, Find("349"));
  78. ASSERT_EQ(2, Find("350"));
  79. ASSERT_EQ(3, Find("351"));
  80. ASSERT_EQ(3, Find("400"));
  81. ASSERT_EQ(3, Find("450"));
  82. ASSERT_EQ(4, Find("451"));
  83. ASSERT_TRUE(! Overlaps("100", "149"));
  84. ASSERT_TRUE(! Overlaps("251", "299"));
  85. ASSERT_TRUE(! Overlaps("451", "500"));
  86. ASSERT_TRUE(! Overlaps("351", "399"));
  87. ASSERT_TRUE(Overlaps("100", "150"));
  88. ASSERT_TRUE(Overlaps("100", "200"));
  89. ASSERT_TRUE(Overlaps("100", "300"));
  90. ASSERT_TRUE(Overlaps("100", "400"));
  91. ASSERT_TRUE(Overlaps("100", "500"));
  92. ASSERT_TRUE(Overlaps("375", "400"));
  93. ASSERT_TRUE(Overlaps("450", "450"));
  94. ASSERT_TRUE(Overlaps("450", "500"));
  95. }
  96. TEST(FindFileTest, OverlapSequenceChecks) {
  97. Add("200", "200", 5000, 3000);
  98. ASSERT_TRUE(! Overlaps("199", "199"));
  99. ASSERT_TRUE(! Overlaps("201", "300"));
  100. ASSERT_TRUE(Overlaps("200", "200"));
  101. ASSERT_TRUE(Overlaps("190", "200"));
  102. ASSERT_TRUE(Overlaps("200", "210"));
  103. }
  104. }
  105. int main(int argc, char** argv) {
  106. return leveldb::test::RunAllTests();
  107. }