|
| 1 | +/* |
| 2 | + * (C) Copyright 1996- ECMWF. |
| 3 | + * |
| 4 | + * This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 | + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + * In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 | + * granted to it by virtue of its status as an intergovernmental organisation nor |
| 8 | + * does it submit to any jurisdiction. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <limits> |
| 13 | +#include <memory> |
| 14 | +#include <random> |
| 15 | +#include <set> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "eckit/config/LibEcKit.h" |
| 19 | +#include "eckit/log/Timer.h" |
| 20 | +#include "eckit/utils/RLE.h" |
| 21 | + |
| 22 | +#include "eckit/testing/Test.h" |
| 23 | + |
| 24 | +using namespace std; |
| 25 | +using namespace eckit; |
| 26 | +using namespace eckit::testing; |
| 27 | + |
| 28 | +namespace eckit::test { |
| 29 | + |
| 30 | +//---------------------------------------------------------------------------------------------------------------------- |
| 31 | + |
| 32 | +void test(const std::vector<long>& in, EncodingClock::duration timeout = std::chrono::milliseconds(2), |
| 33 | + long expectedSize = -1) { |
| 34 | + |
| 35 | + std::vector<long> out; |
| 36 | + std::vector<long> outTimeout; |
| 37 | + int maxLoop = 5000; |
| 38 | + |
| 39 | + long long n; |
| 40 | + long long t; |
| 41 | + { |
| 42 | + eckit::Timer timer; |
| 43 | + n = RLEencode2(in.begin(), in.end(), std::back_inserter(out), maxLoop); |
| 44 | + LOG_DEBUG_LIB(LibEcKit) << "no_timeout - time elapsed: " << timer.elapsed() << " - output size: " << out.size() |
| 45 | + << std::endl; |
| 46 | + } |
| 47 | + { |
| 48 | + eckit::Timer timer; |
| 49 | + t = RLEencode2(in.begin(), in.end(), std::back_inserter(outTimeout), maxLoop, timeout); |
| 50 | + LOG_DEBUG_LIB(LibEcKit) << "timeout " << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 51 | + << "ms - time elapsed: " << timer.elapsed() << " - output size: " << outTimeout.size() |
| 52 | + << std::endl; |
| 53 | + } |
| 54 | + |
| 55 | + if (expectedSize > 0) { |
| 56 | + EXPECT_EQUAL(expectedSize, n); |
| 57 | + EXPECT_EQUAL(expectedSize, t); |
| 58 | + EXPECT_EQUAL(out.size(), outTimeout.size()); |
| 59 | + for (size_t i = 0; i < out.size(); i++) { |
| 60 | + EXPECT_EQUAL(out[i], outTimeout[i]); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + std::vector<long> decoded; |
| 65 | + RLEdecode2(out.begin(), out.end(), std::back_inserter(decoded)); |
| 66 | + EXPECT_EQUAL(in.size(), decoded.size()); |
| 67 | + for (size_t i = 0; i < in.size(); i++) { |
| 68 | + EXPECT_EQUAL(in[i], decoded[i]); |
| 69 | + } |
| 70 | + |
| 71 | + std::vector<long> decodedTimeout; |
| 72 | + RLEdecode2(outTimeout.begin(), outTimeout.end(), std::back_inserter(decodedTimeout)); |
| 73 | + EXPECT_EQUAL(in.size(), decodedTimeout.size()); |
| 74 | + for (size_t i = 0; i < in.size(); i++) { |
| 75 | + EXPECT_EQUAL(in[i], decodedTimeout[i]); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +CASE("single") { |
| 80 | + std::vector<long> in = {4}; |
| 81 | + |
| 82 | + test(in, std::chrono::seconds(1), 1); |
| 83 | +} |
| 84 | + |
| 85 | +CASE("identical") { |
| 86 | + size_t size = 1000; |
| 87 | + std::vector<long> in; |
| 88 | + in.reserve(size); |
| 89 | + for (size_t i = 0; i < size; i++) { |
| 90 | + in.push_back(4); |
| 91 | + } |
| 92 | + |
| 93 | + test(in, std::chrono::seconds(1), 2); /// we expect [-1000,4] |
| 94 | +} |
| 95 | + |
| 96 | +CASE("interleaved") { |
| 97 | + size_t size = 1000; |
| 98 | + std::vector<long> in; |
| 99 | + in.reserve(size); |
| 100 | + for (size_t i = 0; i < size; i++) { |
| 101 | + in.push_back(i % 2); |
| 102 | + } |
| 103 | + |
| 104 | + test(in, std::chrono::seconds(1), 4); /// we expect [-500,-2,0,1] |
| 105 | +} |
| 106 | + |
| 107 | +CASE("pattern") { |
| 108 | + size_t size = 100000; |
| 109 | + std::vector<long> in; |
| 110 | + in.reserve(size); |
| 111 | + for (size_t i = 0; i < size; i++) { |
| 112 | + in.push_back(std::ceil(std::sqrt(i))); |
| 113 | + } |
| 114 | + |
| 115 | + test(in); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +void randTest(int limit) { |
| 120 | + std::random_device rd; // a seed source for the random number engine |
| 121 | + std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() |
| 122 | + std::uniform_int_distribution<> distrib(1, std::numeric_limits<int>::max()); |
| 123 | + |
| 124 | + size_t size = 10000000; |
| 125 | + std::vector<long> in; |
| 126 | + in.reserve(size); |
| 127 | + for (size_t i = 0; i < size; i++) { |
| 128 | + in.push_back(distrib(gen)); |
| 129 | + } |
| 130 | + test(in); |
| 131 | +} |
| 132 | + |
| 133 | +CASE("randMaxInt") { |
| 134 | + randTest(std::numeric_limits<int>::max()); |
| 135 | +} |
| 136 | + |
| 137 | +CASE("rand100") { |
| 138 | + randTest(100); |
| 139 | +} |
| 140 | + |
| 141 | +CASE("rand10") { |
| 142 | + randTest(10); |
| 143 | +} |
| 144 | + |
| 145 | +CASE("rand2") { |
| 146 | + randTest(2); |
| 147 | +} |
| 148 | +//---------------------------------------------------------------------------------------------------------------------- |
| 149 | + |
| 150 | +} // namespace eckit::test |
| 151 | + |
| 152 | +int main(int argc, char* argv[]) { |
| 153 | + return run_tests(argc, argv); |
| 154 | +} |
0 commit comments