From: John Darrington Date: Mon, 26 Nov 2012 20:09:08 +0000 (+0100) Subject: Disable asserts in histogram code except in --testing-mode X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02b9b37698eb0b483fe0338f3b28d8f784a7af9a;p=pspp Disable asserts in histogram code except in --testing-mode The assertions in histogram.c are algebraically correct, but due to floating point errors may not actually be true. This change disables them except when in testing mode. The tests in charts.at rely upon them being enabled. Closes bug #36395 --- diff --git a/src/libpspp/assertion.h b/src/libpspp/assertion.h index 5b10d638ab..237710813d 100644 --- a/src/libpspp/assertion.h +++ b/src/libpspp/assertion.h @@ -36,3 +36,6 @@ #else #define expensive_assert(EXPR) ((void) 0) #endif + + +#define testing_assert(EXPR) do {if (settings_get_testing_mode ()) assert (EXPR); } while (0); diff --git a/src/math/histogram.c b/src/math/histogram.c index afc40013e0..51916a3d8b 100644 --- a/src/math/histogram.c +++ b/src/math/histogram.c @@ -84,6 +84,10 @@ double get_slack (double limit, double half_bin_width, int *n_half_bins) ADJ_MIN and ADJ_MAX are locations of the adjusted values of MIN and MAX (the range will always be equal or slightly larger). Returns the number of bins. + + The "testing_assert" expressions in this function should be algebraically correct. + However, due to floating point rounding they could fail, especially when small numbers + are involved. In normal use, therefore, testing_assert does nothing. */ static int adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, double *adj_max) @@ -97,7 +101,7 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do double lower_slack = get_slack (min, half_bin_width, &lower_limit); double upper_slack = -get_slack (max, half_bin_width, &upper_limit); - assert (max > min); + testing_assert (max > min); /* If min is negative, then lower_slack may be less than zero. In this case, the lower bound must be extended in the negative direction @@ -108,7 +112,7 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do lower_limit--; lower_slack += half_bin_width; } - assert (lower_limit * half_bin_width <= min); + testing_assert (lower_limit * half_bin_width <= min); /* However, the upper bound must be extended regardless, because histogram bins span the range [lower, upper). In other words, the upper bound must be @@ -116,7 +120,7 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do */ upper_limit++;; upper_slack += half_bin_width; - assert (upper_limit * half_bin_width > max); + testing_assert (upper_limit * half_bin_width > max); /* The range must be an EVEN number of half bin_widths */ if ( (upper_limit - lower_limit) % 2) @@ -159,7 +163,7 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do if (upper_slack > lower_slack) { - assert (upper_slack > half_bin_width); + testing_assert (upper_slack > half_bin_width); /* Adjust the range to the left */ lower_limit --; @@ -169,7 +173,7 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do } else { - assert (lower_slack >= half_bin_width); + testing_assert (lower_slack >= half_bin_width); /* Adjust the range to the right */ lower_limit ++; @@ -197,8 +201,8 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do *adj_min = lower_limit * half_bin_width; *adj_max = upper_limit * half_bin_width; - assert (*adj_max > max); - assert (*adj_min <= min); + testing_assert (*adj_max > max); + testing_assert (*adj_min <= min); return (upper_limit - lower_limit) / 2.0; } diff --git a/tests/output/charts.at b/tests/output/charts.at index 302e05b151..049fc28192 100644 --- a/tests/output/charts.at +++ b/tests/output/charts.at @@ -151,6 +151,7 @@ frequencies pos neg pn ]) -AT_CHECK([pspp -O format=csv histogram.sps], [0], [ignore]) +dnl The --testing-mode flag is important!! +AT_CHECK([pspp --testing-mode -O format=csv histogram.sps], [0], [ignore]) AT_CLEANUP