Add unit tests for most of util.cpp
This commit is contained in:
@@ -282,11 +282,11 @@ std::string AnimStep::debug_string() const {
|
|||||||
|
|
||||||
bool AnimStep::from_string(const std::string &config) {
|
bool AnimStep::from_string(const std::string &config) {
|
||||||
clear();
|
clear();
|
||||||
util::stringvec parts = util::split(config, ' ');
|
util::StringVec parts = util::split(config, ' ');
|
||||||
for (int i = 0; i < int(parts.size()); i++) {
|
for (int i = 0; i < int(parts.size()); i++) {
|
||||||
const std::string &part = parts[i];
|
const std::string &part = parts[i];
|
||||||
if (part == "") continue;
|
if (part == "") continue;
|
||||||
util::stringvec lr = util::split(part, '=');
|
util::StringVec lr = util::split(part, '=');
|
||||||
if (lr.size() != 2) return false;
|
if (lr.size() != 2) return false;
|
||||||
const std::string &key = lr[0];
|
const std::string &key = lr[0];
|
||||||
const std::string &val = lr[1];
|
const std::string &val = lr[1];
|
||||||
|
|||||||
@@ -188,24 +188,6 @@ PlaneMap::IdVector PlaneMap::scan_radius(const std::string &plane, double x, dou
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
util::IdVector PlaneMap::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;
|
|
||||||
}
|
|
||||||
|
|
||||||
LuaDefine(unittests_planemap, "c") {
|
LuaDefine(unittests_planemap, "c") {
|
||||||
double SC = CELL_SCALE;
|
double SC = CELL_SCALE;
|
||||||
double E = CELL_SCALE * 0.4;
|
double E = CELL_SCALE * 0.4;
|
||||||
@@ -316,20 +298,5 @@ LuaDefine(unittests_planemap, "c") {
|
|||||||
LuaAssert(L, ids[0] == 123);
|
LuaAssert(L, ids[0] == 123);
|
||||||
LuaAssert(L, ids[1] == 456);
|
LuaAssert(L, ids[1] == 456);
|
||||||
|
|
||||||
// Test the unioning of ID vectors.
|
|
||||||
PlaneMap::IdVector idv1,idv2;
|
|
||||||
idv1.push_back(1);
|
|
||||||
idv1.push_back(6);
|
|
||||||
idv1.push_back(4);
|
|
||||||
idv2.push_back(1);
|
|
||||||
idv2.push_back(5);
|
|
||||||
idv2.push_back(6);
|
|
||||||
PlaneMap::IdVector joined = PlaneMap::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);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,9 +126,6 @@ public:
|
|||||||
// Caution: scan_radius is not deterministically ordered.
|
// Caution: scan_radius is not deterministically ordered.
|
||||||
IdVector scan_radius(const std::string &plane, double x, double y, double radius, int64_t prepend) const;
|
IdVector scan_radius(const std::string &plane, double x, double y, double radius, int64_t prepend) const;
|
||||||
|
|
||||||
// Unions and sorts two ID vectors.
|
|
||||||
static IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// unit testing stuff.
|
// unit testing stuff.
|
||||||
friend int unittests_planemap(lua_State *L);
|
friend int unittests_planemap(lua_State *L);
|
||||||
|
|||||||
@@ -20,9 +20,9 @@
|
|||||||
// - remove blank lines
|
// - remove blank lines
|
||||||
// - remove comment lines
|
// - remove comment lines
|
||||||
//
|
//
|
||||||
util::stringvec read_control_lst(const std::string &path) {
|
util::StringVec read_control_lst(const std::string &path) {
|
||||||
util::stringvec lines = util::get_file_lines(path);
|
util::StringVec lines = util::get_file_lines(path);
|
||||||
util::stringvec result;
|
util::StringVec result;
|
||||||
for (int i = 0; i < int(lines.size()); i++) {
|
for (int i = 0; i < int(lines.size()); i++) {
|
||||||
std::string trimmed = util::trim(lines[i]);
|
std::string trimmed = util::trim(lines[i]);
|
||||||
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
|
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
|
||||||
@@ -173,7 +173,7 @@ void SourceDB::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read the list of filenames.
|
// Read the list of filenames.
|
||||||
util::stringvec filenames = read_control_lst("lua/control.lst");
|
util::StringVec filenames = read_control_lst("lua/control.lst");
|
||||||
if (filenames.empty()) {
|
if (filenames.empty()) {
|
||||||
luaL_error(L, "cannot read source database control.lst");
|
luaL_error(L, "cannot read source database control.lst");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,26 @@
|
|||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
stringvec split(const std::string &s, char sep) {
|
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2) {
|
||||||
stringvec result;
|
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;
|
int start = 0;
|
||||||
for (int i = 0; i < int(s.size()); i++) {
|
for (int i = 0; i < int(s.size()); i++) {
|
||||||
if (s[i] == sep) {
|
if (s[i] == sep) {
|
||||||
@@ -47,12 +65,14 @@ std::string toupper(std::string input) {
|
|||||||
|
|
||||||
bool validinteger(const std::string &value) {
|
bool validinteger(const std::string &value) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
|
if (value.size() == 0) return false;
|
||||||
strtoll(value.c_str(), &endptr, 10);
|
strtoll(value.c_str(), &endptr, 10);
|
||||||
return (endptr == value.c_str() + value.size());
|
return (endptr == value.c_str() + value.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t strtoint(const std::string &value, int64_t errval) {
|
int64_t strtoint(const std::string &value, int64_t errval) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
|
if (value.size() == 0) return errval;
|
||||||
int64_t result = strtoll(value.c_str(), &endptr, 10);
|
int64_t result = strtoll(value.c_str(), &endptr, 10);
|
||||||
if (endptr == value.c_str() + value.size()) {
|
if (endptr == value.c_str() + value.size()) {
|
||||||
return result;
|
return result;
|
||||||
@@ -63,6 +83,7 @@ int64_t strtoint(const std::string &value, int64_t errval) {
|
|||||||
|
|
||||||
double strtodouble(const std::string &value) {
|
double strtodouble(const std::string &value) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
|
if (value.size() == 0) return std::nan("");
|
||||||
double result = strtod(value.c_str(), &endptr);
|
double result = strtod(value.c_str(), &endptr);
|
||||||
if (endptr == value.c_str() + value.size()) {
|
if (endptr == value.c_str() + value.size()) {
|
||||||
return result;
|
return result;
|
||||||
@@ -87,6 +108,11 @@ std::string trim(std::string s) {
|
|||||||
return ltrim(rtrim(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::string get_file_contents(const std::string &fn) {
|
||||||
std::ifstream fs(fn);
|
std::ifstream fs(fn);
|
||||||
@@ -95,8 +121,8 @@ std::string get_file_contents(const std::string &fn) {
|
|||||||
return buffer.str();
|
return buffer.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
stringvec get_file_lines(const std::string &path) {
|
StringVec get_file_lines(const std::string &path) {
|
||||||
stringvec result;
|
StringVec result;
|
||||||
std::ifstream f;
|
std::ifstream f;
|
||||||
f.open(path);
|
f.open(path);
|
||||||
if (f) {
|
if (f) {
|
||||||
@@ -120,12 +146,6 @@ std::string get_file_fingerprint(const std::string &fn) {
|
|||||||
return "";
|
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::string XYZ::debug_string() const {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "(" << x << "," << y << "," << z << ")";
|
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;
|
oss << "0x" << std::setw(2) << std::setfill('0') << std::hex;
|
||||||
return oss;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,15 +19,17 @@ enum WorldType {
|
|||||||
WORLD_TYPE_MASTER,
|
WORLD_TYPE_MASTER,
|
||||||
};
|
};
|
||||||
|
|
||||||
using stringvec = std::vector<std::string>;
|
using StringVec = std::vector<std::string>;
|
||||||
using stringset = std::set<std::string>;
|
|
||||||
using HashValue = std::pair<uint64_t, uint64_t>;
|
using HashValue = std::pair<uint64_t, uint64_t>;
|
||||||
using IdVector = std::vector<int64_t>;
|
using IdVector = std::vector<int64_t>;
|
||||||
|
|
||||||
// Split a string into multiple strings
|
// Unions and sorts two ID vectors.
|
||||||
stringvec split(const std::string &s, char sep);
|
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2);
|
||||||
|
|
||||||
// String to lowercase/uppercase
|
// Split a string into multiple strings
|
||||||
|
StringVec split(const std::string &s, char sep);
|
||||||
|
|
||||||
|
// String to lowercase/uppercase. Ascii only, no unicode.
|
||||||
std::string tolower(std::string input);
|
std::string tolower(std::string input);
|
||||||
std::string toupper(std::string input);
|
std::string toupper(std::string input);
|
||||||
|
|
||||||
@@ -45,19 +47,18 @@ std::string ltrim(std::string s);
|
|||||||
std::string rtrim(std::string s);
|
std::string rtrim(std::string s);
|
||||||
std::string trim(std::string s);
|
std::string trim(std::string s);
|
||||||
|
|
||||||
|
// Calculate distance between two points
|
||||||
|
double distance_squared(double x1, double y1, double x2, double y2);
|
||||||
|
|
||||||
// Read a file as one big string.
|
// Read a file as one big string.
|
||||||
std::string get_file_contents(const std::string &path);
|
std::string get_file_contents(const std::string &path);
|
||||||
|
|
||||||
// Read a file as a vector of lines.
|
// Read a file as a vector of lines.
|
||||||
stringvec get_file_lines(const std::string &path);
|
StringVec get_file_lines(const std::string &path);
|
||||||
|
|
||||||
// Get a file's fingerprint - ie, size and modification time.
|
// Get a file's fingerprint - ie, size and modification time.
|
||||||
std::string get_file_fingerprint(const std::string &path);
|
std::string get_file_fingerprint(const std::string &path);
|
||||||
|
|
||||||
// Calculate distance between two points
|
|
||||||
double distance_squared(double x1, double y1, double x2, double y2);
|
|
||||||
|
|
||||||
|
|
||||||
// An XYZ coordinate, general purpose.
|
// An XYZ coordinate, general purpose.
|
||||||
struct XYZ {
|
struct XYZ {
|
||||||
float x, y, z;
|
float x, y, z;
|
||||||
|
|||||||
@@ -520,7 +520,7 @@ void World::difference_transmit(int64_t actor_id, const World *master, StreamBuf
|
|||||||
|
|
||||||
// Get the list of tangibles visible in either model.
|
// Get the list of tangibles visible in either model.
|
||||||
// Some tangibles may be missing in the master, some may be missing in the sync.
|
// Some tangibles may be missing in the master, some may be missing in the sync.
|
||||||
util::IdVector visible = PlaneMap::sort_union_id_vectors(
|
util::IdVector visible = util::sort_union_id_vectors(
|
||||||
master->get_near(actor_id, 100.0, true),
|
master->get_near(actor_id, 100.0, true),
|
||||||
this->get_near(actor_id, 100.0, true));
|
this->get_near(actor_id, 100.0, true));
|
||||||
TanVector m_visible = tangible_get_all(visible);
|
TanVector m_visible = tangible_get_all(visible);
|
||||||
|
|||||||
Reference in New Issue
Block a user