Example #1
0
int main() {
	for(int t = 0; t < 100000; ++t) {
		int n = rand_int(2, 40);
		vector<V> points;
		for(int i = 0; i < n; ++i) {
			points.push_back(V(rand_real(-1.0, 1.0), rand_real(-1.0, 1.0)));
		}
		
		double d = computeClosestPoints(points);
		double cmpd = 100;
		for(int i = 0; i < n; ++i) {
			for(int j = i + 1; j < n; ++j) {
				cmpd = min(cmpd, abs(points[i] - points[j]));
			}
		}
		
		if(d != cmpd) fail();
	}
	
	return 0;
}
Example #2
0
bool test(int N)
{
    // create random range
    double f[6];
    for (int i = 0; i < 6; ++i) {
        f[i] = rand_real();
    }
    for (int i = 0; i < 3; ++i) {
        if (f[i] > f[i+3]) {
            double tmp = f[i];
            f[i] = f[i+3];
            f[i+3] = tmp;
        }
    }
    point min(f[0], f[1], f[2]);
    point max(f[3], f[4], f[5]);

    std::vector<point> points;
    int count = 0;
    for (int i = 0; i < N; ++i) {
        double x = rand_real(), y = rand_real(), z = rand_real();
        point p(x, y, z);
        if (in_range(p, min, max)) {
            ++count;
        }
        points.push_back(p);
    }

    clock_t t;

    printf("| tree construction started... ");
    fflush(stdout);
    t = clock();
    range_tree rt(points);
    t = clock() - t;
    printf("done (took %f sec)\n", (double)t/CLOCKS_PER_SEC);

    printf("| *info* count = %d\n", count);

    printf("| quering stared... ");
    fflush(stdout);
    t = clock();
    rt.query(min, max);		// query!
    t = clock() - t;
    printf("done (took %f sec)\n", (double)t/CLOCKS_PER_SEC);

    if (rt.result().size() != count) {
        std::cout << "expected\tmin : ";
        std::cout << stringify(min);
        std::cout << "\n\t\tmax : ";
        std::cout << stringify(max);
        std::cout << "\n\t\tcount : ";
        std::cout << count;
        std::cout << "\n\n";
        std::cout << "but got\tcount : ";
        std::cout << rt.result().size();
        std::cout << "\n\n";

        for (std::vector<point>::const_iterator it = rt.result().begin();
                it != rt.result().end();
                ++it) {
            if (! in_range(*it, min, max)) {
                std::cout << "out of range: " << stringify(*it) << std::endl;
            }
        }
        return false;
    }
    return true;
}