-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathCompactIntegers.cpp
More file actions
80 lines (69 loc) · 2.66 KB
/
Copy pathCompactIntegers.cpp
File metadata and controls
80 lines (69 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include "rapidcheck/detail/Serialization.h"
#include "util/Meta.h"
#include "util/TypeListMacros.h"
#include "util/Serialization.h"
using namespace rc;
using namespace rc::detail;
using namespace rc::test;
struct SerializeCompactProperties {
template <typename T>
static void exec() {
templatedProp<T>("returns an iterator past the written data",
[](T value) {
std::vector<std::uint8_t> data(11, 0xFF);
const auto it = serializeCompact(value, begin(data));
RC_ASSERT(*it == 0xFF);
});
}
};
TEST_CASE("serializeCompact") {
forEachType<SerializeCompactProperties, RC_INTEGRAL_TYPES>();
}
struct DeserializeCompactProperties {
template <typename T>
static void exec() {
templatedProp<T>("deserializes output of serializeCompact",
[](T value) {
std::vector<std::uint8_t> data;
serializeCompact(value, std::back_inserter(data));
T output;
deserializeCompact(begin(data), end(data), output);
RC_ASSERT(output == value);
});
templatedProp<T>(
"returns an iterator past the end of the deserialized data",
[](T value) {
std::vector<std::uint8_t> data(11, 0);
const auto it = serializeCompact(value, begin(data));
T output;
const auto rit = deserializeCompact(begin(data), end(data), output);
RC_ASSERT(rit == it);
});
templatedProp<T>("throws SerializationException if data has unexpected end",
[](T value) {
std::vector<std::uint8_t> data;
serializeCompact(value, std::back_inserter(data));
data.erase(end(data) - 1);
T output;
RC_ASSERT_THROWS_AS(
deserializeCompact(begin(data), end(data), output),
SerializationException);
});
}
};
TEST_CASE("deserializeCompact") {
forEachType<DeserializeCompactProperties, RC_INTEGRAL_TYPES>();
prop(
"representation for a number is identical regardless of data type for "
"unsigned",
[](std::uint32_t value) {
std::vector<std::uint8_t> data32;
std::vector<std::uint8_t> data64;
serializeCompact(value, std::back_inserter(data32));
serializeCompact(static_cast<std::uint64_t>(value),
std::back_inserter(data64));
RC_ASSERT(data32 == data64);
});
}