github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/stats/sample_test.go (about)

     1  // Copyright 2021 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package stats
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestMedian(t *testing.T) {
    12  	tests := []struct {
    13  		input     []float64
    14  		minMedian float64
    15  		maxMedian float64
    16  	}{
    17  		{
    18  			input:     []float64{1, 2, 3},
    19  			minMedian: 1.99, // we cannot do exact floating point equality comparison
    20  			maxMedian: 2.01,
    21  		},
    22  		{
    23  			input:     []float64{0, 1, 2, 3},
    24  			minMedian: 1.0,
    25  			maxMedian: 2.0,
    26  		},
    27  	}
    28  	for _, test := range tests {
    29  		sample := Sample{Xs: test.input}
    30  		median := sample.Median()
    31  		if median < test.minMedian || median > test.maxMedian {
    32  			t.Errorf("sample %v, median got %v, median expected [%v;%v]",
    33  				test.input, median, test.minMedian, test.maxMedian)
    34  		}
    35  	}
    36  }
    37  
    38  func TestRemoveOutliers(t *testing.T) {
    39  	// Some tests just to check the overall sanity of the method.
    40  	tests := []struct {
    41  		input  []float64
    42  		output []float64
    43  	}{
    44  		{
    45  			input:  []float64{-20, 1, 2, 3, 4, 5},
    46  			output: []float64{1, 2, 3, 4, 5},
    47  		},
    48  		{
    49  			input:  []float64{1, 2, 3, 4, 25},
    50  			output: []float64{1, 2, 3, 4},
    51  		},
    52  		{
    53  			input:  []float64{-10, -5, 0, 5, 10, 15},
    54  			output: []float64{-10, -5, 0, 5, 10, 15},
    55  		},
    56  	}
    57  	for _, test := range tests {
    58  		sample := Sample{Xs: test.input}
    59  		result := sample.RemoveOutliers()
    60  		result.Sort()
    61  		if !reflect.DeepEqual(result.Xs, test.output) {
    62  			t.Errorf("input: %v, expected no outliers: %v, got: %v",
    63  				test.input, test.output, result.Xs)
    64  		}
    65  	}
    66  }