测试
//
// Created by www on 2024/8/8.
//
#include "include/cxstructs.h"
#include "include/cxml/k-NN.h"
// 可扩展Rect内搜索子Rect或Point
void testRectSearch() {
using namespace cxstructs;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> distr(0, 200);
std::cout << "TESTING QUAD TREE" << std::endl;
std::cout << " Testing insert..." << std::endl;
QuadTree<Point> tree({0, 0, 200, 200});
for (uint_fast32_t i = 0; i < 1000; i++) {
tree.insert({distr(gen), distr(gen)});
}
CX_ASSERT(tree.depth() == 3, "");
CX_ASSERT(tree.size() == 1000, "");
std::cout << " Testing count subrect..." << std::endl;
CX_ASSERT(tree.count_subrect({0, 0, 200, 200}) == 1000, "");
CX_ASSERT(tree.count_subrect({10, 10, 50, 50}) > 10, "");
tree.insert({2, 2});
CX_ASSERT(tree.size() == 1001, "");
tree.erase({2, 2});
CX_ASSERT(tree.size() == 1000, "");
std::cout << " Testing max capacity..." << std::endl;
QuadTree<Point> tree1({0, 0, 200, 200}, 2, 10);
for (uint_fast32_t i = 0; i < 100000; i++) {
tree.insert({distr(gen), distr(gen)});
}
std::cout << " Testing object retrieval..." << std::endl;
tree1.clear();
tree1.insert({2, 2});
for (auto ptr : tree1.get_subrect({0, 0, 2, 2})) {
CX_ASSERT(*ptr == Point(2, 2), "");
}
}
// 点集处理,可分类
void testKNN() {
using namespace cxstructs;
enum Category { A, B, C };
struct DataPoint : public DataPoint_<Category> {
Category category;
float x_;
float y_;
float weight = 1;
DataPoint() {}
DataPoint(float x, float y, Category category) : x_(x), y_(y), category(category) {}
float x() const final { return x_; }
float y() const final { return y_; }
Category getCategory() final { return category; }
float getWeight() const final { return weight; }
};
std::cout << "TESTING k-NN" << std::endl;
std::vector<DataPoint> data{};
data.emplace_back(1, 2, Category::A);
data.emplace_back(2, 3, Category::A);
data.emplace_back(3, 4, Category::A);
data.emplace_back(4, 5, Category::B);
data.emplace_back(5, 6, Category::B);
data.emplace_back(6, 7, Category::B);
data.emplace_back(7, 8, Category::C);
data.emplace_back(8, 9, Category::C);
data.emplace_back(9, 10, Category::C);
Category cat1;
kNN_2D<DataPoint> knn(data, DISTANCE_FUNCTION_2D::EUCLIDEAN);
std::cout << " Testing absolute classification" << std::endl;
cat1 = knn.classify_by_category_count(0, 0, 4);
CX_ASSERT((int)cat1 == 0, "");
cat1 = knn.classify_by_category_count(5, 5, 4);
CX_ASSERT((int)cat1 == 1, "");
std::cout << " Testing distance weighted classification" << std::endl;
cat1 = knn.classify_by_sum_distance(0, 0, 4);
CX_ASSERT((int)cat1 == 0, "");
cat1 = knn.classify_by_sum_distance(5, 5, 4);
CX_ASSERT((int)cat1 == 1, "");
std::cout << " Testing weight classification" << std::endl;
cat1 = knn.classify_by_sum_weight(0, 0, 4);
CX_ASSERT((int)cat1 == 0, "");
cat1 = knn.classify_by_sum_weight(5, 5, 4);
CX_ASSERT((int)cat1 == 1, "");
std::cout << " Testing weighted * dist classification" << std::endl;
cat1 = knn.classify_by_sum_weighted_distance(0, 0, 4);
CX_ASSERT((int)cat1 == 0, "");
cat1 = knn.classify_by_sum_weighted_distance(5, 5, 4);
CX_ASSERT((int)cat1 == 1, "");
}
// 字典
void testTrie() {
using namespace cxstructs;
std::cout << "TESTING TRIE" << std::endl;
std::cout << " Testing insertion..." << std::endl;
Trie trie;
trie.insert("hello");
std::cout << " Testing contained..." << std::endl;
CX_ASSERT(trie.contains("hello") == true, "");
CX_ASSERT(trie.contains("helloh") == false, "");
std::cout << " Testing startsWith..." << std::endl;
CX_ASSERT(trie.startsWith("he")[0] == "hello", "");
}
int main() {
// testKNN();
testRectSearch();
testTrie();
return 0;
}
输出
TESTING QUAD TREE
Testing insert...
Testing count subrect...
Testing max capacity...
Testing object retrieval...
TESTING TRIE
Testing insertion...
Testing contained...
Testing startsWith...
参考
https://github.com/gk646/cxstructs
标签:Category,std,Search,emplace,Algorithm,tree,back,data,Rect From: https://blog.csdn.net/qq_30220519/article/details/141036995