|
@@ -1,6 +1,10 @@
|
|
|
package slice
|
|
|
|
|
|
-import "testing"
|
|
|
+import (
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
|
|
|
func TestRemoveRepeatElement(t *testing.T) {
|
|
|
strSlice := []string{"1", "2", "1", "3", "2", "4", "3"}
|
|
@@ -24,3 +28,23 @@ func TestRemoveRepeatElement(t *testing.T) {
|
|
|
t.Fatal("整型slice移除元素后不正确")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func TestToMap(t *testing.T) {
|
|
|
+ testSlice := []string{"1::one", "2::two"}
|
|
|
+ resultMap, err := ToMap(testSlice, func(v string) (int, string, error) {
|
|
|
+ parts := strings.Split(v, "::")
|
|
|
+ key, err := strconv.Atoi(parts[0])
|
|
|
+ if err != nil {
|
|
|
+ return 0, "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ return key, parts[1], nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if resultMap[1] != "one" || resultMap[2] != "two" {
|
|
|
+ t.Fatal("Map生成错误")
|
|
|
+ }
|
|
|
+}
|