fix/scale: fix typo and add some more tests

This commit is contained in:
Andy Cheng 2023-03-10 13:51:29 +08:00
parent f92bcda51d
commit d51a802315
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28
2 changed files with 4 additions and 1 deletions

View File

@ -151,7 +151,7 @@ func (s *LinearScale) Call(x float64) (y float64) {
if x <= s.Domain[0] { if x <= s.Domain[0] {
return s.Range[0] return s.Range[0]
} else if x >= s.Domain[1] { } else if x >= s.Domain[1] {
return s.Range[0] return s.Range[1]
} }
y = s.Range[0] + (x-s.Domain[0])*s.a y = s.Range[0] + (x-s.Domain[0])*s.a

View File

@ -75,6 +75,7 @@ func TestLinearScale(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "f(x) = 3.000000 + (x - 1000.000000) * 0.007000", scale.String()) assert.Equal(t, "f(x) = 3.000000 + (x - 1000.000000) * 0.007000", scale.String())
assert.InDelta(t, 3, scale.Call(1000), delta) assert.InDelta(t, 3, scale.Call(1000), delta)
assert.InDelta(t, 6.5, scale.Call(1500), delta)
assert.InDelta(t, 10, scale.Call(2000), delta) assert.InDelta(t, 10, scale.Call(2000), delta)
for x := 1000; x <= 2000; x += 100 { for x := 1000; x <= 2000; x += 100 {
y := scale.Call(float64(x)) y := scale.Call(float64(x))
@ -92,6 +93,7 @@ func TestLinearScale2(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "f(x) = 0.100000 + (x - 1.000000) * 0.150000", scale.String()) assert.Equal(t, "f(x) = 0.100000 + (x - 1.000000) * 0.150000", scale.String())
assert.InDelta(t, 0.1, scale.Call(1), delta) assert.InDelta(t, 0.1, scale.Call(1), delta)
assert.InDelta(t, 0.25, scale.Call(2), delta)
assert.InDelta(t, 0.4, scale.Call(3), delta) assert.InDelta(t, 0.4, scale.Call(3), delta)
} }
@ -105,6 +107,7 @@ func TestLinearScaleNegative(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "f(x) = 0.100000 + (x - -1.000000) * 0.075000", scale.String()) assert.Equal(t, "f(x) = 0.100000 + (x - -1.000000) * 0.075000", scale.String())
assert.InDelta(t, 0.1, scale.Call(-1), delta) assert.InDelta(t, 0.1, scale.Call(-1), delta)
assert.InDelta(t, 0.25, scale.Call(1), delta)
assert.InDelta(t, 0.4, scale.Call(3), delta) assert.InDelta(t, 0.4, scale.Call(3), delta)
} }