inital commit
[c11concurrency-benchmarks.git] / mabain / src / unittest / memory_only_test.cpp
1 /**
2  * Copyright (C) 2018 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #include <gtest/gtest.h>
20
21 #include "../db.h"
22 #include "../resource_pool.h"
23
24 using namespace mabain;
25
26 namespace {
27
28 class MemoryOnlyTest : public ::testing::Test
29 {
30 public:
31     MemoryOnlyTest() {
32     }
33     virtual ~MemoryOnlyTest() {
34     }
35
36     virtual void SetUp() {
37         db = nullptr;
38     }
39     virtual void TearDown() {
40         if(db != nullptr) {
41             db->Close();
42             delete db;
43         }
44         ResourcePool::getInstance().RemoveAll();
45     }
46
47 protected:
48     DB *db;
49 };
50
51 TEST_F(MemoryOnlyTest, MemoryOnlyTest_test_constructor_1)
52 {
53     int options = CONSTS::WriterOptions() | CONSTS::MEMORY_ONLY_MODE;
54     db = new DB("test_constructor_1", options);
55     EXPECT_EQ(MBError::SUCCESS, db->Status());
56 }
57
58 TEST_F(MemoryOnlyTest, MemoryOnlyTest_test_constructor_2)
59 {
60     int options = CONSTS::ReaderOptions() | CONSTS::MEMORY_ONLY_MODE;
61     db = new DB("test_constructor_2", options);
62     EXPECT_EQ(MBError::NO_DB, db->Status());
63 }
64
65 TEST_F(MemoryOnlyTest, MemoryOnlyTest_test_add)
66 {
67     int options = CONSTS::WriterOptions() | CONSTS::MEMORY_ONLY_MODE;
68     db = new DB("test_add", options);
69
70     int num = 10000;
71     std::string key;
72     for(int i = 0; i < num; i++) {
73         key = std::string("test_add_") + std::to_string(i);
74         db->Add(key, key);
75     }
76
77     MBData mbd;
78     int rval;
79     std::string value;
80     for(int i = 0; i < num; i++) {
81         key = std::string("test_add_") + std::to_string(i);
82         rval = db->Find(key, mbd);
83         EXPECT_EQ(MBError::SUCCESS, rval); 
84         value = std::string((char *)mbd.buff, mbd.data_len);
85         EXPECT_EQ(key, value);
86     }
87 }
88
89 TEST_F(MemoryOnlyTest, MemoryOnlyTest_test_async)
90 {
91     int options = CONSTS::WriterOptions() | CONSTS::MEMORY_ONLY_MODE;
92     options |= CONSTS::ASYNC_WRITER_MODE;
93     db = new DB("test_async", options);
94
95     options = CONSTS::ReaderOptions() | CONSTS::MEMORY_ONLY_MODE;
96     DB db_r = DB("test_async", options);
97
98     db_r.Close();
99 }
100
101 }