diff options
author | Maxim Pimenov <m@maps.me> | 2015-07-15 21:19:37 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:56:13 +0300 |
commit | eaa56751946aff0514bfa12101dc87b0bc4441a4 (patch) | |
tree | add66a6bee58e8e9469ed54944a24e36aa039555 /coding | |
parent | 21cad7ee4693c97b0fed3dc1c46a2102faf5f94c (diff) |
[omim] Remove base/pseudo_random.hpp and add std/random.hpp instead.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding_tests/arithmetic_codec_test.cpp | 8 | ||||
-rw-r--r-- | coding/coding_tests/base64_for_user_id_test.cpp | 10 | ||||
-rw-r--r-- | coding/coding_tests/bit_streams_test.cpp | 10 | ||||
-rw-r--r-- | coding/coding_tests/compressed_bit_vector_test.cpp | 33 | ||||
-rw-r--r-- | coding/coding_tests/compressed_varnum_vector_test.cpp | 9 | ||||
-rw-r--r-- | coding/coding_tests/file_sort_test.cpp | 8 | ||||
-rw-r--r-- | coding/coding_tests/hex_test.cpp | 8 | ||||
-rw-r--r-- | coding/coding_tests/reader_cache_test.cpp | 9 | ||||
-rw-r--r-- | coding/coding_tests/var_serial_vector_test.cpp | 10 | ||||
-rw-r--r-- | coding/coding_tests/varint_vector_test.cpp | 17 |
10 files changed, 67 insertions, 55 deletions
diff --git a/coding/coding_tests/arithmetic_codec_test.cpp b/coding/coding_tests/arithmetic_codec_test.cpp index 27fbc83529..4ed548ebf8 100644 --- a/coding/coding_tests/arithmetic_codec_test.cpp +++ b/coding/coding_tests/arithmetic_codec_test.cpp @@ -3,12 +3,12 @@ #include "coding/arithmetic_codec.hpp" #include "coding/reader.hpp" -#include "base/pseudo_random.hpp" +#include "std/random.hpp" UNIT_TEST(ArithmeticCodec) { - PseudoRNG32 rng; + mt19937 rng(0); uint32_t const MAX_FREQ = 2048; uint32_t const ALPHABET_SIZE = 256; @@ -16,7 +16,7 @@ UNIT_TEST(ArithmeticCodec) vector<uint32_t> freqs; // Generate random freqs. for (uint32_t i = 0; i < ALPHABET_SIZE; ++i) { - uint32_t freq = rng.Generate() % MAX_FREQ; + uint32_t freq = rng() % MAX_FREQ; freqs.push_back(freq); } // Make at least one frequency zero for corner cases. @@ -25,7 +25,7 @@ UNIT_TEST(ArithmeticCodec) for (uint32_t i = 0; i < freqs.size(); ++i) { uint32_t freq = freqs[i]; for (uint32_t j = 0; j < freq; ++j) { - uint32_t pos = rng.Generate() % (symbols.size() + 1); + uint32_t pos = rng() % (symbols.size() + 1); symbols.insert(symbols.begin() + pos, 1, i); } } diff --git a/coding/coding_tests/base64_for_user_id_test.cpp b/coding/coding_tests/base64_for_user_id_test.cpp index 5bf9c3e40b..527c7f91e6 100644 --- a/coding/coding_tests/base64_for_user_id_test.cpp +++ b/coding/coding_tests/base64_for_user_id_test.cpp @@ -1,9 +1,11 @@ #include "testing/testing.hpp" +#include "coding/base64.hpp" + #include "base/logging.hpp" -#include "base/pseudo_random.hpp" -#include "coding/base64.hpp" +#include "std/random.hpp" + using namespace base64_for_user_ids; @@ -23,7 +25,7 @@ UNIT_TEST(Base64_Encode_User_Ids) UNIT_TEST(Base64_QualityTest_User_Ids) { size_t const NUMBER_OF_TESTS = 10000; - LCG32 generator(NUMBER_OF_TESTS); + mt19937 rng(0); for (size_t i = 0; i < NUMBER_OF_TESTS; ++i) { string randomBytes; @@ -34,7 +36,7 @@ UNIT_TEST(Base64_QualityTest_User_Ids) randomBytes.push_back('\0'); continue; } - randomBytes.push_back(static_cast<char>(generator.Generate())); + randomBytes.push_back(static_cast<char>(rng())); } string const result = encode(randomBytes); TEST_GREATER_OR_EQUAL(result.size(), randomBytes.size(), diff --git a/coding/coding_tests/bit_streams_test.cpp b/coding/coding_tests/bit_streams_test.cpp index c7a15ec1d9..f9ad0d23ed 100644 --- a/coding/coding_tests/bit_streams_test.cpp +++ b/coding/coding_tests/bit_streams_test.cpp @@ -4,22 +4,20 @@ #include "coding/reader.hpp" #include "coding/writer.hpp" -#include "base/pseudo_random.hpp" - +#include "std/random.hpp" #include "std/utility.hpp" #include "std/vector.hpp" -using namespace rnd; - UNIT_TEST(BitStream_ReadWrite) { + mt19937 rng(0); uint32_t const NUMS_CNT = 1000; vector< pair<uint64_t, uint32_t> > nums; for (uint32_t i = 0; i < NUMS_CNT; ++i) { - uint32_t numBits = GetRand64() % 65; - uint64_t num = GetRand64() & ((uint64_t(1) << numBits) - 1); + uint32_t numBits = rng() % 65; + uint64_t num = rng() & ((uint64_t(1) << numBits) - 1); nums.push_back(make_pair(num, numBits)); } diff --git a/coding/coding_tests/compressed_bit_vector_test.cpp b/coding/coding_tests/compressed_bit_vector_test.cpp index 026abe1a8d..70e4892388 100644 --- a/coding/coding_tests/compressed_bit_vector_test.cpp +++ b/coding/coding_tests/compressed_bit_vector_test.cpp @@ -4,21 +4,20 @@ #include "coding/reader.hpp" #include "coding/writer.hpp" -#include "base/pseudo_random.hpp" +#include "std/random.hpp" -using namespace rnd; - uint32_t const NUMS_COUNT = 12345; UNIT_TEST(CompressedBitVector_Sparse) { + mt19937 rng(0); vector<uint32_t> posOnes; uint32_t sum = 0; for (uint32_t i = 0; i < NUMS_COUNT; ++i) { - uint32_t byteSize = GetRand64() % 2 + 1; - uint64_t num = GetRand64() & ((uint64_t(1) << (byteSize * 7)) - 1); + uint32_t byteSize = rng() % 2 + 1; + uint64_t num = rng() & ((uint64_t(1) << (byteSize * 7)) - 1); if (num == 0) num = 1; sum += num; posOnes.push_back(sum); @@ -43,16 +42,17 @@ UNIT_TEST(CompressedBitVector_Sparse) UNIT_TEST(CompressedBitVector_Dense) { + mt19937 rng(0); vector<uint32_t> posOnes; //uint32_t prevPos = 0; uint32_t sum = 0; for (uint32_t i = 0; i < NUMS_COUNT; ++i) { - uint32_t zeroesByteSize = GetRand64() % 2 + 1; - uint64_t zeroesRangeSize = (GetRand64() & ((uint64_t(1) << (zeroesByteSize * 7)) - 1)) + 1; + uint32_t zeroesByteSize = rng() % 2 + 1; + uint64_t zeroesRangeSize = (rng() & ((uint64_t(1) << (zeroesByteSize * 7)) - 1)) + 1; sum += zeroesRangeSize; - uint32_t onesByteSize = GetRand64() % 1 + 1; - uint64_t onesRangeSize = (GetRand64() & ((uint64_t(1) << (onesByteSize * 7)) - 1)) + 1; + uint32_t onesByteSize = rng() % 1 + 1; + uint64_t onesRangeSize = (rng() & ((uint64_t(1) << (onesByteSize * 7)) - 1)) + 1; for (uint32_t j = 0; j < onesRangeSize; ++j) posOnes.push_back(sum + j); sum += onesRangeSize; } @@ -76,11 +76,12 @@ UNIT_TEST(CompressedBitVector_Dense) UNIT_TEST(BitVectors_And) { + mt19937 rng(0); vector<bool> v1(NUMS_COUNT * 2, false), v2(NUMS_COUNT * 2, false); for (uint32_t i = 0; i < NUMS_COUNT; ++i) { - v1[GetRand64() % v1.size()] = true; - v2[GetRand64() % v2.size()] = true; + v1[rng() % v1.size()] = true; + v2[rng() % v2.size()] = true; } vector<uint32_t> posOnes1, posOnes2, andPos; for (uint32_t i = 0; i < v1.size(); ++i) @@ -95,11 +96,12 @@ UNIT_TEST(BitVectors_And) UNIT_TEST(BitVectors_Or) { + mt19937 rng(0); vector<bool> v1(NUMS_COUNT * 2, false), v2(NUMS_COUNT * 2, false); for (uint32_t i = 0; i < NUMS_COUNT; ++i) { - v1[GetRand64() % v1.size()] = true; - v2[GetRand64() % v2.size()] = true; + v1[rng() % v1.size()] = true; + v2[rng() % v2.size()] = true; } vector<uint32_t> posOnes1, posOnes2, orPos; for (uint32_t i = 0; i < v1.size(); ++i) @@ -114,13 +116,14 @@ UNIT_TEST(BitVectors_Or) UNIT_TEST(BitVectors_SubAnd) { + mt19937 rng(0); vector<bool> v1(NUMS_COUNT * 2, false); //uint64_t numV1Ones = 0; - for (uint32_t i = 0; i < v1.size(); ++i) v1[i] = (GetRand64() % 2) == 0; + for (uint32_t i = 0; i < v1.size(); ++i) v1[i] = (rng() % 2) == 0; vector<uint32_t> posOnes1; for (uint32_t i = 0; i < v1.size(); ++i) if (v1[i]) posOnes1.push_back(i); vector<bool> v2(posOnes1.size(), false); - for (uint32_t i = 0; i < v2.size(); ++i) v2[i] = (GetRand64() % 2) == 0; + for (uint32_t i = 0; i < v2.size(); ++i) v2[i] = (rng() % 2) == 0; vector<uint32_t> posOnes2, subandPos; for (uint32_t i = 0; i < v2.size(); ++i) if (v2[i]) posOnes2.push_back(i); for (uint32_t i = 0, j = 0; i < v1.size(); ++i) diff --git a/coding/coding_tests/compressed_varnum_vector_test.cpp b/coding/coding_tests/compressed_varnum_vector_test.cpp index a696f4c8a3..fdeb353fcb 100644 --- a/coding/coding_tests/compressed_varnum_vector_test.cpp +++ b/coding/coding_tests/compressed_varnum_vector_test.cpp @@ -6,11 +6,9 @@ #include "coding/reader.hpp" #include "coding/writer.hpp" -#include "base/pseudo_random.hpp" +#include "std/random.hpp" -using namespace rnd; - struct NumsSource { NumsSource(vector<uint64_t> const & v) : m_v(v) {} @@ -21,14 +19,15 @@ struct NumsSource UNIT_TEST(CompressedVarnumVector) { + mt19937 rng(0); uint32_t const NUMS_CNT = 5000; uint32_t const MAX_NUM_BYTESIZE = 5; vector<uint64_t> nums, sums(1, 0); uint64_t sum = 0; for (uint32_t i = 0; i < NUMS_CNT; ++i) { - uint32_t byteSize = GetRand64() % MAX_NUM_BYTESIZE + 1; - uint64_t num = GetRand64() & ((uint64_t(1) << (byteSize * 8)) - 1); + uint32_t byteSize = rng() % MAX_NUM_BYTESIZE + 1; + uint64_t num = rng() & ((uint64_t(1) << (byteSize * 8)) - 1); nums.push_back(num); sum += num; sums.push_back(sum); diff --git a/coding/coding_tests/file_sort_test.cpp b/coding/coding_tests/file_sort_test.cpp index 0c4751c445..4a9feecac4 100644 --- a/coding/coding_tests/file_sort_test.cpp +++ b/coding/coding_tests/file_sort_test.cpp @@ -1,8 +1,10 @@ #include "testing/testing.hpp" + #include "coding/file_sort.hpp" #include "coding/write_to_sink.hpp" #include "coding/reader.hpp" -#include "base/pseudo_random.hpp" + +#include "std/random.hpp" namespace { @@ -40,10 +42,10 @@ UNIT_TEST(FileSorter_Smoke) UNIT_TEST(FileSorter_Random) { - PseudoRNG32 rng; + mt19937 rng(0); vector<uint32_t> data(1000); for (size_t i = 0; i < data.size(); ++i) - data[i] = ((i+1 % 100) ? rng.Generate() : data[i - 20]); + data[i] = ((i+1 % 100) ? rng() : data[i - 20]); TestFileSorter(data, "file_sorter_test_random.tmp", data.size() / 10); } diff --git a/coding/coding_tests/hex_test.cpp b/coding/coding_tests/hex_test.cpp index 2b9993c59d..aabc66f4fc 100644 --- a/coding/coding_tests/hex_test.cpp +++ b/coding/coding_tests/hex_test.cpp @@ -2,8 +2,7 @@ #include "coding/hex.hpp" -#include "base/pseudo_random.hpp" - +#include "std/random.hpp" #include "std/string.hpp" @@ -18,9 +17,12 @@ UNIT_TEST(GoldenRecode) UNIT_TEST(RandomRecode) { + mt19937 rng(0); for (size_t i = 0; i < 256; ++i) { - string const data = rnd::GenerateString(); + string data(1 + (rng() % 20), 0); + for (size_t j = 0; j < data.size(); ++j) + data[j] = static_cast<char>(rng() % 26) + 'A'; TEST_EQUAL(data, FromHex(ToHex(data)), ()); } } diff --git a/coding/coding_tests/reader_cache_test.cpp b/coding/coding_tests/reader_cache_test.cpp index 11969a6370..66079d7034 100644 --- a/coding/coding_tests/reader_cache_test.cpp +++ b/coding/coding_tests/reader_cache_test.cpp @@ -3,7 +3,8 @@ #include "coding/reader_cache.hpp" #include "coding/reader.hpp" -#include "base/pseudo_random.hpp" +#include "std/algorithm.hpp" +#include "std/random.hpp" namespace @@ -31,11 +32,11 @@ UNIT_TEST(CacheReaderRandomTest) data[i] = static_cast<char>(i % 253); MemReader memReader(&data[0], data.size()); CacheReader<MemReader> cacheReader(MemReader(&data[0], data.size()), 10, 5); - PseudoRNG32 rng; + mt19937 rng(0); for (size_t i = 0; i < 100000; ++i) { - size_t pos = rng.Generate() % data.size(); - size_t len = min(1 + (rng.Generate() % 127), data.size() - pos); + size_t pos = rng() % data.size(); + size_t len = min(static_cast<size_t>(1 + (rng() % 127)), data.size() - pos); string readMem(len, '0'), readCache(len, '0'); memReader.Read(pos, &readMem[0], len); cacheReader.Read(pos, &readCache[0], len); diff --git a/coding/coding_tests/var_serial_vector_test.cpp b/coding/coding_tests/var_serial_vector_test.cpp index 911b82dee6..b2152ffe98 100644 --- a/coding/coding_tests/var_serial_vector_test.cpp +++ b/coding/coding_tests/var_serial_vector_test.cpp @@ -8,8 +8,8 @@ #include "coding/writer.hpp" #include "base/macros.hpp" -#include "base/pseudo_random.hpp" +#include "std/random.hpp" #include "std/string.hpp" #include "std/vector.hpp" @@ -87,10 +87,16 @@ UNIT_TEST(ReadSerial) UNIT_TEST(EncodeDecode) { + mt19937 rng(0); vector<string> elements; for (size_t i = 0; i < 1024; ++i) - elements.push_back(rnd::GenerateString()); + { + string s(1 + (rng() % 20), 0); + for (size_t j = 0; j < s.size(); ++j) + s[j] = static_cast<char>(rng() % 26) + 'a'; + elements.push_back(s); + } string serial; PushBackByteSink<string> sink(serial); diff --git a/coding/coding_tests/varint_vector_test.cpp b/coding/coding_tests/varint_vector_test.cpp index b5090b8ccb..be0aa896df 100644 --- a/coding/coding_tests/varint_vector_test.cpp +++ b/coding/coding_tests/varint_vector_test.cpp @@ -1,12 +1,11 @@ #include "testing/testing.hpp" /* +#include "coding/reader.hpp" #include "coding/varint_vector.hpp" #include "coding/writer.hpp" -#include "coding/reader.hpp" - -#include "base/pseudo_random.hpp" +#include "std/random.hpp" using namespace varint; @@ -23,7 +22,7 @@ UNIT_TEST(VarintVector_Use) uint32_t const c_index_tests_count = 50000; uint32_t const c_sum_tests_count = 20000; - PseudoRNG32 rnd; + mt19937 rng(0); // Generate vector. { @@ -32,8 +31,8 @@ UNIT_TEST(VarintVector_Use) for (uint32_t i = 0; i < c_nums_count; ++i) { g_nums_sums.push_back(sum); - uint8_t const byte_size = rnd.Generate() % 6 + 1; - uint64_t const num = rnd.Generate() & ((uint64_t(1) << (byte_size * 7)) - 1); + uint8_t const byte_size = rng() % 6 + 1; + uint64_t const num = rng() & ((uint64_t(1) << (byte_size * 7)) - 1); g_nums.push_back(num); builder.AddNum(num); @@ -70,7 +69,7 @@ UNIT_TEST(VarintVector_Use) Vector v(&reader); for (uint32_t i = 0; i < c_index_tests_count; ++i) { - uint64_t const index = rnd.Generate() % g_nums.size(); + uint64_t const index = rng() % g_nums.size(); uint32_t serial_pos = 0; uint64_t sum_before = 0; @@ -113,7 +112,7 @@ UNIT_TEST(VarintVector_Use) Vector v(&reader); for (uint32_t i = 0; i < c_sum_tests_count; ++i) { - uint64_t index = rnd.Generate() % (g_nums_sums.size() - 2); + uint64_t index = rng() % (g_nums_sums.size() - 2); while (g_nums_sums[index] == g_nums_sums[index + 1]) { ++index; @@ -141,7 +140,7 @@ UNIT_TEST(VarintVector_Use) Vector v(&reader); for (uint32_t i = 0; i < c_sum_tests_count; ++i) { - uint64_t index = rnd.Generate() % (g_nums_sums.size() - 2); + uint64_t index = rng() % (g_nums_sums.size() - 2); while (g_nums_sums[index] + 1 >= g_nums_sums[index + 1]) { ++index; |