小组成员: 曹可心-10223903406 朴祉燕-10224602413
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.2 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/filename.h"
  5. #include "db/dbformat.h"
  6. #include "port/port.h"
  7. #include "util/logging.h"
  8. #include "util/testharness.h"
  9. namespace leveldb {
  10. class FileNameTest {};
  11. TEST(FileNameTest, Parse) {
  12. Slice db;
  13. FileType type;
  14. uint64_t number;
  15. // Successful parses
  16. static struct {
  17. const char* fname;
  18. uint64_t number;
  19. FileType type;
  20. } cases[] = {
  21. {"100.log", 100, kLogFile},
  22. {"0.log", 0, kLogFile},
  23. {"0.sst", 0, kTableFile},
  24. {"0.ldb", 0, kTableFile},
  25. {"CURRENT", 0, kCurrentFile},
  26. {"LOCK", 0, kDBLockFile},
  27. {"MANIFEST-2", 2, kDescriptorFile},
  28. {"MANIFEST-7", 7, kDescriptorFile},
  29. {"LOG", 0, kInfoLogFile},
  30. {"LOG.old", 0, kInfoLogFile},
  31. {"18446744073709551615.log", 18446744073709551615ull, kLogFile},
  32. };
  33. for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  34. std::string f = cases[i].fname;
  35. ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
  36. ASSERT_EQ(cases[i].type, type) << f;
  37. ASSERT_EQ(cases[i].number, number) << f;
  38. }
  39. // Errors
  40. static const char* errors[] = {"",
  41. "foo",
  42. "foo-dx-100.log",
  43. ".log",
  44. "",
  45. "manifest",
  46. "CURREN",
  47. "CURRENTX",
  48. "MANIFES",
  49. "MANIFEST",
  50. "MANIFEST-",
  51. "XMANIFEST-3",
  52. "MANIFEST-3x",
  53. "LOC",
  54. "LOCKx",
  55. "LO",
  56. "LOGx",
  57. "18446744073709551616.log",
  58. "184467440737095516150.log",
  59. "100",
  60. "100.",
  61. "100.lop"};
  62. for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
  63. std::string f = errors[i];
  64. ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
  65. }
  66. }
  67. TEST(FileNameTest, Construction) {
  68. uint64_t number;
  69. FileType type;
  70. std::string fname;
  71. fname = CurrentFileName("foo");
  72. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  73. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  74. ASSERT_EQ(0, number);
  75. ASSERT_EQ(kCurrentFile, type);
  76. fname = LockFileName("foo");
  77. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  78. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  79. ASSERT_EQ(0, number);
  80. ASSERT_EQ(kDBLockFile, type);
  81. fname = LogFileName("foo", 192);
  82. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  83. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  84. ASSERT_EQ(192, number);
  85. ASSERT_EQ(kLogFile, type);
  86. fname = TableFileName("bar", 200);
  87. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  88. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  89. ASSERT_EQ(200, number);
  90. ASSERT_EQ(kTableFile, type);
  91. fname = DescriptorFileName("bar", 100);
  92. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  93. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  94. ASSERT_EQ(100, number);
  95. ASSERT_EQ(kDescriptorFile, type);
  96. fname = TempFileName("tmp", 999);
  97. ASSERT_EQ("tmp/", std::string(fname.data(), 4));
  98. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  99. ASSERT_EQ(999, number);
  100. ASSERT_EQ(kTempFile, type);
  101. fname = InfoLogFileName("foo");
  102. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  103. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  104. ASSERT_EQ(0, number);
  105. ASSERT_EQ(kInfoLogFile, type);
  106. fname = OldInfoLogFileName("foo");
  107. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  108. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  109. ASSERT_EQ(0, number);
  110. ASSERT_EQ(kInfoLogFile, type);
  111. }
  112. } // namespace leveldb
  113. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }