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