2014-04-09 08:15:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "suggest/core/dictionary/bloom_filter.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2014-04-09 08:33:57 +00:00
|
|
|
#include <algorithm>
|
2014-04-09 08:15:55 +00:00
|
|
|
#include <cstdlib>
|
2014-04-09 08:33:57 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <random>
|
2014-04-09 08:15:55 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(BloomFilterTest, TestFilter) {
|
2014-04-09 08:33:57 +00:00
|
|
|
static const int TEST_RANDOM_DATA_MAX = 65536;
|
2014-04-09 08:15:55 +00:00
|
|
|
static const int ELEMENT_COUNT = 1000;
|
|
|
|
std::vector<int> elements;
|
2014-04-09 08:33:57 +00:00
|
|
|
|
|
|
|
// Initialize data set with random integers.
|
|
|
|
{
|
|
|
|
// Use the uniform integer distribution [0, TEST_RANDOM_DATA_MAX].
|
|
|
|
std::uniform_int_distribution<int> distribution(0, TEST_RANDOM_DATA_MAX);
|
|
|
|
auto randomNumberGenerator = std::bind(distribution, std::mt19937());
|
|
|
|
for (int i = 0; i < ELEMENT_COUNT; ++i) {
|
|
|
|
elements.push_back(randomNumberGenerator());
|
|
|
|
}
|
2014-04-09 08:15:55 +00:00
|
|
|
}
|
2014-04-09 08:33:57 +00:00
|
|
|
|
|
|
|
// Make sure BloomFilter contains nothing by default.
|
2014-04-09 08:15:55 +00:00
|
|
|
BloomFilter bloomFilter;
|
|
|
|
for (const int elem : elements) {
|
|
|
|
ASSERT_FALSE(bloomFilter.isInFilter(elem));
|
|
|
|
}
|
2014-04-09 08:33:57 +00:00
|
|
|
|
|
|
|
// Copy some of the test vector into bloom filter.
|
|
|
|
std::unordered_set<int> elementsThatHaveBeenSetInFilter;
|
|
|
|
{
|
|
|
|
// Use the uniform integer distribution [0, 1].
|
|
|
|
std::uniform_int_distribution<int> distribution(0, 1);
|
|
|
|
auto randomBitGenerator = std::bind(distribution, std::mt19937());
|
|
|
|
for (const int elem : elements) {
|
|
|
|
if (randomBitGenerator() == 0) {
|
|
|
|
bloomFilter.setInFilter(elem);
|
|
|
|
elementsThatHaveBeenSetInFilter.insert(elem);
|
|
|
|
}
|
2014-04-09 08:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-09 08:33:57 +00:00
|
|
|
|
2014-04-09 08:15:55 +00:00
|
|
|
for (const int elem : elements) {
|
|
|
|
const bool existsInFilter = bloomFilter.isInFilter(elem);
|
|
|
|
const bool hasBeenSetInFilter =
|
|
|
|
elementsThatHaveBeenSetInFilter.find(elem) != elementsThatHaveBeenSetInFilter.end();
|
|
|
|
if (hasBeenSetInFilter) {
|
2014-04-09 08:33:57 +00:00
|
|
|
EXPECT_TRUE(existsInFilter) << "elem: " << elem;
|
2014-04-09 08:15:55 +00:00
|
|
|
}
|
|
|
|
if (!existsInFilter) {
|
2014-04-09 08:33:57 +00:00
|
|
|
EXPECT_FALSE(hasBeenSetInFilter) << "elem: " << elem;
|
2014-04-09 08:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace latinime
|