bp_manager_test.cpp 3.6 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by wangyunlai.wyl on 2021
//

#include "storage/default/disk_buffer_pool.h"
#include "gtest/gtest.h"

羽飞's avatar
羽飞 已提交
18
void test_get(BPFrameManager &frame_manager)
L
Longda 已提交
19
{
羽飞's avatar
羽飞 已提交
20 21 22
  const int file_desc = 0;
  PageNum page_num = 1;
  Frame *frame1 = frame_manager.alloc(file_desc, page_num);
羽飞's avatar
羽飞 已提交
23 24
  ASSERT_NE(frame1, nullptr);

羽飞's avatar
羽飞 已提交
25 26
  frame1->set_file_desc(file_desc);
  frame1->set_page_num(page_num);
羽飞's avatar
羽飞 已提交
27

羽飞's avatar
羽飞 已提交
28
  ASSERT_EQ(frame1, frame_manager.get(file_desc, 1));
羽飞's avatar
羽飞 已提交
29

羽飞's avatar
羽飞 已提交
30
  Frame *frame2 = frame_manager.alloc(file_desc, 2);
羽飞's avatar
羽飞 已提交
31
  ASSERT_NE(frame2, nullptr);
羽飞's avatar
羽飞 已提交
32 33
  frame2->set_file_desc(0);
  frame2->set_page_num(2);
羽飞's avatar
羽飞 已提交
34

羽飞's avatar
羽飞 已提交
35
  ASSERT_EQ(frame1, frame_manager.get(file_desc, 1));
羽飞's avatar
羽飞 已提交
36

羽飞's avatar
羽飞 已提交
37
  Frame *frame3 = frame_manager.alloc(file_desc, 3);
羽飞's avatar
羽飞 已提交
38
  ASSERT_NE(frame3, nullptr);
羽飞's avatar
羽飞 已提交
39
  frame3->set_file_desc(file_desc);
羽飞's avatar
羽飞 已提交
40
  frame3->set_page_num(3);
羽飞's avatar
羽飞 已提交
41

羽飞's avatar
羽飞 已提交
42
  frame2 = frame_manager.get(file_desc, 2);
L
Longda 已提交
43
  ASSERT_NE(frame2, nullptr);
羽飞's avatar
羽飞 已提交
44

羽飞's avatar
羽飞 已提交
45 46
  Frame *frame4 = frame_manager.alloc(file_desc, 4);
  frame4->set_file_desc(file_desc);
羽飞's avatar
羽飞 已提交
47
  frame4->set_page_num(4);
羽飞's avatar
羽飞 已提交
48

羽飞's avatar
羽飞 已提交
49 50
  frame_manager.free(file_desc, frame1->page_num(), frame1);
  frame1 = frame_manager.get(file_desc, 1);
羽飞's avatar
羽飞 已提交
51 52
  ASSERT_EQ(frame1, nullptr);

羽飞's avatar
羽飞 已提交
53
  ASSERT_EQ(frame3, frame_manager.get(file_desc, 3));
L
Longda 已提交
54

羽飞's avatar
羽飞 已提交
55
  ASSERT_EQ(frame4, frame_manager.get(file_desc, 4));
L
Longda 已提交
56

羽飞's avatar
羽飞 已提交
57 58 59
  frame_manager.free(file_desc, frame2->page_num(), frame2);
  frame_manager.free(file_desc, frame3->page_num(), frame3);
  frame_manager.free(file_desc, frame4->page_num(), frame4);
羽飞's avatar
羽飞 已提交
60

羽飞's avatar
羽飞 已提交
61 62 63
  ASSERT_EQ(nullptr, frame_manager.get(file_desc, 2));
  ASSERT_EQ(nullptr, frame_manager.get(file_desc, 3));
  ASSERT_EQ(nullptr, frame_manager.get(file_desc, 4));
羽飞's avatar
羽飞 已提交
64 65
}

羽飞's avatar
羽飞 已提交
66
void test_alloc(BPFrameManager &frame_manager)
L
Longda 已提交
67 68 69
{
  std::list<Frame *> used_list;

羽飞's avatar
羽飞 已提交
70 71 72 73 74 75 76 77 78 79 80
  const int file_desc = 0;
  size_t size = 0;
  for ( ; true; size++) {
    Frame *item = frame_manager.alloc(file_desc, size);
    if (item != nullptr) {
      item->set_file_desc(file_desc);
      item->set_page_num(size);
      used_list.push_back(item);
    } else {
      break;
    }
L
Longda 已提交
81
  }
羽飞's avatar
羽飞 已提交
82
  size++;
L
Longda 已提交
83

羽飞's avatar
羽飞 已提交
84
  ASSERT_EQ(used_list.size(), frame_manager.frame_num());
L
Longda 已提交
85

羽飞's avatar
羽飞 已提交
86 87
  for (size_t i = size; i < size * 2; i++) {
    Frame *item = frame_manager.alloc(file_desc, i);
L
Longda 已提交
88 89 90 91

    ASSERT_EQ(item, nullptr);
  }

羽飞's avatar
羽飞 已提交
92 93
  for (size_t i = size * 2; i < size * 10; i++) {
    if (i % 2 == 0) { // from size * 2, that free one frame first
L
Longda 已提交
94 95 96
      Frame *item = used_list.front();
      used_list.pop_front();

羽飞's avatar
羽飞 已提交
97 98
      RC rc = frame_manager.free(file_desc, item->page_num(), item);
      ASSERT_EQ(rc, RC::SUCCESS);
L
Longda 已提交
99
    } else {
羽飞's avatar
羽飞 已提交
100 101 102 103
      Frame *item = frame_manager.alloc(file_desc, i);
      ASSERT_NE(item, nullptr);
      item->set_file_desc(file_desc);
      item->set_page_num(i);
L
Longda 已提交
104 105 106
      used_list.push_back(item);
    }

羽飞's avatar
羽飞 已提交
107
    ASSERT_EQ(used_list.size(), frame_manager.frame_num());
L
Longda 已提交
108 109 110
  }
}

羽飞's avatar
羽飞 已提交
111
TEST(test_frame_manager, test_frame_manager_simple_lru)
L
Longda 已提交
112
{
羽飞's avatar
羽飞 已提交
113
  BPFrameManager frame_manager("Test");
羽飞's avatar
羽飞 已提交
114
  frame_manager.init(2);
L
Longda 已提交
115

羽飞's avatar
羽飞 已提交
116
  test_get(frame_manager);
L
Longda 已提交
117

羽飞's avatar
羽飞 已提交
118
  test_alloc(frame_manager);
L
Longda 已提交
119

羽飞's avatar
羽飞 已提交
120
  frame_manager.cleanup();
L
Longda 已提交
121
}
羽飞's avatar
羽飞 已提交
122

L
Longda 已提交
123 124
int main(int argc, char **argv)
{
羽飞's avatar
羽飞 已提交
125 126 127 128 129 130 131

  // 分析gtest程序的命令行参数
  testing::InitGoogleTest(&argc, argv);

  // 调用RUN_ALL_TESTS()运行所有测试用例
  // main函数返回RUN_ALL_TESTS()的运行结果
  return RUN_ALL_TESTS();
羽飞's avatar
羽飞 已提交
132
}