forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapping_integers_unwrap.cc
44 lines (39 loc) · 1.97 KB
/
wrapping_integers_unwrap.cc
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
#include "test_should_be.hh"
#include "wrapping_integers.hh"
#include <cstdint>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
int main() {
try {
// Unwrap the first byte after ISN
test_should_be(unwrap(WrappingInt32(1), WrappingInt32(0), 0), 1ul);
// Unwrap the first byte after the first wrap
test_should_be(unwrap(WrappingInt32(1), WrappingInt32(0), UINT32_MAX), (1ul << 32) + 1);
// Unwrap the last byte before the third wrap
test_should_be(unwrap(WrappingInt32(UINT32_MAX - 1), WrappingInt32(0), 3 * (1ul << 32)), 3 * (1ul << 32) - 2);
// Unwrap the 10th from last byte before the third wrap
test_should_be(unwrap(WrappingInt32(UINT32_MAX - 10), WrappingInt32(0), 3 * (1ul << 32)), 3 * (1ul << 32) - 11);
// Non-zero ISN
test_should_be(unwrap(WrappingInt32(UINT32_MAX), WrappingInt32(10), 3 * (1ul << 32)), 3 * (1ul << 32) - 11);
// Big unwrap
test_should_be(unwrap(WrappingInt32(UINT32_MAX), WrappingInt32(0), 0), static_cast<uint64_t>(UINT32_MAX));
// Unwrap a non-zero ISN
test_should_be(unwrap(WrappingInt32(16), WrappingInt32(16), 0), 0ul);
// Big unwrap with non-zero ISN
test_should_be(unwrap(WrappingInt32(15), WrappingInt32(16), 0), static_cast<uint64_t>(UINT32_MAX));
// Big unwrap with non-zero ISN
test_should_be(unwrap(WrappingInt32(0), WrappingInt32(INT32_MAX), 0), static_cast<uint64_t>(INT32_MAX) + 2);
// Barely big unwrap with non-zero ISN
test_should_be(unwrap(WrappingInt32(UINT32_MAX), WrappingInt32(INT32_MAX), 0), static_cast<uint64_t>(1) << 31);
// Nearly big unwrap with non-zero ISN
test_should_be(unwrap(WrappingInt32(UINT32_MAX), WrappingInt32(1ul << 31), 0),
static_cast<uint64_t>(UINT32_MAX) >> 1);
} catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
return EXIT_SUCCESS;
}