Пример #1
0
func TestMaxSpecialValues(t *testing.T) {
	testArray := []float64{0.0, math.NaN(), math.Inf(-1), math.Inf(0)}
	max := NewMax()
	if !math.IsNaN(max.GetResult()) {
		t.Error("Max: it should be NaN")
	}

	max.Increment(testArray[0])
	if !assert.EqualFloat64(max.GetResult(), 0.0, 1e-10, 1) {
		t.Errorf("Max: result: %f, but expect: %f", max.GetResult(), 0.0)
	}

	max.Increment(testArray[1])
	if !assert.EqualFloat64(max.GetResult(), 0.0, 1e-10, 1) {
		t.Errorf("Max: result: %f, but expect: %f", max.GetResult(), 0.0)
	}

	max.Increment(testArray[2])
	if !assert.EqualFloat64(max.GetResult(), 0.0, 1e-10, 1) {
		t.Errorf("Max: result: %f, but expect: %f", max.GetResult(), 0.0)
	}

	max.Increment(testArray[3])
	if !assert.EqualFloat64(max.GetResult(), math.Inf(0), 1e-10, 1) {
		t.Errorf("Max: result: %f, but expect: %f", max.GetResult(), math.Inf(0))
	}

	maxV := EvaluateMax(testArray, 0, len(testArray))
	if !assert.EqualFloat64(maxV, math.Inf(0), 1e-10, 1) {
		t.Errorf("Max: result: %f, but expect: %f", maxV, math.Inf(0))
	}
}
Пример #2
0
func TestSumSpecialValues(t *testing.T) {
	sum := NewSum()
	if !assert.EqualFloat64(0.0, sum.GetResult(), 1e-10, 1) {
		t.Errorf("Sum: result: %f, but expect: %f\n", sum.GetResult(), 0.0)
	}

	sum.Increment(1.0)
	if !assert.EqualFloat64(1.0, sum.GetResult(), 1e-10, 1) {
		t.Errorf("Sum: result: %f, but expect: %f\n", sum.GetResult(), 1.0)
	}

	sum.Increment(math.Inf(0))
	if !assert.EqualFloat64(math.Inf(0), sum.GetResult(), 1e-10, 1) {
		t.Errorf("Sum: result: %f, but expect: %f\n", sum.GetResult(), math.Inf(0))
	}

	sum.Increment(math.Inf(-1))
	if !math.IsNaN(sum.GetResult()) {
		t.Error("Sum: result should be NaN")
	}

	sum.Increment(1.0)
	if !math.IsNaN(sum.GetResult()) {
		t.Error("Sum: result should be NaN")
	}
}
Пример #3
0
func TestMinSpecialValues(t *testing.T) {
	min := NewMin()
	if !math.IsNaN(min.GetResult()) {
		t.Error("Min: the result should be NaN")
	}
	if min.GetN() != 0 {
		t.Error("Min: the N of Min should be 0")
	}

	testArray := []float64{0.0, math.NaN(), math.Inf(0), math.Inf(-1)}
	min.Increment(testArray[0])
	if !assert.EqualFloat64(min.GetResult(), 0.0, 1e-10, 1) {
		t.Errorf("Min: result: %f, but expect: %f", min.GetResult(), 0.0)
	}

	min.Increment(testArray[1])
	if !assert.EqualFloat64(min.GetResult(), 0.0, 1e-10, 1) {
		t.Errorf("Min: result: %f, but expect: %f", min.GetResult(), 0.0)
	}

	min.Increment(testArray[2])
	if !assert.EqualFloat64(min.GetResult(), 0.0, 1e-10, 1) {
		t.Errorf("Min: result: %f, but expect: %f", min.GetResult(), 0.0)
	}

	min.Increment(testArray[3])
	if !assert.EqualFloat64(min.GetResult(), math.Inf(-1), 1e-10, 1) {
		t.Errorf("Min: result: %f, but expect: %f", min.GetResult(), math.Inf(-1))
	}

	if min.GetN() != len(testArray) {
		t.Errorf("Min: N: %d, but expect: %d", min.GetN(), len(testArray))
	}

	minV := EvaluateMin(testArray, 0, len(testArray))
	if !assert.EqualFloat64(minV, math.Inf(-1), 1e-10, 1) {
		t.Errorf("Min: result: %f, but expect: %f", minV, math.Inf(-1))
	}
}
Пример #4
0
func TestIncrement(t *testing.T) {
	var (
		result    float64
		expected  float64
		tolerance float64
		statistic StorelessUnivariateStatistic
	)
	tolerance = 1e-10
	// FIRST MOMENT
	statistic = NewFirstMoment()
	expected = mean
	for i := 0; i < len(testArray); i++ {
		statistic.Increment(testArray[i])
	}
	result = statistic.GetResult()
	if !assert.EqualFloat64(result, expected, tolerance, 1) {
		t.Errorf("FirstMoment Increment, result: %.10f, expected: %.10f\n", result, expected)
	}

	// Mean
	statistic = NewMean()
	expected = mean
	for i := 0; i < len(testArray); i++ {
		statistic.Increment(testArray[i])
	}
	result = statistic.GetResult()
	if !assert.EqualFloat64(result, expected, tolerance, 1) {
		t.Errorf("FirstMoment Increment, result: %.10f, expected: %.10f\n", result, expected)
	}

	// SECOND MOMENT
	statistic = NewSecondMoment()
	expected = secondMoment
	for i := 0; i < len(testArray); i++ {
		statistic.Increment(testArray[i])
	}
	result = statistic.GetResult()
	if !assert.EqualFloat64(result, expected, tolerance, 1) {
		t.Errorf("SecondMoment Increment, result: %.10f, expected: %.10f\n", result, expected)
	}
	// Variance
	statistic = NewVarianceWithBiasCorrection()
	expected = variance
	for i := 0; i < len(testArray); i++ {
		statistic.Increment(testArray[i])
	}
	result = statistic.GetResult()
	if !assert.EqualFloat64(result, expected, tolerance, 1) {
		t.Errorf("Variance Increment, result:%.10f, expected: %.10f\n", result, expected)
	}
	// StandardDeviation
	statistic = NewStandardDeviationWithBiasCorrection()
	expected = std
	for i := 0; i < len(testArray); i++ {
		statistic.Increment(testArray[i])
	}
	result = statistic.GetResult()
	if !assert.EqualFloat64(result, expected, tolerance, 1) {
		t.Errorf("StandardDeviation Increment, result:%.10f, expected: %.10f\n", result, expected)
	}
}