Skip to content

Commit

Permalink
tests: add tests sample.go
Browse files Browse the repository at this point in the history
Signed-off-by: Manik Rana <[email protected]>
  • Loading branch information
Maniktherana committed Jan 27, 2024
1 parent 9fb4a78 commit bc938fd
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions go/mathstats/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ func TestSamplePercentile(t *testing.T) {
})
}

func TestSamplePercentileEmpty(t *testing.T) {
s := Sample{Xs: []float64{}}
assert.True(t, math.IsNaN(s.Percentile(0.5)), "Percentile should return NaN for empty sample")
}

func TestSampleStdDev(t *testing.T) {
values := []float64{2, 4, 4, 4, 5, 5, 7, 9}
expected := 2.138089935299395

sample := Sample{Xs: values}
result := sample.StdDev()

assert.Equal(t, expected, result)
}

func TestBounds(t *testing.T) {
tt := []struct {
xs []float64
Expand Down Expand Up @@ -170,3 +185,45 @@ func TestSampleSort(t *testing.T) {
assert.Equal(t, tc.expected, sortedSample.Xs, "Sorted values mismatch")
}
}

func TestGeoMean(t *testing.T) {
tt := []struct {
name string
values []float64
expected float64
}{
{
name: "Valid_case",
values: []float64{2, 4, 8, 16},
expected: 5.65685424949238,
},
{
name: "Empty_values",
values: []float64{},
expected: math.NaN(),
},
{
name: "Zero_value",
values: []float64{1, 0, 3},
expected: math.NaN(),
},
{
name: "Negative_value",
values: []float64{2, -4, 8, 16},
expected: math.NaN(),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
sample := Sample{Xs: tc.values}
result := sample.GeoMean()

if math.IsNaN(tc.expected) {
assert.True(t, math.IsNaN(result))
} else {
assert.Equal(t, tc.expected, result)
}
})
}
}

0 comments on commit bc938fd

Please sign in to comment.