Exemplo n.º 1
0
// Attempts to sort x.
//
// If x is a supported slice type, this library will be used to sort it. Otherwise,
// if x implements sort.Interface it will passthrough to the sort.Sort() algorithm.
// Returns an error on unsupported types.
func Sort(x interface{}) error {
	switch xAsCase := x.(type) {
	case []float32:
		zfloat32.Sort(xAsCase)
	case []float64:
		zfloat64.Sort(xAsCase)
	case []int:
		zint.Sort(xAsCase)
	case []int32:
		zint32.Sort(xAsCase)
	case []int64:
		zint64.Sort(xAsCase)
	case []string:
		sort.Strings(xAsCase)
	case []uint:
		zuint.Sort(xAsCase)
	case []uint32:
		zuint32.Sort(xAsCase)
	case []uint64:
		zuint64.Sort(xAsCase)
	case sort.Interface:
		sort.Sort(xAsCase)
	default:
		return errors.New("type not supported")
	}
	return nil
}
Exemplo n.º 2
0
func TestReflectSortInt(t *testing.T) {
	g := make([]int, TEST_SIZE)
	r := make([]int, TEST_SIZE)
	genTestDataInt(g)
	copy(r, g)
	zint.Sort(g)
	Sort(r)
	for i, val := range g {
		if r[i] != val {
			log.Printf("exp: [%d]\tact: [%d]\n", val, r[i])
			t.FailNow()
		}
		if i > 0 && val < r[i-1] {
			log.Printf("Not Sorted!")
			t.FailNow()
		}
	}
}