This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
package mathext
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestClampIntWithinRange(t *testing.T) {
|
||||
if got := ClampInt(5, 1, 10); got != 5 {
|
||||
t.Errorf("ClampInt(5, 1, 10) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampIntBelowRange(t *testing.T) {
|
||||
if got := ClampInt(-3, 1, 10); got != 1 {
|
||||
t.Errorf("ClampInt(-3, 1, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampIntAboveRange(t *testing.T) {
|
||||
if got := ClampInt(15, 1, 10); got != 10 {
|
||||
t.Errorf("ClampInt(15, 1, 10) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampIntAtLowerBound(t *testing.T) {
|
||||
if got := ClampInt(1, 1, 10); got != 1 {
|
||||
t.Errorf("ClampInt(1, 1, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampIntAtUpperBound(t *testing.T) {
|
||||
if got := ClampInt(10, 1, 10); got != 10 {
|
||||
t.Errorf("ClampInt(10, 1, 10) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampInt32WithinRange(t *testing.T) {
|
||||
if got := ClampInt32(int32(5), int32(1), int32(10)); got != 5 {
|
||||
t.Errorf("ClampInt32(5, 1, 10) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampInt32BelowRange(t *testing.T) {
|
||||
if got := ClampInt32(int32(-3), int32(1), int32(10)); got != 1 {
|
||||
t.Errorf("ClampInt32(-3, 1, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampInt32AboveRange(t *testing.T) {
|
||||
if got := ClampInt32(int32(15), int32(1), int32(10)); got != 10 {
|
||||
t.Errorf("ClampInt32(15, 1, 10) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampFloat32WithinRange(t *testing.T) {
|
||||
if got := ClampFloat32(float32(5.5), float32(1.0), float32(10.0)); got != 5.5 {
|
||||
t.Errorf("ClampFloat32(5.5, 1.0, 10.0) = %v, want 5.5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampFloat32BelowRange(t *testing.T) {
|
||||
if got := ClampFloat32(float32(-1.5), float32(0.0), float32(10.0)); got != 0.0 {
|
||||
t.Errorf("ClampFloat32(-1.5, 0.0, 10.0) = %v, want 0.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampFloat32AboveRange(t *testing.T) {
|
||||
if got := ClampFloat32(float32(11.5), float32(0.0), float32(10.0)); got != 10.0 {
|
||||
t.Errorf("ClampFloat32(11.5, 0.0, 10.0) = %v, want 10.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampFloat64WithinRange(t *testing.T) {
|
||||
if got := ClampFloat64(5.5, 1.0, 10.0); got != 5.5 {
|
||||
t.Errorf("ClampFloat64(5.5, 1.0, 10.0) = %v, want 5.5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampFloat64BelowRange(t *testing.T) {
|
||||
if got := ClampFloat64(-1.5, 0.0, 10.0); got != 0.0 {
|
||||
t.Errorf("ClampFloat64(-1.5, 0.0, 10.0) = %v, want 0.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampFloat64AboveRange(t *testing.T) {
|
||||
if got := ClampFloat64(11.5, 0.0, 10.0); got != 10.0 {
|
||||
t.Errorf("ClampFloat64(11.5, 0.0, 10.0) = %v, want 10.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampGenericIntWithinRange(t *testing.T) {
|
||||
if got := Clamp(5, 1, 10); got != 5 {
|
||||
t.Errorf("Clamp(5, 1, 10) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampGenericIntBelowRange(t *testing.T) {
|
||||
if got := Clamp(-3, 1, 10); got != 1 {
|
||||
t.Errorf("Clamp(-3, 1, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampGenericIntAboveRange(t *testing.T) {
|
||||
if got := Clamp(15, 1, 10); got != 10 {
|
||||
t.Errorf("Clamp(15, 1, 10) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampGenericFloat64WithinRange(t *testing.T) {
|
||||
if got := Clamp(5.5, 1.0, 10.0); got != 5.5 {
|
||||
t.Errorf("Clamp(5.5, 1.0, 10.0) = %v, want 5.5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampGenericFloat64BelowRange(t *testing.T) {
|
||||
if got := Clamp(-2.0, 0.0, 10.0); got != 0.0 {
|
||||
t.Errorf("Clamp(-2.0, 0.0, 10.0) = %v, want 0.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampGenericFloat64AboveRange(t *testing.T) {
|
||||
if got := Clamp(20.5, 0.0, 10.0); got != 10.0 {
|
||||
t.Errorf("Clamp(20.5, 0.0, 10.0) = %v, want 10.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampOptNilFallback(t *testing.T) {
|
||||
var v *int = nil
|
||||
if got := ClampOpt(v, 7, 1, 10); got != 7 {
|
||||
t.Errorf("ClampOpt(nil, 7, 1, 10) = %v, want 7", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampOptValueWithinRange(t *testing.T) {
|
||||
val := 5
|
||||
if got := ClampOpt(&val, 7, 1, 10); got != 5 {
|
||||
t.Errorf("ClampOpt(&5, 7, 1, 10) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampOptValueBelowRange(t *testing.T) {
|
||||
val := -3
|
||||
if got := ClampOpt(&val, 7, 1, 10); got != 1 {
|
||||
t.Errorf("ClampOpt(&-3, 7, 1, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampOptValueAboveRange(t *testing.T) {
|
||||
val := 15
|
||||
if got := ClampOpt(&val, 7, 1, 10); got != 10 {
|
||||
t.Errorf("ClampOpt(&15, 7, 1, 10) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClampOptFloat64Nil(t *testing.T) {
|
||||
var v *float64 = nil
|
||||
if got := ClampOpt(v, 2.5, 0.0, 10.0); got != 2.5 {
|
||||
t.Errorf("ClampOpt(nil, 2.5, 0.0, 10.0) = %v, want 2.5", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package mathext
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFloat64EpsilonEqExactlyEqual(t *testing.T) {
|
||||
if !Float64EpsilonEq(1.0, 1.0, 1e-9) {
|
||||
t.Errorf("Float64EpsilonEq(1.0, 1.0, 1e-9) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqWithinEpsilon(t *testing.T) {
|
||||
if !Float64EpsilonEq(1.0, 1.0+1e-10, 1e-9) {
|
||||
t.Errorf("Float64EpsilonEq(1.0, 1.0+1e-10, 1e-9) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqOutsideEpsilon(t *testing.T) {
|
||||
if Float64EpsilonEq(1.0, 1.1, 1e-9) {
|
||||
t.Errorf("Float64EpsilonEq(1.0, 1.1, 1e-9) = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqAtEpsilonBoundary(t *testing.T) {
|
||||
if !Float64EpsilonEq(0.0, 0.5, 0.5) {
|
||||
t.Errorf("Float64EpsilonEq(0.0, 0.5, 0.5) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqNegativeDifference(t *testing.T) {
|
||||
if !Float64EpsilonEq(2.0, 2.0-1e-10, 1e-9) {
|
||||
t.Errorf("Float64EpsilonEq(2.0, 2.0-1e-10, 1e-9) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqLargeDifference(t *testing.T) {
|
||||
if Float64EpsilonEq(0.0, 100.0, 0.5) {
|
||||
t.Errorf("Float64EpsilonEq(0.0, 100.0, 0.5) = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqNegativeNumbers(t *testing.T) {
|
||||
if !Float64EpsilonEq(-1.0, -1.0+1e-10, 1e-9) {
|
||||
t.Errorf("Float64EpsilonEq(-1.0, -1.0+1e-10, 1e-9) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqZeroEpsilonEqualValues(t *testing.T) {
|
||||
if !Float64EpsilonEq(3.14, 3.14, 0.0) {
|
||||
t.Errorf("Float64EpsilonEq(3.14, 3.14, 0.0) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64EpsilonEqZeroEpsilonDifferentValues(t *testing.T) {
|
||||
if Float64EpsilonEq(3.14, 3.15, 0.0) {
|
||||
t.Errorf("Float64EpsilonEq(3.14, 3.15, 0.0) = true, want false")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package mathext
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSumFloat64HappyPath(t *testing.T) {
|
||||
values := []float64{1.0, 2.0, 3.0, 4.0}
|
||||
expected := 10.0
|
||||
if got := SumFloat64(values); got != expected {
|
||||
t.Errorf("SumFloat64(%v) = %v, want %v", values, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSumFloat64Empty(t *testing.T) {
|
||||
values := []float64{}
|
||||
if got := SumFloat64(values); got != 0.0 {
|
||||
t.Errorf("SumFloat64(empty) = %v, want 0.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSumFloat64Negatives(t *testing.T) {
|
||||
values := []float64{-1.0, -2.0, 3.0}
|
||||
expected := 0.0
|
||||
if got := SumFloat64(values); got != expected {
|
||||
t.Errorf("SumFloat64(%v) = %v, want %v", values, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvgFloat64HappyPath(t *testing.T) {
|
||||
values := []float64{2.0, 4.0, 6.0, 8.0}
|
||||
expected := 5.0
|
||||
if got := AvgFloat64(values); got != expected {
|
||||
t.Errorf("AvgFloat64(%v) = %v, want %v", values, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvgFloat64SingleValue(t *testing.T) {
|
||||
values := []float64{42.0}
|
||||
expected := 42.0
|
||||
if got := AvgFloat64(values); got != expected {
|
||||
t.Errorf("AvgFloat64(%v) = %v, want %v", values, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvgFloat64EmptyReturnsNaN(t *testing.T) {
|
||||
values := []float64{}
|
||||
got := AvgFloat64(values)
|
||||
if !math.IsNaN(got) {
|
||||
t.Errorf("AvgFloat64(empty) = %v, want NaN", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxIntFirstLarger(t *testing.T) {
|
||||
if got := Max(5, 3); got != 5 {
|
||||
t.Errorf("Max(5, 3) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxIntSecondLarger(t *testing.T) {
|
||||
if got := Max(3, 5); got != 5 {
|
||||
t.Errorf("Max(3, 5) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxIntEqual(t *testing.T) {
|
||||
if got := Max(5, 5); got != 5 {
|
||||
t.Errorf("Max(5, 5) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxFloat64(t *testing.T) {
|
||||
if got := Max(2.7, 3.1); got != 3.1 {
|
||||
t.Errorf("Max(2.7, 3.1) = %v, want 3.1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxString(t *testing.T) {
|
||||
if got := Max("apple", "banana"); got != "banana" {
|
||||
t.Errorf(`Max("apple", "banana") = %v, want "banana"`, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax3FirstLargest(t *testing.T) {
|
||||
if got := Max3(10, 5, 3); got != 10 {
|
||||
t.Errorf("Max3(10, 5, 3) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax3MiddleLargest(t *testing.T) {
|
||||
if got := Max3(5, 10, 3); got != 10 {
|
||||
t.Errorf("Max3(5, 10, 3) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax3LastLargest(t *testing.T) {
|
||||
if got := Max3(5, 3, 10); got != 10 {
|
||||
t.Errorf("Max3(5, 3, 10) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax4(t *testing.T) {
|
||||
if got := Max4(1, 5, 3, 7); got != 7 {
|
||||
t.Errorf("Max4(1, 5, 3, 7) = %v, want 7", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax4FirstLargest(t *testing.T) {
|
||||
if got := Max4(10, 5, 3, 7); got != 10 {
|
||||
t.Errorf("Max4(10, 5, 3, 7) = %v, want 10", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMax4ThirdLargest(t *testing.T) {
|
||||
if got := Max4(1, 5, 100, 7); got != 100 {
|
||||
t.Errorf("Max4(1, 5, 100, 7) = %v, want 100", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinIntFirstSmaller(t *testing.T) {
|
||||
if got := Min(3, 5); got != 3 {
|
||||
t.Errorf("Min(3, 5) = %v, want 3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinIntSecondSmaller(t *testing.T) {
|
||||
if got := Min(5, 3); got != 3 {
|
||||
t.Errorf("Min(5, 3) = %v, want 3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinIntEqual(t *testing.T) {
|
||||
if got := Min(5, 5); got != 5 {
|
||||
t.Errorf("Min(5, 5) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinFloat64(t *testing.T) {
|
||||
if got := Min(2.7, 3.1); got != 2.7 {
|
||||
t.Errorf("Min(2.7, 3.1) = %v, want 2.7", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMin3FirstSmallest(t *testing.T) {
|
||||
if got := Min3(1, 5, 10); got != 1 {
|
||||
t.Errorf("Min3(1, 5, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMin3MiddleSmallest(t *testing.T) {
|
||||
if got := Min3(5, 1, 10); got != 1 {
|
||||
t.Errorf("Min3(5, 1, 10) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMin3LastSmallest(t *testing.T) {
|
||||
if got := Min3(5, 10, 1); got != 1 {
|
||||
t.Errorf("Min3(5, 10, 1) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMin4(t *testing.T) {
|
||||
if got := Min4(7, 3, 5, 1); got != 1 {
|
||||
t.Errorf("Min4(7, 3, 5, 1) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMin4FirstSmallest(t *testing.T) {
|
||||
if got := Min4(1, 5, 3, 7); got != 1 {
|
||||
t.Errorf("Min4(1, 5, 3, 7) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMin4ThirdSmallest(t *testing.T) {
|
||||
if got := Min4(10, 5, 1, 7); got != 1 {
|
||||
t.Errorf("Min4(10, 5, 1, 7) = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsPositiveInt(t *testing.T) {
|
||||
if got := Abs(5); got != 5 {
|
||||
t.Errorf("Abs(5) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsNegativeInt(t *testing.T) {
|
||||
if got := Abs(-5); got != 5 {
|
||||
t.Errorf("Abs(-5) = %v, want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsZeroInt(t *testing.T) {
|
||||
if got := Abs(0); got != 0 {
|
||||
t.Errorf("Abs(0) = %v, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsPositiveFloat64(t *testing.T) {
|
||||
if got := Abs(3.14); got != 3.14 {
|
||||
t.Errorf("Abs(3.14) = %v, want 3.14", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsNegativeFloat64(t *testing.T) {
|
||||
if got := Abs(-3.14); got != 3.14 {
|
||||
t.Errorf("Abs(-3.14) = %v, want 3.14", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsNegativeFloat32(t *testing.T) {
|
||||
if got := Abs(float32(-2.5)); got != float32(2.5) {
|
||||
t.Errorf("Abs(-2.5) = %v, want 2.5", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user