提供基本的ttl测试用例
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.

131 lines
4.3 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/dbformat.h"
  5. #include "util/logging.h"
  6. #include "util/testharness.h"
  7. namespace leveldb {
  8. static std::string IKey(const std::string& user_key, uint64_t seq,
  9. ValueType vt) {
  10. std::string encoded;
  11. AppendInternalKey(&encoded, ParsedInternalKey(user_key, seq, vt));
  12. return encoded;
  13. }
  14. static std::string Shorten(const std::string& s, const std::string& l) {
  15. std::string result = s;
  16. InternalKeyComparator(BytewiseComparator()).FindShortestSeparator(&result, l);
  17. return result;
  18. }
  19. static std::string ShortSuccessor(const std::string& s) {
  20. std::string result = s;
  21. InternalKeyComparator(BytewiseComparator()).FindShortSuccessor(&result);
  22. return result;
  23. }
  24. static void TestKey(const std::string& key, uint64_t seq, ValueType vt) {
  25. std::string encoded = IKey(key, seq, vt);
  26. Slice in(encoded);
  27. ParsedInternalKey decoded("", 0, kTypeValue);
  28. ASSERT_TRUE(ParseInternalKey(in, &decoded));
  29. ASSERT_EQ(key, decoded.user_key.ToString());
  30. ASSERT_EQ(seq, decoded.sequence);
  31. ASSERT_EQ(vt, decoded.type);
  32. ASSERT_TRUE(!ParseInternalKey(Slice("bar"), &decoded));
  33. }
  34. class FormatTest {};
  35. TEST(FormatTest, InternalKey_EncodeDecode) {
  36. const char* keys[] = {"", "k", "hello", "longggggggggggggggggggggg"};
  37. const uint64_t seq[] = {1,
  38. 2,
  39. 3,
  40. (1ull << 8) - 1,
  41. 1ull << 8,
  42. (1ull << 8) + 1,
  43. (1ull << 16) - 1,
  44. 1ull << 16,
  45. (1ull << 16) + 1,
  46. (1ull << 32) - 1,
  47. 1ull << 32,
  48. (1ull << 32) + 1};
  49. for (int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
  50. for (int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {
  51. TestKey(keys[k], seq[s], kTypeValue);
  52. TestKey("hello", 1, kTypeDeletion);
  53. }
  54. }
  55. }
  56. TEST(FormatTest, InternalKey_DecodeFromEmpty) {
  57. InternalKey internal_key;
  58. ASSERT_TRUE(!internal_key.DecodeFrom(""));
  59. }
  60. TEST(FormatTest, InternalKeyShortSeparator) {
  61. // When user keys are same
  62. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  63. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 99, kTypeValue)));
  64. ASSERT_EQ(
  65. IKey("foo", 100, kTypeValue),
  66. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 101, kTypeValue)));
  67. ASSERT_EQ(
  68. IKey("foo", 100, kTypeValue),
  69. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeValue)));
  70. ASSERT_EQ(
  71. IKey("foo", 100, kTypeValue),
  72. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeDeletion)));
  73. // When user keys are misordered
  74. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  75. Shorten(IKey("foo", 100, kTypeValue), IKey("bar", 99, kTypeValue)));
  76. // When user keys are different, but correctly ordered
  77. ASSERT_EQ(
  78. IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  79. Shorten(IKey("foo", 100, kTypeValue), IKey("hello", 200, kTypeValue)));
  80. // When start user key is prefix of limit user key
  81. ASSERT_EQ(
  82. IKey("foo", 100, kTypeValue),
  83. Shorten(IKey("foo", 100, kTypeValue), IKey("foobar", 200, kTypeValue)));
  84. // When limit user key is prefix of start user key
  85. ASSERT_EQ(
  86. IKey("foobar", 100, kTypeValue),
  87. Shorten(IKey("foobar", 100, kTypeValue), IKey("foo", 200, kTypeValue)));
  88. }
  89. TEST(FormatTest, InternalKeyShortestSuccessor) {
  90. ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  91. ShortSuccessor(IKey("foo", 100, kTypeValue)));
  92. ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),
  93. ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));
  94. }
  95. TEST(FormatTest, ParsedInternalKeyDebugString) {
  96. ParsedInternalKey key("The \"key\" in 'single quotes'", 42, kTypeValue);
  97. ASSERT_EQ("'The \"key\" in 'single quotes'' @ 42 : 1", key.DebugString());
  98. }
  99. TEST(FormatTest, InternalKeyDebugString) {
  100. InternalKey key("The \"key\" in 'single quotes'", 42, kTypeValue);
  101. ASSERT_EQ("'The \"key\" in 'single quotes'' @ 42 : 1", key.DebugString());
  102. InternalKey invalid_key;
  103. ASSERT_EQ("(bad)", invalid_key.DebugString());
  104. }
  105. } // namespace leveldb
  106. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }