aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/stat/sample/sample_test.go
blob: ac7845ccff547e946a4790279ae8774bcbf570f9 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2021 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package sample

import (
	"reflect"
	"testing"
)

func TestMedian(t *testing.T) {
	tests := []struct {
		input     []float64
		minMedian float64
		maxMedian float64
	}{
		{
			input:     []float64{1, 2, 3},
			minMedian: 1.99, // we cannot do exact floating point equality comparison
			maxMedian: 2.01,
		},
		{
			input:     []float64{0, 1, 2, 3},
			minMedian: 1.0,
			maxMedian: 2.0,
		},
	}
	for _, test := range tests {
		sample := Sample{Xs: test.input}
		median := sample.Median()
		if median < test.minMedian || median > test.maxMedian {
			t.Errorf("sample %v, median got %v, median expected [%v;%v]",
				test.input, median, test.minMedian, test.maxMedian)
		}
	}
}

func TestRemoveOutliers(t *testing.T) {
	// Some tests just to check the overall sanity of the method.
	tests := []struct {
		input  []float64
		output []float64
	}{
		{
			input:  []float64{-20, 1, 2, 3, 4, 5},
			output: []float64{1, 2, 3, 4, 5},
		},
		{
			input:  []float64{1, 2, 3, 4, 25},
			output: []float64{1, 2, 3, 4},
		},
		{
			input:  []float64{-10, -5, 0, 5, 10, 15},
			output: []float64{-10, -5, 0, 5, 10, 15},
		},
	}
	for _, test := range tests {
		sample := Sample{Xs: test.input}
		result := sample.RemoveOutliers()
		result.Sort()
		if !reflect.DeepEqual(result.Xs, test.output) {
			t.Errorf("input: %v, expected no outliers: %v, got: %v",
				test.input, test.output, result.Xs)
		}
	}
}