-
Notifications
You must be signed in to change notification settings - Fork 42
/
inEpsilon.go
49 lines (39 loc) · 1.66 KB
/
inEpsilon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package testutil
import (
"testing"
alloraMath "github.com/allora-network/allora-chain/math"
require "github.com/stretchr/testify/require"
)
func InEpsilon(t *testing.T, value alloraMath.Dec, target string, epsilon string) {
epsilonDec := alloraMath.MustNewDecFromString(epsilon)
targetDec := alloraMath.MustNewDecFromString(target)
one := alloraMath.MustNewDecFromString("1")
lowerMultiplier, err := one.Sub(epsilonDec)
require.NoError(t, err)
lowerBound, err := targetDec.Mul(lowerMultiplier)
require.NoError(t, err)
upperMultiplier, err := one.Add(epsilonDec)
require.NoError(t, err)
upperBound, err := targetDec.Mul(upperMultiplier)
require.NoError(t, err)
if lowerBound.Lt(upperBound) { // positive values, lower < value < upper
require.True(t, value.Gte(lowerBound), "value: %s, lowerBound: %s", value.String(), lowerBound.String())
require.True(t, value.Lte(upperBound), "value: %s, upperBound: %s", value.String(), upperBound.String())
} else { // negative values, upper < value < lower
require.True(t, value.Lte(lowerBound), "value: %s, lowerBound: %s", value.String(), lowerBound.String())
require.True(t, value.Gte(upperBound), "value: %s, upperBound: %s", value.String(), upperBound.String())
}
}
func InEpsilon2(t *testing.T, value alloraMath.Dec, target string) {
InEpsilon(t, value, target, "0.01")
}
func InEpsilon3(t *testing.T, value alloraMath.Dec, target string) {
InEpsilon(t, value, target, "0.001")
}
/*unused
func InEpsilon4(t *testing.T, value alloraMath.Dec, target string) {
s.inEpsilon(t, value, target, "0.0001")
}*/
func InEpsilon5(t *testing.T, value alloraMath.Dec, target string) {
InEpsilon(t, value, target, "0.00001")
}