作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

135 lignes
3.8 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. "",
  42. "foo",
  43. "foo-dx-100.log",
  44. ".log",
  45. "",
  46. "manifest",
  47. "CURREN",
  48. "CURRENTX",
  49. "MANIFES",
  50. "MANIFEST",
  51. "MANIFEST-",
  52. "XMANIFEST-3",
  53. "MANIFEST-3x",
  54. "LOC",
  55. "LOCKx",
  56. "LO",
  57. "LOGx",
  58. "18446744073709551616.log",
  59. "184467440737095516150.log",
  60. "100",
  61. "100.",
  62. "100.lop"
  63. };
  64. for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
  65. std::string f = errors[i];
  66. ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
  67. }
  68. }
  69. TEST(FileNameTest, Construction) {
  70. uint64_t number;
  71. FileType type;
  72. std::string fname;
  73. fname = CurrentFileName("foo");
  74. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  75. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  76. ASSERT_EQ(0, number);
  77. ASSERT_EQ(kCurrentFile, type);
  78. fname = LockFileName("foo");
  79. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  80. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  81. ASSERT_EQ(0, number);
  82. ASSERT_EQ(kDBLockFile, type);
  83. fname = LogFileName("foo", 192);
  84. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  85. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  86. ASSERT_EQ(192, number);
  87. ASSERT_EQ(kLogFile, type);
  88. fname = TableFileName("bar", 200);
  89. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  90. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  91. ASSERT_EQ(200, number);
  92. ASSERT_EQ(kTableFile, type);
  93. fname = DescriptorFileName("bar", 100);
  94. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  95. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  96. ASSERT_EQ(100, number);
  97. ASSERT_EQ(kDescriptorFile, type);
  98. fname = TempFileName("tmp", 999);
  99. ASSERT_EQ("tmp/", std::string(fname.data(), 4));
  100. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  101. ASSERT_EQ(999, number);
  102. ASSERT_EQ(kTempFile, type);
  103. fname = InfoLogFileName("foo");
  104. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  105. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  106. ASSERT_EQ(0, number);
  107. ASSERT_EQ(kInfoLogFile, type);
  108. fname = OldInfoLogFileName("foo");
  109. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  110. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  111. ASSERT_EQ(0, number);
  112. ASSERT_EQ(kInfoLogFile, type);
  113. }
  114. } // namespace leveldb
  115. int main(int argc, char** argv) {
  116. return leveldb::test::RunAllTests();
  117. }