rippled
Loading...
Searching...
No Matches
STInteger_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2025 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/LedgerFormats.h>
22#include <xrpl/protocol/Permissions.h>
23#include <xrpl/protocol/STInteger.h>
24#include <xrpl/protocol/TxFormats.h>
25
26namespace ripple {
27
29{
30 void
32 {
33 testcase("UInt8");
34 STUInt8 u8(255);
35 BEAST_EXPECT(u8.value() == 255);
36 BEAST_EXPECT(u8.getText() == "255");
37 BEAST_EXPECT(u8.getSType() == STI_UINT8);
38 BEAST_EXPECT(u8.getJson(JsonOptions::none) == 255);
39
40 // there is some special handling for sfTransactionResult
41 STUInt8 tr(sfTransactionResult, 0);
42 BEAST_EXPECT(tr.value() == 0);
43 BEAST_EXPECT(
44 tr.getText() ==
45 "The transaction was applied. Only final in a validated ledger.");
46 BEAST_EXPECT(tr.getSType() == STI_UINT8);
47 BEAST_EXPECT(tr.getJson(JsonOptions::none) == "tesSUCCESS");
48
49 // invalid transaction result
50 STUInt8 tr2(sfTransactionResult, 255);
51 BEAST_EXPECT(tr2.value() == 255);
52 BEAST_EXPECT(tr2.getText() == "255");
53 BEAST_EXPECT(tr2.getSType() == STI_UINT8);
54 BEAST_EXPECT(tr2.getJson(JsonOptions::none) == 255);
55 }
56
57 void
59 {
60 testcase("UInt16");
61 STUInt16 u16(65535);
62 BEAST_EXPECT(u16.value() == 65535);
63 BEAST_EXPECT(u16.getText() == "65535");
64 BEAST_EXPECT(u16.getSType() == STI_UINT16);
65 BEAST_EXPECT(u16.getJson(JsonOptions::none) == 65535);
66
67 // there is some special handling for sfLedgerEntryType
68 STUInt16 let(sfLedgerEntryType, ltACCOUNT_ROOT);
69 BEAST_EXPECT(let.value() == ltACCOUNT_ROOT);
70 BEAST_EXPECT(let.getText() == "AccountRoot");
71 BEAST_EXPECT(let.getSType() == STI_UINT16);
72 BEAST_EXPECT(let.getJson(JsonOptions::none) == "AccountRoot");
73
74 // there is some special handling for sfTransactionType
75 STUInt16 tlt(sfTransactionType, ttPAYMENT);
76 BEAST_EXPECT(tlt.value() == ttPAYMENT);
77 BEAST_EXPECT(tlt.getText() == "Payment");
78 BEAST_EXPECT(tlt.getSType() == STI_UINT16);
79 BEAST_EXPECT(tlt.getJson(JsonOptions::none) == "Payment");
80 }
81
82 void
84 {
85 testcase("UInt32");
86 STUInt32 u32(4'294'967'295u);
87 BEAST_EXPECT(u32.value() == 4'294'967'295u);
88 BEAST_EXPECT(u32.getText() == "4294967295");
89 BEAST_EXPECT(u32.getSType() == STI_UINT32);
90 BEAST_EXPECT(u32.getJson(JsonOptions::none) == 4'294'967'295u);
91
92 // there is some special handling for sfPermissionValue
93 STUInt32 pv(sfPermissionValue, ttPAYMENT + 1);
94 BEAST_EXPECT(pv.value() == ttPAYMENT + 1);
95 BEAST_EXPECT(pv.getText() == "Payment");
96 BEAST_EXPECT(pv.getSType() == STI_UINT32);
97 BEAST_EXPECT(pv.getJson(JsonOptions::none) == "Payment");
98 STUInt32 pv2(sfPermissionValue, PaymentMint);
99 BEAST_EXPECT(pv2.value() == PaymentMint);
100 BEAST_EXPECT(pv2.getText() == "PaymentMint");
101 BEAST_EXPECT(pv2.getSType() == STI_UINT32);
102 BEAST_EXPECT(pv2.getJson(JsonOptions::none) == "PaymentMint");
103 }
104
105 void
107 {
108 testcase("UInt64");
109 STUInt64 u64(0xFFFFFFFFFFFFFFFFull);
110 BEAST_EXPECT(u64.value() == 0xFFFFFFFFFFFFFFFFull);
111 BEAST_EXPECT(u64.getText() == "18446744073709551615");
112 BEAST_EXPECT(u64.getSType() == STI_UINT64);
113
114 // By default, getJson returns hex string
115 auto jsonVal = u64.getJson(JsonOptions::none);
116 BEAST_EXPECT(jsonVal.isString());
117 BEAST_EXPECT(jsonVal.asString() == "ffffffffffffffff");
118
119 STUInt64 u64_2(sfMaximumAmount, 0xFFFFFFFFFFFFFFFFull);
120 BEAST_EXPECT(u64_2.value() == 0xFFFFFFFFFFFFFFFFull);
121 BEAST_EXPECT(u64_2.getText() == "18446744073709551615");
122 BEAST_EXPECT(u64_2.getSType() == STI_UINT64);
123 BEAST_EXPECT(
124 u64_2.getJson(JsonOptions::none) == "18446744073709551615");
125 }
126
127 void
129 {
130 testcase("Int32");
131 {
132 int const minInt32 = -2147483648;
133 STInt32 i32(minInt32);
134 BEAST_EXPECT(i32.value() == minInt32);
135 BEAST_EXPECT(i32.getText() == "-2147483648");
136 BEAST_EXPECT(i32.getSType() == STI_INT32);
137 BEAST_EXPECT(i32.getJson(JsonOptions::none) == minInt32);
138 }
139
140 {
141 int const maxInt32 = 2147483647;
142 STInt32 i32(maxInt32);
143 BEAST_EXPECT(i32.value() == maxInt32);
144 BEAST_EXPECT(i32.getText() == "2147483647");
145 BEAST_EXPECT(i32.getSType() == STI_INT32);
146 BEAST_EXPECT(i32.getJson(JsonOptions::none) == maxInt32);
147 }
148 }
149
150 void
151 run() override
152 {
153 testUInt8();
154 testUInt16();
155 testUInt32();
156 testUInt64();
157 testInt32();
158 }
159};
160
161BEAST_DEFINE_TESTSUITE(STInteger, protocol, ripple);
162
163} // namespace ripple
A testsuite class.
Definition suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
std::string getText() const override
Definition STInteger.cpp:56
value_type value() const noexcept
Definition STInteger.h:148
Json::Value getJson(JsonOptions) const override
Definition STInteger.cpp:76
SerializedTypeID getSType() const override
Definition STInteger.cpp:49
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void run() override
Runs the suite.