Fabcoin Core  0.16.2
P2P Digital Currency
CommonJS.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 */
23 #include <boost/test/unit_test.hpp>
24 #include <libdevcore/CommonJS.h>
26 
27 using namespace dev;
28 using namespace std;
29 using namespace test;
30 
31 BOOST_FIXTURE_TEST_SUITE(CommonJSTests, TestOutputHelper)
32 
34 {
35  h64 a("0xbaadf00ddeadbeef");
36  u64 b("0xffff0000bbbaaaa");
37  uint64_t c = 38990234243;
38  bytes d = {0xff, 0x0, 0xef, 0xbc};
39 
40  BOOST_CHECK(toJS(a) == "0xbaadf00ddeadbeef");
41  BOOST_CHECK(toJS(b) == "0xffff0000bbbaaaa");
42  BOOST_CHECK(toJS(c) == "0x913ffc283");
43  BOOST_CHECK(toJS(d) == "0xff00efbc");
44 }
45 
46 BOOST_AUTO_TEST_CASE(test_jsToBytes)
47 {
48  bytes a = {0xff, 0xaa, 0xbb, 0xcc};
49  bytes b = {0x03, 0x89, 0x90, 0x23, 0x42, 0x43};
50  BOOST_CHECK(a == jsToBytes("0xffaabbcc"));
51  BOOST_CHECK(b == jsToBytes("38990234243"));
52  BOOST_CHECK(bytes() == jsToBytes(""));
53  BOOST_CHECK(bytes() == jsToBytes("Invalid hex chars"));
54 }
55 
57 {
58  bytes a = {0xff, 0xaa};
59  BOOST_CHECK(bytes({0x00, 0x00, 0xff, 0xaa}) == padded(a, 4));
60  bytes b = {};
61  BOOST_CHECK(bytes({0x00, 0x00, 0x00, 0x00}) == padded(b, 4));
62  bytes c = {0xff, 0xaa, 0xbb, 0xcc};
63  BOOST_CHECK(bytes{0xcc} == padded(c, 1));
64 }
65 
66 BOOST_AUTO_TEST_CASE(test_paddedRight)
67 {
68  bytes a = {0xff, 0xaa};
69  BOOST_CHECK(bytes({0xff, 0xaa, 0x00, 0x00}) == paddedRight(a, 4));
70  bytes b = {};
71  BOOST_CHECK(bytes({0x00, 0x00, 0x00, 0x00}) == paddedRight(b, 4));
72  bytes c = {0xff, 0xaa, 0xbb, 0xcc};
73  BOOST_CHECK(bytes{0xff} == paddedRight(c, 1));
74 }
75 
76 BOOST_AUTO_TEST_CASE(test_unpadded)
77 {
78  bytes a = {0xff, 0xaa, 0x00, 0x00, 0x00};
79  BOOST_CHECK(bytes({0xff, 0xaa}) == unpadded(a));
80  bytes b = {0x00, 0x00};
81  BOOST_CHECK(bytes() == unpadded(b));
82  bytes c = {};
83  BOOST_CHECK(bytes() == unpadded(c));
84 }
85 
86 BOOST_AUTO_TEST_CASE(test_unpaddedLeft)
87 {
88  bytes a = {0x00, 0x00, 0x00, 0xff, 0xaa};
89  BOOST_CHECK(bytes({0xff, 0xaa}) == unpadLeft(a));
90  bytes b = {0x00, 0x00};
91  BOOST_CHECK(bytes() == unpadLeft(b));
92  bytes c = {};
93  BOOST_CHECK(bytes() == unpadLeft(c));
94 }
95 
96 BOOST_AUTO_TEST_CASE(test_fromRaw)
97 {
98  //non ascii characters means empty string
99  h256 a("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
100  BOOST_CHECK("" == fromRaw(a));
101  h256 b("");
102  BOOST_CHECK("" == fromRaw(b));
103  h256 c("0x4173636969436861726163746572730000000000000000000000000000000000");
104  BOOST_CHECK("AsciiCharacters" == fromRaw(c));
105 }
106 
107 BOOST_AUTO_TEST_CASE(test_jsToFixed)
108 {
109  h256 a("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
110  BOOST_CHECK(a == jsToFixed<32>("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
111  h256 b("0x000000000000000000000000000000000000000000000000000000740c54b42f");
112  BOOST_CHECK(b == jsToFixed<32>("498423084079"));
113  BOOST_CHECK(h256() == jsToFixed<32>("NotAHexadecimalOrDecimal"));
114 }
115 
116 BOOST_AUTO_TEST_CASE(test_jsToInt)
117 {
118  BOOST_CHECK(43832124 == jsToInt("43832124"));
119  BOOST_CHECK(1342356623 == jsToInt("0x5002bc8f"));
120  BOOST_CHECK(3483942 == jsToInt("015224446"));
121  BOOST_CHECK(0 == jsToInt("NotAHexadecimalOrDecimal"));
122 
123  BOOST_CHECK(u256("983298932490823474234") == jsToInt<32>("983298932490823474234"));
124  BOOST_CHECK(u256("983298932490823474234") == jsToInt<32>("0x354e03915c00571c3a"));
125  BOOST_CHECK(u256() == jsToInt<32>("NotAHexadecimalOrDecimal"));
126  BOOST_CHECK(u128("228273101986715476958866839113050921216") == jsToInt<16>("0xabbbccddeeff11223344556677889900"));
127  BOOST_CHECK(u128() == jsToInt<16>("NotAHexadecimalOrDecimal"));
128 }
129 
130 BOOST_AUTO_TEST_CASE(test_jsToU256)
131 {
132  BOOST_CHECK(u256("983298932490823474234") == jsToU256("983298932490823474234"));
133  BOOST_CHECK(u256() == jsToU256("NotAHexadecimalOrDecimal"));
134 }
135 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
BOOST_AUTO_TEST_CASE(test_toJS)
Definition: CommonJS.cpp:33
#define c(i)
std::hash for asio::adress
Definition: Common.h:323
bytes unpadded(bytes _b)
Removing all trailing &#39;0&#39;. Returns empty array if input contains only &#39;0&#39; char.
Definition: CommonJS.cpp:60
#define a(i)
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
string fromRaw(h256 _n)
Convert h256 into user-readable string (by directly using std::string constructor). If it can&#39;t be interpreted as an ASCII string, empty string is returned.
Definition: CommonJS.cpp:81
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bytes paddedRight(bytes _b, unsigned _l)
Add &#39;0&#39; on, or remove items from, the back of _b until it is of length _l.
Definition: CommonJS.cpp:54
bytes jsToBytes(string const &_s, OnFailed _f)
Definition: CommonJS.cpp:31
#define b(i, j)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u64
Definition: Common.h:123
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 128, 128, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u128
Definition: Common.h:124
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
bytes padded(bytes _b, unsigned _l)
Add &#39;0&#39; on, or remove items from, the front of _b until it is of length _l.
Definition: CommonJS.cpp:47
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define d(i)
Definition: sha.cpp:732
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > jsToInt(std::string const &_s)
Convert a string representation of a number to an int String can be a normal decimal number...
Definition: CommonJS.h:97
bytes unpadLeft(bytes _b)
Remove all 0 byte on the head of _s.
Definition: CommonJS.cpp:67
u256 jsToU256(std::string const &_s)
Definition: CommonJS.h:110
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17