|
| 1 | +// Copyright (c) 2015, Peter Mrekaj. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE.txt file. |
| 4 | + |
| 5 | +package sorting |
| 6 | + |
| 7 | +import ( |
| 8 | + "math/rand" |
| 9 | + "reflect" |
| 10 | + "sort" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestIntersectSorted(t *testing.T) { |
| 15 | + for _, test := range []struct { |
| 16 | + x, y []int |
| 17 | + want []int |
| 18 | + }{ |
| 19 | + {[]int(nil), []int(nil), []int(nil)}, |
| 20 | + {[]int{0}, []int(nil), []int(nil)}, |
| 21 | + {[]int(nil), []int{0}, []int(nil)}, |
| 22 | + {[]int{0}, []int{1}, []int(nil)}, |
| 23 | + {[]int{0}, []int{0}, []int{0}}, |
| 24 | + {[]int{0, 1}, []int{2}, []int(nil)}, |
| 25 | + {[]int{0}, []int{0, 1}, []int{0}}, |
| 26 | + {[]int{0, 1}, []int{1}, []int{1}}, |
| 27 | + {[]int{0, 1}, []int{1, 2}, []int{1}}, |
| 28 | + {[]int{1, 1, 1}, []int{1, 1}, []int{1}}, |
| 29 | + {[]int{1, 1}, []int{1, 1, 1}, []int{1}}, |
| 30 | + {[]int{1, 1, 2, 3, 4}, []int{0, 0, 1}, []int{1}}, |
| 31 | + {[]int{0, 0, 1}, []int{1, 1, 2, 3, 4}, []int{1}}, |
| 32 | + {[]int{2, 3, 3, 5, 5, 6, 7, 7, 8, 12}, []int{5, 5, 6, 8, 8, 9, 10, 10}, []int{5, 6, 8}}, |
| 33 | + } { |
| 34 | + if got := IntersectSorted(test.x, test.y); !reflect.DeepEqual(got, test.want) { |
| 35 | + t.Errorf("IntersectSorted(%v, %v) = %v; want %v", test.x, test.y, got, test.want) |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func benchIntersectSorted(b *testing.B, size int) { |
| 41 | + b.StopTimer() |
| 42 | + x := rand.New(rand.NewSource(int64(size))).Perm(size) |
| 43 | + sort.Ints(x) |
| 44 | + y := x[size-(size/2):] |
| 45 | + b.StartTimer() |
| 46 | + for i := 0; i < b.N; i++ { |
| 47 | + IntersectSorted(x, y) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func BenchmarkIntersectSorted1e2(b *testing.B) { benchIntersectSorted(b, 1e2) } |
| 52 | +func BenchmarkIntersectSorted1e4(b *testing.B) { benchIntersectSorted(b, 1e4) } |
| 53 | +func BenchmarkIntersectSorted1e6(b *testing.B) { benchIntersectSorted(b, 1e6) } |
0 commit comments