小组成员:10215300402-朱维清 & 10222140408 谷杰
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.

122 line
3.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/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. { "CURRENT", 0, kCurrentFile },
  25. { "LOCK", 0, kDBLockFile },
  26. { "MANIFEST-2", 2, kDescriptorFile },
  27. { "MANIFEST-7", 7, kDescriptorFile },
  28. { "LOG", 0, kInfoLogFile },
  29. { "LOG.old", 0, kInfoLogFile },
  30. { "18446744073709551615.log", 18446744073709551615ull, kLogFile },
  31. };
  32. for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  33. std::string f = cases[i].fname;
  34. ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
  35. ASSERT_EQ(cases[i].type, type) << f;
  36. ASSERT_EQ(cases[i].number, number) << f;
  37. }
  38. // Errors
  39. static const char* errors[] = {
  40. "",
  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. };
  63. for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
  64. std::string f = errors[i];
  65. ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
  66. };
  67. }
  68. TEST(FileNameTest, Construction) {
  69. uint64_t number;
  70. FileType type;
  71. std::string fname;
  72. fname = CurrentFileName("foo");
  73. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  74. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  75. ASSERT_EQ(0, number);
  76. ASSERT_EQ(kCurrentFile, type);
  77. fname = LockFileName("foo");
  78. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  79. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  80. ASSERT_EQ(0, number);
  81. ASSERT_EQ(kDBLockFile, type);
  82. fname = LogFileName("foo", 192);
  83. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  84. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  85. ASSERT_EQ(192, number);
  86. ASSERT_EQ(kLogFile, type);
  87. fname = TableFileName("bar", 200);
  88. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  89. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  90. ASSERT_EQ(200, number);
  91. ASSERT_EQ(kTableFile, type);
  92. fname = DescriptorFileName("bar", 100);
  93. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  94. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  95. ASSERT_EQ(100, number);
  96. ASSERT_EQ(kDescriptorFile, type);
  97. fname = TempFileName("tmp", 999);
  98. ASSERT_EQ("tmp/", std::string(fname.data(), 4));
  99. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  100. ASSERT_EQ(999, number);
  101. ASSERT_EQ(kTempFile, type);
  102. }
  103. } // namespace leveldb
  104. int main(int argc, char** argv) {
  105. return leveldb::test::RunAllTests();
  106. }