aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYohei Yukawa <yukawa@google.com>2014-04-09 17:33:57 +0900
committerYohei Yukawa <yukawa@google.com>2014-04-09 17:59:31 +0900
commitd4dbbd5008438855c56e38eba9c0ee1368a53a5a (patch)
treeb01b9233369d33f21140f4e5444f43a07362470b
parent111c05ff35763d6fe08a05ff341086de61498dc2 (diff)
downloadlatinime-d4dbbd5008438855c56e38eba9c0ee1368a53a5a.tar.gz
latinime-d4dbbd5008438855c56e38eba9c0ee1368a53a5a.tar.xz
latinime-d4dbbd5008438855c56e38eba9c0ee1368a53a5a.zip
Use C++11 random library
srand() and its friends are supposed to be discouraged since C++14. This CL replaces srand() with new C++11 random libraries. Change-Id: If2c25158c88c674cd3fd6d891559a9e32283e19e
-rw-r--r--native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp41
1 files changed, 30 insertions, 11 deletions
diff --git a/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp
index b1ab5e3ec..b62021784 100644
--- a/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp
+++ b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp
@@ -18,8 +18,10 @@
#include <gtest/gtest.h>
+#include <algorithm>
#include <cstdlib>
-#include <ctime>
+#include <functional>
+#include <random>
#include <unordered_set>
#include <vector>
@@ -27,32 +29,49 @@ namespace latinime {
namespace {
TEST(BloomFilterTest, TestFilter) {
+ static const int TEST_RANDOM_DATA_MAX = 65536;
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());
+
+ // 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));
}
- for (const int elem : elements) {
- if (rand() % 2 == 0) {
- bloomFilter.setInFilter(elem);
- elementsThatHaveBeenSetInFilter.insert(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) {
- ASSERT_TRUE(existsInFilter);
+ EXPECT_TRUE(existsInFilter) << "elem: " << elem;
}
if (!existsInFilter) {
- ASSERT_FALSE(hasBeenSetInFilter);
+ EXPECT_FALSE(hasBeenSetInFilter) << "elem: " << elem;
}
}
}