github.com/zhongdalu/gf@v1.0.0/g/container/garray/garray_z_unit_basic_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // go test *.go 8 9 package garray_test 10 11 import ( 12 "github.com/zhongdalu/gf/g/container/garray" 13 "github.com/zhongdalu/gf/g/test/gtest" 14 "github.com/zhongdalu/gf/g/util/gconv" 15 "strings" 16 "testing" 17 ) 18 19 func Test_IntArray_Unique(t *testing.T) { 20 expect := []int{1, 2, 3, 4, 5, 6} 21 array := garray.NewIntArray() 22 array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6) 23 array.Unique() 24 gtest.Assert(array.Slice(), expect) 25 } 26 27 func Test_SortedIntArray1(t *testing.T) { 28 expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 29 array := garray.NewSortedIntArray() 30 for i := 10; i > -1; i-- { 31 array.Add(i) 32 } 33 gtest.Assert(array.Slice(), expect) 34 gtest.Assert(array.Add().Slice(), expect) 35 } 36 37 func Test_SortedIntArray2(t *testing.T) { 38 expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 39 array := garray.NewSortedIntArray() 40 for i := 0; i <= 10; i++ { 41 array.Add(i) 42 } 43 gtest.Assert(array.Slice(), expect) 44 } 45 46 func Test_SortedStringArray1(t *testing.T) { 47 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 48 array1 := garray.NewSortedStringArray() 49 array2 := garray.NewSortedStringArray(true) 50 for i := 10; i > -1; i-- { 51 array1.Add(gconv.String(i)) 52 array2.Add(gconv.String(i)) 53 } 54 gtest.Assert(array1.Slice(), expect) 55 gtest.Assert(array2.Slice(), expect) 56 57 } 58 59 func Test_SortedStringArray2(t *testing.T) { 60 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 61 array := garray.NewSortedStringArray() 62 for i := 0; i <= 10; i++ { 63 array.Add(gconv.String(i)) 64 } 65 gtest.Assert(array.Slice(), expect) 66 array.Add() 67 gtest.Assert(array.Slice(), expect) 68 } 69 70 func Test_SortedArray1(t *testing.T) { 71 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 72 array := garray.NewSortedArray(func(v1, v2 interface{}) int { 73 return strings.Compare(gconv.String(v1), gconv.String(v2)) 74 }) 75 for i := 10; i > -1; i-- { 76 array.Add(gconv.String(i)) 77 } 78 gtest.Assert(array.Slice(), expect) 79 } 80 81 func Test_SortedArray2(t *testing.T) { 82 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 83 func1 := func(v1, v2 interface{}) int { 84 return strings.Compare(gconv.String(v1), gconv.String(v2)) 85 } 86 array := garray.NewSortedArray(func1) 87 array2 := garray.NewSortedArray(func1, true) 88 for i := 0; i <= 10; i++ { 89 array.Add(gconv.String(i)) 90 array2.Add(gconv.String(i)) 91 } 92 gtest.Assert(array.Slice(), expect) 93 gtest.Assert(array.Add().Slice(), expect) 94 gtest.Assert(array2.Slice(), expect) 95 } 96 97 func TestNewFromCopy(t *testing.T) { 98 gtest.Case(t, func() { 99 a1 := []interface{}{"100", "200", "300", "400", "500", "600"} 100 array1 := garray.NewFromCopy(a1) 101 gtest.AssertIN(array1.PopRands(2), a1) 102 gtest.Assert(len(array1.PopRands(1)), 1) 103 gtest.Assert(len(array1.PopRands(9)), 3) 104 }) 105 }