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.

115 righe
3.1 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. FileMetaData* f = new FileMetaData;
  19. f->number = files_.size() + 1;
  20. f->smallest = InternalKey(smallest, 100, kTypeValue);
  21. f->largest = InternalKey(largest, 100, kTypeValue);
  22. files_.push_back(f);
  23. }
  24. int Find(const char* key) {
  25. InternalKey target(key, 100, kTypeValue);
  26. InternalKeyComparator cmp(BytewiseComparator());
  27. return FindFile(cmp, files_, target.Encode());
  28. }
  29. bool Overlaps(const char* smallest, const char* largest) {
  30. InternalKey s(smallest, 100, kTypeValue);
  31. InternalKey l(largest, 100, kTypeValue);
  32. InternalKeyComparator cmp(BytewiseComparator());
  33. return SomeFileOverlapsRange(cmp, files_, s, l);
  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. }
  97. int main(int argc, char** argv) {
  98. return leveldb::test::RunAllTests();
  99. }