1 /**************************************************************************
2    Copyright (c) 2017 sewenew
3 
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7 
8        http://www.apache.org/licenses/LICENSE-2.0
9 
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15  *************************************************************************/
16 
17 #ifndef SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_HPP
18 #define SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_HPP
19 
20 #include "utils.h"
21 
22 namespace sw {
23 
24 namespace redis {
25 
26 namespace test {
27 
28 template <typename RedisInstance>
run()29 void HyperloglogCmdTest<RedisInstance>::run() {
30     auto k1 = test_key("k1");
31     auto k2 = test_key("k2");
32     auto k3 = test_key("k3");
33 
34     KeyDeleter<RedisInstance> deleter(_redis, {k1, k2, k3});
35 
36     _redis.pfadd(k1, "a");
37     auto members1 = {"b", "c", "d", "e", "f", "g"};
38     _redis.pfadd(k1, members1);
39 
40     auto cnt = _redis.pfcount(k1);
41     auto err = cnt * 1.0 / (1 + members1.size());
42     REDIS_ASSERT(err < 1.02 && err > 0.98, "failed to test pfadd and pfcount");
43 
44     auto members2 = {"a", "b", "c", "h", "i", "j", "k"};
45     _redis.pfadd(k2, members2);
46     auto total = 1 + members1.size() + members2.size() - 3;
47 
48     cnt = _redis.pfcount({k1, k2});
49     err = cnt * 1.0 / total;
50     REDIS_ASSERT(err < 1.02 && err > 0.98, "failed to test pfcount");
51 
52     _redis.pfmerge(k3, {k1, k2});
53     cnt = _redis.pfcount(k3);
54     err = cnt * 1.0 / total;
55     REDIS_ASSERT(err < 1.02 && err > 0.98, "failed to test pfcount");
56 
57     _redis.pfmerge(k3, k1);
58     REDIS_ASSERT(cnt == _redis.pfcount(k3), "failed to test pfmerge");
59 }
60 
61 }
62 
63 }
64 
65 }
66 
67 #endif // end SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_HPP
68