github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/exec/topn_test.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache 2.0 3 // license that can be found in the LICENSE file. 4 5 package exec 6 7 import ( 8 "reflect" 9 "sort" 10 "testing" 11 12 fuzz "github.com/google/gofuzz" 13 ) 14 15 func TestTopN(t *testing.T) { 16 const ( 17 min, max = 50, 100 18 n = 15 19 ) 20 fz := fuzz.New() 21 fz.NilChance(0) 22 fz.NumElements(min, max) 23 var counts []int 24 fz.Fuzz(&counts) 25 sorted := make([]int, len(counts)) 26 copy(sorted, counts) 27 sort.Ints(sorted) 28 sorted = sorted[len(sorted)-n:] 29 topIndices := topn(counts, n) 30 top := make([]int, n) 31 for i, j := range topIndices { 32 top[i] = counts[j] 33 } 34 sort.Ints(top) 35 if got, want := top, sorted; !reflect.DeepEqual(got, want) { 36 t.Errorf("got %v, want %v", got, want) 37 } 38 }