github.com/jfcg/sorty@v1.2.0/tmain_test.go (about)

     1  // +build !tuneparam
     2  
     3  /*	Copyright (c) 2021, Serhat Şevki Dinçer.
     4  	This Source Code Form is subject to the terms of the Mozilla Public
     5  	License, v. 2.0. If a copy of the MPL was not distributed with this
     6  	file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7  */
     8  
     9  package sorty
    10  
    11  import (
    12  	"fmt"
    13  	"sort"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/jfcg/sixb"
    18  	//"github.com/shawnsmithdev/zermelo/zfloat32"
    19  	//"github.com/shawnsmithdev/zermelo/zuint32"
    20  	//"github.com/twotwotwo/sorts/sortutil"
    21  	//"github.com/yourbasic/radix"
    22  )
    23  
    24  func printSec(tn string, d time.Duration) float64 {
    25  	sec := d.Seconds()
    26  	fmt.Printf("%10s %5.2fs\n", tn, sec)
    27  	return sec
    28  }
    29  
    30  // sort and signal
    31  func sasU8(sd int64, al []uint64, ch chan bool) {
    32  	fstU8(sd, al, SortU8)
    33  	ch <- false
    34  }
    35  
    36  func sasF8(sd int64, al []float64, ch chan bool) {
    37  	fstF8(sd, al, SortF8)
    38  	ch <- false
    39  }
    40  
    41  func sasI4(sd int64, al []int32, ch chan bool) {
    42  	fstI4(sd, al, SortI4)
    43  	ch <- false
    44  }
    45  
    46  func sasI8(sd int64, al []int64, ch chan bool) {
    47  	fstI8(sd, al, SortI8)
    48  	ch <- false
    49  }
    50  
    51  // test & time sorting uint32 slices for different libraries, compare their results
    52  func TestUint(t *testing.T) {
    53  	tsPtr = t
    54  
    55  	fmt.Println("\nSorting []uint32")
    56  	mfcU4("sort.Slice", func(al []uint32) {
    57  		sort.Slice(al, func(i, k int) bool { return al[i] < al[k] })
    58  	}, bufbu, nil)
    59  	//mfcU4("sortutil", sortutil.Uint32s, bufau, bufbu)
    60  	//mfcU4("zermelo", zuint32.Sort, bufau, bufbu)
    61  	sumtU4(bufau, bufbu) // sorty
    62  	sumtLswU4(bufau, bufbu)
    63  
    64  	if IsSorted(len(bufau), func(i, k, r, s int) bool { return bufau[i] < bufau[k] }) != 0 {
    65  		t.Fatal("IsSorted() does not work")
    66  	}
    67  }
    68  
    69  // test & time sorting float32 slices for different libraries, compare their results
    70  func TestFloat(t *testing.T) {
    71  	tsPtr = t
    72  
    73  	fmt.Println("\nSorting []float32")
    74  	mfcF4("sort.Slice", func(al []float32) {
    75  		sort.Slice(al, func(i, k int) bool { return al[i] < al[k] })
    76  	}, bufbf, nil)
    77  	//mfcF4("sortutil", sortutil.Float32s, bufaf, bufbf)
    78  	//mfcF4("zermelo", zfloat32.Sort, bufaf, bufbf)
    79  	sumtF4(bufaf, bufbf) // sorty
    80  	sumtLswF4(bufaf, bufbf)
    81  
    82  	if IsSorted(len(bufaf), func(i, k, r, s int) bool { return bufaf[i] < bufaf[k] }) != 0 {
    83  		t.Fatal("IsSorted() does not work")
    84  	}
    85  }
    86  
    87  // test & time sorting string/[]byte slices for
    88  // different libraries, compare their results
    89  func TestString(t *testing.T) {
    90  	tsPtr = t
    91  
    92  	fmt.Println("\nSorting []string")
    93  	mfcS("sort.Slice", func(al []string) {
    94  		sort.Slice(al, func(i, k int) bool { return al[i] < al[k] })
    95  	}, bufbu, nil)
    96  	//mfcS("sortutil", sortutil.Strings, bufau, bufbu)
    97  	//mfcS("radix", radix.Sort, bufau, bufbu)
    98  	sumtS(bufau, bufbu) // sorty
    99  	sumtLswS(bufau, bufbu)
   100  
   101  	fmt.Println("\nSorting [][]byte")
   102  	mfcB("sort.Slice", func(al [][]byte) {
   103  		sort.Slice(al, func(i, k int) bool { return sixb.BtoS(al[i]) < sixb.BtoS(al[k]) })
   104  	}, bufbu, nil)
   105  	sumtB(bufau, bufbu) // sorty
   106  }
   107  
   108  // test & time sorting string/[]byte slices 'by length'
   109  // for different libraries, compare their results
   110  func TestLength(t *testing.T) {
   111  	tsPtr = t
   112  
   113  	fmt.Println("\nSorting []string by length")
   114  	mfcLenS("sort.Slice", func(al []string) {
   115  		sort.Slice(al, func(i, k int) bool { return len(al[i]) < len(al[k]) })
   116  	}, bufbu, nil)
   117  	sumtLenS(bufau, bufbu) // sorty
   118  
   119  	fmt.Println("\nSorting [][]byte by length")
   120  	mfcLenB("sort.Slice", func(al [][]byte) {
   121  		sort.Slice(al, func(i, k int) bool { return len(al[i]) < len(al[k]) })
   122  	}, bufbu, nil)
   123  	sumtLenB(bufau, bufbu) // sorty
   124  }
   125  
   126  // Is Sort*() multi-goroutine safe?
   127  func TestConcurrent(t *testing.T) {
   128  	tsPtr = t
   129  
   130  	fmt.Println("\nConcurrent calls to Sort*()")
   131  	K, L, ch := N/2, N/4, make(chan bool)
   132  	Mxg = 2
   133  
   134  	// two concurrent calls to SortU8() & SortF8() each
   135  	// up to 8 goroutines total
   136  	go sasU8(96, bufbu2[:L:L], ch)
   137  	go sasF8(97, bufaf2[:L:L], ch)
   138  	go sasU8(96, bufbu2[L:], ch)
   139  	fstF8(97, bufaf2[L:], SortF8)
   140  
   141  	for i := 3; i > 0; i-- {
   142  		<-ch // wait others
   143  	}
   144  	compareU4(bufbu[:K:K], bufbu[K:]) // same buffers
   145  	compareU4(bufau[:K:K], bufau[K:])
   146  
   147  	// two concurrent calls to SortI4() & SortI8() each
   148  	// up to 8 goroutines total
   149  	go sasI4(98, bufai[:K:K], ch)
   150  	go sasI8(99, bufbi2[:L:L], ch)
   151  	go sasI4(98, bufai[K:], ch)
   152  	fstI8(99, bufbi2[L:], SortI8)
   153  
   154  	for i := 3; i > 0; i-- {
   155  		<-ch // wait others
   156  	}
   157  	compareU4(bufbu[:K:K], bufbu[K:]) // same buffers
   158  	compareU4(bufau[:K:K], bufau[K:])
   159  }
   160  
   161  // Sort()ing short slices
   162  func TestShort(t *testing.T) {
   163  	tsPtr = t
   164  
   165  	for l := -3; l < 2; l++ {
   166  		Sort(l, iarlsw)
   167  		if iArr[0] != 9 || iArr[1] != 8 {
   168  			t.Fatal("Sort()ing short slices does not work")
   169  		}
   170  	}
   171  	for l := 2; l < 4; l++ {
   172  		Sort(l, iarlsw)
   173  		for k := 2; k >= 0; k-- {
   174  			if iArr[k] != iArr[12+k-l] {
   175  				t.Fatal("Sort()ing short slices does not work")
   176  			}
   177  		}
   178  	}
   179  
   180  	// SortI() calls SortI4() (on 32-bit) or SortI8() (on 64-bit).
   181  	SortI(iArr)
   182  	if IsSortedI(iArr) != 0 {
   183  		t.Fatal("SortI() does not work")
   184  	}
   185  
   186  	// test Search()
   187  	n := len(iArr)
   188  	k := Search(n, func(i int) bool { return iArr[i] >= 5 })
   189  	l := Search(n, func(i int) bool { return iArr[i] >= 10 })
   190  	if k <= 0 || k >= n || iArr[k] != 5 || iArr[k-1] != 4 || l != n {
   191  		t.Fatal("Search() does not work")
   192  	}
   193  }
   194  
   195  var iArr = []int{
   196  	9, 8, 7, 6, 5, 4, 3, 2, 1, 7, 8, 9, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 0, -1, 1, 2, 0,
   197  	-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 0, -1,
   198  	9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 0, -1,
   199  	-9, 8, -7, 6, -5, 4, -3, 2, -1, 0, 9, -8, 7, -6, 5, -4, 3, -2, 1, 0, 1, 2, 0, -1,
   200  	9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 0, -1,
   201  	-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, -9}
   202  
   203  func iarlsw(i, k, r, s int) bool {
   204  	if iArr[i] < iArr[k] {
   205  		if r != s {
   206  			iArr[r], iArr[s] = iArr[s], iArr[r]
   207  		}
   208  		return true
   209  	}
   210  	return false
   211  }