Merge PR #3679: Consistent Operators

* Minus->Sub Plus->Add Div->Quo

* pending

* Update PENDING.md

Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com>

* fix

* typo
This commit is contained in:
frog power 4000
2019-02-21 12:35:55 -05:00
committed by GitHub
parent 992dc8b2dc
commit a814e5ce66
37 changed files with 188 additions and 186 deletions
+19 -19
View File
@@ -86,7 +86,7 @@ func (coin Coin) IsEqual(other Coin) bool {
// Adds amounts of two coins with same denom. If the coins differ in denom then
// it panics.
func (coin Coin) Plus(coinB Coin) Coin {
func (coin Coin) Add(coinB Coin) Coin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
}
@@ -96,7 +96,7 @@ func (coin Coin) Plus(coinB Coin) Coin {
// Subtracts amounts of two coins with same denom. If the coins differ in denom
// then it panics.
func (coin Coin) Minus(coinB Coin) Coin {
func (coin Coin) Sub(coinB Coin) Coin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
}
@@ -178,27 +178,27 @@ func (coins Coins) IsValid() bool {
}
}
// Plus adds two sets of coins.
// Add adds two sets of coins.
//
// e.g.
// {2A} + {A, 2B} = {3A, 2B}
// {2A} + {0B} = {2A}
//
// NOTE: Plus operates under the invariant that coins are sorted by
// NOTE: Add operates under the invariant that coins are sorted by
// denominations.
//
// CONTRACT: Plus will never return Coins where one Coin has a non-positive
// CONTRACT: Add will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins Coins) Plus(coinsB Coins) Coins {
return coins.safePlus(coinsB)
func (coins Coins) Add(coinsB Coins) Coins {
return coins.safeAdd(coinsB)
}
// safePlus will perform addition of two coins sets. If both coin sets are
// safeAdd will perform addition of two coins sets. If both coin sets are
// empty, then an empty set is returned. If only a single set is empty, the
// other set is returned. Otherwise, the coins are compared in order of their
// denomination and addition only occurs when the denominations match, otherwise
// the coin is simply added to the sum assuming it's not zero.
func (coins Coins) safePlus(coinsB Coins) Coins {
func (coins Coins) safeAdd(coinsB Coins) Coins {
sum := ([]Coin)(nil)
indexA, indexB := 0, 0
lenA, lenB := len(coins), len(coinsB)
@@ -228,7 +228,7 @@ func (coins Coins) safePlus(coinsB Coins) Coins {
indexA++
case 0: // coin A denom == coin B denom
res := coinA.Plus(coinB)
res := coinA.Add(coinB)
if !res.IsZero() {
sum = append(sum, res)
}
@@ -246,17 +246,17 @@ func (coins Coins) safePlus(coinsB Coins) Coins {
}
}
// Minus subtracts a set of coins from another.
// Sub subtracts a set of coins from another.
//
// e.g.
// {2A, 3B} - {A} = {A, 3B}
// {2A} - {0B} = {2A}
// {A, B} - {A} = {B}
//
// CONTRACT: Minus will never return Coins where one Coin has a non-positive
// CONTRACT: Sub will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins Coins) Minus(coinsB Coins) Coins {
diff, hasNeg := coins.SafeMinus(coinsB)
func (coins Coins) Sub(coinsB Coins) Coins {
diff, hasNeg := coins.SafeSub(coinsB)
if hasNeg {
panic("negative coin amount")
}
@@ -264,17 +264,17 @@ func (coins Coins) Minus(coinsB Coins) Coins {
return diff
}
// SafeMinus performs the same arithmetic as Minus but returns a boolean if any
// SafeSub performs the same arithmetic as Sub but returns a boolean if any
// negative coin amount was returned.
func (coins Coins) SafeMinus(coinsB Coins) (Coins, bool) {
diff := coins.safePlus(coinsB.negative())
func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
diff := coins.safeAdd(coinsB.negative())
return diff, diff.IsAnyNegative()
}
// IsAllGT returns true if for every denom in coins, the denom is present at a
// greater amount in coinsB.
func (coins Coins) IsAllGT(coinsB Coins) bool {
diff, _ := coins.SafeMinus(coinsB)
diff, _ := coins.SafeSub(coinsB)
if len(diff) == 0 {
return false
}
@@ -285,7 +285,7 @@ func (coins Coins) IsAllGT(coinsB Coins) bool {
// IsAllGTE returns true iff for every denom in coins, the denom is present at
// an equal or greater amount in coinsB.
func (coins Coins) IsAllGTE(coinsB Coins) bool {
diff, _ := coins.SafeMinus(coinsB)
diff, _ := coins.SafeSub(coinsB)
if len(diff) == 0 {
return true
}
+2 -2
View File
@@ -21,7 +21,7 @@ func BenchmarkCoinsAdditionIntersect(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
coinsA.Plus(coinsB)
coinsA.Add(coinsB)
}
}
}
@@ -50,7 +50,7 @@ func BenchmarkCoinsAdditionNoIntersect(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
coinsA.Plus(coinsB)
coinsA.Add(coinsB)
}
}
}
+12 -12
View File
@@ -47,7 +47,7 @@ func TestIsEqualCoin(t *testing.T) {
}
}
func TestPlusCoin(t *testing.T) {
func TestAddCoin(t *testing.T) {
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -61,15 +61,15 @@ func TestPlusCoin(t *testing.T) {
for tcIndex, tc := range cases {
if tc.shouldPanic {
require.Panics(t, func() { tc.inputOne.Plus(tc.inputTwo) })
require.Panics(t, func() { tc.inputOne.Add(tc.inputTwo) })
} else {
res := tc.inputOne.Plus(tc.inputTwo)
res := tc.inputOne.Add(tc.inputTwo)
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
}
}
}
func TestMinusCoin(t *testing.T) {
func TestSubCoin(t *testing.T) {
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -85,9 +85,9 @@ func TestMinusCoin(t *testing.T) {
for tcIndex, tc := range cases {
if tc.shouldPanic {
require.Panics(t, func() { tc.inputOne.Minus(tc.inputTwo) })
require.Panics(t, func() { tc.inputOne.Sub(tc.inputTwo) })
} else {
res := tc.inputOne.Minus(tc.inputTwo)
res := tc.inputOne.Sub(tc.inputTwo)
require.Equal(t, tc.expected, res, "difference of coins is incorrect, tc #%d", tcIndex)
}
}
@@ -97,7 +97,7 @@ func TestMinusCoin(t *testing.T) {
inputTwo Coin
expected int64
}{NewInt64Coin(testDenom1, 1), NewInt64Coin(testDenom1, 1), 0}
res := tc.inputOne.Minus(tc.inputTwo)
res := tc.inputOne.Sub(tc.inputTwo)
require.Equal(t, tc.expected, res.Amount.Int64())
}
@@ -205,7 +205,7 @@ func TestEqualCoins(t *testing.T) {
}
}
func TestPlusCoins(t *testing.T) {
func TestAddCoins(t *testing.T) {
zero := NewInt(0)
one := NewInt(1)
two := NewInt(2)
@@ -223,13 +223,13 @@ func TestPlusCoins(t *testing.T) {
}
for tcIndex, tc := range cases {
res := tc.inputOne.Plus(tc.inputTwo)
res := tc.inputOne.Add(tc.inputTwo)
assert.True(t, res.IsValid())
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
}
}
func TestMinusCoins(t *testing.T) {
func TestSubCoins(t *testing.T) {
zero := NewInt(0)
one := NewInt(1)
two := NewInt(2)
@@ -249,9 +249,9 @@ func TestMinusCoins(t *testing.T) {
for i, tc := range testCases {
if tc.shouldPanic {
require.Panics(t, func() { tc.inputOne.Minus(tc.inputTwo) })
require.Panics(t, func() { tc.inputOne.Sub(tc.inputTwo) })
} else {
res := tc.inputOne.Minus(tc.inputTwo)
res := tc.inputOne.Sub(tc.inputTwo)
assert.True(t, res.IsValid())
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", i)
}
+17 -17
View File
@@ -99,7 +99,7 @@ func (coin DecCoin) IsEqual(other DecCoin) bool {
}
// Adds amounts of two coins with same denom
func (coin DecCoin) Plus(coinB DecCoin) DecCoin {
func (coin DecCoin) Add(coinB DecCoin) DecCoin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom))
}
@@ -107,7 +107,7 @@ func (coin DecCoin) Plus(coinB DecCoin) DecCoin {
}
// Subtracts amounts of two coins with same denom
func (coin DecCoin) Minus(coinB DecCoin) DecCoin {
func (coin DecCoin) Sub(coinB DecCoin) DecCoin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom))
}
@@ -179,29 +179,29 @@ func (coins DecCoins) TruncateDecimal() (Coins, DecCoins) {
for i, coin := range coins {
truncated, change := coin.TruncateDecimal()
out[i] = truncated
changeSum = changeSum.Plus(DecCoins{change})
changeSum = changeSum.Add(DecCoins{change})
}
return out, changeSum
}
// Plus adds two sets of DecCoins.
// Add adds two sets of DecCoins.
//
// NOTE: Plus operates under the invariant that coins are sorted by
// NOTE: Add operates under the invariant that coins are sorted by
// denominations.
//
// CONTRACT: Plus will never return Coins where one Coin has a non-positive
// CONTRACT: Add will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins DecCoins) Plus(coinsB DecCoins) DecCoins {
return coins.safePlus(coinsB)
func (coins DecCoins) Add(coinsB DecCoins) DecCoins {
return coins.safeAdd(coinsB)
}
// safePlus will perform addition of two DecCoins sets. If both coin sets are
// safeAdd will perform addition of two DecCoins sets. If both coin sets are
// empty, then an empty set is returned. If only a single set is empty, the
// other set is returned. Otherwise, the coins are compared in order of their
// denomination and addition only occurs when the denominations match, otherwise
// the coin is simply added to the sum assuming it's not zero.
func (coins DecCoins) safePlus(coinsB DecCoins) DecCoins {
func (coins DecCoins) safeAdd(coinsB DecCoins) DecCoins {
sum := ([]DecCoin)(nil)
indexA, indexB := 0, 0
lenA, lenB := len(coins), len(coinsB)
@@ -231,7 +231,7 @@ func (coins DecCoins) safePlus(coinsB DecCoins) DecCoins {
indexA++
case 0: // coin A denom == coin B denom
res := coinA.Plus(coinB)
res := coinA.Add(coinB)
if !res.IsZero() {
sum = append(sum, res)
}
@@ -261,9 +261,9 @@ func (coins DecCoins) negative() DecCoins {
return res
}
// Minus subtracts a set of DecCoins from another (adds the inverse).
func (coins DecCoins) Minus(coinsB DecCoins) DecCoins {
diff, hasNeg := coins.SafeMinus(coinsB)
// Sub subtracts a set of DecCoins from another (adds the inverse).
func (coins DecCoins) Sub(coinsB DecCoins) DecCoins {
diff, hasNeg := coins.SafeSub(coinsB)
if hasNeg {
panic("negative coin amount")
}
@@ -271,10 +271,10 @@ func (coins DecCoins) Minus(coinsB DecCoins) DecCoins {
return diff
}
// SafeMinus performs the same arithmetic as Minus but returns a boolean if any
// SafeSub performs the same arithmetic as Sub but returns a boolean if any
// negative coin amount was returned.
func (coins DecCoins) SafeMinus(coinsB DecCoins) (DecCoins, bool) {
diff := coins.safePlus(coinsB.negative())
func (coins DecCoins) SafeSub(coinsB DecCoins) (DecCoins, bool) {
diff := coins.safeAdd(coinsB.negative())
return diff, diff.IsAnyNegative()
}
+5 -5
View File
@@ -60,22 +60,22 @@ func TestDecCoinIsPositive(t *testing.T) {
require.False(t, dc.IsPositive())
}
func TestPlusDecCoin(t *testing.T) {
func TestAddDecCoin(t *testing.T) {
decCoinA1 := NewDecCoinFromDec(testDenom1, NewDecWithPrec(11, 1))
decCoinA2 := NewDecCoinFromDec(testDenom1, NewDecWithPrec(22, 1))
decCoinB1 := NewDecCoinFromDec(testDenom2, NewDecWithPrec(11, 1))
// regular add
res := decCoinA1.Plus(decCoinA1)
res := decCoinA1.Add(decCoinA1)
require.Equal(t, decCoinA2, res, "sum of coins is incorrect")
// bad denom add
require.Panics(t, func() {
decCoinA1.Plus(decCoinB1)
decCoinA1.Add(decCoinB1)
}, "expected panic on sum of different denoms")
}
func TestPlusDecCoins(t *testing.T) {
func TestAddDecCoins(t *testing.T) {
one := NewDec(1)
zero := NewDec(0)
two := NewDec(2)
@@ -91,7 +91,7 @@ func TestPlusDecCoins(t *testing.T) {
}
for tcIndex, tc := range cases {
res := tc.inputOne.Plus(tc.inputTwo)
res := tc.inputOne.Add(tc.inputTwo)
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
}
}
+3 -3
View File
@@ -157,7 +157,7 @@ func TestDecsEqual(t *testing.T) {
func TestArithmetic(t *testing.T) {
tests := []struct {
d1, d2 Dec
expMul, expDiv, expAdd, expSub Dec
expMul, expQuo, expAdd, expSub Dec
}{
// d1 d2 MUL DIV ADD SUB
{NewDec(0), NewDec(0), NewDec(0), NewDec(0), NewDec(0), NewDec(0)},
@@ -192,8 +192,8 @@ func TestArithmetic(t *testing.T) {
if tc.d2.IsZero() { // panic for divide by zero
require.Panics(t, func() { tc.d1.Quo(tc.d2) })
} else {
resDiv := tc.d1.Quo(tc.d2)
require.True(t, tc.expDiv.Equal(resDiv), "exp %v, res %v, tc %d", tc.expDiv.String(), resDiv.String(), tcIndex)
resQuo := tc.d1.Quo(tc.d2)
require.True(t, tc.expQuo.Equal(resQuo), "exp %v, res %v, tc %d", tc.expQuo.String(), resQuo.String(), tcIndex)
}
}
}
+6 -6
View File
@@ -31,7 +31,7 @@ func sub(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Sub(i, i2) }
func mul(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Mul(i, i2) }
func div(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Div(i, i2) }
func div(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Quo(i, i2) }
func mod(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Mod(i, i2) }
@@ -276,8 +276,8 @@ func (i Int) MulRaw(i2 int64) Int {
return i.Mul(NewInt(i2))
}
// Div divides Int with Int
func (i Int) Div(i2 Int) (res Int) {
// Quo divides Int with Int
func (i Int) Quo(i2 Int) (res Int) {
// Check division-by-zero
if i2.i.Sign() == 0 {
panic("Division by zero")
@@ -285,9 +285,9 @@ func (i Int) Div(i2 Int) (res Int) {
return Int{div(i.i, i2.i)}
}
// DivRaw divides Int with int64
func (i Int) DivRaw(i2 int64) Int {
return i.Div(NewInt(i2))
// QuoRaw divides Int with int64
func (i Int) QuoRaw(i2 int64) Int {
return i.Quo(NewInt(i2))
}
// Mod returns remainder after dividing with Int
+7 -7
View File
@@ -69,7 +69,7 @@ func TestIntPanic(t *testing.T) {
require.Panics(t, func() { intmin.Sub(OneInt()) })
// Division-by-zero check
require.Panics(t, func() { i1.Div(NewInt(0)) })
require.Panics(t, func() { i1.Quo(NewInt(0)) })
}
// Tests below uses randomness
@@ -126,11 +126,11 @@ func TestArithInt(t *testing.T) {
{i1.Add(i2), n1 + n2},
{i1.Sub(i2), n1 - n2},
{i1.Mul(i2), n1 * n2},
{i1.Div(i2), n1 / n2},
{i1.Quo(i2), n1 / n2},
{i1.AddRaw(n2), n1 + n2},
{i1.SubRaw(n2), n1 - n2},
{i1.MulRaw(n2), n1 * n2},
{i1.DivRaw(n2), n1 / n2},
{i1.QuoRaw(n2), n1 / n2},
{MinInt(i1, i2), minint(n1, n2)},
{MaxInt(i1, i2), maxint(n1, n2)},
{i1.Neg(), -n1},
@@ -188,11 +188,11 @@ func TestImmutabilityAllInt(t *testing.T) {
func(i *Int) { _ = i.Add(randint()) },
func(i *Int) { _ = i.Sub(randint()) },
func(i *Int) { _ = i.Mul(randint()) },
func(i *Int) { _ = i.Div(randint()) },
func(i *Int) { _ = i.Quo(randint()) },
func(i *Int) { _ = i.AddRaw(rand.Int63()) },
func(i *Int) { _ = i.SubRaw(rand.Int63()) },
func(i *Int) { _ = i.MulRaw(rand.Int63()) },
func(i *Int) { _ = i.DivRaw(rand.Int63()) },
func(i *Int) { _ = i.QuoRaw(rand.Int63()) },
func(i *Int) { _ = i.Neg() },
func(i *Int) { _ = i.IsZero() },
func(i *Int) { _ = i.Sign() },
@@ -240,11 +240,11 @@ func TestImmutabilityArithInt(t *testing.T) {
intarith(Int.Add, (*big.Int).Add),
intarith(Int.Sub, (*big.Int).Sub),
intarith(Int.Mul, (*big.Int).Mul),
intarith(Int.Div, (*big.Int).Div),
intarith(Int.Quo, (*big.Int).Quo),
intarithraw(Int.AddRaw, (*big.Int).Add),
intarithraw(Int.SubRaw, (*big.Int).Sub),
intarithraw(Int.MulRaw, (*big.Int).Mul),
intarithraw(Int.DivRaw, (*big.Int).Div),
intarithraw(Int.QuoRaw, (*big.Int).Quo),
}
for i := 0; i < 100; i++ {
+1 -1
View File
@@ -46,7 +46,7 @@ var PowerReduction = NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewIn
// TokensToTendermintPower - convert input tokens to potential tendermint power
func TokensToTendermintPower(tokens Int) int64 {
return (tokens.Div(PowerReduction)).Int64()
return (tokens.Quo(PowerReduction)).Int64()
}
// TokensFromTendermintPower - convert input power to tokens
+4 -4
View File
@@ -91,11 +91,11 @@ func (u Uint) Mul(u2 Uint) (res Uint) {
// Mul multiplies two Uints
func (u Uint) MulUint64(u2 uint64) (res Uint) { return u.Mul(NewUint(u2)) }
// Div divides Uint with Uint
func (u Uint) Div(u2 Uint) (res Uint) { return NewUintFromBigInt(div(u.i, u2.i)) }
// Quo divides Uint with Uint
func (u Uint) Quo(u2 Uint) (res Uint) { return NewUintFromBigInt(div(u.i, u2.i)) }
// Div divides Uint with uint64
func (u Uint) DivUint64(u2 uint64) Uint { return u.Div(NewUint(u2)) }
// Quo divides Uint with uint64
func (u Uint) QuoUint64(u2 uint64) Uint { return u.Quo(NewUint(u2)) }
// Return the minimum of the Uints
func MinUint(u1, u2 Uint) Uint { return NewUintFromBigInt(min(u1.i, u2.i)) }
+10 -10
View File
@@ -35,8 +35,8 @@ func TestUintPanics(t *testing.T) {
require.True(t, u1.SubUint64(0).Equal(ZeroUint()))
require.True(t, u2.Add(OneUint()).Sub(OneUint()).Equal(OneUint())) // i2 == 1
require.True(t, u2.Add(OneUint()).Mul(NewUint(5)).Equal(NewUint(10))) // i2 == 10
require.True(t, NewUint(7).Div(NewUint(2)).Equal(NewUint(3)))
require.True(t, NewUint(0).Div(NewUint(2)).Equal(ZeroUint()))
require.True(t, NewUint(7).Quo(NewUint(2)).Equal(NewUint(3)))
require.True(t, NewUint(0).Quo(NewUint(2)).Equal(ZeroUint()))
require.True(t, NewUint(5).MulUint64(4).Equal(NewUint(20)))
require.True(t, NewUint(5).MulUint64(0).Equal(ZeroUint()))
@@ -45,10 +45,10 @@ func TestUintPanics(t *testing.T) {
// divs by zero
require.Panics(t, func() { OneUint().Mul(ZeroUint().SubUint64(uint64(1))) })
require.Panics(t, func() { OneUint().DivUint64(0) })
require.Panics(t, func() { OneUint().Div(ZeroUint()) })
require.Panics(t, func() { ZeroUint().DivUint64(0) })
require.Panics(t, func() { OneUint().Div(ZeroUint().Sub(OneUint())) })
require.Panics(t, func() { OneUint().QuoUint64(0) })
require.Panics(t, func() { OneUint().Quo(ZeroUint()) })
require.Panics(t, func() { ZeroUint().QuoUint64(0) })
require.Panics(t, func() { OneUint().Quo(ZeroUint().Sub(OneUint())) })
require.Panics(t, func() { uintmax.Add(OneUint()) })
require.Panics(t, func() { uintmin.Sub(OneUint()) })
@@ -108,10 +108,10 @@ func TestArithUint(t *testing.T) {
}{
{u1.Add(u2), n1 + n2},
{u1.Mul(u2), n1 * n2},
{u1.Div(u2), n1 / n2},
{u1.Quo(u2), n1 / n2},
{u1.AddUint64(n2), n1 + n2},
{u1.MulUint64(n2), n1 * n2},
{u1.DivUint64(n2), n1 / n2},
{u1.QuoUint64(n2), n1 / n2},
{MinUint(u1, u2), minuint(n1, n2)},
{MaxUint(u1, u2), maxuint(n1, n2)},
}
@@ -168,11 +168,11 @@ func TestImmutabilityAllUint(t *testing.T) {
func(i *Uint) { _ = i.Add(NewUint(rand.Uint64())) },
func(i *Uint) { _ = i.Sub(NewUint(rand.Uint64() % i.Uint64())) },
func(i *Uint) { _ = i.Mul(randuint()) },
func(i *Uint) { _ = i.Div(randuint()) },
func(i *Uint) { _ = i.Quo(randuint()) },
func(i *Uint) { _ = i.AddUint64(rand.Uint64()) },
func(i *Uint) { _ = i.SubUint64(rand.Uint64() % i.Uint64()) },
func(i *Uint) { _ = i.MulUint64(rand.Uint64()) },
func(i *Uint) { _ = i.DivUint64(rand.Uint64()) },
func(i *Uint) { _ = i.QuoUint64(rand.Uint64()) },
func(i *Uint) { _ = i.IsZero() },
func(i *Uint) { _ = i.Equal(randuint()) },
func(i *Uint) { _ = i.GT(randuint()) },