Add unit tests for most of util.cpp

This commit is contained in:
2021-07-30 13:22:23 -04:00
parent 49484cf2f0
commit ec4d5fc3da
7 changed files with 118 additions and 63 deletions

View File

@@ -16,8 +16,26 @@
namespace util {
stringvec split(const std::string &s, char sep) {
stringvec result;
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2) {
IdVector result(v1.size() + v2.size());
int next = 0;
for (int64_t id : v1) result[next++] = id;
for (int64_t id : v2) result[next++] = id;
std::sort(result.begin(), result.end());
int64_t prev = -1;
int64_t count = 0;
for (int64_t id : result) {
if (id != prev) {
prev = id;
result[count++] = id;
}
}
result.resize(count);
return result;
}
StringVec split(const std::string &s, char sep) {
StringVec result;
int start = 0;
for (int i = 0; i < int(s.size()); i++) {
if (s[i] == sep) {
@@ -47,12 +65,14 @@ std::string toupper(std::string input) {
bool validinteger(const std::string &value) {
char *endptr;
if (value.size() == 0) return false;
strtoll(value.c_str(), &endptr, 10);
return (endptr == value.c_str() + value.size());
}
int64_t strtoint(const std::string &value, int64_t errval) {
char *endptr;
if (value.size() == 0) return errval;
int64_t result = strtoll(value.c_str(), &endptr, 10);
if (endptr == value.c_str() + value.size()) {
return result;
@@ -63,6 +83,7 @@ int64_t strtoint(const std::string &value, int64_t errval) {
double strtodouble(const std::string &value) {
char *endptr;
if (value.size() == 0) return std::nan("");
double result = strtod(value.c_str(), &endptr);
if (endptr == value.c_str() + value.size()) {
return result;
@@ -87,6 +108,11 @@ std::string trim(std::string s) {
return ltrim(rtrim(s));
}
double distance_squared(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
return dx*dx + dy*dy;
}
std::string get_file_contents(const std::string &fn) {
std::ifstream fs(fn);
@@ -95,8 +121,8 @@ std::string get_file_contents(const std::string &fn) {
return buffer.str();
}
stringvec get_file_lines(const std::string &path) {
stringvec result;
StringVec get_file_lines(const std::string &path) {
StringVec result;
std::ifstream f;
f.open(path);
if (f) {
@@ -120,12 +146,6 @@ std::string get_file_fingerprint(const std::string &fn) {
return "";
}
double distance_squared(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
return dx*dx + dy*dy;
}
std::string XYZ::debug_string() const {
std::ostringstream oss;
oss << "(" << x << "," << y << "," << z << ")";
@@ -153,3 +173,73 @@ std::ostream &operator<<(std::ostream &oss, const util::hex8 &v) {
oss << "0x" << std::setw(2) << std::setfill('0') << std::hex;
return oss;
}
LuaDefine(unittests_util, "c") {
// Test the unioning of ID vectors.
util::IdVector idv1,idv2;
idv1.push_back(1);
idv1.push_back(6);
idv1.push_back(4);
idv2.push_back(5);
idv2.push_back(1);
idv2.push_back(6);
util::IdVector joined = util::sort_union_id_vectors(idv1, idv2);
LuaAssert(L, joined.size() == 4);
LuaAssert(L, joined[0] == 1);
LuaAssert(L, joined[1] == 4);
LuaAssert(L, joined[2] == 5);
LuaAssert(L, joined[3] == 6);
// Test the string split routine.
util::StringVec sv1 = util::split("foo,bar,baz", ',');
LuaAssert(L, sv1.size() == 3);
LuaAssert(L, sv1[0] == "foo");
LuaAssert(L, sv1[1] == "bar");
LuaAssert(L, sv1[2] == "baz");
util::StringVec sv2 = util::split(",foo,,bar", ',');
LuaAssert(L, sv2.size() == 4);
LuaAssert(L, sv2[0]=="");
LuaAssert(L, sv2[1]=="foo");
LuaAssert(L, sv2[2]=="");
LuaAssert(L, sv2[3]=="bar");
// test toupper and tolower
LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
LuaAssert(L, util::tolower("fooBar") == "foobar");
// test validinteger, strtoint, strtodouble
LuaAssert(L, util::validinteger("123") == true);
LuaAssert(L, util::validinteger("123.4") == false);
LuaAssert(L, util::validinteger("12ab") == false);
LuaAssert(L, util::validinteger("") == false);
LuaAssert(L, util::strtoint("123", -5) == 123);
LuaAssert(L, util::strtoint("123.4", -5) == -5);
LuaAssert(L, util::strtoint("12ab", -5) == -5);
LuaAssert(L, util::strtoint("", -5) == -5);
LuaAssert(L, util::strtodouble("123.5") == 123.5);
LuaAssert(L, std::isnan(util::strtodouble("12ab")));
LuaAssert(L, std::isnan(util::strtodouble("")));
// Test trim, ltrim, rtrim
LuaAssert(L, util::ltrim(" foo ") == "foo ");
LuaAssert(L, util::rtrim(" foo ") == " foo");
LuaAssert(L, util::trim(" foo ") == "foo");
LuaAssert(L, util::trim("foo") == "foo");
LuaAssert(L, util::trim("") == "");
// Test distance_squared
LuaAssert(L, util::distance_squared(1, 1, 5, 4) == 25.0);
LuaAssert(L, util::distance_squared(5, 4, 1, 1) == 25.0);
// Test XYZ.
util::XYZ xyza(3,4,5), xyzb(3,4,5), xyzc(3,4,6);
LuaAssert(L, xyza.x == 3);
LuaAssert(L, xyza.y == 4);
LuaAssert(L, xyza.z == 5);
LuaAssert(L, xyza == xyzb);
LuaAssert(L, xyza != xyzc);
LuaAssert(L, xyza.debug_string() == "(3,4,5)");
return 0;
}