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 "components/rappor/byte_vector_utils.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace rappor {
11
12TEST(BloomFilterTest, TinyFilter) {
13  BloomFilter filter(1u, 4u, 0u);
14
15  // Size is 1 and it's initially empty
16  EXPECT_EQ(1u, filter.bytes().size());
17  EXPECT_EQ(0x00, filter.bytes()[0]);
18
19  // "Test" has a self-collision, and only sets 3 bits.
20  filter.SetString("Test");
21  EXPECT_EQ(0x2a, filter.bytes()[0]);
22
23  // Setting the same value shouldn't change anything.
24  filter.SetString("Test");
25  EXPECT_EQ(0x2a, filter.bytes()[0]);
26
27  BloomFilter filter2(1u, 4u, 0u);
28  EXPECT_EQ(0x00, filter2.bytes()[0]);
29  filter2.SetString("Bar");
30  EXPECT_EQ(0xa8, filter2.bytes()[0]);
31
32  // The new string should replace the old one.
33  filter.SetString("Bar");
34  EXPECT_EQ(0xa8, filter.bytes()[0]);
35}
36
37TEST(BloomFilterTest, HugeFilter) {
38  // Create a 500 bit filter, and use a large seed offset to see if anything
39  // breaks.
40  BloomFilter filter(500u, 1u, 0xabdef123);
41
42  // Size is 500 and it's initially empty
43  EXPECT_EQ(500u, filter.bytes().size());
44  EXPECT_EQ(0, CountBits(filter.bytes()));
45
46  filter.SetString("Bar");
47  EXPECT_EQ(1, CountBits(filter.bytes()));
48
49  // Adding the same value shouldn't change anything.
50  filter.SetString("Bar");
51  EXPECT_EQ(1, CountBits(filter.bytes()));
52}
53
54}  // namespace rappor
55