159 lines
4.1 KiB
Go
159 lines
4.1 KiB
Go
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)
|
|
}
|
|
}
|