From 0adf4482f0d3ee036bf9babd66f3974493a6b0e1 Mon Sep 17 00:00:00 2001
From: antirez <antirez@gmail.com>
Date: Wed, 23 Jul 2014 11:43:57 +0200
Subject: [PATCH] PFSELFTEST: less false positives.

This is just a quickfix, for the nature of the test the right way to fix
it is to average the error of N runs, since otherwise it is always
possible to get a false positive with a bad run, or to minimize too much
this possibility we may end testing with too much "large" error ranges.
---
 src/hyperloglog.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/hyperloglog.c b/src/hyperloglog.c
index ecc061c2..63052a78 100644
--- a/src/hyperloglog.c
+++ b/src/hyperloglog.c
@@ -1386,7 +1386,7 @@ void pfselftestCommand(redisClient *c) {
      * The test adds unique elements and check that the estimated value
      * is always reasonable bounds.
      *
-     * We check that the error is smaller than 4 times than the expected
+     * We check that the error is smaller than a few times than the expected
      * standard error, to make it very unlikely for the test to fail because
      * of a "bad" run.
      *
@@ -1422,8 +1422,16 @@ void pfselftestCommand(redisClient *c) {
         /* Check error. */
         if (j == checkpoint) {
             int64_t abserr = checkpoint - (int64_t)hllCount(hdr,NULL);
+            uint64_t maxerr = ceil(relerr*6*checkpoint);
+
+            /* Adjust the max error we expect for cardinality 10
+             * since from time to time it is statistically likely to get
+             * much higher error due to collision, resulting into a false
+             * positive. */
+            if (j == 10) maxerr = 1;
+
             if (abserr < 0) abserr = -abserr;
-            if (abserr > (uint64_t)(relerr*4*checkpoint)) {
+            if (abserr > maxerr) {
                 addReplyErrorFormat(c,
                     "TESTFAILED Too big error. card:%llu abserr:%llu",
                     (unsigned long long) checkpoint,