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