xref: /aosp_15_r20/external/leveldb/util/env_windows_test.cc (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
1 // Copyright (c) 2018 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 "gtest/gtest.h"
6 #include "leveldb/env.h"
7 #include "port/port.h"
8 #include "util/env_windows_test_helper.h"
9 #include "util/testutil.h"
10 
11 namespace leveldb {
12 
13 static const int kMMapLimit = 4;
14 
15 class EnvWindowsTest : public testing::Test {
16  public:
SetFileLimits(int mmap_limit)17   static void SetFileLimits(int mmap_limit) {
18     EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit);
19   }
20 
EnvWindowsTest()21   EnvWindowsTest() : env_(Env::Default()) {}
22 
23   Env* env_;
24 };
25 
TEST_F(EnvWindowsTest,TestOpenOnRead)26 TEST_F(EnvWindowsTest, TestOpenOnRead) {
27   // Write some test data to a single file that will be opened |n| times.
28   std::string test_dir;
29   ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
30   std::string test_file = test_dir + "/open_on_read.txt";
31 
32   FILE* f = std::fopen(test_file.c_str(), "w");
33   ASSERT_TRUE(f != nullptr);
34   const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
35   fputs(kFileData, f);
36   std::fclose(f);
37 
38   // Open test file some number above the sum of the two limits to force
39   // leveldb::WindowsEnv to switch from mapping the file into memory
40   // to basic file reading.
41   const int kNumFiles = kMMapLimit + 5;
42   leveldb::RandomAccessFile* files[kNumFiles] = {0};
43   for (int i = 0; i < kNumFiles; i++) {
44     ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(test_file, &files[i]));
45   }
46   char scratch;
47   Slice read_result;
48   for (int i = 0; i < kNumFiles; i++) {
49     ASSERT_LEVELDB_OK(files[i]->Read(i, 1, &read_result, &scratch));
50     ASSERT_EQ(kFileData[i], read_result[0]);
51   }
52   for (int i = 0; i < kNumFiles; i++) {
53     delete files[i];
54   }
55   ASSERT_LEVELDB_OK(env_->RemoveFile(test_file));
56 }
57 
58 }  // namespace leveldb
59 
60