Browse Source

Refactor `BigInt` and add subtraction

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
22fcdab0a0
  1. 180
      sources/BaseRealm/API/Math/BigInt.uc
  2. 24
      sources/BaseRealm/API/Math/MathAPI.uc
  3. 246
      sources/BaseRealm/API/Math/Tests/TEST_BigInt.uc
  4. 176
      sources/Data/Database/Tests/TEST_BigInt.uc

180
sources/Data/Database/BigInt.uc → sources/BaseRealm/API/Math/BigInt.uc

@ -41,10 +41,13 @@ var private bool negative;
// Valid `BigInt` should not have this array empty. // Valid `BigInt` should not have this array empty.
var private array<byte> digits; var private array<byte> digits;
private const ALMOST_MAX_INT = 147483647;
private const DIGITS_IN_MAX_INT = 10;
protected function Constructor() protected function Constructor()
{ {
// Init with zero // Init with zero
digits[0] = 0; SetZero();
} }
protected function Finalizer() protected function Finalizer()
@ -53,26 +56,32 @@ protected function Finalizer()
digits.length = 0; digits.length = 0;
} }
// ???
private function BigInt SetZero()
{
negative = false;
digits.length = 1;
digits[0] = 0;
return self;
}
// Minimal `int` value `-2,147,483,648` is slightly a pain to handle, so just // Minimal `int` value `-2,147,483,648` is slightly a pain to handle, so just
// use this pre-made constructor for it // use this pre-made constructor for it
private static function BigInt CreateMinimalNegative() private function BigInt SetMinimalNegative()
{ {
local array<byte> newDigits; negative = true;
local BigInt result; digits.length = 10;
newDigits[0] = 8; digits[0] = 8;
newDigits[1] = 4; digits[1] = 4;
newDigits[2] = 6; digits[2] = 6;
newDigits[3] = 3; digits[3] = 3;
newDigits[4] = 8; digits[4] = 8;
newDigits[5] = 4; digits[5] = 4;
newDigits[6] = 7; digits[6] = 7;
newDigits[7] = 4; digits[7] = 4;
newDigits[8] = 1; digits[8] = 1;
newDigits[9] = 2; digits[9] = 2;
result = BigInt(__().memory.Allocate(class'BigInt')); return self;
result.digits = newDigits;
result.negative = true;
return result;
} }
// Removes unnecessary zeroes from leading digit positions `digits`. // Removes unnecessary zeroes from leading digit positions `digits`.
@ -92,40 +101,36 @@ private final function TrimLeadingZeroes()
zeroesToRemove += 1; zeroesToRemove += 1;
} }
// `digits` must not be empty, enforce `0` value in that case // `digits` must not be empty, enforce `0` value in that case
if (zeroesToRemove >= digits.length) if (zeroesToRemove >= digits.length) {
{ SetZero();
digits.length = 1;
digits[0] = 0;
negative = false;
} }
else { else {
digits.length = digits.length - zeroesToRemove; digits.length = digits.length - zeroesToRemove;
} }
} }
public final static function BigInt FromInt(int value) public final function BigInt SetInt(int value)
{ {
local bool valueIsNegative;
local array<byte> newDigits;
local BigInt result;
local MathAPI.IntegerDivisionResult divisionResult; local MathAPI.IntegerDivisionResult divisionResult;
negative = false;
digits.length = 0;
if (value < 0) if (value < 0)
{ {
// Treat special case of minimal `int` value `-2,147,483,648` that // Treat special case of minimal `int` value `-2,147,483,648` that
// won't fit into positive `int` as special and use pre-made // won't fit into positive `int` as special and use pre-made
// specialized constructor `CreateMinimalNegative()` // specialized constructor `CreateMinimalNegative()`
if (value < -MaxInt) { if (value < -MaxInt) {
return CreateMinimalNegative(); return SetMinimalNegative();
} }
else else
{ {
valueIsNegative = true; negative = true;
value *= -1; value *= -1;
} }
} }
if (value == 0) { if (value == 0) {
newDigits[0] = 0; digits[0] = 0;
} }
else else
{ {
@ -133,38 +138,34 @@ public final static function BigInt FromInt(int value)
{ {
divisionResult = __().math.IntegerDivision(value, 10); divisionResult = __().math.IntegerDivision(value, 10);
value = divisionResult.quotient; value = divisionResult.quotient;
newDigits[newDigits.length] = divisionResult.remainder; digits[digits.length] = divisionResult.remainder;
} }
} }
result = BigInt(__().memory.Allocate(class'BigInt')); TrimLeadingZeroes();
result.digits = newDigits; return self;
result.negative = valueIsNegative;
result.TrimLeadingZeroes();
return result;
} }
public final static function BigInt FromDecimal(BaseText value) public final function BigInt Set(BaseText value)
{ {
local int i; local int i;
local bool valueIsNegative;
local byte nextDigit; local byte nextDigit;
local array<byte> newDigits;
local Parser parser; local Parser parser;
local BigInt result;
local Basetext.Character nextCharacter; local Basetext.Character nextCharacter;
if (value == none) { if (value == none) {
return none; return none;
} }
parser = value.Parse(); parser = value.Parse();
if (parser.Match(P("-")).Ok()) negative = parser.Match(P("-")).Ok();
{
valueIsNegative = true;
parser.Confirm(); parser.Confirm();
}
parser.R(); parser.R();
newDigits.length = parser.GetRemainingLength(); digits.length = parser.GetRemainingLength();
i = newDigits.length - 1; /*if (digits.length <= 0)
{
parser.FreeSelf();
return SetZero();
}*/
i = digits.length - 1;
while (!parser.HasFinished()) while (!parser.HasFinished())
{ {
// This should not happen, but just in case // This should not happen, but just in case
@ -173,26 +174,22 @@ public final static function BigInt FromDecimal(BaseText value)
} }
parser.MCharacter(nextCharacter); parser.MCharacter(nextCharacter);
nextDigit = Clamp(__().text.CharacterToInt(nextCharacter), 0, 9); nextDigit = Clamp(__().text.CharacterToInt(nextCharacter), 0, 9);
newDigits[i] = nextDigit; digits[i] = nextDigit;
i -= 1; i -= 1;
} }
result = BigInt(__().memory.Allocate(class'BigInt'));
result.digits = newDigits;
result.negative = valueIsNegative;
parser.FreeSelf(); parser.FreeSelf();
result.TrimLeadingZeroes(); TrimLeadingZeroes();
return result; return self;
} }
public final static function BigInt FromDecimal_S(string value) public final function BigInt Set_S(string value)
{ {
local MutableText wrapper; local MutableText wrapper;
local BigInt result;
wrapper = __().text.FromStringM(value); wrapper = __().text.FromStringM(value);
result = FromDecimal(wrapper); Set(wrapper);
wrapper.FreeSelf(); wrapper.FreeSelf();
return result; return self;
} }
public function BigIntCompareResult _compareModulus(BigInt other) public function BigIntCompareResult _compareModulus(BigInt other)
@ -284,9 +281,7 @@ private function _sub(BigInt other)
resultForModulus = _compareModulus(other); resultForModulus = _compareModulus(other);
if (resultForModulus == BICR_Equal) if (resultForModulus == BICR_Equal)
{ {
negative = false; SetZero();
digits.length = 1;
digits[0] = 0;
return; return;
} }
if (resultForModulus == BICR_Less) if (resultForModulus == BICR_Less)
@ -334,23 +329,72 @@ public function BigInt AddInt(int other)
{ {
local BigInt otherObject; local BigInt otherObject;
otherObject = FromInt(other); otherObject = _.math.ToBigInt(other);
Add(otherObject); Add(otherObject);
_.memory.Free(otherObject); _.memory.Free(otherObject);
return self; return self;
} }
/*public function BigInt Multiply(BigInt other); public function BigInt Subtract(BigInt other)
public function BigInt MultiplyInt(int other); {
if (negative != other.negative) {
_add(other);
}
else {
_sub(other);
}
return self;
}
public function bool IsNegative(); public function BigInt SubtractInt(int other)
public function (int other); {
local BigInt otherObject;
public function int ToInt(); otherObject = _.math.ToBigInt(other);
Add(otherObject);
_.memory.Free(otherObject);
return self;
}
public function Text ToText(); public function bool IsNegative()
{
return negative;
}
public function Text ToText_M();*/ public function int ToInt()
{
local int i;
local int accumulator;
local int mostSignificantDigit;
if (digits.lenght <= 0) {
return 0;
}
if (digits.lenght > DIGITS_IN_MAX_INT)
{
if (negative) {
return (-MaxInt - 1);
}
else {
return MaxInt;
}
}
mostSignificantDigit = -1;
if (digits.lenght == DIGITS_IN_MAX_INT)
{
mostSignificantDigit = digits[digits.length - 1];
digits[i] = DIGITS_IN_MAX_INT - 1;
}
// At most `DIGITS_IN_MAX_INT - 1` iterations
for (i = 0; i < digits.length; i += 1)
{//ALMOST_MAX_INT
accumulator *= 10;
accumulator += digits[i];
}
if (mostSignificantDigit < 0) {
return accumulator;
}
}
public function Text ToText() public function Text ToText()
{ {

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

@ -31,6 +31,30 @@ struct IntegerDivisionResult
var int remainder; var int remainder;
}; };
public function BigInt ToBigInt(int value)
{
local BigInt result;
result = BigInt(_.memory.Allocate(class'BigInt'));
return result.SetInt(value);
}
public function BigInt MakeBigInt(Text value)
{
local BigInt result;
result = BigInt(_.memory.Allocate(class'BigInt'));
return result.Set(value);
}
public function BigInt MakeBigInt_S(string value)
{
local BigInt result;
result = BigInt(_.memory.Allocate(class'BigInt'));
return result.Set_S(value);
}
/** /**
* Computes remainder of the integer division of `number` by `divisor`. * Computes remainder of the integer division of `number` by `divisor`.
* *

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

@ -0,0 +1,246 @@
/**
* Set of tests for `BigInt` class.
* Copyright 2022 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
* Acedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License, or
* (at your option) any later version.
*
* Acedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/
class TEST_BigInt extends TestCase
abstract;
protected static function TESTS()
{
// Here we use `ToString()` method to check `BigInt` creation,
// therefore also testing it
Test_Creating();
// So here we nee to test `ToText()` methods separately
Test_ToText();
Context("Testing basic arithmetic operations on `BigInt`s.");
Test_AddingValues();
Test_SubtractingValues();
}
protected static function Test_Creating()
{
Context("Testing creation of `BigInt`s.");
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");
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("4238756872643464981264982128742389")
.ToString() == "4238756872643464981264982128742389");
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");
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("-238473846327894632879097410348127")
.ToString() == "-238473846327894632879097410348127");
}
protected static function Test_ToText()
{
Context("Testing `ToText()` method of `BigInt`s.");
Issue("`ToText()` doesn't return value `BigInt` was initialized with" @
"a positive integer inside `string`.");
TEST_ExpectTrue(__().math
.MakeBigInt_S("2147483647")
.ToText()
.ToString() == "2147483647");
TEST_ExpectTrue(__().math
.MakeBigInt_S("65784236592763459236597823645978236592378659110571388")
.ToText()
.ToString() == "65784236592763459236597823645978236592378659110571388");
Issue("`ToText()` doesn't return value `BigInt` was initialized with" @
"a negative integer inside `string`.");
TEST_ExpectTrue(__().math
.MakeBigInt_S("-2147483648")
.ToText()
.ToString() == "-2147483648");
TEST_ExpectTrue(__().math
.MakeBigInt_S("-9827657892365923510176386357863078603212901078175829")
.ToText()
.ToString() == "-9827657892365923510176386357863078603212901078175829");
}
protected static function Test_AddingValues()
{
SubTest_AddingSameSignValues();
SubTest_AddingDifferentSignValues();
}
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);
TEST_ExpectTrue(main.ToString() == "927658286558305219024116453103");
main = __().math.MakeBigInt_S("16324234842947239847239239");
addition = __().math.MakeBigInt_S("927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927658286558305219024116453103");
main = __().math.MakeBigInt_S("728965872936589276");
addition = __().math.MakeBigInt_S("728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "1457931745873178552");
Issue("Two negative `BigInt`s are incorrectly added.");
main = __().math.MakeBigInt_S("-27641962323462271784269213864");
addition = __().math.MakeBigInt_S("-6324234842947239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-27648286558305219024116453103");
main = __().math.MakeBigInt_S("-16324234842947239847239239");
addition = __().math.MakeBigInt_S("-927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-927658286558305219024116453103");
main = __().math.MakeBigInt_S("-728965872936589276");
addition = __().math.MakeBigInt_S("-728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-1457931745873178552");
}
protected static function SubTest_AddingDifferentSignValues()
{
local BigInt main, addition;
Issue("Negative `BigInt`s is incorrectly added to positive one.");
main = __().math.MakeBigInt_S("927641962323462271784269213864");
addition = __().math.MakeBigInt_S("-1632423484294239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927640329899977977544421974625");
main = __().math.MakeBigInt_S("16324234842947239847239239");
addition = __().math.MakeBigInt_S("-927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-927625638088619324544421974625");
main = __().math.MakeBigInt_S("728965872936589276");
addition = __().math.MakeBigInt_S("-728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "0");
Issue("Positive `BigInt`s is incorrectly added to negative one.");
main = __().math.MakeBigInt_S("-27641962323462271784269213864");
addition = __().math.MakeBigInt_S("6324234842947239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-27635638088619324544421974625");
main = __().math.MakeBigInt_S("-16324234842947239847239239");
addition = __().math.MakeBigInt_S("927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927625638088619324544421974625");
main = __().math.MakeBigInt_S("-728965872936589276");
addition = __().math.MakeBigInt_S("728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "0");
}
protected static function Test_SubtractingValues()
{
SubTest_SubtractingSameSignValues();
SubTest_SubtractingDifferentSignValues();
}
protected static function SubTest_SubtractingSameSignValues()
{
local BigInt main, sub;
Issue("Two positive `BigInt`s are incorrectly subtracted.");
main = __().math.MakeBigInt_S("213721893712893789123798123912");
sub = __().math.MakeBigInt_S("91283172381723712893718931");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "213630610540512065410904404981");
main = __().math.MakeBigInt_S("328478923749827489237948");
sub = __().math.MakeBigInt_S("652578623458293527957923579235792529");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "-652578623457965049034173751746554581");
main = __().math.MakeBigInt_S("728965872936589276");
sub = __().math.MakeBigInt_S("728965872936589276");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "0");
Issue("Two negative `BigInt`s are incorrectly subtracted.");
main = __().math.MakeBigInt_S("-47283948923742901278492345984");
sub = __().math.MakeBigInt_S("-234782394728937402983200234");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "-47049166529013963875509145750");
main = __().math.MakeBigInt_S("-8920758902379");
sub = __().math.MakeBigInt_S("-2975234896823956283952");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "2975234887903197381573");
main = __().math.MakeBigInt_S("-728965872936589276");
sub = __().math.MakeBigInt_S("-728965872936589276");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "0");
}
//Negative `BigInt`s is incorrectly subtracted from positive one. [2]
protected static function SubTest_SubtractingDifferentSignValues()
{
local BigInt main, sub;
Issue("Negative `BigInt`s is incorrectly subtracted from positive one.");
main = __().math.MakeBigInt_S("927641962323462271784269213864");
sub = __().math.MakeBigInt_S("-1632423484294239847239239");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "927643594746946566024116453103");
main = __().math.MakeBigInt_S("16324234842947239847239239");
sub = __().math.MakeBigInt_S("-927641962323462271784269213864");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "927658286558305219024116453103");
main = __().math.MakeBigInt_S("728965872936589276");
sub = __().math.MakeBigInt_S("-728965872936589276");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "1457931745873178552");
Issue("Positive `BigInt`s is incorrectly subtracted from negative one.");
main = __().math.MakeBigInt_S("-27641962323462271784269213864");
sub = __().math.MakeBigInt_S("6324234842947239847239239");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "-27648286558305219024116453103");
main = __().math.MakeBigInt_S("-16324234842947239847239239");
sub = __().math.MakeBigInt_S("927641962323462271784269213864");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "-927658286558305219024116453103");
main = __().math.MakeBigInt_S("-728965872936589276");
sub = __().math.MakeBigInt_S("728965872936589276");
main.Subtract(sub);
TEST_ExpectTrue(main.ToString() == "-1457931745873178552");
Log("UMBRA TEST");
}
defaultproperties
{
caseGroup = "Math"
caseName = "BigInt"
}

176
sources/Data/Database/Tests/TEST_BigInt.uc

@ -1,176 +0,0 @@
/**
* Set of tests for `BigInt` class.
* Copyright 2022 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
* Acedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License, or
* (at your option) any later version.
*
* Acedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/
class TEST_BigInt extends TestCase
abstract;
protected static function TESTS()
{
// Here we use `ToString()` method to check `BigInt` creation,
// therefore also testing it
Test_Creating();
// So here we nee to test `ToText()` methods separately
Test_ToText();
Context("Testing basic arithmetic operations on `BigInt`s.");
Test_AddingValues();
}
protected static function Test_Creating()
{
Context("Testing creation of `BigInt`s.");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a positive `int`.");
TEST_ExpectTrue(class'BigInt'.static.FromInt(13524).ToString() == "13524");
TEST_ExpectTrue(
class'BigInt'.static.FromInt(MaxInt).ToString() == "2147483647");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a positive integer inside `string`.");
TEST_ExpectTrue(
class'BigInt'.static.FromDecimal_S("2147483647").ToString()
== "2147483647");
TEST_ExpectTrue(
class'BigInt'.static.FromDecimal_S("4238756872643464981264982128742389")
.ToString() == "4238756872643464981264982128742389");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a negative `int`.");
TEST_ExpectTrue(class'BigInt'.static.FromInt(-666).ToString() == "-666");
TEST_ExpectTrue(
class'BigInt'.static.FromInt(-MaxInt).ToString() == "-2147483647");
TEST_ExpectTrue(
class'BigInt'.static.FromInt(-MaxInt - 1).ToString() == "-2147483648");
Issue("`ToString()` doesn't return value `BigInt` was initialized with" @
"a negative integer inside `string`.");
TEST_ExpectTrue(
class'BigInt'.static.FromDecimal_S("-2147483648").ToString()
== "-2147483648");
TEST_ExpectTrue(
class'BigInt'.static.FromDecimal_S("-238473846327894632879097410348127")
.ToString() == "-238473846327894632879097410348127");
}
protected static function Test_ToText()
{
Context("Testing `ToText()` method of `BigInt`s.");
Issue("`ToText()` doesn't return value `BigInt` was initialized with" @
"a positive integer inside `string`.");
TEST_ExpectTrue(class'BigInt'.static
.FromDecimal_S("2147483647")
.ToText()
.ToString() == "2147483647");
TEST_ExpectTrue(class'BigInt'.static
.FromDecimal_S("65784236592763459236597823645978236592378659110571388")
.ToText()
.ToString() == "65784236592763459236597823645978236592378659110571388");
Issue("`ToText()` doesn't return value `BigInt` was initialized with" @
"a negative integer inside `string`.");
TEST_ExpectTrue(class'BigInt'.static
.FromDecimal_S("-2147483648")
.ToText()
.ToString() == "-2147483648");
TEST_ExpectTrue(class'BigInt'.static
.FromDecimal_S("-9827657892365923510176386357863078603212901078175829")
.ToText()
.ToString() == "-9827657892365923510176386357863078603212901078175829");
}
protected static function Test_AddingValues()
{
SubTest_AddingSameSignValues();
SubTest_AddingDifferentSignValues();
}
protected static function SubTest_AddingSameSignValues()
{
local BigInt main, addition;
Issue("Two positive `BigInt`s are incorrectly added.");
main = class'BigInt'.static.FromDecimal_S("927641962323462271784269213864");
addition = class'BigInt'.static.FromDecimal_S("16324234842947239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927658286558305219024116453103");
main = class'BigInt'.static.FromDecimal_S("16324234842947239847239239");
addition = class'BigInt'.static
.FromDecimal_S("927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927658286558305219024116453103");
main = class'BigInt'.static.FromDecimal_S("728965872936589276");
addition = class'BigInt'.static.FromDecimal_S("728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "1457931745873178552");
Issue("Two negative `BigInt`s are incorrectly added.");
main = class'BigInt'.static.FromDecimal_S("-27641962323462271784269213864");
addition = class'BigInt'.static.FromDecimal_S("-6324234842947239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-27648286558305219024116453103");
main = class'BigInt'.static.FromDecimal_S("-16324234842947239847239239");
addition = class'BigInt'.static
.FromDecimal_S("-927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-927658286558305219024116453103");
main = class'BigInt'.static.FromDecimal_S("-728965872936589276");
addition = class'BigInt'.static.FromDecimal_S("-728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-1457931745873178552");
}
protected static function SubTest_AddingDifferentSignValues()
{
local BigInt main, addition;
Issue("Negative `BigInt`s is incorrectly added to positive one.");
main = class'BigInt'.static.FromDecimal_S("927641962323462271784269213864");
addition = class'BigInt'.static.FromDecimal_S("-1632423484294239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927640329899977977544421974625");
main = class'BigInt'.static.FromDecimal_S("16324234842947239847239239");
addition = class'BigInt'.static
.FromDecimal_S("-927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-927625638088619324544421974625");
main = class'BigInt'.static.FromDecimal_S("728965872936589276");
addition = class'BigInt'.static.FromDecimal_S("-728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "0");
Issue("Positive `BigInt`s is incorrectly added to negative one.");
main = class'BigInt'.static.FromDecimal_S("-27641962323462271784269213864");
addition = class'BigInt'.static.FromDecimal_S("6324234842947239847239239");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "-27635638088619324544421974625");
main = class'BigInt'.static.FromDecimal_S("-16324234842947239847239239");
addition = class'BigInt'.static
.FromDecimal_S("927641962323462271784269213864");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "927625638088619324544421974625");
main = class'BigInt'.static.FromDecimal_S("-728965872936589276");
addition = class'BigInt'.static.FromDecimal_S("728965872936589276");
main.Add(addition);
TEST_ExpectTrue(main.ToString() == "0");
}
defaultproperties
{
caseGroup = "Database"
caseName = "BigInt"
}
Loading…
Cancel
Save