github.com/haskelladdict/lizard@v0.0.0-20140131031806-9be25fb5a139/statistic/statistic_test.go (about) 1 // Copyright 2014 Markus Dittrich. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 // 5 // Package statistic provides functions for computing standard 6 // statistic properties (mean, std, ...) for a slice of floats 7 // 8 // NOTE: File processing is done via goroutines using a nummber of 9 // workers 10 package statistic 11 12 import ( 13 "math" 14 "testing" 15 ) 16 17 18 // Tests for mean and standard deviation 19 func Test_Average_1(t *testing.T) { 20 21 data_file_1 := "test_files/test_data_1.txt" 22 result_1 := Statistic([]string{data_file_1}, 0, true, 4) 23 s1_1 := stat{data_file_1, 5.5, 9.166666666666666, 5.5} 24 expected_1 := []stat{s1_1} 25 if !stat_equal(result_1, expected_1) { 26 t.Error("Statistic test 1 failed") 27 } 28 29 data_file_2 := "test_files/test_data_2.txt" 30 result_2 := Statistic([]string{data_file_2}, 0, true, 4) 31 s1_2 := stat{data_file_2, 0.41319134487140002, 0.082911176230414732, 32 0.337045349500000} 33 expected_2 := []stat{s1_2} 34 if !stat_equal(result_2, expected_2) { 35 t.Error("Statistic test 2 failed") 36 } 37 38 data_file_3 := "test_files/test_data_3.txt" 39 result_3 := Statistic([]string{data_file_3}, 1, true, 4) 40 s1_3 := stat{data_file_3, 0.49905688017419975, 0.083507191091550331, 41 0.498817626000000} 42 expected_3 := []stat{s1_3} 43 if !stat_equal(result_3, expected_3) { 44 t.Error("Statistic test 3 failed") 45 } 46 } 47 48 49 // Benchmarks 50 func Benchmark_Average(t *testing.B) { 51 52 data_file_3 := "test_files/test_data_3.txt" 53 Statistic([]string{data_file_3}, 1, true, 4) 54 } 55 56 57 // Support Functions 58 // 59 // stat_equal compares the entries of a slice of stat structures 60 // returned from a call to Average with a reference slice stat structure 61 func stat_equal(s1, s2 []stat) bool { 62 63 if len(s1) != len(s2) { 64 return false 65 } 66 67 status := true 68 for i := 0; i < len(s1); i++ { 69 if s1[i].Name != s2[i].Name { 70 status = false 71 } 72 73 if !float_equal(s1[i].Mean, s2[i].Mean) { 74 status = false 75 } 76 77 if !float_equal(s1[i].Variance, s2[i].Variance) { 78 status = false 79 } 80 81 if !float_equal(s1[i].Median, s2[i].Median) { 82 status = false 83 } 84 85 } 86 87 return status 88 } 89 90 91 92 // float_array_equal compares two float numbers for equality 93 // NOTE: the floating point comparison is based on an epsilon 94 // which was chosen empirically so its not rigorous 95 func float_equal(a1, a2 float64) bool { 96 epsilon := 1e-13 97 if math.Abs(a2-a1) > epsilon * math.Abs(a1) { 98 return false 99 } 100 return true 101 } 102