Electroneum
Loading...
Searching...
No Matches
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
36using namespace cryptonote;
37
38namespace
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
80TEST_pos(0, 00);
81TEST_pos(0, 00000000);
82TEST_pos(0, 000000000);
83TEST_pos(0, 00000000000000000000000000000000);
84
85TEST_pos(0, _0);
86TEST_pos(0, _00);
87TEST_pos(0, _00000000);
88TEST_pos(0, _000000000);
89TEST_pos(0, _00000000000000000000000000000000);
90
91TEST_pos(0, 00000000_);
92TEST_pos(0, 000000000_);
93TEST_pos(0, 00000000000000000000000000000000_);
94
96TEST_pos(0, 0_0);
97TEST_pos(0, 0_00);
98TEST_pos(0, 0_00000000);
99TEST_pos(0, 0_000000000);
100TEST_pos(0, 0_00000000000000000000000000000000);
101
103TEST_pos(0, 00_0);
104TEST_pos(0, 00_00);
105TEST_pos(0, 00_00000000);
106TEST_pos(0, 00_000000000);
107TEST_pos(0, 00_00000000000000000000000000000000);
108
109TEST_pos(1, 0_01);
110TEST_pos(1, 0_010);
111TEST_pos(1, 0_010000000000000000000000000);
112TEST_pos(9, 0_09);
113TEST_pos(9, 0_090);
114TEST_pos(9, 0_090000000000000000000000000);
115
116TEST_pos(100, 1);
117TEST_pos(1000, 10);
118TEST_pos(10000, 100);
119TEST_pos(100000, 1000);
120TEST_pos(655350, 6553_5);
121TEST_pos(42949672, 429496_72);
122TEST_pos(1844674407, 18446744_07);
123
124/* Max supply */
125TEST_pos(2100000000000, 21000000000_00);
126
127// Invalid numbers
128TEST_neg_n(~, empty_string);
129TEST_neg_n(-0, minus_0);
130TEST_neg_n(+0, plus_0);
131TEST_neg_n(-1, minus_1);
132TEST_neg_n(+1, plus_1);
133TEST_neg_n(_, only_point);
134
135// Don't go below 10^-2
136TEST_neg(0_00001);
137TEST_neg(0_00009);
138TEST_neg(21000000000_00001);
139
140// Overflow
141TEST_neg(21000000000_09551616);
142//TEST_neg(21000000000);
143TEST_neg(2100000000009551616);
144
145// Two or more points
152TEST_neg(0_0_0);
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define ASSERT_FALSE(condition)
Definition gtest.h:1868
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
bool parse_amount(uint64_t &amount, const std::string &str_amount_)
#define TEST_pos(expected, str)
#define TEST_neg_n(str, name)
#define TEST_neg(str)
unsigned __int64 uint64_t
Definition stdint.h:136