rippled
Loading...
Searching...
No Matches
STNumber_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/unit_test.h>
21#include <xrpl/protocol/Issue.h>
22#include <xrpl/protocol/STAmount.h>
23#include <xrpl/protocol/STNumber.h>
24
25#include <limits>
26#include <ostream>
27
28namespace ripple {
29
31{
32 void
34 {
35 STNumber const before{sfNumber, number};
36 BEAST_EXPECT(number == before);
37 Serializer s;
38 before.add(s);
39 BEAST_EXPECT(s.size() == 12);
40 SerialIter sit(s.slice());
41 STNumber const after{sit, sfNumber};
42 BEAST_EXPECT(after.isEquivalent(before));
43 BEAST_EXPECT(number == after);
44 }
45
46 void
47 run() override
48 {
49 static_assert(!std::is_convertible_v<STNumber*, Number*>);
50
51 {
52 STNumber const stnum{sfNumber};
53 BEAST_EXPECT(stnum.getSType() == STI_NUMBER);
54 BEAST_EXPECT(stnum.getText() == "0");
55 BEAST_EXPECT(stnum.isDefault() == true);
56 BEAST_EXPECT(stnum.value() == Number{0});
57 }
58
59 std::initializer_list<std::int64_t> const mantissas = {
61 -1,
62 0,
63 1,
65 for (std::int64_t mantissa : mantissas)
66 testCombo(Number{mantissa});
67
68 std::initializer_list<std::int32_t> const exponents = {
70 for (std::int32_t exponent : exponents)
71 testCombo(Number{123, exponent});
72
73 {
74 STAmount const strikePrice{noIssue(), 100};
75 STNumber const factor{sfNumber, 100};
76 auto const iouValue = strikePrice.iou();
77 IOUAmount totalValue{iouValue * factor};
78 STAmount const totalAmount{totalValue, strikePrice.issue()};
79 BEAST_EXPECT(totalAmount == Number{10'000});
80 }
81 }
82};
83
84BEAST_DEFINE_TESTSUITE(STNumber, protocol, ripple);
85
86void
88{
89 STNumber number{sfNumber, 42};
90 out << number;
91}
92
93} // namespace ripple
A testsuite class.
Definition: suite.h:55
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:47
static constexpr int maxExponent
Definition: Number.h:48
static constexpr int minExponent
Definition: Number.h:47
Issue const & issue() const
Definition: STAmount.h:487
A serializable number.
Definition: STNumber.h:43
std::size_t size() const noexcept
Definition: Serializer.h:73
Slice slice() const noexcept
Definition: Serializer.h:67
T max(T... args)
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
void testCompile(std::ostream &out)
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition: Issue.h:126
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: View.cpp:2129
void testCombo(Number number)
void run() override
Runs the suite.