Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 111c05ff authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi
Browse files

Add BloomFilterTest.

Change-Id: If97b9e9cc3bbaa4c7877991c11d7935626268ca3
parent 26c80662
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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
+61 −0
Original line number Diff line number Diff line
/*
 * 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>

#include <cstdlib>
#include <ctime>
#include <unordered_set>
#include <vector>

namespace latinime {
namespace {

TEST(BloomFilterTest, TestFilter) {
    static const int ELEMENT_COUNT = 1000;
    srand(time(0));
    std::vector<int> elements;
    std::unordered_set<int> 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