uint256: Add greater than comparison support.
This adds the ability to determine if a uint256 is greater than another uint256 or a uint64 along with associated tests to ensure proper functionality. This is part of a series of commits to fully implement the uint256 package.
This commit is contained in:
parent
4831b21278
commit
c1b94c2394
@ -18,16 +18,16 @@ var (
|
||||
// fixed-precision arithmetic. All operations are performed modulo 2^256, so
|
||||
// callers may rely on "wrap around" semantics.
|
||||
//
|
||||
// It currently implements support for comparison operations (equals, less),
|
||||
// interpreting and producing big and little endian bytes, and other convenience
|
||||
// methods such as whether or not the value can be represented as a uint64
|
||||
// without loss of precision.
|
||||
// It currently implements support for comparison operations (equals, less,
|
||||
// greater), interpreting and producing big and little endian bytes, and other
|
||||
// convenience methods such as whether or not the value can be represented as a
|
||||
// uint64 without loss of precision.
|
||||
//
|
||||
// Future commits will implement the primary arithmetic operations (addition,
|
||||
// subtraction, multiplication, squaring, division, negation), bitwise
|
||||
// operations (lsh, rsh, not, or, and, xor), comparison operations (greater,
|
||||
// cmp), and other convenience methods such as determining the minimum number of
|
||||
// bits required to represent the current value and text formatting with base
|
||||
// operations (lsh, rsh, not, or, and, xor), comparison operations (cmp), and
|
||||
// other convenience methods such as determining the minimum number of bits
|
||||
// required to represent the current value and text formatting with base
|
||||
// conversion.
|
||||
type Uint256 struct {
|
||||
// The uint256 is represented as 4 unsigned 64-bit integers in base 2^64.
|
||||
@ -398,3 +398,15 @@ func (n *Uint256) LtEq(n2 *Uint256) bool {
|
||||
func (n *Uint256) LtEqUint64(n2 uint64) bool {
|
||||
return n.n[0] <= n2 && (n.n[1]|n.n[2]|n.n[3]) == 0
|
||||
}
|
||||
|
||||
// Gt returns whether or not the uint256 is greater than the given one. That
|
||||
// is, it returns true when n > n2.
|
||||
func (n *Uint256) Gt(n2 *Uint256) bool {
|
||||
return n2.Lt(n)
|
||||
}
|
||||
|
||||
// GtUint64 returns whether or not the uint256 is greater than the given uint64.
|
||||
// That is, it returns true when n > n2.
|
||||
func (n *Uint256) GtUint64(n2 uint64) bool {
|
||||
return n.n[0] > n2 || (n.n[1]|n.n[2]|n.n[3]) != 0
|
||||
}
|
||||
|
||||
@ -876,6 +876,15 @@ func TestUint256Comparison(t *testing.T) {
|
||||
isLtEq, wantLtEq)
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure comparing the numbers produces the expected > result.
|
||||
isGt := n1.Gt(n2)
|
||||
wantGt := test.wantCmp > 0
|
||||
if isGt != wantGt {
|
||||
t.Errorf("%q: incorrect > result -- got: %v, want: %v", test.name,
|
||||
isGt, wantGt)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -933,6 +942,15 @@ func TestUint256ComparisonRandom(t *testing.T) {
|
||||
n1, n2, isLtEq, wantLtEq)
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure comparing the numbers produces the expected > result.
|
||||
isGt := n1.Gt(n2)
|
||||
wantGt := bigCmpResult > 0
|
||||
if isGt != wantGt {
|
||||
t.Errorf("incorrect > result n1: %x, n2: %x -- got: %v, want: %v",
|
||||
n1, n2, isGt, wantGt)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1051,6 +1069,15 @@ func TestUint256ComparisonUint64(t *testing.T) {
|
||||
isLt, wantLtEq)
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure comparing the numbers produces the expected > result.
|
||||
isGt := n1.GtUint64(test.n2)
|
||||
wantGt := test.wantCmp > 0
|
||||
if isGt != wantGt {
|
||||
t.Errorf("%q: incorrect > result -- got: %v, want: %v", test.name,
|
||||
isGt, wantGt)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1110,5 +1137,14 @@ func TestUint256ComparisonUint64Random(t *testing.T) {
|
||||
n1, n2, isLtEq, wantLtEq)
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure comparing the numbers produces the expected > result.
|
||||
isGt := n1.GtUint64(n2)
|
||||
wantGt := bigCmpResult > 0
|
||||
if isGt != wantGt {
|
||||
t.Errorf("incorrect > result n1: %x, n2: %x -- got: %v, want: %v",
|
||||
n1, n2, isGt, wantGt)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user