Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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  { "CURRENT", 0, kCurrentFile },
31  { "LOCK", 0, kDBLockFile },
32  { "MANIFEST-2", 2, kDescriptorFile },
33  { "MANIFEST-7", 7, kDescriptorFile },
34  { "LOG", 0, kInfoLogFile },
35  { "LOG.old", 0, kInfoLogFile },
36  { "18446744073709551615.log", 18446744073709551615ull, kLogFile },
37  };
38  for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
39  std::string f = cases[i].fname;
40  ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
41  ASSERT_EQ(cases[i].type, type) << f;
42  ASSERT_EQ(cases[i].number, number) << f;
43  }
44 
45  // Errors
46  static const char* errors[] = {
47  "",
48  "foo",
49  "foo-dx-100.log",
50  ".log",
51  "",
52  "manifest",
53  "CURREN",
54  "CURRENTX",
55  "MANIFES",
56  "MANIFEST",
57  "MANIFEST-",
58  "XMANIFEST-3",
59  "MANIFEST-3x",
60  "LOC",
61  "LOCKx",
62  "LO",
63  "LOGx",
64  "18446744073709551616.log",
65  "184467440737095516150.log",
66  "100",
67  "100.",
68  "100.lop"
69  };
70  for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
71  std::string f = errors[i];
72  ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
73  }
74 }
75 
76 TEST(FileNameTest, Construction) {
77  uint64_t number;
78  FileType type;
79  std::string fname;
80 
81  fname = CurrentFileName("foo");
82  ASSERT_EQ("foo/", std::string(fname.data(), 4));
83  ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
84  ASSERT_EQ(0, number);
85  ASSERT_EQ(kCurrentFile, type);
86 
87  fname = LockFileName("foo");
88  ASSERT_EQ("foo/", std::string(fname.data(), 4));
89  ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
90  ASSERT_EQ(0, number);
91  ASSERT_EQ(kDBLockFile, type);
92 
93  fname = LogFileName("foo", 192);
94  ASSERT_EQ("foo/", std::string(fname.data(), 4));
95  ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
96  ASSERT_EQ(192, number);
97  ASSERT_EQ(kLogFile, type);
98 
99  fname = TableFileName("bar", 200);
100  ASSERT_EQ("bar/", std::string(fname.data(), 4));
101  ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
102  ASSERT_EQ(200, number);
103  ASSERT_EQ(kTableFile, type);
104 
105  fname = DescriptorFileName("bar", 100);
106  ASSERT_EQ("bar/", std::string(fname.data(), 4));
107  ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
108  ASSERT_EQ(100, number);
109  ASSERT_EQ(kDescriptorFile, type);
110 
111  fname = TempFileName("tmp", 999);
112  ASSERT_EQ("tmp/", std::string(fname.data(), 4));
113  ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
114  ASSERT_EQ(999, number);
115  ASSERT_EQ(kTempFile, type);
116 }
117 
118 } // namespace leveldb
119 
120 int main(int argc, char** argv) {
122 }
bool ParseFileName(const std::string &fname, uint64_t *number, FileType *type)
Definition: filename.cc:75
int RunAllTests()
Definition: testharness.cc:36
std::string TempFileName(const std::string &dbname, uint64_t number)
Definition: filename.cc:53
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:37
unsigned long long uint64_t
Definition: stdint.h:22
TEST(CorruptionTest, Recovery)
std::string CurrentFileName(const std::string &dbname)
Definition: filename.cc:45
#define ASSERT_TRUE(c)
Definition: testharness.h:105
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:49