Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
varint_tests.cpp File Reference
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <vector>
#include <catch2/catch.hpp>
#include <sysio/vm/leb128.hpp>
#include <sysio/vm/types.hpp>
Include dependency graph for varint_tests.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("Testing varuint", "[varuint_tests]")
 
 TEST_CASE ("Testing varint", "[varint_tests]")
 

Function Documentation

◆ TEST_CASE() [1/2]

TEST_CASE ( "Testing varint" ,
"" [varint_tests] )

Definition at line 91 of file varint_tests.cpp.

91 {
92 {
93 std::vector<uint8_t> tv0 = {0x0};
94 std::vector<uint8_t> tv1 = {0x1};
95 std::vector<uint8_t> tv2 = {0x7f};
96
97 guarded_ptr<uint8_t> gp0(tv0.data(), 5);
98 guarded_ptr<uint8_t> gp1(tv1.data(), 5);
99 guarded_ptr<uint8_t> gp2(tv2.data(), 5);
100
101 varint<7> v0(gp0);
102 varint<7> v1(gp1);
103 varint<7> v2(gp2);
104
105 CHECK( v0.to() == 0 );
106 CHECK( v1.to() == 1 );
107 CHECK( (int32_t)v2.to() == -1 );
108 }
109
110 {
111 std::vector<uint8_t> tv0 = {0x0};
112 std::vector<uint8_t> tv1 = {0x1};
113 std::vector<uint8_t> tv2 = {0x7f};
114 std::vector<uint8_t> tv3 = {0x80, 0x7f};
115
116 guarded_ptr<uint8_t> gp0(tv0.data(), 5);
117 guarded_ptr<uint8_t> gp1(tv1.data(), 5);
118 guarded_ptr<uint8_t> gp2(tv2.data(), 5);
119 guarded_ptr<uint8_t> gp3(tv3.data(), 5);
120
121 varint<32> v0(gp0);
122 varint<32> v1(gp1);
123 varint<32> v2(gp2);
124
125 CHECK( v0.to() == 0 );
126 CHECK( v1.to() == 1 );
127 CHECK( v2.to() == -1 );
128 }
129
130 {
131 std::vector<uint8_t> tv0 = {0x0};
132 std::vector<uint8_t> tv1 = {0x1};
133 std::vector<uint8_t> tv2 = {0x7f};
134
135 guarded_ptr<uint8_t> gp0(tv0.data(), 5);
136 guarded_ptr<uint8_t> gp1(tv1.data(), 5);
137 guarded_ptr<uint8_t> gp2(tv2.data(), 5);
138
139 varint<64> v0(gp0);
140 varint<64> v1(gp1);
141 varint<64> v2(gp2);
142
143 CHECK( v0.to() == 0 );
144 CHECK( v1.to() == 1 );
145 CHECK( v2.to() == -1 );
146 }
147
148 {
149 varint<32> v0((int32_t)0);
150 varint<32> v1((int32_t)std::numeric_limits<int32_t>::min());
151 varint<32> v2((int32_t)std::numeric_limits<int32_t>::max());
152
153 varint<64> v3((int64_t)0);
154 varint<64> v4((int64_t)std::numeric_limits<int64_t>::min());
155 varint<64> v5((int64_t)std::numeric_limits<int64_t>::max());
156
157 CHECK( v0.to() == 0 );
158 CHECK( v1.to() == std::numeric_limits<int32_t>::min() );
159 CHECK( v2.to() == std::numeric_limits<int32_t>::max() );
160
161 CHECK( v3.to() == 0 );
162 CHECK( v4.to() == std::numeric_limits<int64_t>::min() );
163 CHECK( v5.to() == std::numeric_limits<int64_t>::max() );
164 }
165}
#define CHECK(cond)
Definition util.h:80
signed __int64 int64_t
Definition stdint.h:135
signed int int32_t
Definition stdint.h:123
Here is the call graph for this function:

◆ TEST_CASE() [2/2]

TEST_CASE ( "Testing varuint" ,
"" [varuint_tests] )

Definition at line 14 of file varint_tests.cpp.

14 {
15 {
16 std::vector<uint8_t> tv = {0};
17 guarded_ptr<uint8_t> gp1_0(tv.data(), 5);
18 guarded_ptr<uint8_t> gp7_0(tv.data(), 5);
19 guarded_ptr<uint8_t> gp32_0(tv.data(), 5);
20 varuint<1> v1(gp1_0);
21 varuint<7> v7(gp7_0);
22 varuint<32> v32(gp32_0);
23
24 CHECK( v1.to() == 0 );
25 CHECK( v7.to() == 0 );
26 CHECK( v32.to() == 0 );
27
28 tv[0] = 1;
29 guarded_ptr<uint8_t> gp1_1(tv.data(), 5);
30 guarded_ptr<uint8_t> gp7_1(tv.data(), 5);
31 guarded_ptr<uint8_t> gp32_1(tv.data(), 5);
32
33 varuint<1> v1_1(gp1_1);
34 varuint<7> v7_1(gp7_1);
35 varuint<32> v32_1(gp32_1);
36 CHECK( v1_1.to() == 1 );
37 CHECK( v7_1.to() == 1 );
38 CHECK( v32_1.to() == 1 );
39 }
40
41 {
42 std::vector<uint8_t> tv = {0x7f};
43 guarded_ptr<uint8_t> gp7_0(tv.data(), 5);
44 guarded_ptr<uint8_t> gp32_0(tv.data(), 5);
45 varuint<7> v7(gp7_0);
46 varuint<32> v32(gp32_0);
47
48 CHECK( v7.to() == 127 );
49 CHECK( v32.to() == 127 );
50
51 tv[0] = 1;
52 std::vector<uint8_t> tv2 = {0x80, 0x7f};
53 guarded_ptr<uint8_t> gp7_1(tv2.data(), 5);
54 guarded_ptr<uint8_t> gp32_1(tv2.data(), 5);
55
56 varuint<32> v32_1(gp32_1);
57 CHECK( v32_1.to() == 16256 );
58 }
59
60 {
61 std::vector<uint8_t> tv0 = {0xb4, 0x7};
62 guarded_ptr<uint8_t> gp32_0(tv0.data(), 5);
63
64 std::vector<uint8_t> tv1 = {0x8c, 0x8};
65 guarded_ptr<uint8_t> gp32_1(tv1.data(), 5);
66
67 std::vector<uint8_t> tv2 = {0xff, 0xff, 0xff, 0xff, 0xf};
68 guarded_ptr<uint8_t> gp32_2(tv2.data(), 5);
69
70 varuint<32> v32_0(gp32_0);
71 varuint<32> v32_1(gp32_1);
72 varuint<32> v32_2(gp32_2);
73
74 CHECK( v32_0.to() == 0x3b4 );
75 CHECK( v32_1.to() == 0x40c );
76 CHECK( v32_2.to() == 0xffffffff );
77 }
78
79 {
80 varuint<32> v0((uint32_t)0);
81 varuint<32> v1((uint32_t)2147483647);
82 varuint<32> v2((uint32_t)4294967295);
83
84 CHECK( v0.to() == 0 );
85 CHECK( v1.to() == 2147483647 );
86 CHECK( v2.to() == 4294967295 );
87 }
88
89}
unsigned int uint32_t
Definition stdint.h:126
Here is the call graph for this function: