Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Adjust config tests for new C++ API
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszstolarczuk committed Jul 30, 2019
1 parent 423d984 commit ff9ca60
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 12 deletions.
6 changes: 4 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ set(TEST_FILES
pmemkv_test.cc
mock_tx_alloc.cc
engines/blackhole_test.cc
config/config.cc
config/config_c.cc
config/config_cpp.cc
config/json_to_config.cc
)
# add each engine's tests source files separately
Expand Down Expand Up @@ -193,7 +194,8 @@ set(memcheck_tests
CMapTest
VCMapTest
VSMapTest
ConfigTest
ConfigCTest
ConfigCppTest
JsonToConfigTest)

# set groups of tests to be run with Valgrind's helgrind and drd tools
Expand Down
14 changes: 6 additions & 8 deletions tests/config/config.cc → tests/config/config_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@
#include "../../src/libpmemkv.hpp"
#include "gtest/gtest.h"

using namespace pmem::kv;

class ConfigTest : public testing::Test {
class ConfigCTest : public testing::Test {
public:
pmemkv_config *config;

ConfigTest()
ConfigCTest()
{
config = pmemkv_config_new();
}

~ConfigTest()
~ConfigCTest()
{
if (config)
pmemkv_config_delete(config);
Expand All @@ -56,13 +54,13 @@ struct custom_type {
char b;
};

void deleter(custom_type *ct_ptr)
static void deleter(custom_type *ct_ptr)
{
ct_ptr->a = -1;
ct_ptr->b = '0';
}

TEST_F(ConfigTest, SimpleTest)
TEST_F(ConfigCTest, SimpleTest)
{
auto ret = pmemkv_config_put_string(config, "string", "abc");
ASSERT_EQ(ret, PMEMKV_STATUS_OK);
Expand Down Expand Up @@ -141,7 +139,7 @@ TEST_F(ConfigTest, SimpleTest)
delete ptr_deleter;
}

TEST_F(ConfigTest, IntegralConversion)
TEST_F(ConfigCTest, IntegralConversion)
{
auto ret = pmemkv_config_put_int64(config, "int", 123);
ASSERT_EQ(ret, PMEMKV_STATUS_OK);
Expand Down
205 changes: 205 additions & 0 deletions tests/config/config_cpp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
* Copyright 2019, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "../../src/libpmemkv.hpp"
#include "gtest/gtest.h"

using namespace pmem::kv;

class ConfigCppTest : public testing::Test {
public:
config *cfg;

ConfigCppTest()
{
cfg = new config();
}

~ConfigCppTest()
{
if (cfg)
delete cfg;
}
};

struct custom_type {
int a;
char b;
};

static void deleter(custom_type *ct_ptr)
{
ct_ptr->a = -1;
ct_ptr->b = '0';
}

TEST_F(ConfigCppTest, SimpleTest)
{
status s = cfg->put_string("string", "abc");
ASSERT_EQ(s, status::OK);

s = cfg->put_int64("int", 123);
ASSERT_EQ(s, status::OK);

s = cfg->put_double("double", 12.43);
ASSERT_EQ(s, status::OK);

custom_type *ptr = new custom_type;
ptr->a = 10;
ptr->b = 'a';
s = cfg->put_object("object_ptr", ptr, nullptr);
ASSERT_EQ(s, status::OK);

s = cfg->put_data("object", ptr);
ASSERT_EQ(s, status::OK);

int array[3] = {1, 15, 77};
s = cfg->put_data("array", array, 3);
ASSERT_EQ(s, status::OK);

custom_type *ptr_deleter = new custom_type;
ptr_deleter->a = 11;
ptr_deleter->b = 'b';
s = cfg->put_object("object_ptr_with_deleter", ptr_deleter,
(void (*)(void *)) & deleter);
ASSERT_EQ(s, status::OK);

std::string value_string;
s = cfg->get_string("string", value_string);
ASSERT_EQ(s, status::OK);
ASSERT_TRUE(value_string == "abc");

int64_t value_int;
s = cfg->get_int64("int", value_int);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(value_int, 123);

double value_double;
s = cfg->get_double("double", value_double);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(value_double, 12.43);

custom_type *value_custom_ptr;
s = cfg->get_object("object_ptr", value_custom_ptr);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(value_custom_ptr->a, 10);
ASSERT_EQ(value_custom_ptr->b, 'a');

custom_type *value_custom_ptr_deleter;
s = cfg->get_object("object_ptr_with_deleter", value_custom_ptr_deleter);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(value_custom_ptr_deleter->a, 11);
ASSERT_EQ(value_custom_ptr_deleter->b, 'b');

custom_type *value_custom;
size_t value_custom_count;
s = cfg->get_data("object", value_custom, value_custom_count);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(value_custom_count, 1);
ASSERT_EQ(value_custom->a, 10);
ASSERT_EQ(value_custom->b, 'a');

int *value_array;
size_t value_array_count;
s = cfg->get_data("array", value_array, value_array_count);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(value_array_count, 3);
ASSERT_EQ(value_array[0], 1);
ASSERT_EQ(value_array[1], 15);
ASSERT_EQ(value_array[2], 77);

int64_t none;
ASSERT_EQ(cfg->get_int64("non-existent", none), status::NOT_FOUND);

delete ptr;

delete cfg;
cfg = nullptr;

ASSERT_EQ(value_custom_ptr_deleter->a, -1);
ASSERT_EQ(value_custom_ptr_deleter->b, '0');

delete ptr_deleter;
}

TEST_F(ConfigCppTest, IntegralConversion)
{
status s = cfg->put_int64("int", 123);
ASSERT_EQ(s, status::OK);

s = cfg->put_uint64("uint", 123);
ASSERT_EQ(s, status::OK);

s = cfg->put_int64("negative-int", -123);
ASSERT_EQ(s, status::OK);

s = cfg->put_uint64("uint-max", std::numeric_limits<size_t>::max());
ASSERT_EQ(s, status::OK);

int64_t int_s;
s = cfg->get_int64("int", int_s);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(int_s, 123);

size_t int_us;
s = cfg->get_uint64("int", int_us);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(int_us, 123U);

int64_t uint_s;
s = cfg->get_int64("uint", uint_s);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(uint_s, 123);

size_t uint_us;
s = cfg->get_uint64("uint", uint_us);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(uint_us, 123U);

int64_t neg_int_s;
s = cfg->get_int64("negative-int", neg_int_s);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(neg_int_s, -123);

size_t neg_int_us;
s = cfg->get_uint64("negative-int", neg_int_us);
ASSERT_EQ(s, status::CONFIG_TYPE_ERROR);

int64_t uint_max_s;
s = cfg->get_int64("uint-max", uint_max_s);
ASSERT_EQ(s, status::CONFIG_TYPE_ERROR);

size_t uint_max_us;
s = cfg->get_uint64("uint-max", uint_max_us);
ASSERT_EQ(s, status::OK);
ASSERT_EQ(uint_max_us, std::numeric_limits<size_t>::max());
}
2 changes: 0 additions & 2 deletions tests/config/json_to_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
#include "../../src/libpmemkv.hpp"
#include "gtest/gtest.h"

using namespace pmem::kv;

class JsonToConfigTest : public testing::Test {
public:
pmemkv_config *config;
Expand Down

0 comments on commit ff9ca60

Please sign in to comment.