github.com/searKing/golang/go@v1.2.74/util/spliterator/spliterator_test.go (about)

     1  // Copyright 2020 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 spliterator_test
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/searKing/golang/go/util/function/consumer"
    12  	"github.com/searKing/golang/go/util/spliterator"
    13  )
    14  
    15  type SpliteratorForEachRemainingTests struct {
    16  	input  []interface{}
    17  	output []string
    18  }
    19  
    20  var spliteratorForEachRemainingTests = []SpliteratorForEachRemainingTests{
    21  	{
    22  		input:  []interface{}{"1", "2", "3", "4"},
    23  		output: []string{"1", "2", "3", "4"},
    24  	},
    25  }
    26  
    27  func TestSliceSpliterator_ForEachRemaining(t *testing.T) {
    28  	for n, test := range spliteratorForEachRemainingTests {
    29  		split := spliterator.NewSliceSpliterator2(spliterator.CharacteristicTODO, test.input...)
    30  
    31  		var gots []string
    32  		split.ForEachRemaining(context.Background(), consumer.ConsumerFunc(func(t interface{}) {
    33  			gots = append(gots, t.(string))
    34  		}))
    35  		for i, got := range gots {
    36  			if got != test.output[i] {
    37  				t.Errorf("#%d[%d]: %v: got %q; expected %q", n, i, test.input, got, test.output[i])
    38  			}
    39  		}
    40  	}
    41  }
    42  
    43  type SpliteratorTrySplitTests struct {
    44  	input []interface{}
    45  	ls    []string
    46  	rs    []string
    47  }
    48  
    49  var spliteratorTrySplitTests = []SpliteratorTrySplitTests{
    50  	{
    51  		input: []interface{}{"1", "2", "3", "4"},
    52  		ls:    []string{"1", "2"},
    53  		rs:    []string{"3", "4"},
    54  	},
    55  }
    56  
    57  func TestSliceSpliterator_TrySplit(t *testing.T) {
    58  	for n, test := range spliteratorTrySplitTests {
    59  		split := spliterator.NewSliceSpliterator2(spliterator.CharacteristicTODO, test.input...)
    60  
    61  		var lsGots []string
    62  		if ls := split.TrySplit(); ls != nil {
    63  			ls.ForEachRemaining(context.Background(), consumer.ConsumerFunc(func(t interface{}) {
    64  				lsGots = append(lsGots, t.(string))
    65  			}))
    66  			for i, got := range lsGots {
    67  				if got != test.ls[i] {
    68  					t.Errorf("ls #%d[%d]: %v: got %q; expected %q", n, i, test.input, got, test.ls[i])
    69  				}
    70  			}
    71  		}
    72  		var rsGots []string
    73  		split.ForEachRemaining(context.Background(), consumer.ConsumerFunc(func(t interface{}) {
    74  			rsGots = append(rsGots, t.(string))
    75  		}))
    76  		for i, got := range rsGots {
    77  			if got != test.rs[i] {
    78  				t.Errorf("rs #%d[%d]: %v: got %q; expected %q", n, i, test.input, got, test.rs[i])
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  type SpliteratorTryAdvanceTests struct {
    85  	input  []interface{}
    86  	output []string
    87  }
    88  
    89  var spliteratorTryAdvanceTests = []SpliteratorTryAdvanceTests{
    90  	{
    91  		input:  []interface{}{"1", "2", "3", "4"},
    92  		output: []string{"1", "2", "3", "4"},
    93  	},
    94  }
    95  
    96  func TestSliceSpliterator_TryAdvance(t *testing.T) {
    97  	for n, test := range spliteratorForEachRemainingTests {
    98  		split := spliterator.NewSliceSpliterator2(spliterator.CharacteristicTODO, test.input...)
    99  
   100  		var gots []string
   101  		for split.TryAdvance(context.Background(), consumer.ConsumerFunc(func(t interface{}) {
   102  			gots = append(gots, t.(string))
   103  		})) {
   104  		}
   105  
   106  		for i, got := range gots {
   107  			if got != test.output[i] {
   108  				t.Errorf("#%d[%d]: %v: got %q; expected %q", n, i, test.input, got, test.output[i])
   109  			}
   110  		}
   111  	}
   112  }
   113  
   114  type SpliteratorEstimateSizeTests struct {
   115  	input  []interface{}
   116  	output []int
   117  }
   118  
   119  var spliteratorEstimateSizeTests = []SpliteratorEstimateSizeTests{
   120  	{
   121  		input:  []interface{}{"1", "2", "3", "4"},
   122  		output: []int{4, 3, 2, 1},
   123  	},
   124  }
   125  
   126  func TestSliceSpliterator_EstimateSize(t *testing.T) {
   127  	for n, test := range spliteratorEstimateSizeTests {
   128  		split := spliterator.NewSliceSpliterator2(spliterator.CharacteristicTODO, test.input...)
   129  
   130  		var gots []int
   131  		for split.TryAdvance(context.Background(), consumer.ConsumerFunc(func(t interface{}) {
   132  			gots = append(gots, split.EstimateSize())
   133  		})) {
   134  		}
   135  
   136  		for i, got := range gots {
   137  			if got != test.output[i] {
   138  				t.Errorf("#%d[%d]: %v: got %q; expected %q", n, i, test.input, got, test.output[i])
   139  			}
   140  		}
   141  	}
   142  }