MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / TestMedian

Function TestMedian

math/median_test.go:13–56  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestMedian(t *testing.T) {
14 testCases := []struct {
15 name string
16 testValues []float64
17 answer float64
18 }{
19
20 {
21 name: "Series of numbers in ascending order",
22 testValues: []float64{12, 14, 16, 18, 19},
23 answer: 16,
24 },
25
26 {
27 name: "Series of numbers in random order",
28 testValues: []float64{21, 10, 22, 33, 11, 88},
29 answer: 21.5,
30 },
31
32 {
33 name: "Series of decimals in random order",
34 testValues: []float64{11.2, 32.5, 2.5, 37.8, 21.8, 5.2},
35 answer: 16.5,
36 },
37
38 {
39 testValues: []float64{},
40 },
41 }
42
43 for _, test := range testCases {
44 t.Run(test.name, func(t *testing.T) {
45
46 returnedMedian := math.Median(test.testValues)
47
48 t.Log(test.testValues, " ", returnedMedian)
49
50 if returnedMedian != test.answer {
51 t.Errorf("Test failed. Median should have been %v but received %v",
52 test.answer, returnedMedian)
53 }
54 })
55 }
56}

Callers

nothing calls this directly

Calls 1

MedianFunction · 0.92

Tested by

no test coverage detected