persist_test.cpp 4.1 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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 qiling on 2022
//

#include <string.h>
#include "gtest/gtest.h"
#include "storage/persist/persist.h"

const int MAX_LEN = 50;

TEST(test_persist, test_persist_file_io)
{
  std::string file_name_1 = "test_persist_file_io-file_name_1";
  std::string file_name_2 = "test_persist_file_io-file_name_2";
  PersistHandler persist_handler;
  PersistHandler persist_handler_2;
  RC rc;

  // prepare
  remove(file_name_1.c_str());
  remove(file_name_2.c_str());

  // create
  rc = persist_handler.create_file(nullptr);
  ASSERT_EQ(rc, RC::FILE_NAME);

  rc = persist_handler.create_file(file_name_1.c_str());
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_NE(access(file_name_1.c_str(), F_OK), -1);

  rc = persist_handler.create_file(file_name_2.c_str());
  ASSERT_EQ(rc, RC::FILE_BOUND);

  rc = persist_handler_2.create_file("");
  ASSERT_EQ(rc, RC::FILE_CREATE);

  // open
  rc = persist_handler_2.open_file();
  ASSERT_EQ(rc, RC::FILE_NAME);

  rc = persist_handler_2.open_file("//**");
  ASSERT_EQ(rc, RC::FILE_OPEN);

  rc = persist_handler.open_file(file_name_2.c_str());
  ASSERT_EQ(rc, RC::FILE_BOUND);

  rc = persist_handler.open_file();
  ASSERT_EQ(rc, RC::SUCCESS);

  // write
  std::string str_1 = "this is a string 001. ";
  std::string str_2 = "this is a string 002002. ";
  std::string str_3 = "THIS IS A STRING 003. ";
  int64_t write_size = 0;

  rc = persist_handler.write_file(str_1.size(), str_1.c_str(), &write_size);
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(write_size, str_1.size());

  rc = persist_handler.append(str_2.size(), str_2.c_str(), &write_size);
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(write_size, str_2.size());

  rc = persist_handler.write_at(str_1.size() + str_2.size(), str_3.size(), str_3.c_str(), &write_size);
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(write_size, str_3.size());

  rc = persist_handler.close_file();
  ASSERT_EQ(rc, RC::SUCCESS);

  rc = persist_handler.append(str_3.size(), str_3.c_str(), &write_size);
  ASSERT_EQ(rc, RC::FILE_NOT_OPENED);

  rc = persist_handler.open_file();
  ASSERT_EQ(rc, RC::SUCCESS);

  rc = persist_handler_2.write_at(0, str_3.size(), str_3.c_str(), &write_size);
  ASSERT_EQ(rc, RC::FILE_NOT_EXIST);

  // read & seek
  int64_t read_size = 0;
  char buf[MAX_LEN] = {0};

  rc = persist_handler.read_at(str_1.size(), str_2.size(), buf, &read_size);
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(read_size, str_2.size());
  ASSERT_EQ(strnlen(buf, MAX_LEN), str_2.size());
  ASSERT_EQ(strncmp(buf, str_2.c_str(), MAX_LEN), 0);

  rc = persist_handler.seek(0);
  ASSERT_EQ(rc, RC::SUCCESS);

  rc = persist_handler.write_file(str_3.size(), str_3.c_str(), &write_size);
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(write_size, str_3.size());

  memset(buf, 0, MAX_LEN);

  rc = persist_handler.seek(0);
  ASSERT_EQ(rc, RC::SUCCESS);

  rc = persist_handler.read_file(str_3.size(), buf, &read_size);
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(read_size, str_3.size());
  ASSERT_EQ(strnlen(buf, MAX_LEN), str_3.size());
  ASSERT_EQ(strncmp(buf, str_3.c_str(), MAX_LEN), 0);

  rc = persist_handler_2.read_at(0, str_3.size(), buf, &read_size);
  ASSERT_EQ(rc, RC::FILE_NOT_EXIST);

  // close
  rc = persist_handler.close_file();
  ASSERT_EQ(rc, RC::SUCCESS);

  rc = persist_handler_2.close_file();
  ASSERT_EQ(rc, RC::SUCCESS);

  // remove
  rc = persist_handler.remove_file();
  ASSERT_EQ(rc, RC::SUCCESS);
  ASSERT_EQ(access(file_name_1.c_str(), F_OK), -1);
}

int main(int argc, char **argv)
{

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

  // 调用RUN_ALL_TESTS()运行所有测试用例
  // main函数返回RUN_ALL_TESTS()的运行结果
  return RUN_ALL_TESTS();
}