Browse Source

Fix style for `BigInt`

core_refactor
Anton Tarasenko 2 years ago
parent
commit
15a49d8ddc
  1. 771
      sources/BaseRealm/API/Math/BigInt.uc
  2. 29
      sources/BaseRealm/API/Math/MathAPI.uc
  3. 64
      sources/BaseRealm/API/Math/Tests/TEST_BigInt.uc

771
sources/BaseRealm/API/Math/BigInt.uc

File diff suppressed because it is too large Load Diff

29
sources/BaseRealm/API/Math/MathAPI.uc

@ -37,36 +37,41 @@ public function BigInt ToBigInt(int value)
local BigInt result;
result = BigInt(_.memory.Allocate(class'BigInt'));
return result.SetInt(value);
result.SetInt(value);
return result;
}
/// Creates new `BigInt` value, based on the decimal number representation.
///
/// Expects valid decimal representation as input (digits only, possibly with leading sign),
/// otherwise contents of returned value are undefined.
/// If invalid decimal representation is given - contents of returned value are undefined.
/// Otherwise cannot fail and is guaranteed to return non-`none` value.
/// If (and only if) `none` or invalid decimal representation (digits only, possibly with
/// leading sign) is given as an argument, method will return `none`.
public function BigInt MakeBigInt(BaseText value)
{
local BigInt result;
result = BigInt(_.memory.Allocate(class'BigInt'));
return result.SetDecimal(value);
if (result.SetDecimal(value)) {
return result;
}
result.FreeSelf();
return none;
}
/// Creates new `BigInt` value, based on the decimal number representation.
///
/// Expects valid decimal representation as input (digits only, possibly with leading sign),
/// otherwise contents of returned value are undefined.
/// If invalid decimal representation is given - contents of returned value are undefined.
/// Otherwise cannot fail and is guaranteed to return non-`none` value.
/// If (and only if) invalid decimal representation (digits only, possibly with leading sign) is
/// given as an argument, method will return `none`.
public function BigInt MakeBigInt_S(string value)
{
local BigInt result;
result = BigInt(_.memory.Allocate(class'BigInt'));
return result.SetDecimal_S(value);
if (result.SetDecimal_S(value)) {
return result;
}
result.FreeSelf();
return none;
}
/// Computes remainder of the integer division of [`number`] by [`divisor`].
@ -79,7 +84,7 @@ public function int Remainder(int number, int divisor)
local int quotient;
quotient = number / divisor;
return (number - quotient/// divisor);
return (number - quotient * divisor);
}
/// Computes quotient and remainder of the integer division of [`number`] by [`divisor`].

64
sources/BaseRealm/API/Math/Tests/TEST_BigInt.uc

@ -1,6 +1,8 @@
/**
* Set of tests for `BigInt` class.
* Copyright 2022 Anton Tarasenko
* Author: dkanus
* Home repo: https://www.insultplayers.ru/git/AcediaFramework/AcediaCore
* License: GPL
* Copyright 2022-2023 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
@ -20,8 +22,7 @@
class TEST_BigInt extends TestCase
abstract;
protected static function TESTS()
{
protected static function TESTS() {
// Here we use `ToString()` method to check `BigInt` creation,
// therefore also testing it
Context("Testing creation of `BigInt`s.");
@ -36,43 +37,33 @@ protected static function TESTS()
Test_SubtractingValues();
}
protected static function Test_Creating()
{
protected static function Test_Creating() {
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a positive `int`.");
TEST_ExpectTrue(__().math.ToBigInt(13524).ToString() == "13524");
TEST_ExpectTrue(
__().math.ToBigInt(MaxInt).ToString() == "2147483647");
TEST_ExpectTrue(__().math.ToBigInt(MaxInt).ToString() == "2147483647");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a positive integer inside `string`.");
TEST_ExpectTrue(
__().math.MakeBigInt_S("2147483647").ToString()
== "2147483647");
TEST_ExpectTrue(__().math.MakeBigInt_S("2147483647").ToString() == "2147483647");
TEST_ExpectTrue(
__().math.MakeBigInt_S("4238756872643464981264982128742389")
.ToString() == "4238756872643464981264982128742389");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a negative `int`.");
Issue("`ToString()` doesn't return value `BigInt` was initialized with a negative `int`.");
TEST_ExpectTrue(__().math.ToBigInt(-666).ToString() == "-666");
TEST_ExpectTrue(
__().math.ToBigInt(-MaxInt).ToString() == "-2147483647");
TEST_ExpectTrue(
__().math.ToBigInt(-MaxInt - 1).ToString() == "-2147483648");
TEST_ExpectTrue(__().math.ToBigInt(-MaxInt).ToString() == "-2147483647");
TEST_ExpectTrue(__().math.ToBigInt(-MaxInt - 1).ToString() == "-2147483648");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a negative integer inside `string`.");
TEST_ExpectTrue(
__().math.MakeBigInt_S("-2147483648").ToString()
== "-2147483648");
TEST_ExpectTrue(__().math.MakeBigInt_S("-2147483648").ToString() == "-2147483648");
TEST_ExpectTrue(
__().math.MakeBigInt_S("-238473846327894632879097410348127")
.ToString() == "-238473846327894632879097410348127");
}
protected static function Test_ToText()
{
protected static function Test_ToText() {
Issue("`ToText()` doesn't return value `BigInt` was initialized with" @
"a positive integer inside `string`.");
TEST_ExpectTrue(__().math
@ -96,20 +87,19 @@ protected static function Test_ToText()
.ToString() == "-9827657892365923510176386357863078603212901078175829");
}
protected static function Test_AddingValues()
{
protected static function Test_AddingValues() {
SubTest_AddingSameSignValues();
SubTest_AddingDifferentSignValues();
}
protected static function SubTest_AddingSameSignValues()
{
protected static function SubTest_AddingSameSignValues() {
local BigInt main, addition;
Issue("Two positive `BigInt`s are incorrectly added.");
main = __().math.MakeBigInt_S("927641962323462271784269213864");
addition = __().math.MakeBigInt_S("16324234842947239847239239");
main.Add(addition);
Log("UMBRA" @ main.ToString());
TEST_ExpectTrue(main.ToString() == "927658286558305219024116453103");
main = __().math.MakeBigInt_S("16324234842947239847239239");
addition = __().math.MakeBigInt_S("927641962323462271784269213864");
@ -135,8 +125,7 @@ protected static function SubTest_AddingSameSignValues()
TEST_ExpectTrue(main.ToString() == "-1457931745873178552");
}
protected static function SubTest_AddingDifferentSignValues()
{
protected static function SubTest_AddingDifferentSignValues() {
local BigInt main, addition;
Issue("Negative `BigInt`s is incorrectly added to positive one.");
@ -168,14 +157,12 @@ protected static function SubTest_AddingDifferentSignValues()
TEST_ExpectTrue(main.ToString() == "0");
}
protected static function Test_SubtractingValues()
{
protected static function Test_SubtractingValues() {
SubTest_SubtractingSameSignValues();
SubTest_SubtractingDifferentSignValues();
}
protected static function SubTest_SubtractingSameSignValues()
{
protected static function SubTest_SubtractingSameSignValues() {
local BigInt main, sub;
Issue("Two positive `BigInt`s are incorrectly subtracted.");
@ -207,8 +194,7 @@ protected static function SubTest_SubtractingSameSignValues()
TEST_ExpectTrue(main.ToString() == "0");
}
protected static function SubTest_SubtractingDifferentSignValues()
{
protected static function SubTest_SubtractingDifferentSignValues() {
local BigInt main, sub;
Issue("Negative `BigInt`s is incorrectly subtracted from positive one.");
@ -240,8 +226,7 @@ protected static function SubTest_SubtractingDifferentSignValues()
TEST_ExpectTrue(main.ToString() == "-1457931745873178552");
}
protected static function Test_ToInt()
{
protected static function Test_ToInt() {
Issue("Testing conversion for non-overflowing values.");
TEST_ExpectTrue(__().math.MakeBigInt_S("0").ToInt() == 0);
TEST_ExpectTrue(__().math.MakeBigInt_S("-0").ToInt() == 0);
@ -264,8 +249,7 @@ protected static function Test_ToInt()
__().math.MakeBigInt_S("-32545657348437563873").ToInt() == -2147483648);
}
defaultproperties
{
caseGroup = "Math"
caseName = "BigInt"
defaultproperties {
caseGroup = "Math"
caseName = "BigInt"
}
Loading…
Cancel
Save