1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
From 1691f53029b1e0821f6c482f5f9f2a23b6721191 Mon Sep 17 00:00:00 2001
From: Michael Sherman <sherm1@gmail.com>
Date: Fri, 24 Jan 2025 11:54:40 -0800
Subject: [PATCH] Test to tolerance to avoid trouble in i386
---
SimTKcommon/tests/RandomTest.cpp | 60 +++++++++++++++++---------------
1 file changed, 31 insertions(+), 29 deletions(-)
diff --git a/SimTKcommon/tests/RandomTest.cpp b/SimTKcommon/tests/RandomTest.cpp
index 99562ac6e..61190b76d 100644
--- a/SimTKcommon/tests/RandomTest.cpp
+++ b/SimTKcommon/tests/RandomTest.cpp
@@ -25,8 +25,6 @@
#include <iostream>
-#define ASSERT(cond) {SimTK_ASSERT_ALWAYS(cond, "Assertion failed");}
-
using std::cout;
using std::endl;
using std::sqrt;
@@ -39,7 +37,7 @@ using namespace SimTK;
void verifyDistribution(int expected[], int found[], int bins) {
for (int i = 0; i < bins; ++i) {
Real dev = sqrt((Real) expected[i]);
- ASSERT(found[i] >= expected[i]-4*dev && found[i] <= expected[i]+4*dev)
+ SimTK_TEST(found[i] >= expected[i]-4*dev && found[i] <= expected[i]+4*dev)
}
}
@@ -54,8 +52,8 @@ void verifyUniformDistribution(Real min, Real max, Real value[], int length) {
found[i] = 0;
}
for (int i = 0; i < length; ++i) {
- ASSERT(value[i] >= min)
- ASSERT(value[i] < max)
+ SimTK_TEST(value[i] >= min)
+ SimTK_TEST(value[i] < max)
int index = (int) ((value[i]-min)*10/(max-min));
found[index]++;
}
@@ -75,8 +73,8 @@ void verifyUniformDistribution(int min, int max, int value[], int length) {
found[i] = 0;
}
for (int i = 0; i < length; ++i) {
- ASSERT(value[i] >= min)
- ASSERT(value[i] < max)
+ SimTK_TEST(value[i] >= min)
+ SimTK_TEST(value[i] < max)
found[value[i]-min]++;
}
verifyDistribution(expected, found, range);
@@ -115,8 +113,8 @@ void verifyGaussianDistribution(Real mean, Real stddev, Real value[], int length
void testUniform() {
Random::Uniform rand;
- ASSERT(rand.getMin() == 0.0)
- ASSERT(rand.getMax() == 1.0)
+ SimTK_TEST(rand.getMin() == 0.0)
+ SimTK_TEST(rand.getMax() == 1.0)
// Try generating a bunch of random numbers, and make sure they are distributed uniformly between 0 and 1.
@@ -131,7 +129,7 @@ void testUniform() {
rand.setSeed(1);
for (int i = 0; i < 2000; ++i)
- ASSERT(value[i] == rand.getValue())
+ SimTK_TEST(value[i] == rand.getValue())
// Now try asking for a whole array at a time, and verify that it still gives the same results.
@@ -140,21 +138,21 @@ void testUniform() {
rand.setSeed(1);
rand.fillArray(value2, 2000);
for (int i = 0; i < 2000; ++i)
- ASSERT(value[i] == value2[i])
+ SimTK_TEST(value[i] == value2[i])
// Set the seed to a different value, and verify that the results are different.
rand.setSeed(2);
rand.fillArray(value2, 2000);
for (int i = 0; i < 2000; ++i)
- ASSERT(value[i] != value2[i])
+ SimTK_TEST(value[i] != value2[i])
// Change the range and test the distribution.
rand.setMin(5.0);
rand.setMax(20.0);
- ASSERT(rand.getMin() == 5.0)
- ASSERT(rand.getMax() == 20.0)
+ SimTK_TEST(rand.getMin() == 5.0)
+ SimTK_TEST(rand.getMax() == 20.0)
rand.fillArray(value2, 2000);
verifyUniformDistribution(5.0, 20.0, value2, 2000);
@@ -173,19 +171,21 @@ void testUniform() {
rand1.fillArray(value, 2000);
rand2.fillArray(value2, 2000);
for (int i = 0; i < 2000; ++i)
- ASSERT(value[i] != value2[i])
+ SimTK_TEST(value[i] != value2[i])
- // Make sure none of the above operations has overwritten the final array element.
+ // Make sure none of the above operations has overwritten the final array
+ // element. On i386 the 32 bit representation of the non-integer values
+ // might not be exact so test to a tolerance.
- ASSERT(value[2000] == 123.4)
- ASSERT(value2[2000] == 567.8)
- ASSERT(value3[2000] == -99)
+ SimTK_TEST_EQ(value[2000], 123.4)
+ SimTK_TEST_EQ(value2[2000], 567.8)
+ SimTK_TEST(value3[2000] == -99)
}
void testGaussian() {
Random::Gaussian rand;
- ASSERT(rand.getMean() == 0.0)
- ASSERT(rand.getStdDev() == 1.0)
+ SimTK_TEST(rand.getMean() == 0.0)
+ SimTK_TEST(rand.getStdDev() == 1.0)
// Try generating a bunch of Gaussian random numbers, and check the distribution.
@@ -203,21 +203,23 @@ void testGaussian() {
rand.setSeed(1);
rand.fillArray(value2, 2000);
for (int i = 0; i < 2000; ++i)
- ASSERT(value[i] == value2[i])
+ SimTK_TEST(value[i] == value2[i])
// Change the parameters and test the distribution.
rand.setMean(10.0);
rand.setStdDev(7.0);
- ASSERT(rand.getMean() == 10.0)
- ASSERT(rand.getStdDev() == 7.0)
+ SimTK_TEST(rand.getMean() == 10.0)
+ SimTK_TEST(rand.getStdDev() == 7.0)
rand.fillArray(value2, 2000);
verifyGaussianDistribution(10.0, 7.0, value2, 2000);
-
- // Make sure none of the above operations has overwritten the final array element.
-
- ASSERT(value[2000] == 123.4)
- ASSERT(value2[2000] == 567.8)
+
+ // Make sure none of the above operations has overwritten the final array
+ // element. On i386 the 32 bit representation of the non-integer values
+ // might not be exact so test to a tolerance.
+
+ SimTK_TEST_EQ(value[2000], 123.4)
+ SimTK_TEST_EQ(value2[2000], 567.8)
}
int main() {
|