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.
5 #include "helpers/memenv/memenv.h"
7 #include "db/db_impl.h"
8 #include "leveldb/db.h"
9 #include "leveldb/env.h"
10 #include "util/testharness.h"
21 : env_(NewMemEnv(Env::Default())) {
28 TEST(MemEnvTest
, Basics
) {
30 WritableFile
* writable_file
;
31 std::vector
<std::string
> children
;
33 ASSERT_OK(env_
->CreateDir("/dir"));
35 // Check that the directory is empty.
36 ASSERT_TRUE(!env_
->FileExists("/dir/non_existent"));
37 ASSERT_TRUE(!env_
->GetFileSize("/dir/non_existent", &file_size
).ok());
38 ASSERT_OK(env_
->GetChildren("/dir", &children
));
39 ASSERT_EQ(0, children
.size());
42 ASSERT_OK(env_
->NewWritableFile("/dir/f", &writable_file
));
43 ASSERT_OK(env_
->GetFileSize("/dir/f", &file_size
));
44 ASSERT_EQ(0, file_size
);
47 // Check that the file exists.
48 ASSERT_TRUE(env_
->FileExists("/dir/f"));
49 ASSERT_OK(env_
->GetFileSize("/dir/f", &file_size
));
50 ASSERT_EQ(0, file_size
);
51 ASSERT_OK(env_
->GetChildren("/dir", &children
));
52 ASSERT_EQ(1, children
.size());
53 ASSERT_EQ("f", children
[0]);
56 ASSERT_OK(env_
->NewWritableFile("/dir/f", &writable_file
));
57 ASSERT_OK(writable_file
->Append("abc"));
60 // Check that append works.
61 ASSERT_OK(env_
->NewAppendableFile("/dir/f", &writable_file
));
62 ASSERT_OK(env_
->GetFileSize("/dir/f", &file_size
));
63 ASSERT_EQ(3, file_size
);
64 ASSERT_OK(writable_file
->Append("hello"));
67 // Check for expected size.
68 ASSERT_OK(env_
->GetFileSize("/dir/f", &file_size
));
69 ASSERT_EQ(8, file_size
);
71 // Check that renaming works.
72 ASSERT_TRUE(!env_
->RenameFile("/dir/non_existent", "/dir/g").ok());
73 ASSERT_OK(env_
->RenameFile("/dir/f", "/dir/g"));
74 ASSERT_TRUE(!env_
->FileExists("/dir/f"));
75 ASSERT_TRUE(env_
->FileExists("/dir/g"));
76 ASSERT_OK(env_
->GetFileSize("/dir/g", &file_size
));
77 ASSERT_EQ(8, file_size
);
79 // Check that opening non-existent file fails.
80 SequentialFile
* seq_file
;
81 RandomAccessFile
* rand_file
;
82 ASSERT_TRUE(!env_
->NewSequentialFile("/dir/non_existent", &seq_file
).ok());
83 ASSERT_TRUE(!seq_file
);
84 ASSERT_TRUE(!env_
->NewRandomAccessFile("/dir/non_existent", &rand_file
).ok());
85 ASSERT_TRUE(!rand_file
);
87 // Check that deleting works.
88 ASSERT_TRUE(!env_
->DeleteFile("/dir/non_existent").ok());
89 ASSERT_OK(env_
->DeleteFile("/dir/g"));
90 ASSERT_TRUE(!env_
->FileExists("/dir/g"));
91 ASSERT_OK(env_
->GetChildren("/dir", &children
));
92 ASSERT_EQ(0, children
.size());
93 ASSERT_OK(env_
->DeleteDir("/dir"));
96 TEST(MemEnvTest
, ReadWrite
) {
97 WritableFile
* writable_file
;
98 SequentialFile
* seq_file
;
99 RandomAccessFile
* rand_file
;
103 ASSERT_OK(env_
->CreateDir("/dir"));
105 ASSERT_OK(env_
->NewWritableFile("/dir/f", &writable_file
));
106 ASSERT_OK(writable_file
->Append("hello "));
107 ASSERT_OK(writable_file
->Append("world"));
108 delete writable_file
;
110 // Read sequentially.
111 ASSERT_OK(env_
->NewSequentialFile("/dir/f", &seq_file
));
112 ASSERT_OK(seq_file
->Read(5, &result
, scratch
)); // Read "hello".
113 ASSERT_EQ(0, result
.compare("hello"));
114 ASSERT_OK(seq_file
->Skip(1));
115 ASSERT_OK(seq_file
->Read(1000, &result
, scratch
)); // Read "world".
116 ASSERT_EQ(0, result
.compare("world"));
117 ASSERT_OK(seq_file
->Read(1000, &result
, scratch
)); // Try reading past EOF.
118 ASSERT_EQ(0, result
.size());
119 ASSERT_OK(seq_file
->Skip(100)); // Try to skip past end of file.
120 ASSERT_OK(seq_file
->Read(1000, &result
, scratch
));
121 ASSERT_EQ(0, result
.size());
125 ASSERT_OK(env_
->NewRandomAccessFile("/dir/f", &rand_file
));
126 ASSERT_OK(rand_file
->Read(6, 5, &result
, scratch
)); // Read "world".
127 ASSERT_EQ(0, result
.compare("world"));
128 ASSERT_OK(rand_file
->Read(0, 5, &result
, scratch
)); // Read "hello".
129 ASSERT_EQ(0, result
.compare("hello"));
130 ASSERT_OK(rand_file
->Read(10, 100, &result
, scratch
)); // Read "d".
131 ASSERT_EQ(0, result
.compare("d"));
134 ASSERT_TRUE(!rand_file
->Read(1000, 5, &result
, scratch
).ok());
138 TEST(MemEnvTest
, Locks
) {
141 // These are no-ops, but we test they return success.
142 ASSERT_OK(env_
->LockFile("some file", &lock
));
143 ASSERT_OK(env_
->UnlockFile(lock
));
146 TEST(MemEnvTest
, Misc
) {
147 std::string test_dir
;
148 ASSERT_OK(env_
->GetTestDirectory(&test_dir
));
149 ASSERT_TRUE(!test_dir
.empty());
151 WritableFile
* writable_file
;
152 ASSERT_OK(env_
->NewWritableFile("/a/b", &writable_file
));
154 // These are no-ops, but we test they return success.
155 ASSERT_OK(writable_file
->Sync());
156 ASSERT_OK(writable_file
->Flush());
157 ASSERT_OK(writable_file
->Close());
158 delete writable_file
;
161 TEST(MemEnvTest
, LargeWrite
) {
162 const size_t kWriteSize
= 300 * 1024;
163 char* scratch
= new char[kWriteSize
* 2];
165 std::string write_data
;
166 for (size_t i
= 0; i
< kWriteSize
; ++i
) {
167 write_data
.append(1, static_cast<char>(i
));
170 WritableFile
* writable_file
;
171 ASSERT_OK(env_
->NewWritableFile("/dir/f", &writable_file
));
172 ASSERT_OK(writable_file
->Append("foo"));
173 ASSERT_OK(writable_file
->Append(write_data
));
174 delete writable_file
;
176 SequentialFile
* seq_file
;
178 ASSERT_OK(env_
->NewSequentialFile("/dir/f", &seq_file
));
179 ASSERT_OK(seq_file
->Read(3, &result
, scratch
)); // Read "foo".
180 ASSERT_EQ(0, result
.compare("foo"));
183 std::string read_data
;
184 while (read
< kWriteSize
) {
185 ASSERT_OK(seq_file
->Read(kWriteSize
- read
, &result
, scratch
));
186 read_data
.append(result
.data(), result
.size());
187 read
+= result
.size();
189 ASSERT_TRUE(write_data
== read_data
);
194 TEST(MemEnvTest
, DBTest
) {
196 options
.create_if_missing
= true;
200 const Slice keys
[] = {Slice("aaa"), Slice("bbb"), Slice("ccc")};
201 const Slice vals
[] = {Slice("foo"), Slice("bar"), Slice("baz")};
203 ASSERT_OK(DB::Open(options
, "/dir/db", &db
));
204 for (size_t i
= 0; i
< 3; ++i
) {
205 ASSERT_OK(db
->Put(WriteOptions(), keys
[i
], vals
[i
]));
208 for (size_t i
= 0; i
< 3; ++i
) {
210 ASSERT_OK(db
->Get(ReadOptions(), keys
[i
], &res
));
211 ASSERT_TRUE(res
== vals
[i
]);
214 Iterator
* iterator
= db
->NewIterator(ReadOptions());
215 iterator
->SeekToFirst();
216 for (size_t i
= 0; i
< 3; ++i
) {
217 ASSERT_TRUE(iterator
->Valid());
218 ASSERT_TRUE(keys
[i
] == iterator
->key());
219 ASSERT_TRUE(vals
[i
] == iterator
->value());
222 ASSERT_TRUE(!iterator
->Valid());
225 DBImpl
* dbi
= reinterpret_cast<DBImpl
*>(db
);
226 ASSERT_OK(dbi
->TEST_CompactMemTable());
228 for (size_t i
= 0; i
< 3; ++i
) {
230 ASSERT_OK(db
->Get(ReadOptions(), keys
[i
], &res
));
231 ASSERT_TRUE(res
== vals
[i
]);
237 } // namespace leveldb
239 int main(int argc
, char** argv
) {
240 return leveldb::test::RunAllTests();