-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort_test.go
64 lines (61 loc) · 2.2 KB
/
mergesort_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"reflect"
"sort"
"testing"
)
func TestMerge(t *testing.T) {
tests := []struct {
name string
inLeft []int
inRight []int
wantSorted []int
wantInversions int
}{
{name: "empty", inLeft: []int{}, inRight: []int{}, wantSorted: []int{}, wantInversions: 0},
{name: "two single", inLeft: []int{0}, inRight: []int{1}, wantSorted: []int{0, 1}, wantInversions: 0},
{name: "single inverted", inLeft: []int{1}, inRight: []int{0}, wantSorted: []int{0, 1}, wantInversions: 1},
{name: "single inverted", inLeft: []int{1, 1}, inRight: []int{0, 0}, wantSorted: []int{0, 0, 1, 1}, wantInversions: 4},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gotSorted, gotInversions := merge(test.inLeft, test.inRight)
if len(gotSorted) != len(test.wantSorted) {
t.Errorf("wrong length. got %v, wanted %v", len(gotSorted), len(test.wantSorted))
}
if len(test.wantSorted) > 0 && !reflect.DeepEqual(gotSorted, test.wantSorted) {
t.Errorf("wrong array. got %v, wanted %v", gotSorted, test.wantSorted)
}
if gotInversions != test.wantInversions {
t.Errorf("wrong num inversions. got %v, wanted %v", gotInversions, test.wantInversions)
}
})
}
}
func TestMergeSort(t *testing.T) {
tests := []struct {
name string
input []int
wantInversions int
}{
{name: "empty", input: []int{}, wantInversions: 0},
{name: "two single", input: []int{0}, wantInversions: 0},
{name: "single", input: []int{1}, wantInversions: 0},
{name: "2 same", input: []int{1, 1}, wantInversions: 0},
{name: "1 inv", input: []int{1, 0}, wantInversions: 1},
{name: "4 inv", input: []int{2, 1, 3, 1, 2}, wantInversions: 4},
{name: "4 inv", input: []int{5, 4, 3, 2, 1, 0}, wantInversions: 15},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gotSorted, gotInversions := MergeSort(test.input)
sort.Ints(test.input)
if len(test.input) > 0 && !reflect.DeepEqual(gotSorted, test.input) {
t.Errorf("wrong sorting. got %v, wanted %v", gotSorted, test.input)
}
if gotInversions != test.wantInversions {
t.Errorf("wrong num inversions. got %v, wanted %v", gotInversions, test.wantInversions)
}
})
}
}