|
| 1 | +package perfcomp |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "gonum.org/v1/gonum/mat" |
| 8 | +) |
| 9 | + |
| 10 | +func createTestVectors(start1 int, stop1 int, step1 int, start2 int, stop2 int, step2 int) (*mat.Dense, *mat.Dense) { |
| 11 | + xData := []float64{} |
| 12 | + yData := []float64{} |
| 13 | + |
| 14 | + for i := start1; i < stop1; i += step1 { |
| 15 | + xData = append(xData, float64(i)) |
| 16 | + } |
| 17 | + for j := start2; j < stop2; j += step2 { |
| 18 | + yData = append(yData, float64(j)) |
| 19 | + } |
| 20 | + |
| 21 | + x := mat.NewDense(len(xData), 1, xData) |
| 22 | + y := mat.NewDense(len(yData), 1, yData) |
| 23 | + |
| 24 | + return x, y |
| 25 | +} |
| 26 | + |
| 27 | +// TestCalcEnergyStatistics verifies that the energy calculation algorithms are correct. |
| 28 | +func TestCalcEnergyStatistics(t *testing.T) { |
| 29 | + t.Run("similar distributions should have small e,t,h values ", func(t *testing.T) { |
| 30 | + x, y := createTestVectors(1, 100, 1, 1, 105, 1) |
| 31 | + e, tstat, h, _ := calcEnergyStatistics(x, y) |
| 32 | + |
| 33 | + del := 1e-3 |
| 34 | + // Limit precision of comparison to 3 digits after the decimal. |
| 35 | + assert.InDelta(t, 0.160, e, del) // |0.160 - e| < 0.001 |
| 36 | + assert.InDelta(t, 8.136, tstat, del) |
| 37 | + assert.InDelta(t, 0.002, h, del) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("different distributions should have large e,t,h values", func(t *testing.T) { |
| 41 | + x, y := createTestVectors(1, 100, 1, 10000, 13000, 14) |
| 42 | + e, tstat, h, _ := calcEnergyStatistics(x, y) |
| 43 | + del := 1e-3 |
| 44 | + |
| 45 | + assert.InDelta(t, 21859.691, e, del) |
| 46 | + assert.InDelta(t, 1481794.709, tstat, del) |
| 47 | + assert.InDelta(t, 0.954, h, del) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("uni-variate distributions", func(t *testing.T) { |
| 51 | + x, y := createTestVectors(1, 300, 1, 1000, 5000, 10) |
| 52 | + e, tstat, h, _ := calcEnergyStatistics(x, y) |
| 53 | + del := 1e-3 |
| 54 | + |
| 55 | + assert.InDelta(t, 4257.009, e, del) |
| 56 | + assert.InDelta(t, 728381.015, tstat, del) |
| 57 | + assert.InDelta(t, 0.748, h, del) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("equal distributions should have all 0 values", func(t *testing.T) { |
| 61 | + x := mat.NewDense(10, 1, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) |
| 62 | + y := mat.NewDense(1, 1, []float64{1}) |
| 63 | + |
| 64 | + e, tstat, h, _ := calcEnergyStatistics(x, y) |
| 65 | + |
| 66 | + assert.Equal(t, 0.0, e) |
| 67 | + assert.Equal(t, 0.0, tstat) |
| 68 | + assert.Equal(t, 0.0, h) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("energy stats returns errors on malformed input", func(t *testing.T) { |
| 72 | + x := mat.NewDense(2, 2, make([]float64, 4)) |
| 73 | + y := mat.NewDense(2, 3, make([]float64, 6)) |
| 74 | + |
| 75 | + _, _, _, err := calcEnergyStatistics(x, y) |
| 76 | + assert.NotEqual(t, nil, err) |
| 77 | + assert.ErrorContains(t, err, "both inputs must have the same number of columns") |
| 78 | + |
| 79 | + x.Reset() |
| 80 | + y = &mat.Dense{} |
| 81 | + |
| 82 | + _, _, _, err = calcEnergyStatistics(x, y) |
| 83 | + assert.NotEqual(t, nil, err) |
| 84 | + assert.ErrorContains(t, err, "inputs cannot be empty") |
| 85 | + |
| 86 | + x = mat.NewDense(2, 2, make([]float64, 4)) |
| 87 | + y = mat.NewDense(3, 2, make([]float64, 6)) |
| 88 | + |
| 89 | + _, _, _, err = calcEnergyStatistics(x, y) |
| 90 | + assert.NotEqual(t, nil, err) |
| 91 | + assert.ErrorContains(t, err, "both inputs must be column vectors") |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +// TestFindSigBenchmarks tests that statistically significant benchmarks are correctly flagged. |
| 96 | +func TestFindSigBenchmarks(t *testing.T) { |
| 97 | + var dummyEnergyStats []*EnergyStats |
| 98 | + assert.Equal(t, 0, len(getStatSigBenchmarks(dummyEnergyStats))) |
| 99 | + |
| 100 | + for i := -2.5; i < 3; i += 0.5 { |
| 101 | + es := EnergyStats{ |
| 102 | + ZScore: i, |
| 103 | + } |
| 104 | + dummyEnergyStats = append(dummyEnergyStats, &es) |
| 105 | + } |
| 106 | + assert.Equal(t, 11, len(dummyEnergyStats)) |
| 107 | + |
| 108 | + sigBenchmarks := getStatSigBenchmarks(dummyEnergyStats) |
| 109 | + assert.Equal(t, 4, len(sigBenchmarks)) |
| 110 | +} |
0 commit comments