github.com/searKing/golang/go@v1.2.117/exp/slices/split_test.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slices_test
     6  
     7  import (
     8  	"maps"
     9  	"slices"
    10  	"testing"
    11  
    12  	slices_ "github.com/searKing/golang/go/exp/slices"
    13  )
    14  
    15  var splitTests = []struct {
    16  	s    []int
    17  	sep  int
    18  	want [][]int
    19  }{
    20  	{nil, -1, nil},
    21  	{nil, 0, nil},
    22  	{nil, 1, nil},
    23  	{[]int{}, -1, [][]int{}},
    24  	{[]int{}, 0, [][]int{}},
    25  	{[]int{}, 1, [][]int{}},
    26  	{[]int{1}, -1, [][]int{{1}}},
    27  	{[]int{1}, 0, [][]int{{1}}},
    28  	{[]int{1}, 1, [][]int{{1}}},
    29  	{[]int{1}, 2, [][]int{{1}}},
    30  	{[]int{1, 2, 3}, -1, [][]int{{1}, {2}, {3}}},
    31  	{[]int{1, 2, 3}, 0, [][]int{{1}, {2}, {3}}},
    32  	{[]int{1, 2, 3}, 1, [][]int{{1}, {2}, {3}}},
    33  	{[]int{1, 2, 3}, 2, [][]int{{1, 2}, {3}}},
    34  	{[]int{1, 2, 3}, 3, [][]int{{1, 2, 3}}},
    35  	{[]int{1, 2, 3}, 4, [][]int{{1, 2, 3}}},
    36  	{[]int{1, 2, 3, 4}, -1, [][]int{{1}, {2}, {3}, {4}}},
    37  	{[]int{1, 2, 3, 4}, 0, [][]int{{1}, {2}, {3}, {4}}},
    38  	{[]int{1, 2, 3, 4}, 1, [][]int{{1}, {2}, {3}, {4}}},
    39  	{[]int{1, 2, 3, 4}, 2, [][]int{{1, 2}, {3, 4}}},
    40  	{[]int{1, 2, 3, 4}, 3, [][]int{{1, 2, 3}, {4}}},
    41  	{[]int{1, 2, 3, 4}, 4, [][]int{{1, 2, 3, 4}}},
    42  	{[]int{1, 2, 3, 4}, 5, [][]int{{1, 2, 3, 4}}},
    43  }
    44  
    45  func TestSplit(t *testing.T) {
    46  	for i, test := range splitTests {
    47  		got := slices_.Split(test.s, test.sep)
    48  		if len(test.want) != len(got) {
    49  			t.Errorf("#%d: Split(%v, %v) = %v, want %v", i, test.s, test.sep, got, test.want)
    50  			continue
    51  		}
    52  		for i := range got {
    53  			if !slices.Equal(got[i], test.want[i]) {
    54  				t.Errorf("#%d: Split(%v, %v) = %v, want %v", i, test.s, test.sep, got, test.want)
    55  				break
    56  			}
    57  		}
    58  	}
    59  }
    60  
    61  var splitNTests = []struct {
    62  	s    []int
    63  	sep  int
    64  	n    int
    65  	want [][]int
    66  }{
    67  	{nil, -1, -1, nil},
    68  	{nil, -1, 0, nil},
    69  	{nil, -1, 1, nil},
    70  	{nil, 0, -1, nil},
    71  	{nil, 0, 0, nil},
    72  	{nil, 0, 1, nil},
    73  	{[]int{}, -1, -1, nil},
    74  	{[]int{}, -1, 0, nil},
    75  	{[]int{}, -1, 1, [][]int{{}}},
    76  	{[]int{}, 0, -1, nil},
    77  	{[]int{}, 0, 0, nil},
    78  	{[]int{}, 0, 1, [][]int{}},
    79  	{[]int{}, 1, -1, nil},
    80  	{[]int{}, 1, 0, nil},
    81  	{[]int{}, 1, 1, [][]int{{}}},
    82  	{[]int{1}, -1, -1, [][]int{{1}}},
    83  	{[]int{1}, -1, 0, nil},
    84  	{[]int{1}, -1, 1, [][]int{{1}}},
    85  	{[]int{1}, -1, 2, [][]int{{1}}},
    86  	{[]int{1}, 0, -1, [][]int{{1}}},
    87  	{[]int{1}, 0, 0, nil},
    88  	{[]int{1}, 0, 1, [][]int{{1}}},
    89  	{[]int{1}, 0, 2, [][]int{{1}}},
    90  	{[]int{1}, 1, -1, [][]int{{1}}},
    91  	{[]int{1}, 1, 0, nil},
    92  	{[]int{1}, 1, 1, [][]int{{1}}},
    93  	{[]int{1}, 1, 2, [][]int{{1}}},
    94  	{[]int{1}, 2, -1, [][]int{{1}}},
    95  	{[]int{1}, 2, 0, nil},
    96  	{[]int{1}, 2, 1, [][]int{{1}}},
    97  	{[]int{1}, 2, 2, [][]int{{1}}},
    98  	{[]int{1, 2, 3}, -1, -1, [][]int{{1}, {2}, {3}}},
    99  	{[]int{1, 2, 3}, -1, 0, nil},
   100  	{[]int{1, 2, 3}, -1, 1, [][]int{{1, 2, 3}}},
   101  	{[]int{1, 2, 3}, -1, 2, [][]int{{1}, {2, 3}}},
   102  	{[]int{1, 2, 3}, -1, 3, [][]int{{1}, {2}, {3}}},
   103  	{[]int{1, 2, 3}, -1, 4, [][]int{{1}, {2}, {3}}},
   104  	{[]int{1, 2, 3}, 0, -1, [][]int{{1}, {2}, {3}}},
   105  	{[]int{1, 2, 3}, 0, 0, nil},
   106  	{[]int{1, 2, 3}, 0, 1, [][]int{{1, 2, 3}}},
   107  	{[]int{1, 2, 3}, 0, 2, [][]int{{1}, {2, 3}}},
   108  	{[]int{1, 2, 3}, 0, 3, [][]int{{1}, {2}, {3}}},
   109  	{[]int{1, 2, 3}, 0, 4, [][]int{{1}, {2}, {3}}},
   110  	{[]int{1, 2, 3}, 1, -1, [][]int{{1}, {2}, {3}}},
   111  	{[]int{1, 2, 3}, 1, 0, nil},
   112  	{[]int{1, 2, 3}, 1, 1, [][]int{{1, 2, 3}}},
   113  	{[]int{1, 2, 3}, 1, 2, [][]int{{1}, {2, 3}}},
   114  	{[]int{1, 2, 3}, 1, 3, [][]int{{1}, {2}, {3}}},
   115  	{[]int{1, 2, 3}, 1, 4, [][]int{{1}, {2}, {3}}},
   116  	{[]int{1, 2, 3}, 2, -1, [][]int{{1, 2}, {3}}},
   117  	{[]int{1, 2, 3}, 2, 0, nil},
   118  	{[]int{1, 2, 3}, 2, 1, [][]int{{1, 2, 3}}},
   119  	{[]int{1, 2, 3}, 2, 2, [][]int{{1, 2}, {3}}},
   120  	{[]int{1, 2, 3}, 2, 3, [][]int{{1, 2}, {3}}},
   121  	{[]int{1, 2, 3}, 2, 4, [][]int{{1, 2}, {3}}},
   122  	{[]int{1, 2, 3}, 3, -1, [][]int{{1, 2, 3}}},
   123  	{[]int{1, 2, 3}, 3, 0, nil},
   124  	{[]int{1, 2, 3}, 3, 1, [][]int{{1, 2, 3}}},
   125  	{[]int{1, 2, 3}, 3, 2, [][]int{{1, 2, 3}}},
   126  	{[]int{1, 2, 3}, 3, 3, [][]int{{1, 2, 3}}},
   127  	{[]int{1, 2, 3}, 3, 4, [][]int{{1, 2, 3}}},
   128  	{[]int{1, 2, 3}, 4, -1, [][]int{{1, 2, 3}}},
   129  	{[]int{1, 2, 3}, 4, 0, nil},
   130  	{[]int{1, 2, 3}, 4, 1, [][]int{{1, 2, 3}}},
   131  	{[]int{1, 2, 3}, 4, 2, [][]int{{1, 2, 3}}},
   132  	{[]int{1, 2, 3}, 4, 3, [][]int{{1, 2, 3}}},
   133  	{[]int{1, 2, 3}, 4, 4, [][]int{{1, 2, 3}}},
   134  	{[]int{1, 2, 3, 4}, -1, -1, [][]int{{1}, {2}, {3}, {4}}},
   135  	{[]int{1, 2, 3, 4}, -1, 0, nil},
   136  	{[]int{1, 2, 3, 4}, -1, 1, [][]int{{1, 2, 3, 4}}},
   137  	{[]int{1, 2, 3, 4}, -1, 2, [][]int{{1, 2}, {3, 4}}},
   138  	{[]int{1, 2, 3, 4}, -1, 3, [][]int{{1}, {2}, {3, 4}}},
   139  	{[]int{1, 2, 3, 4}, -1, 4, [][]int{{1}, {2}, {3}, {4}}},
   140  	{[]int{1, 2, 3, 4}, 0, -1, [][]int{{1}, {2}, {3}, {4}}},
   141  	{[]int{1, 2, 3, 4}, 0, 0, nil},
   142  	{[]int{1, 2, 3, 4}, 0, 1, [][]int{{1, 2, 3, 4}}},
   143  	{[]int{1, 2, 3, 4}, 0, 2, [][]int{{1, 2}, {3, 4}}},
   144  	{[]int{1, 2, 3, 4}, 0, 3, [][]int{{1}, {2}, {3, 4}}},
   145  	{[]int{1, 2, 3, 4}, 0, 4, [][]int{{1}, {2}, {3}, {4}}},
   146  	{[]int{1, 2, 3, 4}, 1, -1, [][]int{{1}, {2}, {3}, {4}}},
   147  	{[]int{1, 2, 3, 4}, 1, 0, nil},
   148  	{[]int{1, 2, 3, 4}, 1, 1, [][]int{{1, 2, 3, 4}}},
   149  	{[]int{1, 2, 3, 4}, 1, 2, [][]int{{1}, {2, 3, 4}}},
   150  	{[]int{1, 2, 3, 4}, 1, 3, [][]int{{1}, {2}, {3, 4}}},
   151  	{[]int{1, 2, 3, 4}, 1, 4, [][]int{{1}, {2}, {3}, {4}}},
   152  	{[]int{1, 2, 3, 4}, 2, -1, [][]int{{1, 2}, {3, 4}}},
   153  	{[]int{1, 2, 3, 4}, 2, 0, nil},
   154  	{[]int{1, 2, 3, 4}, 2, 1, [][]int{{1, 2, 3, 4}}},
   155  	{[]int{1, 2, 3, 4}, 2, 2, [][]int{{1, 2}, {3, 4}}},
   156  	{[]int{1, 2, 3, 4}, 2, 3, [][]int{{1, 2}, {3, 4}}},
   157  	{[]int{1, 2, 3, 4}, 2, 4, [][]int{{1, 2}, {3, 4}}},
   158  	{[]int{1, 2, 3, 4}, 3, -1, [][]int{{1, 2, 3}, {4}}},
   159  	{[]int{1, 2, 3, 4}, 3, 0, nil},
   160  	{[]int{1, 2, 3, 4}, 3, 1, [][]int{{1, 2, 3, 4}}},
   161  	{[]int{1, 2, 3, 4}, 3, 2, [][]int{{1, 2, 3}, {4}}},
   162  	{[]int{1, 2, 3, 4}, 3, 3, [][]int{{1, 2, 3}, {4}}},
   163  	{[]int{1, 2, 3, 4}, 3, 4, [][]int{{1, 2, 3}, {4}}},
   164  	{[]int{1, 2, 3, 4}, 3, 5, [][]int{{1, 2, 3}, {4}}},
   165  	{[]int{1, 2, 3, 4}, 4, -1, [][]int{{1, 2, 3, 4}}},
   166  	{[]int{1, 2, 3, 4}, 4, 0, nil},
   167  	{[]int{1, 2, 3, 4}, 4, 1, [][]int{{1, 2, 3, 4}}},
   168  	{[]int{1, 2, 3, 4}, 4, 2, [][]int{{1, 2, 3, 4}}},
   169  	{[]int{1, 2, 3, 4}, 4, 3, [][]int{{1, 2, 3, 4}}},
   170  	{[]int{1, 2, 3, 4}, 4, 4, [][]int{{1, 2, 3, 4}}},
   171  	{[]int{1, 2, 3, 4}, 4, 5, [][]int{{1, 2, 3, 4}}},
   172  	{[]int{1, 2, 3, 4}, 5, -1, [][]int{{1, 2, 3, 4}}},
   173  	{[]int{1, 2, 3, 4}, 5, 0, nil},
   174  	{[]int{1, 2, 3, 4}, 5, 1, [][]int{{1, 2, 3, 4}}},
   175  	{[]int{1, 2, 3, 4}, 5, 2, [][]int{{1, 2, 3, 4}}},
   176  	{[]int{1, 2, 3, 4}, 5, 3, [][]int{{1, 2, 3, 4}}},
   177  	{[]int{1, 2, 3, 4}, 5, 4, [][]int{{1, 2, 3, 4}}},
   178  	{[]int{1, 2, 3, 4}, 5, 5, [][]int{{1, 2, 3, 4}}},
   179  }
   180  
   181  func TestSplitN(t *testing.T) {
   182  	for i, test := range splitNTests {
   183  		got := slices_.SplitN(test.s, test.sep, test.n)
   184  		if len(test.want) != len(got) {
   185  			t.Errorf("#%d: SplitN(%v, %v, %v) = %v, want %v", i, test.s, test.sep, test.n, got, test.want)
   186  			continue
   187  		}
   188  		for i := range got {
   189  			if !slices.Equal(got[i], test.want[i]) {
   190  				t.Errorf("#%d: SplitN(%v, %v, %v) = %v, want %v", i, test.s, test.sep, test.n, got, test.want)
   191  				break
   192  			}
   193  		}
   194  	}
   195  }
   196  
   197  var splitMapTests = []struct {
   198  	s    []any
   199  	want map[string]int
   200  }{
   201  	{nil, nil},
   202  	{[]any{}, map[string]int{}},
   203  	{[]any{"k1"}, map[string]int{"k1": 0}},
   204  	{[]any{"k1", 1}, map[string]int{"k1": 1}},
   205  	{[]any{"k1", 1, "k2"}, map[string]int{"k1": 1, "k2": 0}},
   206  }
   207  
   208  func TestSplitMap(t *testing.T) {
   209  	for i, tt := range splitMapTests {
   210  		got := slices_.SplitMap[map[string]int](tt.s)
   211  		if len(tt.want) != len(got) {
   212  			t.Errorf("#%d: SplitMap(%v) = %v, want %v", i, tt.s, got, tt.want)
   213  			continue
   214  		}
   215  		if (got == nil && tt.want != nil) || (got != nil && tt.want == nil) {
   216  			t.Errorf("#%d: slices.SplitMap(%v) = %v, want %v", i, tt.s, got, tt.want)
   217  			return
   218  		}
   219  		if !maps.Equal(got, tt.want) {
   220  			t.Errorf("#%d: slices.SplitMap(%v) = %v, want %v", i, tt.s, got, tt.want)
   221  		}
   222  	}
   223  }