github.com/jxskiss/gopkg@v0.17.3/easy/sort_test.go (about)

     1  package easy
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func toInt32s(slice []int64) []int32 {
     9  	out := make([]int32, len(slice))
    10  	for i, x := range slice {
    11  		out[i] = int32(x)
    12  	}
    13  	return out
    14  }
    15  
    16  func toInts(slice []int64) []int {
    17  	out := make([]int, len(slice))
    18  	for i, x := range slice {
    19  		out[i] = int(x)
    20  	}
    21  	return out
    22  }
    23  
    24  func TestInSortedInt64s(t *testing.T) {
    25  	slice1 := []int64{3, 5, 7, 9, 10}
    26  	slice2 := []int64{10, 9, 7, 5, 3}
    27  
    28  	tests := []map[string]interface{}{
    29  		{"elem": 7, "want": true},
    30  		{"elem": 8, "want": false},
    31  		{"elem": 3, "want": true},
    32  		{"elem": 10, "want": true},
    33  		{"elem": 1, "want": false},
    34  		{"elem": 50, "want": false},
    35  	}
    36  	for _, test := range tests {
    37  		got64 := InSortedInt64s(slice1, int64(test["elem"].(int)))
    38  		assert.Equal(t, test["want"], got64)
    39  
    40  		got32 := InSortedInt32s(toInt32s(slice1), int32(test["elem"].(int)))
    41  		assert.Equal(t, test["want"], got32)
    42  
    43  		gotInt := InSortedInts(toInts(slice1), test["elem"].(int))
    44  		assert.Equal(t, test["want"], gotInt)
    45  	}
    46  	for _, test := range tests {
    47  		got64 := InSortedInt64s(slice2, int64(test["elem"].(int)))
    48  		assert.Equal(t, test["want"], got64)
    49  
    50  		got32 := InSortedInt32s(toInt32s(slice2), int32(test["elem"].(int)))
    51  		assert.Equal(t, test["want"], got32)
    52  
    53  		gotInt := InSortedInts(toInts(slice2), test["elem"].(int))
    54  		assert.Equal(t, test["want"], gotInt)
    55  	}
    56  }
    57  
    58  func TestInSortedStrings(t *testing.T) {
    59  	slice1 := []string{"C", "E", "G", "I", "K"}
    60  	slice2 := []string{"K", "I", "G", "E", "C"}
    61  
    62  	tests := []map[string]interface{}{
    63  		{"elem": "G", "want": true},
    64  		{"elem": "H", "want": false},
    65  		{"elem": "C", "want": true},
    66  		{"elem": "K", "want": true},
    67  		{"elem": "A", "want": false},
    68  		{"elem": "Z", "want": false},
    69  	}
    70  	for _, test := range tests {
    71  		got := InSortedStrings(slice1, test["elem"].(string))
    72  		assert.Equal(t, test["want"], got)
    73  	}
    74  	for _, test := range tests {
    75  		got := InSortedStrings(slice2, test["elem"].(string))
    76  		assert.Equal(t, test["want"], got)
    77  	}
    78  }