gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/stream/iter/iter_test.go (about)

     1  package iter
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/sy_183/go-common/maps"
     6  	"gitee.com/sy_183/go-common/slice"
     7  	stringUtil "gitee.com/sy_183/go-common/strings"
     8  	"gitee.com/sy_183/go-common/unit"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestChan(t *testing.T) {
    15  	type IntChan chan int
    16  	ch := make(IntChan, 10)
    17  	for i := 0; i < 10; i++ {
    18  		ch <- i
    19  	}
    20  	close(ch)
    21  	fmt.Println(Collect(Chan(ch)))
    22  }
    23  
    24  func TestRunes(t *testing.T) {
    25  	s := "你好,hello"
    26  	ForEach(func(r rune) {
    27  		fmt.Println(string(r))
    28  	}, Runes(s))
    29  }
    30  
    31  func TestRuneEntries(t *testing.T) {
    32  	s := "你好,hello"
    33  	ForEach(func(e stringUtil.RuneEntry) {
    34  		fmt.Println(e.Index, string(e.Rune))
    35  	}, RuneEntries(s))
    36  }
    37  
    38  func TestMap(t *testing.T) {
    39  	m := map[string]any{
    40  		"host": "192.168.1.1",
    41  		"port": 8000,
    42  	}
    43  	ForEach(func(e maps.Entry[string, any]) {
    44  		fmt.Println(e.Key, e.Value)
    45  	}, MapEntries(m))
    46  	ForEach(func(k string) {
    47  		fmt.Println(k)
    48  	}, Keys(m))
    49  	ForEach(func(v any) {
    50  		fmt.Println(v)
    51  	}, Values(m))
    52  }
    53  
    54  func initBenchmarkSlice() []string {
    55  	var ss []string
    56  	for i := 0; i < unit.MeBiByte; i++ {
    57  		ss = append(ss, "elem-"+strconv.Itoa(i))
    58  	}
    59  	return ss
    60  }
    61  
    62  func BenchmarkSlice(b *testing.B) {
    63  	b.Run("range", func(b *testing.B) {
    64  		ss := initBenchmarkSlice()
    65  		b.ResetTimer()
    66  		for i := 0; i < b.N; i++ {
    67  			for _, s := range ss {
    68  				if !strings.HasPrefix(s, "elem") {
    69  					b.Fatal(s)
    70  				}
    71  			}
    72  		}
    73  	})
    74  	b.Run("iter", func(b *testing.B) {
    75  		ss := initBenchmarkSlice()
    76  		b.ResetTimer()
    77  		for i := 0; i < b.N; i++ {
    78  			iter := Slice(ss)
    79  			for {
    80  				if s, ok := iter(); ok {
    81  					if !strings.HasPrefix(s, "elem") {
    82  						b.Fatal(s)
    83  					}
    84  				} else {
    85  					break
    86  				}
    87  			}
    88  		}
    89  	})
    90  	b.Run("foreach", func(b *testing.B) {
    91  		ss := initBenchmarkSlice()
    92  		b.ResetTimer()
    93  		for i := 0; i < b.N; i++ {
    94  			ForEach(func(s string) {
    95  				if !strings.HasPrefix(s, "elem") {
    96  					b.Fatal(s)
    97  				}
    98  			}, Slice(ss))
    99  		}
   100  	})
   101  }
   102  
   103  func BenchmarkSliceEntries(b *testing.B) {
   104  	b.Run("range", func(b *testing.B) {
   105  		ss := initBenchmarkSlice()
   106  		b.ResetTimer()
   107  		for i := 0; i < b.N; i++ {
   108  			for i, s := range ss {
   109  				if !strings.HasPrefix(s, "elem-") {
   110  					b.Fatal(s)
   111  				}
   112  				if s[len("elem-"):] != strconv.Itoa(i) {
   113  					b.Fatal(i, s)
   114  				}
   115  			}
   116  		}
   117  	})
   118  	b.Run("iter", func(b *testing.B) {
   119  		ss := initBenchmarkSlice()
   120  		b.ResetTimer()
   121  		for i := 0; i < b.N; i++ {
   122  			iter := SliceEntries(ss)
   123  			for {
   124  				if e, ok := iter(); ok {
   125  					if !strings.HasPrefix(e.Elem, "elem-") {
   126  						b.Fatal(e.Elem)
   127  					}
   128  					if e.Elem[len("elem-"):] != strconv.Itoa(e.Index) {
   129  						b.Fatal(e.Index, e.Elem)
   130  					}
   131  				} else {
   132  					break
   133  				}
   134  			}
   135  		}
   136  	})
   137  	b.Run("foreach", func(b *testing.B) {
   138  		ss := initBenchmarkSlice()
   139  		b.ResetTimer()
   140  		for i := 0; i < b.N; i++ {
   141  			ForEach(func(e slice.Entry[string]) {
   142  				if !strings.HasPrefix(e.Elem, "elem-") {
   143  					b.Fatal(e.Elem)
   144  				}
   145  				if e.Elem[len("elem-"):] != strconv.Itoa(e.Index) {
   146  					b.Fatal(e.Index, e.Elem)
   147  				}
   148  			}, SliceEntries(ss))
   149  		}
   150  	})
   151  }