Add STInt32 as a new SType (#5788)

This change adds `STInt32` as a new `SType` under the `STInteger` umbrella, with `SType` value `12`. This is the first and only `STInteger` type that supports negative values.
This commit is contained in:
Mayukha Vadari
2025-09-26 16:13:15 -04:00
committed by GitHub
parent 19c4226d3d
commit 807462b191
13 changed files with 287 additions and 81 deletions

View File

@@ -736,6 +736,107 @@ class STParsedJSON_test : public beast::unit_test::suite
}
}
void
testInt32()
{
testcase("Int32");
{
Json::Value j;
int const minInt32 = -2147483648;
j[sfDummyInt32] = minInt32;
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(obj.object.has_value());
if (BEAST_EXPECT(obj.object->isFieldPresent(sfDummyInt32)))
BEAST_EXPECT(obj.object->getFieldI32(sfDummyInt32) == minInt32);
}
// max value
{
Json::Value j;
int const maxInt32 = 2147483647;
j[sfDummyInt32] = maxInt32;
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(obj.object.has_value());
if (BEAST_EXPECT(obj.object->isFieldPresent(sfDummyInt32)))
BEAST_EXPECT(obj.object->getFieldI32(sfDummyInt32) == maxInt32);
}
// max uint value
{
Json::Value j;
unsigned int const maxUInt32 = 2147483647u;
j[sfDummyInt32] = maxUInt32;
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(obj.object.has_value());
if (BEAST_EXPECT(obj.object->isFieldPresent(sfDummyInt32)))
BEAST_EXPECT(
obj.object->getFieldI32(sfDummyInt32) ==
static_cast<int32_t>(maxUInt32));
}
// Test with string value
{
Json::Value j;
j[sfDummyInt32] = "2147483647";
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(obj.object.has_value());
if (BEAST_EXPECT(obj.object->isFieldPresent(sfDummyInt32)))
BEAST_EXPECT(
obj.object->getFieldI32(sfDummyInt32) == 2147483647u);
}
// Test with string negative value
{
Json::Value j;
int value = -2147483648;
j[sfDummyInt32] = std::to_string(value);
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(obj.object.has_value());
if (BEAST_EXPECT(obj.object->isFieldPresent(sfDummyInt32)))
BEAST_EXPECT(obj.object->getFieldI32(sfDummyInt32) == value);
}
// Test out of range value for int32 (negative)
{
Json::Value j;
j[sfDummyInt32] = "-2147483649";
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(!obj.object.has_value());
}
// Test out of range value for int32 (positive)
{
Json::Value j;
j[sfDummyInt32] = 2147483648u;
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(!obj.object.has_value());
}
// Test string value out of range
{
Json::Value j;
j[sfDummyInt32] = "2147483648";
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(!obj.object.has_value());
}
// Test bad_type (arrayValue)
{
Json::Value j;
j[sfDummyInt32] = Json::Value(Json::arrayValue);
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(!obj.object.has_value());
}
// Test bad_type (objectValue)
{
Json::Value j;
j[sfDummyInt32] = Json::Value(Json::objectValue);
STParsedJSONObject obj("Test", j);
BEAST_EXPECT(!obj.object.has_value());
}
}
void
testBlob()
{
@@ -1338,8 +1439,7 @@ class STParsedJSON_test : public beast::unit_test::suite
issueJson["issuer"] = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
j[sfAsset] = issueJson;
STParsedJSONObject obj("Test", j);
if (BEAST_EXPECTS(
obj.object.has_value(), obj.error.toStyledString()))
if (BEAST_EXPECT(obj.object.has_value()))
{
BEAST_EXPECT(obj.object->isFieldPresent(sfAsset));
auto const& issueField = (*obj.object)[sfAsset];
@@ -2235,6 +2335,7 @@ class STParsedJSON_test : public beast::unit_test::suite
testUInt160();
testUInt192();
testUInt256();
testInt32();
testBlob();
testVector256();
testAccount();