mirror of
https://github.com/asticode/go-astikit.git
synced 2025-12-24 11:50:53 +08:00
Added MinMaxInt
This commit is contained in:
@@ -58,3 +58,12 @@ func (r *Rational) UnmarshalText(b []byte) (err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MinMaxInt(v, min, max int) int {
|
||||||
|
if v < min {
|
||||||
|
return min
|
||||||
|
} else if v > max {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
@@ -53,3 +53,15 @@ func TestRational(t *testing.T) {
|
|||||||
t.Fatalf("expected %s, got %s", e, g)
|
t.Fatalf("expected %s, got %s", e, g)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMinMaxInt(t *testing.T) {
|
||||||
|
if e, g := 0, MinMaxInt(-1, 0, 2); e != g {
|
||||||
|
t.Fatalf("expected %+v, got %+v", e, g)
|
||||||
|
}
|
||||||
|
if e, g := 1, MinMaxInt(1, 0, 2); e != g {
|
||||||
|
t.Fatalf("expected %+v, got %+v", e, g)
|
||||||
|
}
|
||||||
|
if e, g := 2, MinMaxInt(3, 0, 2); e != g {
|
||||||
|
t.Fatalf("expected %+v, got %+v", e, g)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user