From 111c05ff35763d6fe08a05ff341086de61498dc2 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Wed, 9 Apr 2014 17:15:55 +0900 Subject: [PATCH] Add BloomFilterTest. Change-Id: If97b9e9cc3bbaa4c7877991c11d7935626268ca3 --- native/jni/NativeFileList.mk | 1 + .../core/dictionary/bloom_filter_test.cpp | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk index 1230150ea..70a6638fb 100644 --- a/native/jni/NativeFileList.mk +++ b/native/jni/NativeFileList.mk @@ -102,4 +102,5 @@ LATIN_IME_CORE_SRC_FILES := \ LATIN_IME_CORE_TEST_FILES := \ defines_test.cpp \ suggest/core/layout/normal_distribution_2d_test.cpp \ + suggest/core/dictionary/bloom_filter_test.cpp \ utils/autocorrection_threshold_utils_test.cpp diff --git a/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp new file mode 100644 index 000000000..b1ab5e3ec --- /dev/null +++ b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp @@ -0,0 +1,61 @@ +/* + * 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 + +#include +#include +#include +#include + +namespace latinime { +namespace { + +TEST(BloomFilterTest, TestFilter) { + static const int ELEMENT_COUNT = 1000; + srand(time(0)); + std::vector elements; + std::unordered_set elementsThatHaveBeenSetInFilter; + for (int i = 0; i < ELEMENT_COUNT; ++i) { + elements.push_back(rand()); + } + BloomFilter bloomFilter; + for (const int elem : elements) { + ASSERT_FALSE(bloomFilter.isInFilter(elem)); + } + for (const int elem : elements) { + if (rand() % 2 == 0) { + bloomFilter.setInFilter(elem); + elementsThatHaveBeenSetInFilter.insert(elem); + } + } + for (const int elem : elements) { + const bool existsInFilter = bloomFilter.isInFilter(elem); + const bool hasBeenSetInFilter = + elementsThatHaveBeenSetInFilter.find(elem) != elementsThatHaveBeenSetInFilter.end(); + if (hasBeenSetInFilter) { + ASSERT_TRUE(existsInFilter); + } + if (!existsInFilter) { + ASSERT_FALSE(hasBeenSetInFilter); + } + } +} + +} // namespace +} // namespace latinime