Skip to content

Commit e716f45

Browse files
BigInt tests
1 parent e5e9d3d commit e716f45

File tree

2 files changed

+158
-3
lines changed

2 files changed

+158
-3
lines changed

source/types/big_int.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ BigInt::BigInt() : cpp_int(0) {
3636
BigInt::BigInt(const int& value) : cpp_int(value) {
3737
}
3838

39-
BigInt::BigInt(const cpp_int& value) : cpp_int(value) {
40-
}
41-
4239
void
4340
BigInt::fromTrits(const Types::Trits& trits) {
4441
assign(0);

test/source/types/big_int_test.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2017-2018 Thibault Martinez and Simon Ninon
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
//
25+
26+
#include <gtest/gtest.h>
27+
28+
#include <iota/constants.hpp>
29+
#include <iota/types/big_int.hpp>
30+
31+
TEST(BigInt, DefaultCtor) {
32+
IOTA::Types::BigInt big;
33+
34+
EXPECT_EQ(big, 0);
35+
}
36+
37+
TEST(BigInt, ValueCtor) {
38+
IOTA::Types::BigInt big(42);
39+
40+
EXPECT_EQ(big, 42);
41+
}
42+
43+
TEST(BigInt, FromTrits) {
44+
IOTA::Types::BigInt b1;
45+
IOTA::Types::BigInt b2;
46+
IOTA::Types::BigInt b3;
47+
IOTA::Types::BigInt b4;
48+
IOTA::Types::BigInt b5;
49+
IOTA::Types::BigInt b6;
50+
IOTA::Types::BigInt b7;
51+
52+
b1.fromTrits({});
53+
b2.fromTrits({ 1, 0, 0 });
54+
b3.fromTrits({ -1, 0, 0 });
55+
b4.fromTrits({ 0, -1, -1, -1, 1, 0 });
56+
b5.fromTrits({ 0, 1, 1, 1, -1, 0 });
57+
b6.fromTrits({ 0, 0, -1, 0, 1, -1, 0, -1, -1, -1, 1, 0, 1, 0, 0, -1, -1, 1, 0, 1, 0 });
58+
b7.fromTrits({ 0, 0, 1, 0, -1, 1, 0, 1, 1, 1, -1, 0, -1, 0, 0, 1, 1, -1, 0, -1, 0 });
59+
EXPECT_EQ(b1, 0);
60+
EXPECT_EQ(b2, 1);
61+
EXPECT_EQ(b3, -1);
62+
EXPECT_EQ(b4, 42);
63+
EXPECT_EQ(b5, -42);
64+
EXPECT_EQ(b6, 1234567890);
65+
EXPECT_EQ(b7, -1234567890);
66+
}
67+
68+
TEST(BigInt, ToTrits) {
69+
IOTA::Types::BigInt b1(0);
70+
IOTA::Types::BigInt b2(1);
71+
IOTA::Types::BigInt b3(-1);
72+
IOTA::Types::BigInt b4(42);
73+
IOTA::Types::BigInt b5(-42);
74+
IOTA::Types::BigInt b6(1234567890);
75+
IOTA::Types::BigInt b7(-1234567890);
76+
77+
IOTA::Types::Trits t1({});
78+
t1.insert(std::end(t1), IOTA::TritHashLength - t1.size(), 0);
79+
IOTA::Types::Trits t2({ 1, 0, 0 });
80+
t2.insert(std::end(t2), IOTA::TritHashLength - t2.size(), 0);
81+
IOTA::Types::Trits t3({ -1, 0, 0 });
82+
t3.insert(std::end(t3), IOTA::TritHashLength - t3.size(), 0);
83+
IOTA::Types::Trits t4({ 0, -1, -1, -1, 1, 0 });
84+
t4.insert(std::end(t4), IOTA::TritHashLength - t4.size(), 0);
85+
IOTA::Types::Trits t5({ 0, 1, 1, 1, -1, 0 });
86+
t5.insert(std::end(t5), IOTA::TritHashLength - t5.size(), 0);
87+
IOTA::Types::Trits t6({ 0, 0, -1, 0, 1, -1, 0, -1, -1, -1, 1, 0, 1, 0, 0, -1, -1, 1, 0, 1 });
88+
t6.insert(std::end(t6), IOTA::TritHashLength - t6.size(), 0);
89+
IOTA::Types::Trits t7({ 0, 0, 1, 0, -1, 1, 0, 1, 1, 1, -1, 0, -1, 0, 0, 1, 1, -1, 0, -1 });
90+
t7.insert(std::end(t7), IOTA::TritHashLength - t7.size(), 0);
91+
92+
EXPECT_EQ(b1.toTrits(), t1);
93+
EXPECT_EQ(b2.toTrits(), t2);
94+
EXPECT_EQ(b3.toTrits(), t3);
95+
EXPECT_EQ(b4.toTrits(), t4);
96+
EXPECT_EQ(b5.toTrits(), t5);
97+
EXPECT_EQ(b6.toTrits(), t6);
98+
EXPECT_EQ(b7.toTrits(), t7);
99+
}
100+
101+
TEST(BigInt, FromBytes) {
102+
IOTA::Types::BigInt b1;
103+
IOTA::Types::BigInt b2;
104+
IOTA::Types::BigInt b3;
105+
IOTA::Types::BigInt b4;
106+
IOTA::Types::BigInt b5;
107+
IOTA::Types::BigInt b6;
108+
IOTA::Types::BigInt b7;
109+
110+
b1.fromBytes({ (int8_t)0x00, (int8_t)0x00, (int8_t)0x00, (int8_t)0x00 });
111+
b2.fromBytes({ (int8_t)0x00, (int8_t)0x00, (int8_t)0x00, (int8_t)0x01 });
112+
b3.fromBytes({ (int8_t)0xff, (int8_t)0xff, (int8_t)0xff, (int8_t)0xff });
113+
b4.fromBytes({ (int8_t)0x00, (int8_t)0x00, (int8_t)0x00, (int8_t)0x2a });
114+
b5.fromBytes({ (int8_t)0xff, (int8_t)0xff, (int8_t)0xff, (int8_t)0xd6 });
115+
b6.fromBytes({ (int8_t)0x49, (int8_t)0x96, (int8_t)0x02, (int8_t)0xd2 });
116+
b7.fromBytes({ (int8_t)0xb6, (int8_t)0x69, (int8_t)0xfd, (int8_t)0x2e });
117+
118+
EXPECT_EQ(b1, 0);
119+
EXPECT_EQ(b2, 1);
120+
EXPECT_EQ(b3, -1);
121+
EXPECT_EQ(b4, 42);
122+
EXPECT_EQ(b5, -42);
123+
EXPECT_EQ(b6, 1234567890);
124+
EXPECT_EQ(b7, -1234567890);
125+
}
126+
127+
TEST(BigInt, ToBytes) {
128+
IOTA::Types::BigInt b1(0);
129+
IOTA::Types::BigInt b2(1);
130+
IOTA::Types::BigInt b3(-1);
131+
IOTA::Types::BigInt b4(42);
132+
IOTA::Types::BigInt b5(-42);
133+
IOTA::Types::BigInt b6(1234567890);
134+
IOTA::Types::BigInt b7(-1234567890);
135+
136+
std::vector<int8_t> by1({ (int8_t)0x00, (int8_t)0x00, (int8_t)0x00, (int8_t)0x00 });
137+
by1.insert(std::begin(by1), IOTA::ByteHashLength - by1.size(), 0x00);
138+
std::vector<int8_t> by2({ (int8_t)0x00, (int8_t)0x00, (int8_t)0x00, (int8_t)0x01 });
139+
by2.insert(std::begin(by2), IOTA::ByteHashLength - by2.size(), 0x00);
140+
std::vector<int8_t> by3({ (int8_t)0xff, (int8_t)0xff, (int8_t)0xff, (int8_t)0xff });
141+
by3.insert(std::begin(by3), IOTA::ByteHashLength - by3.size(), 0xff);
142+
std::vector<int8_t> by4({ (int8_t)0x00, (int8_t)0x00, (int8_t)0x00, (int8_t)0x2a });
143+
by4.insert(std::begin(by4), IOTA::ByteHashLength - by4.size(), 0x00);
144+
std::vector<int8_t> by5({ (int8_t)0xff, (int8_t)0xff, (int8_t)0xff, (int8_t)0xd6 });
145+
by5.insert(std::begin(by5), IOTA::ByteHashLength - by5.size(), 0xff);
146+
std::vector<int8_t> by6({ (int8_t)0x49, (int8_t)0x96, (int8_t)0x02, (int8_t)0xd2 });
147+
by6.insert(std::begin(by6), IOTA::ByteHashLength - by6.size(), 0x00);
148+
std::vector<int8_t> by7({ (int8_t)0xb6, (int8_t)0x69, (int8_t)0xfd, (int8_t)0x2e });
149+
by7.insert(std::begin(by7), IOTA::ByteHashLength - by7.size(), 0xff);
150+
151+
EXPECT_EQ(b1.toBytes(), by1);
152+
EXPECT_EQ(b2.toBytes(), by2);
153+
EXPECT_EQ(b3.toBytes(), by3);
154+
EXPECT_EQ(b4.toBytes(), by4);
155+
EXPECT_EQ(b5.toBytes(), by5);
156+
EXPECT_EQ(b6.toBytes(), by6);
157+
EXPECT_EQ(b7.toBytes(), by7);
158+
}

0 commit comments

Comments
 (0)