diff --git a/tests/testSymmetry.c b/tests/testSymmetry.c index 8ab9708aa26573d4304c4bf86bfc999ef4cedba8..2e950c5c79f84d601b8d034927b174ac0454de68 100644 --- a/tests/testSymmetry.c +++ b/tests/testSymmetry.c @@ -26,12 +26,13 @@ #include "swift.h" -int main(int argc, char *argv[]) { +void print_bytes(void *p, size_t len) { + printf("("); + for (size_t i = 0; i < len; ++i) printf("%02x", ((unsigned char *)p)[i]); + printf(")\n"); +} -/* Choke on FPEs */ -#ifdef HAVE_FE_ENABLE_EXCEPT - feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); -#endif +void test() { #if defined(SHADOWFAX_SPH) /* Initialize the Voronoi simulation box */ @@ -52,8 +53,8 @@ int main(int argc, char *argv[]) { for (size_t i = 0; i < 3; ++i) pj.x[0] = random_uniform(-1., 1.); pi.h = 2.f; pj.h = 2.f; - pi.id = 1; - pj.id = 2; + pi.id = 1ll; + pj.id = 2ll; #if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) /* Give the primitive variables sensible values, since the Riemann solver does @@ -120,11 +121,11 @@ int main(int argc, char *argv[]) { memcpy(&pi2, &pi, sizeof(struct part)); memcpy(&pj2, &pj, sizeof(struct part)); - int i_ok = memcmp(&pi, &pi2, sizeof(struct part)); - int j_ok = memcmp(&pj, &pj2, sizeof(struct part)); + int i_not_ok = memcmp(&pi, &pi2, sizeof(struct part)); + int j_not_ok = memcmp(&pj, &pj2, sizeof(struct part)); - if (i_ok != 0) error("Particles 'pi' do not match after copy"); - if (j_ok != 0) error("Particles 'pj' do not match after copy"); + if (i_not_ok) error("Particles 'pi' do not match after copy"); + if (j_not_ok) error("Particles 'pj' do not match after copy"); /* Compute distance vector */ float dx[3]; @@ -152,11 +153,11 @@ int main(int argc, char *argv[]) { runner_iact_nonsym_chemistry(r2, dx, pj2.h, pi2.h, &pj2, &pi2); /* Check that the particles are the same */ - i_ok = memcmp(&pi, &pi2, sizeof(struct part)); - j_ok = memcmp(&pj, &pj2, sizeof(struct part)); + i_not_ok = memcmp(&pi, &pi2, sizeof(struct part)); + j_not_ok = memcmp(&pj, &pj2, sizeof(struct part)); - if (i_ok) error("Particles 'pi' do not match after density"); - if (j_ok) error("Particles 'pj' do not match after density"); + if (i_not_ok) error("Particles 'pi' do not match after density"); + if (j_not_ok) error("Particles 'pj' do not match after density"); /* --- Test the force loop --- */ @@ -172,8 +173,8 @@ int main(int argc, char *argv[]) { /* Check that the particles are the same */ #if defined(GIZMO_SPH) - i_ok = 0; - j_ok = 0; + i_not_ok = 0; + j_not_ok = 0; for (size_t i = 0; i < sizeof(struct part) / sizeof(float); ++i) { float a = *(((float *)&pi) + i); float b = *(((float *)&pi2) + i); @@ -200,24 +201,53 @@ int main(int argc, char *argv[]) { message("%.8e, %.8e, %lu", c, d, i); } - i_ok |= a_is_b; - j_ok |= c_is_d; + i_not_ok |= a_is_b; + j_not_ok |= c_is_d; } #else - i_ok = memcmp(&pi, &pi2, sizeof(struct part)); - j_ok = memcmp(&pj, &pj2, sizeof(struct part)); + i_not_ok = + strncmp((const char *)&pi, (const char *)&pi2, sizeof(struct part)); + j_not_ok = + strncmp((const char *)&pj, (const char *)&pj2, sizeof(struct part)); #endif - if (i_ok) { + if (i_not_ok) { printParticle_single(&pi, &xpi); printParticle_single(&pi2, &xpi); - error("Particles 'pi' do not match after force"); + print_bytes(&pj, sizeof(struct part)); + print_bytes(&pj2, sizeof(struct part)); + error("Particles 'pi' do not match after force (byte = %d)", i_not_ok); } - if (j_ok) { + if (j_not_ok) { printParticle_single(&pj, &xpj); printParticle_single(&pj2, &xpj); - error("Particles 'pj' do not match after force"); + print_bytes(&pj, sizeof(struct part)); + print_bytes(&pj2, sizeof(struct part)); + error("Particles 'pj' do not match after force (byte = %d)", j_not_ok); + } +} + +int main(int argc, char *argv[]) { + + /* Initialize CPU frequency, this also starts time. */ + unsigned long long cpufreq = 0; + clocks_set_cpufreq(cpufreq); + +/* Choke on FPEs */ +#ifdef HAVE_FE_ENABLE_EXCEPT + feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); +#endif + + /* Get some randomness going */ + const int seed = time(NULL); + message("Seed = %d", seed); + srand(seed); + + for (int i = 0; i < 100; ++i) { + message("Random test %d/100", i); + test(); } + message("All good"); return 0; }