Exemple #1
0
func TestLinear(t *testing.T) {
	l := Linear{Min: -10, Max: 10}
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			-20: -0.5,
			-10: 0,
			0:   0.5,
			10:  1,
			20:  1.5,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			-0.5: -20,
			0:    -10,
			0.5:  0,
			1:    10,
			1.5:  20,
		})

	l.SetClamp(true)
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			-20: 0,
			-10: 0,
			0:   0.5,
			10:  1,
			20:  1,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			0:   -10,
			0.5: 0,
			1:   10,
		})

	l = Linear{Min: 5, Max: 5}
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			-10: 0.5,
			0:   0.5,
			10:  0.5,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			0:   5,
			0.5: 5,
			1:   5,
		})
}
Exemple #2
0
func TestLog(t *testing.T) {
	l, err := NewLog(0, 10, 10)
	if _, ok := err.(RangeErr); !ok {
		t.Errorf("want RangeErr; got %v", err)
	}
	l, err = NewLog(-10, 0, 10)
	if _, ok := err.(RangeErr); !ok {
		t.Errorf("want RangeErr; got %v", err)
	}
	l, err = NewLog(-10, 10, 10)
	if _, ok := err.(RangeErr); !ok {
		t.Errorf("want RangeErr; got %v", err)
	}
	l, err = NewLog(10, 20, 0)
	if _, ok := err.(RangeErr); !ok {
		t.Errorf("want RangeErr; got %v", err)
	}

	l, _ = NewLog(1, 10, 10)
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			-1:                math.NaN(),
			0:                 math.NaN(),
			0.1:               -1,
			1:                 0,
			math.Pow(10, 0.5): 0.5,
			10:                1,
			100:               2,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			-1:  0.1,
			0:   1,
			0.5: math.Pow(10, 0.5),
			1:   10,
			2:   100,
		})

	l.SetClamp(true)
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			-1:                math.NaN(),
			0:                 math.NaN(),
			0.1:               0,
			1:                 0,
			math.Pow(10, 0.5): 0.5,
			10:                1,
			100:               1,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			0:   1,
			0.5: math.Pow(10, 0.5),
			1:   10,
		})

	l, _ = NewLog(-1, -10, 10)
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			1:                  math.NaN(),
			0:                  math.NaN(),
			-0.1:               2,
			-1:                 1,
			-math.Pow(10, 0.5): 0.5,
			-10:                0,
			-100:               -1,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			2:   -0.1,
			1:   -1,
			0.5: -math.Pow(10, 0.5),
			0:   -10,
			-1:  -100,
		})

	l, _ = NewLog(5, 5, 10)
	mathtest.WantFunc(t, fmt.Sprintf("%v.Map", l), l.Map,
		map[float64]float64{
			-1: math.NaN(),
			0:  math.NaN(),
			1:  0.5,
			10: 0.5,
		})
	mathtest.WantFunc(t, fmt.Sprintf("%v.Unmap", l), l.Unmap,
		map[float64]float64{
			0:   5,
			0.5: 5,
			1:   5,
		})
}