diff options
Diffstat (limited to 'native/jni/tests')
4 files changed, 221 insertions, 0 deletions
diff --git a/native/jni/tests/defines_test.cpp b/native/jni/tests/defines_test.cpp new file mode 100644 index 000000000..f7b80b2b5 --- /dev/null +++ b/native/jni/tests/defines_test.cpp @@ -0,0 +1,34 @@ +/* + * 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 "defines.h" + +#include <gtest/gtest.h> + +namespace latinime { +namespace { + +TEST(DefinesTest, NELEMSForFixedLengthArray) { + const size_t SMALL_ARRAY_SIZE = 1; + const size_t LARGE_ARRAY_SIZE = 100; + int smallArray[SMALL_ARRAY_SIZE]; + int largeArray[LARGE_ARRAY_SIZE]; + EXPECT_EQ(SMALL_ARRAY_SIZE, NELEMS(smallArray)); + EXPECT_EQ(LARGE_ARRAY_SIZE, NELEMS(largeArray)); +} + +} // namespace +} // namespace latinime 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..b62021784 --- /dev/null +++ b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp @@ -0,0 +1,80 @@ +/* + * 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 <algorithm> +#include <cstdlib> +#include <functional> +#include <random> +#include <unordered_set> +#include <vector> + +namespace latinime { +namespace { + +TEST(BloomFilterTest, TestFilter) { + static const int TEST_RANDOM_DATA_MAX = 65536; + static const int ELEMENT_COUNT = 1000; + std::vector<int> elements; + + // 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()); + } + } + + // Make sure BloomFilter contains nothing by default. + BloomFilter bloomFilter; + for (const int elem : elements) { + ASSERT_FALSE(bloomFilter.isInFilter(elem)); + } + + // 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); + } + } + } + + for (const int elem : elements) { + const bool existsInFilter = bloomFilter.isInFilter(elem); + const bool hasBeenSetInFilter = + elementsThatHaveBeenSetInFilter.find(elem) != elementsThatHaveBeenSetInFilter.end(); + if (hasBeenSetInFilter) { + EXPECT_TRUE(existsInFilter) << "elem: " << elem; + } + if (!existsInFilter) { + EXPECT_FALSE(hasBeenSetInFilter) << "elem: " << elem; + } + } +} + +} // namespace +} // namespace latinime diff --git a/native/jni/tests/suggest/core/layout/normal_distribution_2d_test.cpp b/native/jni/tests/suggest/core/layout/normal_distribution_2d_test.cpp new file mode 100644 index 000000000..1d6a27c4f --- /dev/null +++ b/native/jni/tests/suggest/core/layout/normal_distribution_2d_test.cpp @@ -0,0 +1,68 @@ +/* + * 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/layout/normal_distribution_2d.h" + +#include <gtest/gtest.h> + +#include <vector> + +namespace latinime { +namespace { + +static const float ORIGIN_X = 0.0f; +static const float ORIGIN_Y = 0.0f; +static const float LARGE_STANDARD_DEVIATION = 100.0f; +static const float SMALL_STANDARD_DEVIATION = 10.0f; +static const float ZERO_RADIAN = 0.0f; + +TEST(NormalDistribution2DTest, ProbabilityDensity) { + const NormalDistribution2D distribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y, + SMALL_STANDARD_DEVIATION, ZERO_RADIAN); + + static const float SMALL_COORDINATE = 10.0f; + static const float LARGE_COORDINATE = 20.0f; + // The probability density of the point near the distribution center is larger than the + // probability density of the point that is far from distribution center. + EXPECT_GE(distribution.getProbabilityDensity(SMALL_COORDINATE, SMALL_COORDINATE), + distribution.getProbabilityDensity(LARGE_COORDINATE, LARGE_COORDINATE)); + // The probability density of the point shifted toward the direction that has larger standard + // deviation is larger than the probability density of the point shifted towards another + // direction. + EXPECT_GE(distribution.getProbabilityDensity(LARGE_COORDINATE, SMALL_COORDINATE), + distribution.getProbabilityDensity(SMALL_COORDINATE, LARGE_COORDINATE)); +} + +TEST(NormalDistribution2DTest, Rotate) { + static const float COORDINATES[] = {0.0f, 10.0f, 100.0f, -20.0f}; + static const float EPSILON = 0.01f; + const NormalDistribution2D distribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y, + SMALL_STANDARD_DEVIATION, ZERO_RADIAN); + const NormalDistribution2D rotatedDistribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y, + SMALL_STANDARD_DEVIATION, M_PI_4); + for (const float x : COORDINATES) { + for (const float y : COORDINATES) { + // The probability density of the rotated distribution at the point and the probability + // density of the original distribution at the rotated point are the same. + const float probabilityDensity0 = distribution.getProbabilityDensity(x, y); + const float probabilityDensity1 = rotatedDistribution.getProbabilityDensity(-y, x); + EXPECT_NEAR(probabilityDensity0, probabilityDensity1, EPSILON); + } + } +} + +} // namespace +} // namespace latinime diff --git a/native/jni/tests/utils/autocorrection_threshold_utils_test.cpp b/native/jni/tests/utils/autocorrection_threshold_utils_test.cpp new file mode 100644 index 000000000..cc8db700f --- /dev/null +++ b/native/jni/tests/utils/autocorrection_threshold_utils_test.cpp @@ -0,0 +1,39 @@ +/* + * 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 "utils/autocorrection_threshold_utils.h" + +#include <gtest/gtest.h> + +#include <vector> + +namespace latinime { +namespace { + +int CalcEditDistance(const std::vector<int> &before, + const std::vector<int> &after) { + return AutocorrectionThresholdUtils::editDistance( + &before[0], before.size(), &after[0], after.size()); +} + +TEST(AutocorrectionThresholdUtilsTest, SameData) { + EXPECT_EQ(0, CalcEditDistance({1}, {1})); + EXPECT_EQ(0, CalcEditDistance({2, 2}, {2, 2})); + EXPECT_EQ(0, CalcEditDistance({3, 3, 3}, {3, 3, 3})); +} + +} // namespace +} // namespace latinime |