github.com/searKing/golang/go@v1.2.117/exp/maps/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 maps_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	maps_ "github.com/searKing/golang/go/exp/maps"
    11  )
    12  
    13  var splitTests = []struct {
    14  	m    map[int]string
    15  	sep  int
    16  	want []map[int]string
    17  }{
    18  	{nil, -1, nil},
    19  	{nil, 0, nil},
    20  	{nil, 1, nil},
    21  	{map[int]string{}, -1, nil},
    22  	{map[int]string{}, 0, nil},
    23  	{map[int]string{}, 1, nil},
    24  	{map[int]string{1: "One"}, -1, []map[int]string{{1: "One"}}},
    25  	{map[int]string{1: "One"}, 0, []map[int]string{{1: "One"}}},
    26  	{map[int]string{1: "One"}, 1, []map[int]string{{1: "One"}}},
    27  	{map[int]string{1: "One"}, 2, []map[int]string{{1: "One"}}},
    28  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
    29  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
    30  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
    31  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, []map[int]string{{1: "One", 2: "Two"}, {3: "Three"}}},
    32  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
    33  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
    34  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
    35  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
    36  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
    37  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
    38  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three"}, {4: "Four"}}},
    39  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
    40  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
    41  }
    42  
    43  func TestSplit(t *testing.T) {
    44  	for i, test := range splitTests {
    45  		got := maps_.Split(test.m, test.sep)
    46  		if len(test.want) != len(got) {
    47  			t.Errorf("#%d: Split(%v, %v) = %v, want %v", i, test.m, test.sep, got, test.want)
    48  			continue
    49  		}
    50  		for i := range got {
    51  			if len(got[i]) != len(test.want[i]) {
    52  				t.Errorf("#%d: Split(%v, %v) = %v, want %v", i, test.m, test.sep, got, test.want)
    53  				break
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  var splitNTests = []struct {
    60  	m    map[int]string
    61  	sep  int
    62  	n    int
    63  	want []map[int]string
    64  }{
    65  	{nil, -1, -1, nil},
    66  	{nil, -1, 0, nil},
    67  	{nil, -1, 1, nil},
    68  	{nil, 0, -1, nil},
    69  	{nil, 0, 0, nil},
    70  	{nil, 0, 1, nil},
    71  	{nil, 1, -1, nil},
    72  	{nil, 1, 0, nil},
    73  	{nil, 1, 1, nil},
    74  	{map[int]string{}, -1, -1, nil},
    75  	{map[int]string{}, -1, 0, nil},
    76  	{map[int]string{}, -1, 1, []map[int]string{{}}},
    77  	{map[int]string{}, 0, -1, nil},
    78  	{map[int]string{}, 0, 0, nil},
    79  	{map[int]string{}, 0, 1, nil},
    80  	{map[int]string{}, 1, -1, nil},
    81  	{map[int]string{}, 1, 0, nil},
    82  	{map[int]string{}, 1, 1, []map[int]string{{}}},
    83  	{map[int]string{1: "One"}, -1, -1, []map[int]string{{1: "One"}}},
    84  	{map[int]string{1: "One"}, -1, 0, nil},
    85  	{map[int]string{1: "One"}, -1, 1, []map[int]string{{1: "One"}}},
    86  	{map[int]string{1: "One"}, -1, 2, []map[int]string{{1: "One"}}},
    87  	{map[int]string{1: "One"}, 0, -1, []map[int]string{{1: "One"}}},
    88  	{map[int]string{1: "One"}, 0, 0, nil},
    89  	{map[int]string{1: "One"}, 0, 1, []map[int]string{{1: "One"}}},
    90  	{map[int]string{1: "One"}, 0, 2, []map[int]string{{1: "One"}}},
    91  	{map[int]string{1: "One"}, 1, -1, []map[int]string{{1: "One"}}},
    92  	{map[int]string{1: "One"}, 1, 0, nil},
    93  	{map[int]string{1: "One"}, 1, 1, []map[int]string{{1: "One"}}},
    94  	{map[int]string{1: "One"}, 1, 2, []map[int]string{{1: "One"}}},
    95  	{map[int]string{1: "One"}, 2, -1, []map[int]string{{1: "One"}}},
    96  	{map[int]string{1: "One"}, 2, 0, nil},
    97  	{map[int]string{1: "One"}, 2, 1, []map[int]string{{1: "One"}}},
    98  	{map[int]string{1: "One"}, 2, 2, []map[int]string{{1: "One"}}},
    99  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   100  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, 0, nil},
   101  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   102  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, 2, []map[int]string{{1: "One"}, {2: "Two", 3: "Three"}}},
   103  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, 3, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   104  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, -1, 4, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   105  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   106  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, 0, nil},
   107  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   108  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, 2, []map[int]string{{1: "One"}, {2: "Two", 3: "Three"}}},
   109  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, 3, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   110  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 0, 4, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   111  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   112  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, 0, nil},
   113  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   114  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, 2, []map[int]string{{1: "One"}, {2: "Two", 3: "Three"}}},
   115  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, 3, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   116  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 1, 4, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}}},
   117  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, -1, []map[int]string{{1: "One", 2: "Two"}, {3: "Three"}}},
   118  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, 0, nil},
   119  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   120  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, 2, []map[int]string{{1: "One", 2: "Two"}, {3: "Three"}}},
   121  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, 3, []map[int]string{{1: "One", 2: "Two"}, {3: "Three"}}},
   122  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 2, 4, []map[int]string{{1: "One", 2: "Two"}, {3: "Three"}}},
   123  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, -1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   124  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, 0, nil},
   125  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   126  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, 2, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   127  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   128  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 3, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   129  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, -1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   130  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, 0, nil},
   131  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   132  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, 2, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   133  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   134  	{map[int]string{1: "One", 2: "Two", 3: "Three"}, 4, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three"}}},
   135  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   136  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, 0, nil},
   137  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   138  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, 2, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   139  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, 3, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three", 4: "Four"}}},
   140  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, 4, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   141  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, -1, 5, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   142  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   143  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, 0, nil},
   144  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   145  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, 2, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   146  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, 3, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three", 4: "Four"}}},
   147  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, 4, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   148  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 0, 5, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   149  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, -1, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   150  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, 0, nil},
   151  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   152  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, 2, []map[int]string{{1: "One"}, {2: "Two", 3: "Three", 4: "Four"}}},
   153  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, 3, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three", 4: "Four"}}},
   154  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, 4, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   155  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 1, 5, []map[int]string{{1: "One"}, {2: "Two"}, {3: "Three"}, {4: "Four"}}},
   156  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, -1, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   157  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, 0, nil},
   158  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   159  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, 2, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   160  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, 3, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   161  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, 4, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   162  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 2, 5, []map[int]string{{1: "One", 2: "Two"}, {3: "Three", 4: "Four"}}},
   163  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, -1, []map[int]string{{1: "One", 2: "Two", 3: "Three"}, {4: "Four"}}},
   164  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, 0, nil},
   165  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   166  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, 2, []map[int]string{{1: "One", 2: "Two", 3: "Three"}, {4: "Four"}}},
   167  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three"}, {4: "Four"}}},
   168  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three"}, {4: "Four"}}},
   169  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 3, 5, []map[int]string{{1: "One", 2: "Two", 3: "Three"}, {4: "Four"}}},
   170  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, -1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   171  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, 0, nil},
   172  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   173  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, 2, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   174  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   175  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   176  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 4, 5, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   177  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, -1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   178  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, 0, nil},
   179  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, 1, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   180  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, 2, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   181  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, 3, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   182  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, 4, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   183  	{map[int]string{1: "One", 2: "Two", 3: "Three", 4: "Four"}, 5, 5, []map[int]string{{1: "One", 2: "Two", 3: "Three", 4: "Four"}}},
   184  }
   185  
   186  func TestSplitN(t *testing.T) {
   187  	for i, test := range splitNTests {
   188  		got := maps_.SplitN(test.m, test.sep, test.n)
   189  		if len(test.want) != len(got) {
   190  			t.Errorf("#%d: SplitN(%v, %v, %v) = %v, want %v", i, test.m, test.sep, test.n, got, test.want)
   191  			continue
   192  		}
   193  		for i := range got {
   194  			if len(got[i]) != len(test.want[i]) {
   195  				t.Errorf("#%d: SplitN(%v, %v, %v) = %v, want %v", i, test.m, test.sep, test.n, got, test.want)
   196  				break
   197  			}
   198  		}
   199  	}
   200  }