github.com/kmnemon/gostream@v1.0.3/gostream/stream_test.go (about)

     1  package gostream
     2  
     3  import (
     4  	"math/rand"
     5  	"sort"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  type A struct {
    11  	x int
    12  	y int
    13  }
    14  
    15  func TestParallel(t *testing.T) {
    16  	a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
    17  
    18  	x := StreamOf(a).
    19  		Parallel().
    20  		Map(func(x int) int {
    21  			return x + 1
    22  		}).
    23  		ToList()
    24  
    25  	expect := []int{2, 3, 4, 5, 6, 7, 8, 9, 10}
    26  	sort.Ints(x)
    27  
    28  	if !equalSliceHelper(expect, x) {
    29  		t.Error("Parallel operator has some problem")
    30  	}
    31  }
    32  
    33  func TestMap(t *testing.T) {
    34  	a := []int{6, 5, 3, 4, 5}
    35  
    36  	x := StreamOf(a).
    37  		Map(func(x int) int {
    38  			return x + 1
    39  		}).
    40  		ToList()
    41  
    42  	expect := []int{7, 6, 4, 5, 6}
    43  
    44  	if !equalSliceHelper(expect, x) {
    45  		t.Error("Map operator has some problem")
    46  	}
    47  }
    48  
    49  func TestMapWithMap(t *testing.T) {
    50  	ages := map[string]int{
    51  		"r": 1000,
    52  		"v": 2000,
    53  		"c": 500,
    54  	}
    55  
    56  	x := StreamOfMap(ages).Map(func(e EntrySet[string, int]) EntrySet[string, int] {
    57  		e.V = e.V + 1
    58  		return e
    59  	}).ToList()
    60  
    61  	expect := []EntrySet[string, int]{
    62  		{K: "r", V: 1001},
    63  		{K: "v", V: 2001},
    64  		{K: "c", V: 501},
    65  	}
    66  
    67  	if !equalSliceHelper(expect, x) {
    68  		t.Error("MapWithMap operator has some problem")
    69  	}
    70  }
    71  
    72  func TestDistinct(t *testing.T) {
    73  	a := []int{6, 5, 3, 4, 5}
    74  
    75  	x := StreamOf(a).
    76  		Distinct().
    77  		ToList()
    78  
    79  	expect := []int{6, 5, 3, 4}
    80  
    81  	if !equalSliceHelper(expect, x) {
    82  		t.Error("Distinct operator has some problem")
    83  	}
    84  }
    85  
    86  func TestReduce(t *testing.T) {
    87  	a := []int{6, 5, 3, 4, 5}
    88  
    89  	x := StreamOf(a).
    90  		Reduce(func(x int, y int) int {
    91  			return x + y
    92  		}).
    93  		ToList()
    94  
    95  	expect := []int{6, 11, 14, 18, 23}
    96  	if !equalSliceHelper(expect, x) {
    97  		t.Error("Reduce operator has some problem")
    98  	}
    99  }
   100  
   101  func TestReduceWithInitValue(t *testing.T) {
   102  	a := []int{6, 5, 3, 4, 5}
   103  
   104  	x := StreamOf(a).
   105  		ReduceWithInitValue(1, func(x int, y int) int {
   106  			return x + y
   107  		}).
   108  		ToList()
   109  
   110  	expect := []int{7, 12, 15, 19, 24}
   111  	if !equalSliceHelper(expect, x) {
   112  		t.Error("ReduceWithInitValue operator has some problem")
   113  	}
   114  }
   115  
   116  func TestSorted(t *testing.T) {
   117  	a := []int{6, 5, 3, 4, 5}
   118  
   119  	x := StreamOf(a).
   120  		Sorted().
   121  		ToList()
   122  
   123  	expect := []int{3, 4, 5, 5, 6}
   124  
   125  	if !equalSliceHelper(expect, x) {
   126  		t.Error("Sorted operator has some problem")
   127  	}
   128  }
   129  
   130  func TestSortedWith(t *testing.T) {
   131  	a := []A{
   132  		{9, 3}, {4, 6},
   133  	}
   134  
   135  	x := StreamOf(a).
   136  		SortedWith(func(a A, b A) bool {
   137  			return a.x < b.x
   138  		}).
   139  		ToList()
   140  
   141  	expect := []A{
   142  		{4, 6}, {9, 3},
   143  	}
   144  
   145  	if !equalSliceHelper(expect, x) {
   146  		t.Error("SortedWith operator has some problem")
   147  	}
   148  }
   149  
   150  func TestFilter(t *testing.T) {
   151  	a := []int{6, 5, 3, 4, 5}
   152  
   153  	x := StreamOf(a).
   154  		Filter(func(a int) bool {
   155  			return a > 4
   156  		}).
   157  		ToList()
   158  
   159  	expect := []int{6, 5, 5}
   160  
   161  	if !equalSliceHelper(expect, x) {
   162  		t.Error("Filter operator has some problem")
   163  	}
   164  }
   165  
   166  func TestLimit(t *testing.T) {
   167  	a := []int{6, 5, 3, 4, 5}
   168  
   169  	x := StreamOf(a).
   170  		Limit(3).
   171  		ToList()
   172  
   173  	expect := []int{6, 5, 3}
   174  
   175  	if !equalSliceHelper(expect, x) {
   176  		t.Error("Limit operator has some problem")
   177  	}
   178  }
   179  
   180  func TestFindFirst(t *testing.T) {
   181  	a := []int{6, 5, 3, 4, 5}
   182  
   183  	x := StreamOf(a).
   184  		FindFirst()
   185  
   186  	if !(x == 6) {
   187  		t.Error("FindFirst operator has some problem")
   188  	}
   189  }
   190  
   191  func equalSliceHelper[T comparable](a, b []T) bool {
   192  	if len(a) != len(b) {
   193  		return false
   194  	}
   195  
   196  	for i, v := range a {
   197  		if v != b[i] {
   198  			return false
   199  		}
   200  	}
   201  
   202  	return true
   203  }
   204  
   205  func BenchmarkParallel(b *testing.B) {
   206  	rand.Seed(time.Now().Unix())
   207  	a := rand.Perm(1000000)
   208  
   209  	StreamOf(a).
   210  		Map(func(x int) int {
   211  			return x + 1
   212  		}).
   213  		ToList()
   214  }