Skip to content

Commit

Permalink
Added unit tests for the pcp::get_pcp_runtime_version overloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcolby committed Apr 19, 2014
1 parent 88ce83d commit d619af2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ add_executable(test
${PROJECT_SOURCE_DIR}/src/fake_libpcp.cpp
${PROJECT_SOURCE_DIR}/src/fake_libpcp-pmda.cpp
${PROJECT_SOURCE_DIR}/src/test_atom.cpp
${PROJECT_SOURCE_DIR}/src/test_config.cpp
${PROJECT_SOURCE_DIR}/src/test_exception.cpp
${PROJECT_SOURCE_DIR}/src/test_instance_domain.cpp
${PROJECT_SOURCE_DIR}/src/test_metric_cluster.cpp
Expand Down
10 changes: 8 additions & 2 deletions test/unit/src/fake_libpcp.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright Paul Colby 2013.
// Copyright Paul Colby 2013-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "fake_libpcp.h"

// Boost headers that depend on boost/cstdint.hpp must be included before any
// PCP headers, because pcp/config.h sets a number of macros like ULONGLONG_MAX
// which fool Boost into thinking we're on an unknown, non-standard platform.
Expand All @@ -12,6 +14,8 @@
#include <stdexcept>
#include <string>

std::map<std::string, char *> fake_pm_config;

extern "C" {

int pmDebug;
Expand Down Expand Up @@ -46,7 +50,9 @@ const char *pmErrStr(int code)

char *pmGetConfig(const char *variable)
{
return strdup(variable);
const std::map<std::string, char *>::const_iterator iter =
fake_pm_config.find(variable);
return (iter == fake_pm_config.end()) ? strdup(variable) : iter->second;
}

void __pmNotifyErr(int /*priority*/, const char */*message*/, ...)
Expand Down
10 changes: 10 additions & 0 deletions test/unit/src/fake_libpcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Paul Colby 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <map>
#include <string>

/// @brief Test values to be returned by our mock pmGetConfig implementation.
extern std::map<std::string, char *> fake_pm_config;
2 changes: 1 addition & 1 deletion test/unit/src/test_atom.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Paul Colby 2013.
// Copyright Paul Colby 2013-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand Down
53 changes: 53 additions & 0 deletions test/unit/src/test_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright Paul Colby 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "fake_libpcp.h"

#include "pcp-cpp/config.hpp"

#include "gtest/gtest.h"

TEST(get_pcp_runtime_version, numeric) {
fake_pm_config.clear();

// Typical PCP version strings.
fake_pm_config["PCP_VERSION"] = const_cast<char *>("1.2.3");
EXPECT_EQ(0x10203, pcp::get_pcp_runtime_version<int32_t>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("4.67.78");
EXPECT_EQ(0x4434E, pcp::get_pcp_runtime_version<int32_t>());

// Edge cases.
fake_pm_config["PCP_VERSION"] = const_cast<char *>("0.0.0");
EXPECT_EQ(0, pcp::get_pcp_runtime_version<int32_t>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("255.255.255");
EXPECT_EQ(0xFFFFFF, pcp::get_pcp_runtime_version<int32_t>());

// Funky / bad / invalid versions.
fake_pm_config["PCP_VERSION"] = const_cast<char *>("not a version string");
EXPECT_EQ(0, pcp::get_pcp_runtime_version<int32_t>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("1");
EXPECT_EQ(0, pcp::get_pcp_runtime_version<int32_t>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("1.2");
EXPECT_EQ(0, pcp::get_pcp_runtime_version<int32_t>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("1.2.4.5.6.7.8.9");
EXPECT_EQ(0x10209, pcp::get_pcp_runtime_version<int32_t>());

fake_pm_config.clear();
}

TEST(get_pcp_runtime_version, string) {
fake_pm_config.clear();

// The get_pcp_runtime_version<char *> specialisation simply returns the
// pmGetValue value verbatim.
fake_pm_config["PCP_VERSION"] = const_cast<char *>("");
EXPECT_STREQ("", pcp::get_pcp_runtime_version<char *>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("1.2.3");
EXPECT_STREQ("1.2.3", pcp::get_pcp_runtime_version<char *>());
fake_pm_config["PCP_VERSION"] = const_cast<char *>("4.5.6");
EXPECT_STREQ("4.5.6", pcp::get_pcp_runtime_version<char *>());

fake_pm_config.clear();
}
2 changes: 1 addition & 1 deletion test/unit/src/test_metrics_description.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Paul Colby 2013.
// Copyright Paul Colby 2013-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand Down

0 comments on commit d619af2

Please sign in to comment.