Fabcoin Core  0.16.2
P2P Digital Currency
FixedHash.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include <libdevcore/FixedHash.h>
23 #include <libdevcore/SHA3.h>
25 
26 using namespace std;
27 using namespace dev;
28 
29 namespace dev
30 {
31 namespace test
32 {
33 
34 BOOST_FIXTURE_TEST_SUITE(FixedHashTests, TestOutputHelper)
35 
36 BOOST_AUTO_TEST_CASE(FixedHashComparisons)
37 {
38  FixedHash<4> h1(sha3("abcd"));
39  FixedHash<4> h2(sha3("abcd"));
40  FixedHash<4> h3(sha3("aadd"));
41  FixedHash<4> h4(0xBAADF00D);
42  FixedHash<4> h5(0xAAAAAAAA);
43  FixedHash<4> h6(0xBAADF00D);
44 
45  BOOST_CHECK(h1 == h2);
46  BOOST_CHECK(h2 != h3);
47 
48  BOOST_CHECK(h4 > h5);
49  BOOST_CHECK(h5 < h4);
50  BOOST_CHECK(h6 <= h4);
51  BOOST_CHECK(h6 >= h4);
52 }
53 
54 BOOST_AUTO_TEST_CASE(FixedHashXOR)
55 {
56  FixedHash<2> h1("0xAAAA");
57  FixedHash<2> h2("0xBBBB");
58 
59  BOOST_CHECK((h1 ^ h2) == FixedHash<2>("0x1111"));
60  h1 ^= h2;
61  BOOST_CHECK(h1 == FixedHash<2>("0x1111"));
62 }
63 
65 {
66  FixedHash<4> h1("0xD3ADB33F");
67  FixedHash<4> h2("0xBAADF00D");
68  FixedHash<4> res("0xFBADF33F");
69 
70  BOOST_CHECK((h1 | h2) == res);
71  h1 |= h2;
72  BOOST_CHECK(h1 == res);
73 }
74 
75 BOOST_AUTO_TEST_CASE(FixedHashAND)
76 {
77  FixedHash<4> h1("0xD3ADB33F");
78  FixedHash<4> h2("0xBAADF00D");
79  FixedHash<4> h3("0x92aDB00D");
80 
81  BOOST_CHECK((h1 & h2) == h3);
82  h1 &= h2;
83  BOOST_CHECK(h1 = h3);
84 }
85 
86 BOOST_AUTO_TEST_CASE(FixedHashInvert)
87 {
88  FixedHash<4> h1("0xD3ADB33F");
89  FixedHash<4> h2("0x2C524CC0");
90 
91  BOOST_CHECK(~h1 == h2);
92 }
93 
94 BOOST_AUTO_TEST_CASE(FixedHashContains)
95 {
96  FixedHash<4> h1("0xD3ADB331");
97  FixedHash<4> h2("0x0000B331");
98  FixedHash<4> h3("0x0000000C");
99 
100  BOOST_CHECK(h1.contains(h2));
101  BOOST_CHECK(!h1.contains(h3));
102 }
103 
104 void incrementSingleIteration(unsigned seed)
105 {
106  unsigned next = seed + 1;
107 
108  FixedHash<4> h1(seed);
109  FixedHash<4> h2 = h1;
110  FixedHash<4> h3(next);
111 
112  FixedHash<32> hh1(seed);
113  FixedHash<32> hh2 = hh1;
114  FixedHash<32> hh3(next);
115 
116  BOOST_CHECK_EQUAL(++h2, h3);
117  BOOST_CHECK_EQUAL(++hh2, hh3);
118 
119  BOOST_CHECK(h2 > h1);
120  BOOST_CHECK(hh2 > hh1);
121 
122  unsigned reverse1 = ((FixedHash<4>::Arith)h2).convert_to<unsigned>();
123  unsigned reverse2 = ((FixedHash<32>::Arith)hh2).convert_to<unsigned>();
124 
125  BOOST_CHECK_EQUAL(next, reverse1);
126  BOOST_CHECK_EQUAL(next, reverse2);
127 }
128 
129 BOOST_AUTO_TEST_CASE(FixedHashIncrement)
130 {
134  incrementSingleIteration(0xBEEF);
135  incrementSingleIteration(0xFFFF);
136  incrementSingleIteration(0xFEDCBA);
137  incrementSingleIteration(0x7FFFFFFF);
138 
139  FixedHash<4> h(0xFFFFFFFF);
140  FixedHash<4> zero;
141  BOOST_CHECK_EQUAL(++h, zero);
142 }
143 
145 
146 }
147 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
BOOST_AUTO_TEST_CASE(FixedHashIncrement)
Definition: FixedHash.cpp:129
void incrementSingleIteration(unsigned seed)
Definition: FixedHash.cpp:104
#define h3(tab, w)
Definition: skipjack.cpp:75
bool contains(FixedHash const &_c) const
Definition: FixedHash.h:116
#define h(i)
Definition: sha.cpp:736
std::hash for asio::adress
Definition: Common.h:323
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> Arith
The corresponding arithmetic type.
Definition: FixedHash.h:51
#define h1(tab, w)
Definition: skipjack.cpp:73
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define h2(tab, w)
Definition: skipjack.cpp:74
#define h4(tab, w)
Definition: skipjack.cpp:76
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17