| 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 <cstdint> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 37 | |
| 38 | #define VEC_FROM_ARR(vec) \ |
| 39 | std::vector<uint64_t> vec; \ |
| 40 | for (size_t i = 0; i < sizeof(vec##_arr) / sizeof(vec##_arr[0]); ++i) \ |
| 41 | { \ |
| 42 | vec.push_back(vec##_arr[i]); \ |
| 43 | } |
| 44 | |
| 45 | namespace |
| 46 | { |
| 47 | struct chunk_handler_t |
| 48 | { |
| 49 | void operator()(uint64_t chunk) const |
| 50 | { |
| 51 | m_chunks.push_back(chunk); |
| 52 | } |
| 53 | |
| 54 | mutable std::vector<uint64_t> m_chunks; |
| 55 | }; |
| 56 | |
| 57 | struct dust_handler_t |
| 58 | { |
| 59 | dust_handler_t() |
| 60 | : m_dust(0) |
| 61 | , m_has_dust(false) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void operator()(uint64_t dust) const |
| 66 | { |
| 67 | m_dust = dust; |
| 68 | m_has_dust = true; |
| 69 | } |
| 70 | |
| 71 | mutable uint64_t m_dust; |
| 72 | mutable bool m_has_dust; |
| 73 | }; |
| 74 | |
| 75 | class decompose_amount_into_digits_test : public ::testing::Test |
| 76 | { |
| 77 | protected: |
| 78 | chunk_handler_t m_chunk_handler; |
| 79 | dust_handler_t m_dust_handler; |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | TEST_F(decompose_amount_into_digits_test, is_correct_0) |
| 84 | { |
| 85 | std::vector<uint64_t> expected_chunks; |
| 86 | cryptonote::decompose_amount_into_digits(0, 0, m_chunk_handler, m_dust_handler); |
| 87 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 88 | ASSERT_EQ(m_dust_handler.m_has_dust, false); |
| 89 | } |
| 90 | |
| 91 | TEST_F(decompose_amount_into_digits_test, is_correct_1) |
| 92 | { |
| 93 | std::vector<uint64_t> expected_chunks; |
| 94 | cryptonote::decompose_amount_into_digits(0, 10, m_chunk_handler, m_dust_handler); |
| 95 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 96 | ASSERT_EQ(m_dust_handler.m_has_dust, false); |
| 97 | } |
| 98 | |
| 99 | TEST_F(decompose_amount_into_digits_test, is_correct_2) |
| 100 | { |
| 101 | uint64_t expected_chunks_arr[] = {10}; |
| 102 | VEC_FROM_ARR(expected_chunks); |
| 103 | cryptonote::decompose_amount_into_digits(10, 0, m_chunk_handler, m_dust_handler); |
| 104 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 105 | ASSERT_EQ(m_dust_handler.m_has_dust, false); |
| 106 | } |
| 107 | |
| 108 | TEST_F(decompose_amount_into_digits_test, is_correct_3) |
| 109 | { |
| 110 | std::vector<uint64_t> expected_chunks; |
| 111 | uint64_t expected_dust = 10; |
| 112 | cryptonote::decompose_amount_into_digits(10, 10, m_chunk_handler, m_dust_handler); |
| 113 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 114 | ASSERT_EQ(m_dust_handler.m_dust, expected_dust); |
| 115 | } |
| 116 | |
| 117 | TEST_F(decompose_amount_into_digits_test, is_correct_4) |
| 118 | { |
| 119 | uint64_t expected_dust = 8100; |
| 120 | std::vector<uint64_t> expected_chunks; |
| 121 | cryptonote::decompose_amount_into_digits(8100, 1000000, m_chunk_handler, m_dust_handler); |
| 122 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 123 | ASSERT_EQ(m_dust_handler.m_dust, expected_dust); |
| 124 | } |
| 125 | |
| 126 | TEST_F(decompose_amount_into_digits_test, is_correct_5) |
| 127 | { |
| 128 | uint64_t expected_chunks_arr[] = {100, 900000, 8000000}; |
| 129 | VEC_FROM_ARR(expected_chunks); |
| 130 | cryptonote::decompose_amount_into_digits(8900100, 10, m_chunk_handler, m_dust_handler); |
| 131 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 132 | ASSERT_EQ(m_dust_handler.m_has_dust, false); |
| 133 | } |
| 134 | |
| 135 | TEST_F(decompose_amount_into_digits_test, is_correct_6) |
| 136 | { |
| 137 | uint64_t expected_chunks_arr[] = {900000, 8000000}; |
| 138 | VEC_FROM_ARR(expected_chunks); |
| 139 | uint64_t expected_dust = 100; |
| 140 | cryptonote::decompose_amount_into_digits(8900100, 1000, m_chunk_handler, m_dust_handler); |
| 141 | ASSERT_EQ(m_chunk_handler.m_chunks, expected_chunks); |
| 142 | ASSERT_EQ(m_dust_handler.m_dust, expected_dust); |
| 143 | } |