commit 385891cd48efbd2c5e2a35c5632bd6cff015e1b6
parent 04dec7f699800309d334db7230e9d1ef8830c7ec
Author: cfillion <cfillion@users.noreply.github.com>
Date: Thu, 1 Jun 2017 22:00:51 -0400
api: add unit tests for CompareVersions
Diffstat:
2 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/src/api.cpp b/src/api.cpp
@@ -59,7 +59,7 @@ void APIDef::unregister(const char *key, void *ptr)
#define DEFARGS(r, macro, i, arg) \
BOOST_PP_IF(i, ",", BOOST_PP_EMPTY()) BOOST_PP_STRINGIZE(macro(arg))
-#define DEFAPI(type, name, args, help, ...) \
+#define DEFINE_API(type, name, args, help, ...) \
namespace name { \
static type cImpl(BOOST_PP_SEQ_FOR_EACH_I(ARGS, _, args)) __VA_ARGS__ \
void *reascriptImpl(void **argv, int argc) { \
@@ -77,7 +77,7 @@ void APIDef::unregister(const char *key, void *ptr)
"APIdef_" API_PREFIX #name, (void *)name::definition, \
}
-DEFAPI(bool, CompareVersions, ((const char*, ver1))((const char*, ver2))
+DEFINE_API(bool, CompareVersions, ((const char*, ver1))((const char*, ver2))
((int*, resultOut))((char*, errorOut))((int, errorOut_sz)),
"Compare version numbers. Returns 0 if both versions are equal,"
" a positive value if ver1 is higher than ver2"
@@ -91,6 +91,10 @@ DEFAPI(bool, CompareVersions, ((const char*, ver1))((const char*, ver2))
return true;
}
- snprintf(errorOut, errorOut_sz, "%s", error.c_str());
+ if(errorOut)
+ snprintf(errorOut, errorOut_sz, "%s", error.c_str());
+
+ *resultOut = 0;
+
return false;
});
diff --git a/test/api.cpp b/test/api.cpp
@@ -0,0 +1,44 @@
+#include <catch.hpp>
+
+#include "helper/io.hpp"
+
+#include <api.hpp>
+
+using namespace std;
+
+static const char *M = "[api]";
+
+TEST_CASE("CompareVersions", M) {
+ auto CompareVersions = (bool (*)(const char *, const char *,
+ int*, char *, int))API::CompareVersions.cImpl;
+
+ int result;
+ char error[255] = {};
+
+ SECTION("equal") {
+ REQUIRE(CompareVersions("1.0", "1.0", &result, error, sizeof(error)));
+ REQUIRE(result == 0);
+ REQUIRE(strcmp(error, "") == 0);
+ }
+
+ SECTION("lower") {
+ REQUIRE(CompareVersions("1.0", "2.0", &result, error, sizeof(error)));
+ REQUIRE(result < 0);
+ REQUIRE(strcmp(error, "") == 0);
+ }
+
+ SECTION("higher") {
+ REQUIRE(CompareVersions("2.0", "1.0", &result, error, sizeof(error)));
+ REQUIRE(result > 0);
+ REQUIRE(strcmp(error, "") == 0);
+ }
+
+ SECTION("invalid") {
+ REQUIRE_FALSE(CompareVersions("abc", "def", &result, error, sizeof(error)));
+ REQUIRE(result == 0);
+ REQUIRE(strcmp(error, "invalid version name 'abc'") == 0);
+ }
+
+ SECTION("invalid no error buffer")
+ REQUIRE_FALSE(CompareVersions("abc", "def", &result, nullptr, 0));
+}