Squashed 'src/leveldb/' changes from a31c8aa40..196962ff0
[bitcoinplatinum.git] / util / env_posix_test.cc
blob295f8ae4409f2a156c4e298cce4d6c8311e1e1b1
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 "leveldb/env.h"
7 #include "port/port.h"
8 #include "util/testharness.h"
9 #include "util/env_posix_test_helper.h"
11 namespace leveldb {
13 static const int kDelayMicros = 100000;
14 static const int kReadOnlyFileLimit = 4;
15 static const int kMMapLimit = 4;
17 class EnvPosixTest {
18 public:
19 Env* env_;
20 EnvPosixTest() : env_(Env::Default()) { }
22 static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
23 EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
24 EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit);
28 TEST(EnvPosixTest, TestOpenOnRead) {
29 // Write some test data to a single file that will be opened |n| times.
30 std::string test_dir;
31 ASSERT_OK(env_->GetTestDirectory(&test_dir));
32 std::string test_file = test_dir + "/open_on_read.txt";
34 FILE* f = fopen(test_file.c_str(), "w");
35 ASSERT_TRUE(f != NULL);
36 const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
37 fputs(kFileData, f);
38 fclose(f);
40 // Open test file some number above the sum of the two limits to force
41 // open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
42 const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
43 leveldb::RandomAccessFile* files[kNumFiles] = {0};
44 for (int i = 0; i < kNumFiles; i++) {
45 ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));
47 char scratch;
48 Slice read_result;
49 for (int i = 0; i < kNumFiles; i++) {
50 ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
51 ASSERT_EQ(kFileData[i], read_result[0]);
53 for (int i = 0; i < kNumFiles; i++) {
54 delete files[i];
56 ASSERT_OK(env_->DeleteFile(test_file));
59 } // namespace leveldb
61 int main(int argc, char** argv) {
62 // All tests currently run with the same read-only file limits.
63 leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
64 leveldb::kMMapLimit);
65 return leveldb::test::RunAllTests();