slice_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package slice
  2. import (
  3. "strconv"
  4. "strings"
  5. "testing"
  6. )
  7. func TestRemoveRepeatElement(t *testing.T) {
  8. strSlice := []string{"1", "2", "1", "3", "2", "4", "3"}
  9. intSlice := []int{1, 2, 1, 3, 2, 4, 3}
  10. strSliceRemovedRepeatElement := RemoveRepeatElement(strSlice)
  11. if strSliceRemovedRepeatElement[0] != "1" ||
  12. strSliceRemovedRepeatElement[1] != "2" ||
  13. strSliceRemovedRepeatElement[2] != "3" ||
  14. strSliceRemovedRepeatElement[3] != "4" {
  15. t.Fatal("字符串slice移除元素后不正确")
  16. }
  17. intSliceRemovedRepeatElement := RemoveRepeatElement(intSlice)
  18. if intSliceRemovedRepeatElement[0] != 1 ||
  19. intSliceRemovedRepeatElement[1] != 2 ||
  20. intSliceRemovedRepeatElement[2] != 3 ||
  21. intSliceRemovedRepeatElement[3] != 4 {
  22. t.Fatal("整型slice移除元素后不正确")
  23. }
  24. }
  25. func TestToMap(t *testing.T) {
  26. testSlice := []string{"1::one", "2::two"}
  27. resultMap, err := ToMap(testSlice, func(v string) (int, string, error) {
  28. parts := strings.Split(v, "::")
  29. key, err := strconv.Atoi(parts[0])
  30. if err != nil {
  31. return 0, "", err
  32. }
  33. return key, parts[1], nil
  34. })
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if resultMap[1] != "one" || resultMap[2] != "two" {
  39. t.Fatal("Map生成错误")
  40. }
  41. }