adding ranged number type, for use in stats (#581)

This commit is contained in:
dk 2020-07-13 06:03:19 -07:00 committed by GitHub
parent 8c16ea2880
commit 7a32e7bbac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 218 additions and 0 deletions

View File

@ -0,0 +1,101 @@
package d2math
import "fmt"
// RangedNumber is a number with a min and max range
type RangedNumber struct {
min int
max int
}
// Min returns the min value, swapping min/max if not ordered
func (rn *RangedNumber) Min() int {
if rn.min > rn.max {
rn.Set(rn.max, rn.min)
}
return rn.min
}
// Max returns the max value, swapping min/max if not ordered
func (rn *RangedNumber) Max() int {
if rn.min > rn.max {
rn.Set(rn.max, rn.min)
}
return rn.max
}
// Set sets the min and max values, ordering the arguments if necessary
func (rn *RangedNumber) Set(min, max int) *RangedNumber {
rn.SetMin(min)
rn.SetMax(max)
return rn
}
// SetMin sets the minimum value
func (rn *RangedNumber) SetMin(min int) *RangedNumber {
rn.min = min
if rn.min > rn.max {
rn.min = rn.max
rn.max = min
}
return rn
}
// SetMax sets the maximum value
func (rn *RangedNumber) SetMax(max int) *RangedNumber {
rn.max = max
if rn.min > rn.max {
rn.max = rn.min
rn.min = max
}
return rn
}
// Clone creates a new copy of a ranged number, with the same min/max
func (rn RangedNumber) Clone() *RangedNumber {
return &rn
}
// Copy copies the min/max values of the given ranged number
func (rn *RangedNumber) Copy(other *RangedNumber) *RangedNumber {
return rn.Set(other.min, other.max)
}
// Equals checks equality with the given ranged number
func (rn *RangedNumber) Equals(other *RangedNumber) bool {
return rn.min == other.min && rn.max == other.max
}
// Add adds the given ranged number to this one, returning this one
func (rn *RangedNumber) Add(other *RangedNumber) *RangedNumber {
return rn.Set(rn.min+other.min, rn.max+other.max)
}
// Sub subtracts the given ranged number from this one, returning this one
func (rn *RangedNumber) Sub(other *RangedNumber) *RangedNumber {
return rn.Set(rn.min - other.min, rn.max - other.max)
}
// Mul multiplies this ranged number by the given ranged number, returning this one
func (rn *RangedNumber) Mul(other *RangedNumber) *RangedNumber {
return rn.Set(rn.min * other.min, rn.max * other.max)
}
// Div divides this ranged number by the given ranged number, returning this one
func (rn *RangedNumber) Div(other *RangedNumber) *RangedNumber {
return rn.Set(rn.min / other.min, rn.max / other.max)
}
func (rn *RangedNumber) String() string {
if rn.Min() == rn.Max() { // ensures ordering
return fmt.Sprintf("%d", rn.min)
}
return fmt.Sprintf("%d to %d", rn.min, rn.max)
}

View File

@ -0,0 +1,117 @@
package d2math
import "testing"
func TestRangedNumber_Clone(t *testing.T) {
rn1 := &RangedNumber{1, 1}
rn2 := rn1.Clone()
if &rn1 == &rn2 {
t.Errorf("Cloned ranged number is not unique: *%d == *%d", &rn1, &rn2)
}
}
func TestRangedNumber_Copy(t *testing.T) {
rn1 := &RangedNumber{1, 1}
rn2 := rn1.Clone().Set(-1, -1)
rn1.Copy(rn2)
if rn1.Min() != rn2.Min() || rn1.Max() != rn2.Max() {
t.Errorf("Min/Max values were not copied: %d != %d", rn1, rn2)
}
}
func TestRangedNumber_Set(t *testing.T) {
badMin, badMax := 10, -10
rn := &RangedNumber{badMin, badMax} // should get swapped when used
if rn.Min() == badMin || rn.Max() == badMax {
t.Errorf("Min/Max values were not ordered: %d", rn)
}
rn.Set(badMin, badMax)
if rn.Min() == badMin || rn.Max() == badMax {
t.Errorf("Min/Max values were not ordered: %d", rn)
}
}
func TestRangedNumber_Equals(t *testing.T) {
min, max := 1, 2
rn1 := &RangedNumber{min, max}
rn2 := rn1.Clone()
if !rn1.Equals(rn2) {
t.Errorf("Not equal when they should be: %d == %d", rn1, rn2)
}
rn1.Set(3, 4)
if rn1.Equals(rn2) {
t.Errorf("equal when they should NOT be: %d != %d", rn1, rn2)
}
}
func TestRangedNumber_Add(t *testing.T) {
min, max := 1, 2
rn1 := &RangedNumber{min, max}
rn2 := rn1.Clone()
rn1.Add(rn2)
if rn1.Min() != 2 || rn1.Max() != 4 {
t.Errorf("Unexpected value after addition: %d ", rn1)
}
}
func TestRangedNumber_Sub(t *testing.T) {
min, max := 1, 2
rn1 := &RangedNumber{min, max}
rn2 := rn1.Clone()
rn1.Sub(rn2)
if rn1.Min() != 0 || rn1.Max() != 0 {
t.Errorf("Unexpected value after subtraction: %d ", rn1)
}
}
func TestRangedNumber_Mul(t *testing.T) {
min, max := 1, 2
rn1 := &RangedNumber{min, max}
rn2 := rn1.Clone()
rn1.Mul(rn2)
if rn1.Min() != 1 || rn1.Max() != 4 {
t.Errorf("Unexpected value after multiplication: %d ", rn1)
}
}
func TestRangedNumber_Div(t *testing.T) {
min, max := 1, 2
rn1 := &RangedNumber{min, max}
rn2 := rn1.Clone()
rn1.Div(rn2)
if rn1.Min() != 1 || rn1.Max() != 1 {
t.Errorf("Unexpected value after division: %d ", rn1)
}
}
func TestRangedNumber_String(t *testing.T) {
rn := &RangedNumber{}
if rn.String() != "0" {
t.Errorf("Unexpected string output: %d ", rn)
}
if rn.Set(0, -1).String() != "-1 to 0" {
t.Errorf("Unexpected string output: %d ", rn)
}
if rn.Set(-10, 20).String() != "-10 to 20" {
t.Errorf("Unexpected string output: %d ", rn)
}
}