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