-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslowsort_test.go
107 lines (95 loc) · 2.36 KB
/
slowsort_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package sortof
import (
"context"
"fmt"
"math"
"slices"
"testing"
"time"
)
func TestSlowsortFloat(t *testing.T) {
ctx := context.Background()
testcases := [][]float64{
{1, 2, 3},
{math.MaxFloat64, 2, 0, -1, math.SmallestNonzeroFloat64, math.Log(-1)},
}
for _, tc := range testcases {
tc := tc
t.Run(fmt.Sprint(tc), func(t *testing.T) {
t.Parallel()
collection := slices.Clone(tc)
err := Slowsort(ctx, collection)
if err != nil {
t.Errorf("Slowsort(%v, %v) returns error: %v", ctx, tc, err)
}
if !slices.IsSorted(collection) {
want := slices.Clone(tc)
slices.Sort(want)
t.Errorf("Slowsort(%v, %v) cannot sort; got %v, want %v", ctx, tc, collection, want)
}
})
}
}
func TestSlowsortInt(t *testing.T) {
ctx := context.Background()
testcases := [][]int{
{1, 2, 3},
{math.MaxInt, 0, math.MinInt},
}
for _, tc := range testcases {
tc := tc
t.Run(fmt.Sprint(tc), func(t *testing.T) {
t.Parallel()
collection := slices.Clone(tc)
err := Slowsort(ctx, collection)
if err != nil {
t.Errorf("Slowsort(%v, %v) returns error: %v", ctx, tc, err)
}
if !slices.IsSorted(collection) {
want := slices.Clone(tc)
slices.Sort(want)
t.Errorf("Slowsort(%v, %v) cannot sort; got %v, want %v", ctx, tc, collection, want)
}
})
}
}
func TestSlowsortString(t *testing.T) {
ctx := context.Background()
testcases := [][]string{
{"1", "2", "3"},
{"100", "2", "0", "-1"},
{"1", "a", "."},
{"a", "", "b"},
}
for _, tc := range testcases {
tc := tc
t.Run(fmt.Sprint(tc), func(t *testing.T) {
t.Parallel()
collection := slices.Clone(tc)
err := Slowsort(ctx, collection)
if err != nil {
t.Errorf("Slowsort(%v, %v) returns error: %v", ctx, tc, err)
}
if !slices.IsSorted(collection) {
want := slices.Clone(tc)
slices.Sort(want)
t.Errorf("Slowsort(%v, %v) cannot sort; got %v, want %v", ctx, tc, collection, want)
}
})
}
}
func TestSlowsortCancellation(t *testing.T) {
const testCaseSize = 10000
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
tc := make([]int, testCaseSize)
want := make([]int, testCaseSize)
for i := 0; i < testCaseSize; i++ {
tc[i] = testCaseSize - i
want[i] = i
}
err := Slowsort(ctx, tc)
if err != context.DeadlineExceeded {
t.Errorf("Slowsort(ctx, tc) returned unexpected error: %v", err)
}
}