作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

63 linhas
1.9 KiB

  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. #include "leveldb/env.h"
  5. #include "port/port.h"
  6. #include "util/env_windows_test_helper.h"
  7. #include "util/testharness.h"
  8. namespace leveldb {
  9. static const int kMMapLimit = 4;
  10. class EnvWindowsTest {
  11. public:
  12. Env* env_;
  13. EnvWindowsTest() : env_(Env::Default()) {}
  14. static void SetFileLimits(int mmap_limit) {
  15. EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit);
  16. }
  17. };
  18. TEST(EnvWindowsTest, TestOpenOnRead) {
  19. // Write some test data to a single file that will be opened |n| times.
  20. std::string test_dir;
  21. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  22. std::string test_file = test_dir + "/open_on_read.txt";
  23. FILE* f = fopen(test_file.c_str(), "w");
  24. ASSERT_TRUE(f != nullptr);
  25. const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
  26. fputs(kFileData, f);
  27. fclose(f);
  28. // Open test file some number above the sum of the two limits to force
  29. // leveldb::WindowsEnv to switch from mapping the file into memory
  30. // to basic file reading.
  31. const int kNumFiles = kMMapLimit + 5;
  32. leveldb::RandomAccessFile* files[kNumFiles] = {0};
  33. for (int i = 0; i < kNumFiles; i++) {
  34. ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));
  35. }
  36. char scratch;
  37. Slice read_result;
  38. for (int i = 0; i < kNumFiles; i++) {
  39. ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
  40. ASSERT_EQ(kFileData[i], read_result[0]);
  41. }
  42. for (int i = 0; i < kNumFiles; i++) {
  43. delete files[i];
  44. }
  45. ASSERT_OK(env_->DeleteFile(test_file));
  46. }
  47. } // namespace leveldb
  48. int main(int argc, char** argv) {
  49. // All tests currently run with the same read-only file limits.
  50. leveldb::EnvWindowsTest::SetFileLimits(leveldb::kMMapLimit);
  51. return leveldb::test::RunAllTests();
  52. }