/************************************** Universal Transverse Mercator Tests */ int testa_utm_arr(project_parameters_t * pps, char * name, double lat, double lon, double x_correct, double y_correct, int we_allocate) { double *xarr, *yarr; double *xarr_out, *yarr_out; int i, ok; xarr = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); yarr = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); if (we_allocate) { xarr_out = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); yarr_out = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); } else { xarr_out = NULL; yarr_out = NULL; } for (i = 0; i < ARR_TEST_SIZE; ++i) { xarr[i] = lat; yarr[i] = lon; } project_utm_arr(pps, xarr, yarr, &xarr_out, &yarr_out, ARR_TEST_SIZE); ok = 1; for (i = 1; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xarr_out[i], xarr_out[0]) || !within_tol(yarr_out[i], yarr_out[0])) { ok = 0; printf("Fail: %s. Result [%d] (%f,%f) " "disagrees with [0] (%f,%f).\n", name, i, xarr_out[i], yarr_out[i], xarr_out[0], yarr_out[0]); ++nfail; break; } } if (ok) ++nok; check(name, xarr_out[0], yarr_out[0], x_correct, y_correct); free(yarr_out); free(xarr_out); free(yarr); free(xarr); return ok; }
int check(char * name, double x, double y, double x_correct, double y_correct) { if (!within_tol(x, x_correct) || !within_tol(y, y_correct)) { printf("Fail: %s. Expected: (%f,%f) Got: (%f,%f)\n", name, x_correct, y_correct, x, y); ++nfail; return FALSE; } else { ++nok; return TRUE; } }
void test_complex() { complexFloat c = complex_new(1,2); CU_ASSERT(c.real==1); CU_ASSERT(c.imag==2); complexFloat d = complex_add(c,complex_zero()); CU_ASSERT(d.real==1); CU_ASSERT(d.imag==2); d = complex_sub(c,complex_new(-1,-2)); CU_ASSERT(d.real==2); CU_ASSERT(d.imag==4); CU_ASSERT(within_tol(complex_amp(c),sqrt(5))); CU_ASSERT(within_tol(complex_amp(d),sqrt(20))); CU_ASSERT(within_tol(complex_amp_sqr(c),5)); CU_ASSERT(within_tol(complex_amp_sqr(d),20)); CU_ASSERT(within_tol(complex_amp(complex_scale(d,.5)),sqrt(5))); d = complex_conj(c); CU_ASSERT(d.real==1); CU_ASSERT(d.imag==-2); complexFloat e = complex_conj(complex_add(d,complex_conj(c))); CU_ASSERT(e.real==2); CU_ASSERT(e.imag==4); complexVector v = complex_vector_new(c,d,e); CU_ASSERT(v.A.real==1); CU_ASSERT(v.A.imag==2); CU_ASSERT(v.B.real==1); CU_ASSERT(v.B.imag==-2); CU_ASSERT(v.C.real==2); CU_ASSERT(v.C.imag==4); complexVector vc = complex_vector_conj(v); CU_ASSERT(vc.A.real==1); CU_ASSERT(vc.A.imag==-2); CU_ASSERT(vc.B.real==1); CU_ASSERT(vc.B.imag==2); CU_ASSERT(vc.C.real==2); CU_ASSERT(vc.C.imag==-4); }
void test_vector() { Vector *v = vector_new(1,2,3); CU_ASSERT(v->x == 1); CU_ASSERT(v->y == 2); CU_ASSERT(v->z == 3); Vector *vc = vector_copy(v); CU_ASSERT(vc->x == 1); CU_ASSERT(vc->y == 2); CU_ASSERT(vc->z == 3); Vector *u = vector_new(4,5,6); vector_add(v,u); CU_ASSERT(v->x == 5); CU_ASSERT(v->y == 7); CU_ASSERT(v->z == 9); vector_subtract(v,u); CU_ASSERT(v->x == 1); CU_ASSERT(v->y == 2); CU_ASSERT(v->z == 3); vector_multiply(v,2); CU_ASSERT(v->x == 2); CU_ASSERT(v->y == 4); CU_ASSERT(v->z == 6); Vector *w = vector_cross(v,u); CU_ASSERT(w->x == -6); CU_ASSERT(w->y == 12); CU_ASSERT(w->z == -6); vector_free(v); vector_free(u); vector_free(w); v = vector_new(3,4,0); u = vector_new(0,3,4); CU_ASSERT(v->x==u->y); CU_ASSERT(v->y==u->z); w = vector_cross(v,u); CU_ASSERT(w->x == 16); CU_ASSERT(w->y == -12); CU_ASSERT(w->z == 9); CU_ASSERT(within_tol(vector_magnitude(v),5)); CU_ASSERT(within_tol(vector_magnitude(u),5)); vector_free(v); vector_free(u); vector_free(w); v = vector_new(1,0,0); u = vector_new(0,0,1); double a = vector_angle(v,u)/D2R; CU_ASSERT(within_tol(a,90)); CU_ASSERT(within_tol(vector_angle(u,v),90.*D2R)); w = vector_cross(v,u); CU_ASSERT(within_tol(vector_magnitude(w),1)); CU_ASSERT(w->x==0); CU_ASSERT(w->y==-1); CU_ASSERT(w->z==0); }
void perf_test_ps() { const int N = ARR_TEST_SIZE * NUM_REPS * 10; double *x, *x1, *xo; double *y, *y1, *yo; double lat0, lon0; project_parameters_t pps; int i, n=0; struct timeval tv1, tv2; int elapsed1, elapsed2; lat0 = 71 * DEG_TO_RAD; lon0 = -96 * DEG_TO_RAD; x = (double *) malloc(sizeof(double) * N); y = (double *) malloc(sizeof(double) * N); x1 = (double *) malloc(sizeof(double) * N); y1 = (double *) malloc(sizeof(double) * N); xo = (double *) malloc(sizeof(double) * N); yo = (double *) malloc(sizeof(double) * N); pps.ps.slat = lat0; pps.ps.slon = lon0; pps.ps.is_north_pole = 1; pps.ps.false_easting = 0; pps.ps.false_northing = 0; /* use same seed each time */ srand(10101); for (i = 0; i < N; ++i) { x[i] = (double)rand() / (double)RAND_MAX * .707 + .707; y[i] = (double)rand() / (double)RAND_MAX * .707 + .707; } gettimeofday(&tv1, NULL); project_ps_arr(&pps, x, y, &x1, &y1, N); gettimeofday(&tv2, NULL); elapsed1 = tv2.tv_sec - tv1.tv_sec; gettimeofday(&tv1, NULL); for (i = 0; i < N; ++i) { project_ps(&pps, x[i], y[i], &xo[i], &yo[i]); } gettimeofday(&tv2, NULL); elapsed2 = tv2.tv_sec - tv1.tv_sec; /* do they agree? */ for (i = 0; i < N; ++i) { if (!within_tol(x1[i], xo[i]) || !within_tol(y1[i], yo[i])) { printf("x[%d] = %f, xo[%d] = %f, y[%d] = %f, yo[%d] = %f\n", i, x[i], i, xo[i], i, y[i], i, yo[i]); printf("x[%d] = %f, x1[%d] = %f, y[%d] = %f, y1[%d] = %f\n", i, x[i], i, x1[i], i, y[i], i, y1[i]); ++n; } } if (n > 0) { ++nfail; printf("Fail: perf_test_ps results don't agree! wrong: %d of %d\n", n, N); } else { ++nok; printf("perf_test_ps results -- (iters: %d)\n", N); printf(" Elapsed #1 (array): %d sec\n", elapsed1); printf(" Elapsed #2 (point): %d sec\n", elapsed2); } free(yo); free(xo); free(y1); free(x1); free(y); free(x); }
void testa_random(char * name, project_parameters_t * pps, project_f_t *project_f, project_f_arr_t *project_f_arr, project_f_inv_t *project_f_inv, project_f_arr_inv_t *project_f_arr_inv, gen_lat_t *gen_lat, gen_lon_t *gen_lon) { { double lat, lon, x, y, lat_out, lon_out, lat_deg, lon_deg; int ok; lat_deg = gen_lat(); lon_deg = gen_lon(); lat = lat_deg * DEG_TO_RAD; lon = lon_deg * DEG_TO_RAD; x = y = lat_out = lon_out = HUGE_VAL; ok = FALSE; if (project_f(pps, lat, lon, &x, &y)) { if (project_f_inv(pps, x, y, &lat_out, &lon_out)) { ok = within_tol(lat, lat_out) && within_tol(lon, lon_out); } } if (ok) { ++nok; } else { printf("Fail: %s(%f,%f):" "(%f,%f) forward-> (%f,%f) inverse-> (%f,%f)\n", name, lat_deg, lon_deg, lat, lon, x, y, lat_out, lon_out); ++nfail; } } { double *lat_rad, *lon_rad; double *px, *py; double *lat_out, *lon_out; int i, ok = FALSE; lat_rad = (double *) malloc (sizeof(double) * ARR_TEST_SIZE); lon_rad = (double *) malloc (sizeof(double) * ARR_TEST_SIZE); px = py = lat_out = lon_out = NULL; for (i = 0; i < ARR_TEST_SIZE; ++i) { lat_rad[i] = gen_lat() * DEG_TO_RAD; lon_rad[i] = gen_lon() * DEG_TO_RAD; } if (project_f_arr(pps, lat_rad, lon_rad, &px, &py, ARR_TEST_SIZE)) { if (project_f_arr_inv(pps, px, py, &lat_out, &lon_out, ARR_TEST_SIZE)) { ok = TRUE; for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(lat_rad[i], lat_out[i]) || !within_tol(lon_rad[i], lon_out[i])) { printf("Fail: %s_arr:" "(%f,%f) forward-> (%f,%f) inverse-> (%f,%f)\n", name, lat_rad[i], lon_rad[i], px[i], py[i], lat_out[i], lon_out[i]); ok = FALSE; } } } } if (ok) { ++nok; } else { printf("Fail: %s_arr\n", name); ++nfail; } free(lon_out); free(lat_out); free(px); free(py); free(lon_rad); free(lat_rad); } }
void testa_random_ps() { /* check that randomly generated values agree between array version and by-point version */ double *x, *xp, *xa; double *y, *yp, *ya; int i, ok=1; int reference_lat; int reference_lon; project_parameters_t pps; /* do these dynamically, might be too big for the stack */ x = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); y = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); xp = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); yp = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); xa = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); ya = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); srand(101); for (i = 0; i < ARR_TEST_SIZE; ++i) { /* latitude: between pi/4 and pi/2 (northern latitudes) */ x[i] = (double)rand() / (double)RAND_MAX * .7 + .7; /* longitude: between -pi and pi */ y[i] = (double)rand() / (double)RAND_MAX * 6.26 - 3.14; } /* should be able to use any reference latitude > 45 and any ref lon */ reference_lat = rand() % 45 + 45; reference_lon = rand() % 360 - 180; pps.ps.slat = reference_lat * DEG_TO_RAD; pps.ps.slon = reference_lon * DEG_TO_RAD; pps.ps.is_north_pole = 1; project_ps_arr(&pps, x, y, &xa, &ya, ARR_TEST_SIZE); for (i = 0; i < ARR_TEST_SIZE; ++i) { project_ps(&pps, x[i], y[i], &xp[i], &yp[i]); } /* do they agree? */ for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xa[i], xp[i]) || !within_tol(ya[i], yp[i])) { printf("Fail: project_ps{_arr} - Random[%d] (%d,%d,%f,%f)" " Array Result: (%f, %f) Point Result: (%f,%f)\n", i, reference_lat, reference_lon, x[i], y[i], xa[i], ya[i], xp[i], yp[i]); ++nfail; ok = 0; break; } } if (ok) ++nok; /* now reverse-transform via both methods. First by-point */ ok = 1; for (i = 0; i < ARR_TEST_SIZE; ++i) { project_ps_inv(&pps, xp[i], yp[i], &xp[i], &yp[i]); } /* agrees with original values ? */ for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xp[i], x[i]) || !within_tol(yp[i], y[i])) { printf("Fail: project_ps/project_ps_inv doesn't match." " Random[%d] (%d,%d,%f,%f) Got: (%f,%f)\n", i, reference_lat, reference_lon, x[i], y[i], xp[i], yp[i]); ++nfail; ok = 0; break; } } if (ok) ++nok; /* Second, by array */ ok = 1; for (i = 0; i < ARR_TEST_SIZE; ++i) { xp[i] = xa[i]; yp[i] = ya[i]; } project_ps_arr_inv(&pps, xp, yp, &xa, &ya, ARR_TEST_SIZE); for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xa[i], x[i]) || !within_tol(ya[i], y[i])) { printf("Fail: project_ps_arr/project_ps_arr_inv doesn't match." " Random[%d] (%d,%d,%f,%f) Got: (%f,%f)\n", i, reference_lat, reference_lon, x[i], y[i], xa[i], ya[i]); ++nfail; ok = 0; break; } } free(ya); free(xa); free(yp); free(xp); free(x); free(y); }
void testa_random_utm() { /* check that randomly generated values agree between array version and by-point version */ double *x, *xp, *xa; double *y, *yp, *ya; int i, ok=1; int reference_lon; int reference_lat; project_parameters_t pps; /* do these dynamically, might be too big for the stack */ x = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); y = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); xp = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); yp = (double *) malloc(sizeof(double) * ARR_TEST_SIZE); xa = NULL; ya = NULL; srand(102); /* any longitude, and latitudes (-60,60) should be ok */ reference_lon = rand() % 360 - 180; reference_lat = rand() % 120 - 60; pps.utm.lon0 = reference_lon * DEG_TO_RAD; pps.utm.zone = MAGIC_UNSET_INT; for (i = 0; i < ARR_TEST_SIZE; ++i) { /* latitude: close to the reference latitude (within 2 degrees) */ x[i] = DEG_TO_RAD * ( reference_lat + ( (double)rand() / (double)RAND_MAX * 1 - .5 ) ); /* longitude: close to the reference longitude (within 2 degrees) */ y[i] = DEG_TO_RAD * ( reference_lon + ( (double)rand() / (double)RAND_MAX * 1 - .5 ) ); } project_utm_arr(&pps, x, y, &xa, &ya, ARR_TEST_SIZE); for (i = 0; i < ARR_TEST_SIZE; ++i) { project_utm(&pps, x[i], y[i], &xp[i], &yp[i]); } /* do they agree? */ for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xa[i], xp[i]) || !within_tol(ya[i], yp[i])) { printf("Fail: project_utm{_arr} - Random[%d] (%d,%f,%f)" " Array Result: (%f,%f) Point Result: (%f,%f)\n", i, reference_lon, x[i], y[i], xa[i], ya[i], xp[i], yp[i]); ++nfail; ok = 0; break; } } if (ok) ++nok; /* now reverse-transform via both methods. First by-point */ ok = 1; for (i = 0; i < ARR_TEST_SIZE; ++i) { project_utm_inv(&pps,xp[i], yp[i], &xp[i], &yp[i]); } /* agrees with original values ? */ for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xp[i], x[i]) || !within_tol(yp[i], y[i])) { printf("Fail: project_utm/project_utm_inv doesn't match." " Random[%d] (%d,%f,%f) Got: (%f,%f)\n", i, reference_lon, x[i], y[i], xp[i], yp[i]); ++nfail; ok = 0; break; } } if (ok) ++nok; /* Second, by array */ ok = 1; for (i = 0; i < ARR_TEST_SIZE; ++i) { xp[i] = xa[i]; yp[i] = ya[i]; } project_utm_arr_inv(&pps, xp, yp, &xa, &ya, ARR_TEST_SIZE); for (i = 0; i < ARR_TEST_SIZE; ++i) { if (!within_tol(xa[i], x[i]) || !within_tol(ya[i], y[i])) { printf("Fail: project_utm_arr/project_utm_arr_inv doesn't match." " Random[%d] (%d,%f,%f) Got: (%f,%f)\n", i, reference_lon, x[i], y[i], xa[i], ya[i]); ++nfail; ok = 0; break; } } free(ya); free(xa); free(yp); free(xp); free(x); free(y); }