github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/sorts/rdxsort/sort_test.go (about)

     1  package rdxsort_test
     2  
     3  import (
     4  	"math/rand"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/egonelbre/exp/sorts/rdxsort"
     9  )
    10  
    11  func TestSort(t *testing.T) {
    12  	var data = []uint64{2, 1}
    13  	buf := make([]uint64, len(data))
    14  	rdxsort.Uint64(data, buf)
    15  	if !isSorted(data) {
    16  		t.Errorf("   got %v", data)
    17  	}
    18  }
    19  
    20  func TestRandom(t *testing.T) {
    21  	for _, size := range []int{1, 2, 3, 4, 10, 32, 64, 100, 1000, 10000} {
    22  		for k := 0; k < 10; k++ {
    23  			data := make([]uint64, size)
    24  			for i := range data {
    25  				data[i] = rand.Uint64()
    26  			}
    27  
    28  			rdxsort.Uint64(data, make([]uint64, size))
    29  			if !isSorted(data) {
    30  				t.Errorf("   got %v", data)
    31  			}
    32  		}
    33  	}
    34  }
    35  
    36  func isSorted(vs []uint64) bool {
    37  	return sort.SliceIsSorted(vs, func(i, k int) bool { return vs[i] < vs[k] })
    38  }