Electroneum
parse_amount.cpp
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #include "gtest/gtest.h"
33 
35 
36 using namespace cryptonote;
37 
38 namespace
39 {
40  void do_pos_test(uint64_t expected, const std::string& str)
41  {
42  uint64_t val;
43  std::string number_str = str;
44  std::replace(number_str.begin(), number_str.end(), '_', '.');
45  number_str.erase(std::remove(number_str.begin(), number_str.end(), '~'), number_str.end());
46  ASSERT_TRUE(parse_amount(val, number_str));
47  ASSERT_EQ(expected, val);
48  }
49 
50  void do_neg_test(const std::string& str)
51  {
52  uint64_t val;
53  std::string number_str = str;
54  std::replace(number_str.begin(), number_str.end(), '_', '.');
55  number_str.erase(std::remove(number_str.begin(), number_str.end(), '~'), number_str.end());
56  ASSERT_FALSE(parse_amount(val, number_str));
57  }
58 }
59 
60 #define TEST_pos(expected, str) \
61  TEST(parse_amount, handles_pos_ ## str) \
62  { \
63  do_pos_test(UINT64_C(expected), #str); \
64  }
65 
66 #define TEST_neg(str) \
67  TEST(parse_amount, handles_neg_ ## str) \
68  { \
69  do_neg_test(#str); \
70  }
71 
72 #define TEST_neg_n(str, name) \
73  TEST(parse_amount, handles_neg_ ## name) \
74  { \
75  do_neg_test(#str); \
76  }
77 
78 
79 TEST_pos(0, 0);
80 TEST_pos(0, 00);
81 TEST_pos(0, 00000000);
82 TEST_pos(0, 000000000);
83 TEST_pos(0, 00000000000000000000000000000000);
84 
85 TEST_pos(0, _0);
86 TEST_pos(0, _00);
87 TEST_pos(0, _00000000);
88 TEST_pos(0, _000000000);
89 TEST_pos(0, _00000000000000000000000000000000);
90 
91 TEST_pos(0, 00000000_);
92 TEST_pos(0, 000000000_);
93 TEST_pos(0, 00000000000000000000000000000000_);
94 
95 TEST_pos(0, 0_);
96 TEST_pos(0, 0_0);
97 TEST_pos(0, 0_00);
98 TEST_pos(0, 0_00000000);
99 TEST_pos(0, 0_000000000);
100 TEST_pos(0, 0_00000000000000000000000000000000);
101 
102 TEST_pos(0, 00_);
103 TEST_pos(0, 00_0);
104 TEST_pos(0, 00_00);
105 TEST_pos(0, 00_00000000);
106 TEST_pos(0, 00_000000000);
107 TEST_pos(0, 00_00000000000000000000000000000000);
108 
109 TEST_pos(1, 0_01);
110 TEST_pos(1, 0_010);
111 TEST_pos(1, 0_010000000000000000000000000);
112 TEST_pos(9, 0_09);
113 TEST_pos(9, 0_090);
114 TEST_pos(9, 0_090000000000000000000000000);
115 
116 TEST_pos(100, 1);
117 TEST_pos(1000, 10);
118 TEST_pos(10000, 100);
119 TEST_pos(100000, 1000);
120 TEST_pos(655350, 6553_5);
121 TEST_pos(42949672, 429496_72);
122 TEST_pos(1844674407, 18446744_07);
123 
124 /* Max supply */
125 TEST_pos(2100000000000, 21000000000_00);
126 
127 // Invalid numbers
128 TEST_neg_n(~, empty_string);
129 TEST_neg_n(-0, minus_0);
130 TEST_neg_n(+0, plus_0);
131 TEST_neg_n(-1, minus_1);
132 TEST_neg_n(+1, plus_1);
133 TEST_neg_n(_, only_point);
134 
135 // Don't go below 10^-2
136 TEST_neg(0_00001);
137 TEST_neg(0_00009);
138 TEST_neg(21000000000_00001);
139 
140 // Overflow
141 TEST_neg(21000000000_09551616);
142 //TEST_neg(21000000000);
143 TEST_neg(2100000000009551616);
144 
145 // Two or more points
146 TEST_neg(__);
147 TEST_neg(0__);
148 TEST_neg(__0);
149 TEST_neg(0__0);
150 TEST_neg(0_0_);
151 TEST_neg(_0_0);
152 TEST_neg(0_0_0);
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
bool parse_amount(uint64_t &amount, const std::string &str_amount_)
#define TEST_pos(expected, str)
unsigned __int64 uint64_t
Definition: stdint.h:136
#define TEST_neg_n(str, name)
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
#define TEST_neg(str)
const internal::AnythingMatcher _