]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/unittest/test_random.cpp
Fix BSD iconv declaration
[dragonfireclient.git] / src / unittest / test_random.cpp
index 4f50bf9b887d8f68d38424fbf794a2adab6bede4..14de764e1dbc110526662530ba79837e851d7a38 100644 (file)
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "test.h"
 
+#include <cmath>
 #include "util/numeric.h"
 #include "exceptions.h"
 #include "noise.h"
@@ -74,13 +75,10 @@ void TestRandom::testPseudoRandomRange()
        EXCEPTION_CHECK(PrngException, pr.range(5, 1));
 
        for (u32 i = 0; i != 32768; i++) {
-               int min = pr.next() % 3000;
-               int max = pr.next() % 3000;
-               if (min > max) {
-                       int temp = min;
-                       min = max;
-                       max = temp;
-               }
+               int min = (pr.next() % 3000) - 500;
+               int max = (pr.next() % 3000) - 500;
+               if (min > max)
+                       SWAP(int, min, max);
 
                int randval = pr.range(min, max);
                UASSERT(randval >= min);
@@ -104,14 +102,14 @@ void TestRandom::testPcgRandomRange()
 
        EXCEPTION_CHECK(PrngException, pr.range(5, 1));
 
+       // Regression test for bug 3027
+       pr.range(pr.RANDOM_MIN, pr.RANDOM_MAX);
+
        for (u32 i = 0; i != 32768; i++) {
-               int min = pr.next() % 3000;
-               int max = pr.next() % 3000;
-               if (min > max) {
-                       int temp = min;
-                       min = max;
-                       max = temp;
-               }
+               int min = (pr.next() % 3000) - 500;
+               int max = (pr.next() % 3000) - 500;
+               if (min > max)
+                       SWAP(int, min, max);
 
                int randval = pr.range(min, max);
                UASSERT(randval >= min);
@@ -146,7 +144,7 @@ void TestRandom::testPcgRandomNormalDist()
        s32 bins[max - min + 1];
        memset(bins, 0, sizeof(bins));
 
-       PcgRandom r(486456179 + (int)time(NULL));
+       PcgRandom r(486179 + (int)time(NULL));
 
        for (u32 i = 0; i != num_samples; i++) {
                s32 randval = r.randNormalDist(min, max, num_trials);
@@ -160,7 +158,7 @@ void TestRandom::testPcgRandomNormalDist()
        int range      = (max - min + 1);
        float mean     = (max + min) / 2;
        float variance = ((range * range - 1) / 12) / num_trials;
-       float stddev   = sqrt(variance);
+       float stddev   = std::sqrt(variance);
 
        static const float prediction_intervals[] = {
                0.68269f, // 1.0
@@ -173,17 +171,17 @@ void TestRandom::testPcgRandomNormalDist()
        //// Simple normality test using the 68-95-99.7% rule
        for (u32 i = 0; i != ARRLEN(prediction_intervals); i++) {
                float deviations = i / 2.f + 1.f;
-               int lbound = round(mean - deviations * stddev);
-               int ubound = round(mean + deviations * stddev);
+               int lbound = myround(mean - deviations * stddev);
+               int ubound = myround(mean + deviations * stddev);
                UASSERT(lbound >= min);
                UASSERT(ubound <= max);
 
                int accum = 0;
-               for (int i = lbound; i != ubound; i++)
-                       accum += bins[i - min];
+               for (int j = lbound; j != ubound; j++)
+                       accum += bins[j - min];
 
                float actual = (float)accum / num_samples;
-               UASSERT(fabs(actual - prediction_intervals[i]) < 0.02);
+               UASSERT(std::fabs(actual - prediction_intervals[i]) < 0.02f);
        }
 }