github.com/puellanivis/breton@v0.2.16/lib/sort/radix_test.go (about)

     1  package sort
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFloat64s(t *testing.T) {
     8  	qsortInstead = qsortNever
     9  
    10  	l := []float64{42.37, 5.3, -7.5, 2, 3, 0.5, -6, 100000, 0}
    11  
    12  	if Float64sAreSorted(l) {
    13  		t.Error("unsorted float64 list reports as sorted")
    14  	}
    15  
    16  	radix(Float64Slice(l))
    17  
    18  	if !Float64sAreSorted(l) {
    19  		t.Error("after sorting float64 list reports as not sorted")
    20  		t.Log("Got:", l)
    21  	}
    22  
    23  	if SearchFloat64s(l, 42.37) != 7 {
    24  		t.Error("binary search failed for float64 list")
    25  	}
    26  }
    27  
    28  func TestFloat32s(t *testing.T) {
    29  	qsortInstead = qsortNever
    30  
    31  	l := []float32{42.37, 5.3, -7.5, 2, 3, 0.5, -6, 100000, 0}
    32  
    33  	if Float32sAreSorted(l) {
    34  		t.Error("unsorted float32 list reports as sorted")
    35  	}
    36  
    37  	radix(Float32Slice(l))
    38  
    39  	if !Float32sAreSorted(l) {
    40  		t.Error("after sorting float32 list reports as not sorted")
    41  		t.Log("Got:", l)
    42  	}
    43  
    44  	if SearchFloat32s(l, 42.37) != 7 {
    45  		t.Error("binary search failed for float32 list")
    46  	}
    47  }
    48  
    49  func TestUints(t *testing.T) {
    50  	qsortInstead = qsortNever
    51  
    52  	l := []uint{42, 5, 7, 2, 3}
    53  
    54  	if UintsAreSorted(l) {
    55  		t.Error("unsorted uint list reports as sorted")
    56  	}
    57  
    58  	radix(UintSlice(l))
    59  
    60  	if !UintsAreSorted(l) {
    61  		t.Error("after sorting uint list reports as not sorted")
    62  		t.Log(l)
    63  	}
    64  
    65  	if SearchUints(l, 42) != 4 {
    66  		t.Error("binary search failed for uint list")
    67  	}
    68  }
    69  
    70  func TestInts(t *testing.T) {
    71  	qsortInstead = qsortNever
    72  
    73  	l := []int{42, 5, -7, -2, 3}
    74  
    75  	if IntsAreSorted(l) {
    76  		t.Error("unsorted int list reports as sorted")
    77  	}
    78  
    79  	radix(IntSlice(l))
    80  
    81  	if !IntsAreSorted(l) {
    82  		t.Error("after sorting int list reports as not sorted")
    83  		t.Log(l)
    84  	}
    85  
    86  	if SearchInts(l, 42) != 4 {
    87  		t.Error("binary search failed for int list")
    88  	}
    89  }
    90  
    91  func TestStrings(t *testing.T) {
    92  	qsortInstead = qsortNever
    93  
    94  	l := []string{"zomg", "stuff", "things", "blah", "asdf"}
    95  
    96  	if StringsAreSorted(l) {
    97  		t.Error("unsorted string list reports as sorted")
    98  	}
    99  
   100  	radix(StringSlice(l))
   101  
   102  	if !StringsAreSorted(l) {
   103  		t.Error("after sorting string list reports as not sorted")
   104  		t.Log(l)
   105  	}
   106  
   107  	if SearchStrings(l, "zomg") != 4 {
   108  		t.Error("binary search failed for string list")
   109  	}
   110  }