Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: third_party/leveldatabase/env_chromium_unittest.cc

Issue 416633002: Cleanup Chrome's LevelDB env to conform to C++ style guidelines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/files/file.h" 6 #include "base/files/file.h"
7 #include "base/files/file_enumerator.h" 7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/test/test_suite.h" 10 #include "base/test/test_suite.h"
11 #include "env_chromium_stdio.h" 11 #include "third_party/leveldatabase/env_chromium_stdio.h"
12 #if defined(OS_WIN) 12 #if defined(OS_WIN)
13 #include "env_chromium_win.h" 13 #include "third_party/leveldatabase/env_chromium_win.h"
14 #endif 14 #endif
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/leveldatabase/env_idb.h" 16 #include "third_party/leveldatabase/env_idb.h"
17 #include "third_party/leveldatabase/src/include/leveldb/db.h" 17 #include "third_party/leveldatabase/src/include/leveldb/db.h"
18 18
19 using namespace leveldb_env;
20 using namespace leveldb;
21
22 #define FPL FILE_PATH_LITERAL 19 #define FPL FILE_PATH_LITERAL
23 20
21 using leveldb::DB;
22 using leveldb::Env;
23 using leveldb::IDBEnv;
24 using leveldb::Options;
25 using leveldb::ReadOptions;
26 using leveldb::Slice;
27 using leveldb::Status;
28 using leveldb::WritableFile;
29 using leveldb::WriteOptions;
30 using leveldb_env::ChromiumEnvStdio;
31 using leveldb_env::MethodID;
32
24 TEST(ErrorEncoding, OnlyAMethod) { 33 TEST(ErrorEncoding, OnlyAMethod) {
25 const MethodID in_method = kSequentialFileRead; 34 const MethodID in_method = leveldb_env::kSequentialFileRead;
26 const Status s = MakeIOError("Somefile.txt", "message", in_method); 35 const Status s = MakeIOError("Somefile.txt", "message", in_method);
27 MethodID method; 36 MethodID method;
28 int error = -75; 37 int error = -75;
29 EXPECT_EQ(METHOD_ONLY, 38 EXPECT_EQ(leveldb_env::METHOD_ONLY,
30 ParseMethodAndError(s.ToString().c_str(), &method, &error)); 39 ParseMethodAndError(s.ToString().c_str(), &method, &error));
31 EXPECT_EQ(in_method, method); 40 EXPECT_EQ(in_method, method);
32 EXPECT_EQ(-75, error); 41 EXPECT_EQ(-75, error);
33 } 42 }
34 43
35 TEST(ErrorEncoding, FileError) { 44 TEST(ErrorEncoding, FileError) {
36 const MethodID in_method = kWritableFileClose; 45 const MethodID in_method = leveldb_env::kWritableFileClose;
37 const base::File::Error fe = base::File::FILE_ERROR_INVALID_OPERATION; 46 const base::File::Error fe = base::File::FILE_ERROR_INVALID_OPERATION;
38 const Status s = MakeIOError("Somefile.txt", "message", in_method, fe); 47 const Status s = MakeIOError("Somefile.txt", "message", in_method, fe);
39 MethodID method; 48 MethodID method;
40 int error; 49 int error;
41 EXPECT_EQ(METHOD_AND_PFE, 50 EXPECT_EQ(leveldb_env::METHOD_AND_PFE,
42 ParseMethodAndError(s.ToString().c_str(), &method, &error)); 51 ParseMethodAndError(s.ToString().c_str(), &method, &error));
43 EXPECT_EQ(in_method, method); 52 EXPECT_EQ(in_method, method);
44 EXPECT_EQ(fe, error); 53 EXPECT_EQ(fe, error);
45 } 54 }
46 55
47 TEST(ErrorEncoding, Errno) { 56 TEST(ErrorEncoding, Errno) {
48 const MethodID in_method = kWritableFileFlush; 57 const MethodID in_method = leveldb_env::kWritableFileFlush;
49 const int some_errno = ENOENT; 58 const int some_errno = ENOENT;
50 const Status s = 59 const Status s =
51 MakeIOError("Somefile.txt", "message", in_method, some_errno); 60 MakeIOError("Somefile.txt", "message", in_method, some_errno);
52 MethodID method; 61 MethodID method;
53 int error; 62 int error;
54 EXPECT_EQ(METHOD_AND_ERRNO, 63 EXPECT_EQ(leveldb_env::METHOD_AND_ERRNO,
55 ParseMethodAndError(s.ToString().c_str(), &method, &error)); 64 ParseMethodAndError(s.ToString().c_str(), &method, &error));
56 EXPECT_EQ(in_method, method); 65 EXPECT_EQ(in_method, method);
57 EXPECT_EQ(some_errno, error); 66 EXPECT_EQ(some_errno, error);
58 } 67 }
59 68
60 #if defined(OS_WIN) 69 #if defined(OS_WIN)
61 TEST(ErrorEncoding, ErrnoWin32) { 70 TEST(ErrorEncoding, ErrnoWin32) {
62 const MethodID in_method = kWritableFileFlush; 71 const MethodID in_method = leveldb_env::kWritableFileFlush;
63 const DWORD some_errno = ERROR_FILE_NOT_FOUND; 72 const DWORD some_errno = ERROR_FILE_NOT_FOUND;
64 const Status s = 73 const Status s =
65 MakeIOErrorWin("Somefile.txt", "message", in_method, some_errno); 74 MakeIOErrorWin("Somefile.txt", "message", in_method, some_errno);
66 MethodID method; 75 MethodID method;
67 int error; 76 int error;
68 EXPECT_EQ(METHOD_AND_ERRNO, 77 EXPECT_EQ(leveldb_env::METHOD_AND_ERRNO,
69 ParseMethodAndError(s.ToString().c_str(), &method, &error)); 78 ParseMethodAndError(s.ToString().c_str(), &method, &error));
70 EXPECT_EQ(in_method, method); 79 EXPECT_EQ(in_method, method);
71 EXPECT_EQ(some_errno, error); 80 EXPECT_EQ(some_errno, error);
72 } 81 }
73 #endif 82 #endif
74 83
75 TEST(ErrorEncoding, NoEncodedMessage) { 84 TEST(ErrorEncoding, NoEncodedMessage) {
76 Status s = Status::IOError("Some message", "from leveldb itself"); 85 Status s = Status::IOError("Some message", "from leveldb itself");
77 MethodID method = kRandomAccessFileRead; 86 MethodID method = leveldb_env::kRandomAccessFileRead;
78 int error = 4; 87 int error = 4;
79 EXPECT_EQ(NONE, ParseMethodAndError(s.ToString().c_str(), &method, &error)); 88 EXPECT_EQ(leveldb_env::NONE,
80 EXPECT_EQ(kRandomAccessFileRead, method); 89 ParseMethodAndError(s.ToString().c_str(), &method, &error));
90 EXPECT_EQ(leveldb_env::kRandomAccessFileRead, method);
81 EXPECT_EQ(4, error); 91 EXPECT_EQ(4, error);
82 } 92 }
83 93
84 template <typename T> 94 template <typename T>
85 class MyEnv : public T { 95 class MyEnv : public T {
86 public: 96 public:
87 MyEnv() : directory_syncs_(0) {} 97 MyEnv() : directory_syncs_(0) {}
88 int directory_syncs() { return directory_syncs_; } 98 int directory_syncs() { return directory_syncs_; }
89 99
90 protected: 100 protected:
91 virtual void DidSyncDir(const std::string& fname) { 101 virtual void DidSyncDir(const std::string& fname) {
92 ++directory_syncs_; 102 ++directory_syncs_;
93 ChromiumEnv::DidSyncDir(fname); 103 leveldb_env::ChromiumEnv::DidSyncDir(fname);
94 } 104 }
95 105
96 private: 106 private:
97 int directory_syncs_; 107 int directory_syncs_;
98 }; 108 };
99 109
100 template <typename T> 110 template <typename T>
101 class ChromiumEnvMultiPlatformTests : public ::testing::Test { 111 class ChromiumEnvMultiPlatformTests : public ::testing::Test {
102 public: 112 public:
103 }; 113 };
104 114
105 #if defined(OS_WIN) 115 #if defined(OS_WIN)
106 typedef ::testing::Types<ChromiumEnvStdio, ChromiumEnvWin> ChromiumEnvMultiPlatf ormTestsTypes; 116 typedef ::testing::Types<ChromiumEnvStdio, ChromiumEnvWin>
117 ChromiumEnvMultiPlatformTestsTypes;
107 #else 118 #else
108 typedef ::testing::Types<ChromiumEnvStdio> ChromiumEnvMultiPlatformTestsTypes; 119 typedef ::testing::Types<ChromiumEnvStdio> ChromiumEnvMultiPlatformTestsTypes;
109 #endif 120 #endif
110 TYPED_TEST_CASE(ChromiumEnvMultiPlatformTests, ChromiumEnvMultiPlatformTestsType s); 121 TYPED_TEST_CASE(ChromiumEnvMultiPlatformTests,
122 ChromiumEnvMultiPlatformTestsTypes);
111 123
112 TYPED_TEST(ChromiumEnvMultiPlatformTests, DirectorySyncing) { 124 TYPED_TEST(ChromiumEnvMultiPlatformTests, DirectorySyncing) {
113 MyEnv<TypeParam> env; 125 MyEnv<TypeParam> env;
114 126
115 base::ScopedTempDir dir; 127 base::ScopedTempDir dir;
116 ASSERT_TRUE(dir.CreateUniqueTempDir()); 128 ASSERT_TRUE(dir.CreateUniqueTempDir());
117 base::FilePath dir_path = dir.path(); 129 base::FilePath dir_path = dir.path();
118 std::string some_data = "some data"; 130 std::string some_data = "some data";
119 Slice data = some_data; 131 Slice data = some_data;
120 132
121 std::string manifest_file_name = 133 std::string manifest_file_name = leveldb_env::FilePathToString(
122 FilePathToString(dir_path.Append(FILE_PATH_LITERAL("MANIFEST-001"))); 134 dir_path.Append(FILE_PATH_LITERAL("MANIFEST-001")));
123 WritableFile* manifest_file_ptr; 135 WritableFile* manifest_file_ptr;
124 Status s = env.NewWritableFile(manifest_file_name, &manifest_file_ptr); 136 Status s = env.NewWritableFile(manifest_file_name, &manifest_file_ptr);
125 EXPECT_TRUE(s.ok()); 137 EXPECT_TRUE(s.ok());
126 scoped_ptr<WritableFile> manifest_file(manifest_file_ptr); 138 scoped_ptr<WritableFile> manifest_file(manifest_file_ptr);
127 manifest_file->Append(data); 139 manifest_file->Append(data);
128 EXPECT_EQ(0, env.directory_syncs()); 140 EXPECT_EQ(0, env.directory_syncs());
129 manifest_file->Append(data); 141 manifest_file->Append(data);
130 EXPECT_EQ(0, env.directory_syncs()); 142 EXPECT_EQ(0, env.directory_syncs());
131 143
132 std::string sst_file_name = 144 std::string sst_file_name = leveldb_env::FilePathToString(
133 FilePathToString(dir_path.Append(FILE_PATH_LITERAL("000003.sst"))); 145 dir_path.Append(FILE_PATH_LITERAL("000003.sst")));
134 WritableFile* sst_file_ptr; 146 WritableFile* sst_file_ptr;
135 s = env.NewWritableFile(sst_file_name, &sst_file_ptr); 147 s = env.NewWritableFile(sst_file_name, &sst_file_ptr);
136 EXPECT_TRUE(s.ok()); 148 EXPECT_TRUE(s.ok());
137 scoped_ptr<WritableFile> sst_file(sst_file_ptr); 149 scoped_ptr<WritableFile> sst_file(sst_file_ptr);
138 sst_file->Append(data); 150 sst_file->Append(data);
139 EXPECT_EQ(0, env.directory_syncs()); 151 EXPECT_EQ(0, env.directory_syncs());
140 152
141 manifest_file->Append(data); 153 manifest_file->Append(data);
142 EXPECT_EQ(1, env.directory_syncs()); 154 EXPECT_EQ(1, env.directory_syncs());
143 manifest_file->Append(data); 155 manifest_file->Append(data);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 EXPECT_TRUE(status.ok()); 260 EXPECT_TRUE(status.ok());
249 EXPECT_EQ(1U, result.size()); 261 EXPECT_EQ(1U, result.size());
250 262
251 // And a second time should also return one result 263 // And a second time should also return one result
252 status = env->GetChildren(dir.AsUTF8Unsafe(), &result); 264 status = env->GetChildren(dir.AsUTF8Unsafe(), &result);
253 EXPECT_TRUE(status.ok()); 265 EXPECT_TRUE(status.ok());
254 EXPECT_EQ(1U, result.size()); 266 EXPECT_EQ(1U, result.size());
255 } 267 }
256 268
257 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); } 269 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698