-- ----------------------------------------------------------------
-- Regression tests for explicit threshold control.
-- ----------------------------------------------------------------

SELECT hll_set_output_version(1);

-- Should become explicit.
SELECT hll_add(E'\\x118c49'::hll, hll_hash_integer(1,0));

-- Also explicit.
SELECT hll_add(hll_empty(12,5,256,1), hll_hash_integer(1,0));

-- ----------------

-- Should go straight to sparse.
SELECT hll_add(hll_empty(12,5,0,1),
           hll_hash_integer(1,0));

-- Should stay sparse.
SELECT hll_add(
       hll_add(hll_empty(12,5,0,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0));

-- ----------------

-- Should stay explicit for one add.
SELECT hll_add(hll_empty(12,5,1,1),
           hll_hash_integer(1,0));

-- But not on the second add.
SELECT hll_add(
       hll_add(hll_empty(12,5,1,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0));

-- Nor the third.
SELECT hll_add(
       hll_add(
       hll_add(hll_empty(12,5,1,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0)),
           hll_hash_integer(3,0));

-- ----------------

-- Should stay explicit for two adds.
SELECT hll_add(hll_empty(12,5,2,1),
           hll_hash_integer(1,0));

SELECT hll_add(
       hll_add(hll_empty(12,5,2,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0));

-- Not explicit anymore.
SELECT hll_add(
       hll_add(
       hll_add(hll_empty(12,5,2,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0)),
           hll_hash_integer(3,0));

-- ----------------

-- Should stay explicit for four adds.
SELECT hll_add(hll_empty(12,5,4,1),
           hll_hash_integer(1,0));

SELECT hll_add(
       hll_add(hll_empty(12,5,4,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0));

SELECT hll_add(
       hll_add(
       hll_add(hll_empty(12,5,4,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0)),
           hll_hash_integer(3,0));

SELECT hll_add(
       hll_add(
       hll_add(
       hll_add(hll_empty(12,5,4,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0)),
           hll_hash_integer(3,0)),
           hll_hash_integer(4,0));

-- Not anymore.
SELECT hll_add(
       hll_add(
       hll_add(
       hll_add(
       hll_add(hll_empty(12,5,4,1),
           hll_hash_integer(1,0)),
           hll_hash_integer(2,0)),
           hll_hash_integer(3,0)),
           hll_hash_integer(4,0)),
           hll_hash_integer(5,0));

-- ----------------------------------------------------------------

-- Should stay explicit.
SELECT E'\\[encoded data, 4103 characters]'::hll(12,5,256,0);

-- Should become sparse
SELECT hll_add(E'\\[encoded data, 4103 characters]'::hll(12,5,256,1),
hll_hash_integer(1,0));

-- ----------------------------------------------------------------
-- Test auto explicit threshold values.
-- ----------------------------------------------------------------

-- ((((2^12 * 5) + 7) / 8) / 8) = 320
SELECT hll_print(hll_empty(12,5,-1,1));

-- ((((2^10 * 5) + 7) / 8) / 8) = 80
SELECT hll_print(hll_empty(10,5,-1,1));

-- ((((2^10 * 4) + 7) / 8) / 8) = 64
SELECT hll_print(hll_empty(10,4,-1,1));

-- ((((2^10 * 3) + 7) / 8) / 8) = 48
SELECT hll_print(hll_empty(10,3,-1,1));

-- ((((2^2 * 5) + 7) / 8) / 8) = 0
SELECT hll_print(hll_empty(2,5,-1,1));

-- ((((2^4 * 5) + 7) / 8) / 8) = 1
SELECT hll_print(hll_empty(4,5,-1,1));

-- ((((2^4 * 4) + 7) / 8) / 8) = 1
SELECT hll_print(hll_empty(4,4,-1,1));

-- ----------------------------------------------------------------
-- Test auto explicit threshold action.
-- ----------------------------------------------------------------

-- Should have auto-explicit value of 2.
-- ((((2^5 * 5) + 7) / 8) / 8) = 2
SELECT hll_print(hll_empty(5,5,-1,1));

-- Empty
SELECT hll_empty(5,5,-1,1);

-- Explicit
SELECT hll_add(
           hll_empty(5,5,-1,1), 
               hll_hash_integer(1,0));

-- Explicit
SELECT hll_add(
       hll_add(
           hll_empty(5,5,-1,1), 
               hll_hash_integer(1,0)),
               hll_hash_integer(2,0));

-- Now Sparse
SELECT hll_add(
       hll_add(
       hll_add(
           hll_empty(5,5,-1,1), 
               hll_hash_integer(1,0)),
               hll_hash_integer(2,0)),
               hll_hash_integer(3,0));
