OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/rappor/bloom_filter.h" | |
6 | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 | |
9 namespace rappor { | |
10 | |
11 namespace { | |
12 | |
13 int CountBits(const ByteVector& v) { | |
Alexei Svitkine (slow)
2014/01/21 18:14:04
Is it me or is this duplicate in like 3 test files
Steven Holte
2014/01/21 20:25:14
Done.
| |
14 int bit_count = 0; | |
15 for(size_t i = 0; i < v.size(); i++) { | |
16 uint8_t byte = v[i]; | |
17 for (int j = 0; j < 8 ; j++) { | |
18 if(byte & 1 << j) { | |
19 bit_count++; | |
20 } | |
21 } | |
22 } | |
23 return bit_count; | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 TEST(BloomFilterTest, TestTinyFilter) { | |
29 BloomFilter filter(1u, 4u); | |
30 | |
31 // Size is 1 and it's initially empty | |
32 EXPECT_EQ(1u, filter.bytes().size()); | |
33 EXPECT_EQ(0x00, filter.bytes()[0]); | |
34 | |
35 // "Bar" has a self-collision, and only sets 3 bits. | |
36 filter.AddString("Bar"); | |
37 EXPECT_EQ(0x13, filter.bytes()[0]); | |
38 | |
39 // Adding the same value shouldn't change anything. | |
40 filter.AddString("Bar"); | |
41 EXPECT_EQ(0x13, filter.bytes()[0]); | |
42 | |
43 BloomFilter filter2(1, 4); | |
44 EXPECT_EQ(0x00, filter2.bytes()[0]); | |
45 filter2.AddString("Foo"); | |
46 EXPECT_EQ(0x2B, filter2.bytes()[0]); | |
47 | |
48 // Adding a colliding string should just set new bits. | |
49 filter.AddString("Foo"); | |
50 EXPECT_EQ(0x3B, filter.bytes()[0]); | |
51 } | |
52 | |
53 TEST(BloomFilterTest, TestHugeFilter) { | |
54 BloomFilter filter(500u, 1u); | |
55 | |
56 // Size is 500 and it's initially empty | |
57 EXPECT_EQ(500u, filter.bytes().size()); | |
58 EXPECT_EQ(0, CountBits(filter.bytes())); | |
59 | |
60 filter.AddString("Bar"); | |
61 EXPECT_EQ(1, CountBits(filter.bytes())); | |
62 | |
63 // Adding the same value shouldn't change anything. | |
64 filter.AddString("Bar"); | |
65 EXPECT_EQ(1, CountBits(filter.bytes())); | |
66 } | |
67 | |
68 TEST(BloomFilterTest, TestAddStrings) { | |
69 BloomFilter filter(1u, 4u); | |
70 std::vector<std::string> strs; | |
71 strs.push_back("Bar"); | |
72 strs.push_back("Foo"); | |
73 filter.AddStrings(strs); | |
74 | |
75 EXPECT_EQ(1u, filter.bytes().size()); | |
76 EXPECT_EQ(0x3B, filter.bytes()[0]); | |
77 } | |
78 | |
79 } // namespace rappor | |
OLD | NEW |