gtest_kvdb.cpp 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>
16 17
#include <chrono>
#include <fstream>
18
#include <functional>
19 20
#include <string>
#include <thread>
21 22 23
#include "kvdb/kvdb_impl.h"
#include "kvdb/paddle_rocksdb.h"
#include "kvdb/rocksdb_impl.h"
24
class KVDBTest : public ::testing::Test {
25 26 27 28 29 30 31 32 33 34 35 36 37
 protected:
  void SetUp() override {}

  static void SetUpTestCase() {
    kvdb = std::make_shared<RocksKVDB>();
    dict_reader = std::make_shared<FileReader>();
    param_dict = std::make_shared<ParamDict>();
  }

  static AbsKVDBPtr kvdb;
  static FileReaderPtr dict_reader;
  static ParamDictPtr param_dict;
  static ParamDictMgr dict_mgr;
38 39
};
AbsKVDBPtr KVDBTest::kvdb;
40 41
FileReaderPtr KVDBTest::dict_reader;
ParamDictPtr KVDBTest::param_dict;
42 43 44 45 46 47
ParamDictMgr KVDBTest::dict_mgr;

void GenerateTestIn(std::string);
void UpdateTestIn(std::string);

TEST_F(KVDBTest, AbstractKVDB_Unit_Test) {
48 49 50 51 52 53 54 55 56
  kvdb->CreateDB();
  kvdb->SetDBName("test_kvdb");
  for (int i = 0; i < 100; i++) {
    kvdb->Set(std::to_string(i), std::to_string(i * 2));
  }
  for (int i = 0; i < 100; i++) {
    std::string val = kvdb->Get(std::to_string(i));
    ASSERT_EQ(val, std::to_string(i * 2));
  }
57
  kvdb->Close();
58 59
}

60
TEST_F(KVDBTest, FileReader_Unit_Test) {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  std::string test_in_filename = "abs_dict_reader_test_in.txt";
  GenerateTestIn(test_in_filename);
  dict_reader->SetFileName(test_in_filename);

  std::string md5_1 = dict_reader->GetMD5();
  std::chrono::system_clock::time_point timestamp_1 =
      dict_reader->GetTimeStamp();

  std::string md5_2 = dict_reader->GetMD5();
  std::chrono::system_clock::time_point timestamp_2 =
      dict_reader->GetTimeStamp();

  ASSERT_EQ(md5_1, md5_2);
  ASSERT_EQ(timestamp_1, timestamp_2);

  UpdateTestIn(test_in_filename);

  std::string md5_3 = dict_reader->GetMD5();
  std::chrono::system_clock::time_point timestamp_3 =
      dict_reader->GetTimeStamp();

  ASSERT_NE(md5_2, md5_3);
  ASSERT_NE(timestamp_2, timestamp_3);
84 85 86
}
#include <cmath>
void GenerateTestIn(std::string filename) {
87 88 89 90 91 92 93 94 95 96 97
  std::ifstream in_file(filename);
  if (in_file.good()) {
    in_file.close();
    std::string cmd = "rm -rf " + filename;
    system(cmd.c_str());
  }
  std::ofstream out_file(filename);
  for (size_t i = 0; i < 100000; i++) {
    out_file << i << " " << i << " ";
    for (size_t j = 0; j < 3; j++) {
      out_file << i << " ";
98
    }
99 100 101
    out_file << std::endl;
  }
  out_file.close();
102 103 104
}

void UpdateTestIn(std::string filename) {
105 106 107 108 109 110 111 112 113 114 115
  std::ifstream in_file(filename);
  if (in_file.good()) {
    in_file.close();
    std::string cmd = "rm -rf " + filename;
    system(cmd.c_str());
  }
  std::ofstream out_file(filename);
  for (size_t i = 0; i < 10000; i++) {
    out_file << i << " " << i << " ";
    for (size_t j = 0; j < 3; j++) {
      out_file << i + 1 << " ";
116
    }
117 118 119
    out_file << std::endl;
  }
  out_file.close();
120 121 122
}

int main(int argc, char** argv) {
123 124
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
125
}