| 1 |
#include "gtest/gtest.h" |
| 2 |
#include <vector> |
| 3 |
#include <memory> |
| 4 |
#include <random> |
| 5 |
#include <cstring> // std::memcpy() |
| 6 |
|
| 7 |
#include "icecream.h" |
| 8 |
|
| 9 |
namespace { |
| 10 |
|
| 11 |
TEST(icecream, StreamWriteRead) |
| 12 |
{ |
| 13 |
const size_t N = 159; |
| 14 |
auto mem = std::make_unique<uint64_t[]>(4); |
| 15 |
auto s1 = icecream(); |
| 16 |
icecream_use_mem(&s1, mem.get(), 4); |
| 17 |
auto vec = std::vector<bool>(N); |
| 18 |
|
| 19 |
// Make N writes |
| 20 |
std::random_device rd; |
| 21 |
std::mt19937 gen(rd()); |
| 22 |
std::uniform_int_distribution<unsigned int> distrib1(0, 1); |
| 23 |
for (size_t i = 0; i < N; i++) { |
| 24 |
const auto bit = distrib1(gen); |
| 25 |
vec[i] = bit; |
| 26 |
EXPECT_EQ(icecream_wtell(&s1), i) << " at idx = " << i; |
| 27 |
icecream_wbit(&s1, bit); |
| 28 |
EXPECT_EQ(icecream_wtell(&s1), i + 1) << " at idx = " << i; |
| 29 |
} |
| 30 |
EXPECT_EQ(icecream_wtell(&s1), N); |
| 31 |
icecream_flush(&s1); |
| 32 |
EXPECT_EQ(icecream_wtell(&s1), 192); |
| 33 |
|
| 34 |
icecream_rewind(&s1); |
| 35 |
for (size_t i = 0; i < N; i++) { |
| 36 |
EXPECT_EQ(icecream_rtell(&s1), i) << " at idx = " << i; |
| 37 |
EXPECT_EQ(icecream_rbit(&s1), vec[i]) << " at idx = " << i; |
| 38 |
EXPECT_EQ(icecream_rtell(&s1), i + 1) << " at idx = " << i; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
TEST(icecream, PartialWord) |
| 43 |
{ |
| 44 |
auto mem = std::make_unique<char[]>(20); |
| 45 |
auto s1 = icecream(); |
| 46 |
icecream_use_mem(&s1, mem.get(), 20); |
| 47 |
auto vec = std::vector<bool>(); |
| 48 |
|
| 49 |
// Make 80 writes. The first 10 bytes are supposed to keep the result. |
| 50 |
std::random_device rd; |
| 51 |
std::mt19937 gen(rd()); |
| 52 |
std::uniform_int_distribution<unsigned int> distrib(0, 1); |
| 53 |
for (int i = 0; i < 80; i++) { |
| 54 |
const auto bit = distrib(gen); |
| 55 |
vec.push_back(bit); |
| 56 |
icecream_wbit(&s1, bit); |
| 57 |
} |
| 58 |
icecream_flush(&s1); |
| 59 |
|
| 60 |
// Copy over the first 10 bytes, and test if the bits are the same. |
| 61 |
auto mem2 = std::make_unique<char[]>(16); |
| 62 |
for (int i = 0; i < 16; i++) |
| 63 |
mem2[i] = 15; |
| 64 |
std::memcpy(mem2.get(), mem.get(), 10); |
| 65 |
icecream_use_mem(&s1, mem2.get(), 16); |
| 66 |
for (int i = 0; i < 80; i++) |
| 67 |
EXPECT_EQ(icecream_rbit(&s1), vec[i]) << " at idx = " << i; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
} |