gui: after review of quadtree by msgctl, tougher tests

This commit is contained in:
Sergiusz Bazanski 2018-07-26 22:41:10 +01:00
parent df908374dc
commit 402be30268
2 changed files with 18 additions and 8 deletions

View File

@ -61,6 +61,15 @@ class QuadTreeNode
return false; return false;
return true; return true;
} }
// Sort the bounding box coordinates.
void fixup()
{
if (x1_ < x0_)
std::swap(x0_, x1_);
if (y1_ < y0_)
std::swap(y0_, y1_);
}
}; };
private: private:
@ -281,7 +290,7 @@ class QuadTreeNode
// Return count of BoundingBoxes/Elements contained. // Return count of BoundingBoxes/Elements contained.
// @returns count of elements contained. // @returns count of elements contained.
size_t size(void) const size_t size() const
{ {
size_t res = elems_.size(); size_t res = elems_.size();
if (children_ != nullptr) { if (children_ != nullptr) {
@ -351,20 +360,21 @@ class QuadTree
// @param k Bounding box at which to store value. // @param k Bounding box at which to store value.
// @param v Value at a given bounding box. // @param v Value at a given bounding box.
// @returns Whether the insert was succesful. // @returns Whether the insert was succesful.
bool insert(const BoundingBox &k, ElementT v) bool insert(BoundingBox k, ElementT v)
{ {
k.fixup();
return root_.insert(k, v); return root_.insert(k, v);
} }
// Dump a human-readable representation of the tree to stdout. // Dump a human-readable representation of the tree to stdout.
void dump(void) const void dump() const
{ {
root_.dump(0); root_.dump(0);
} }
// Return count of BoundingBoxes/Elements contained. // Return count of BoundingBoxes/Elements contained.
// @returns count of elements contained. // @returns count of elements contained.
size_t size(void) const size_t size() const
{ {
return root_.size(); return root_.size();
} }
@ -378,7 +388,7 @@ class QuadTree
{ {
std::vector<ElementT> res; std::vector<ElementT> res;
root_.get(x, y, res); root_.get(x, y, res);
return std::move(res); return res;
} }
}; };

View File

@ -58,7 +58,7 @@ TEST_F(QuadTreeTest, insert_count)
auto rng = NEXTPNR_NAMESPACE::DeterministicRNG(); auto rng = NEXTPNR_NAMESPACE::DeterministicRNG();
// Add 10000 random rectangles. // Add 10000 random rectangles.
for (int i = 0; i < 10000; i++) { for (unsigned int i = 0; i < 10000; i++) {
int x0 = rng.rng(width_); int x0 = rng.rng(width_);
int y0 = rng.rng(height_); int y0 = rng.rng(height_);
int w = rng.rng(width_ - x0); int w = rng.rng(width_ - x0);
@ -69,7 +69,7 @@ TEST_F(QuadTreeTest, insert_count)
ASSERT_EQ(qt_->size(), i+1); ASSERT_EQ(qt_->size(), i+1);
} }
// Add 100000 random points. // Add 100000 random points.
for (int i = 0; i < 100000; i++) { for (unsigned int i = 0; i < 100000; i++) {
int x0 = rng.rng(width_); int x0 = rng.rng(width_);
int y0 = rng.rng(height_); int y0 = rng.rng(height_);
int x1 = x0; int x1 = x0;
@ -113,7 +113,7 @@ TEST_F(QuadTreeTest, insert_retrieve_same)
auto res = qt_->get(x, y); auto res = qt_->get(x, y);
// Somewhat arbirary test to make sure we don't return obscene // Somewhat arbirary test to make sure we don't return obscene
// amounts of data. // amounts of data.
ASSERT_LT(res.size(), 200); ASSERT_LT(res.size(), 200UL);
bool found = false; bool found = false;
for (auto elem : res) { for (auto elem : res) {
// Is this what we're looking for? // Is this what we're looking for?