Added MinMaxInt

This commit is contained in:
Quentin Renard
2025-06-25 15:42:05 +02:00
parent 3eedad89c3
commit a96d869708
2 changed files with 21 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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)
}
}