github.com/searKing/golang/go@v1.2.117/go/generator/generator_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 generator_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/searKing/golang/go/go/generator"
    13  )
    14  
    15  // Test the basic function calling behavior. Correct queueing
    16  // behavior is tested elsewhere, since After and AfterFunc share
    17  // the same code.
    18  func TestGeneratorFuncWithSupplier(t *testing.T) {
    19  	const unit = 25 * time.Millisecond
    20  	var i, j int
    21  	n := 10
    22  	c := make(chan bool)
    23  	supplierC := make(chan any)
    24  	f := func(msg any) {
    25  		j++
    26  		if j == n {
    27  			c <- true
    28  		}
    29  	}
    30  
    31  	go func() {
    32  		for {
    33  			i++
    34  			if i <= n {
    35  				supplierC <- i
    36  				if i == 0 {
    37  					return
    38  				}
    39  				time.Sleep(unit)
    40  				continue
    41  			}
    42  			return
    43  		}
    44  	}()
    45  
    46  	generator.GeneratorFuncWithSupplier(supplierC, f)
    47  	<-c
    48  }
    49  
    50  func TestGenerator_Stop(t *testing.T) {
    51  	const unit = 25 * time.Millisecond
    52  	var i, j int
    53  	n := 10
    54  	accept := 5
    55  	c := make(chan bool)
    56  	supplierC := make(chan any)
    57  	var g *generator.Generator
    58  	f := func(msg any) {
    59  		j++
    60  		if j == accept {
    61  			g.Stop()
    62  			c <- true
    63  		}
    64  	}
    65  
    66  	go func() {
    67  		for {
    68  			i++
    69  			if i <= n {
    70  				supplierC <- i
    71  				if i == 0 {
    72  					return
    73  				}
    74  				time.Sleep(unit)
    75  				continue
    76  			}
    77  			return
    78  		}
    79  	}()
    80  
    81  	g = generator.GeneratorFuncWithSupplier(supplierC, f)
    82  	<-c
    83  }
    84  
    85  func TestGenerator_Next(t *testing.T) {
    86  	const unit = 25 * time.Millisecond
    87  	var i, j int
    88  	n := 10
    89  	accept := 5
    90  	c := make(chan bool)
    91  	supplierC := make(chan any)
    92  	go func() {
    93  		for {
    94  			i++
    95  			if i <= n {
    96  				supplierC <- i
    97  				if i == 0 {
    98  					return
    99  				}
   100  				time.Sleep(unit)
   101  				continue
   102  			}
   103  			return
   104  		}
   105  	}()
   106  
   107  	g := generator.GeneratorWithSupplier(supplierC)
   108  
   109  	go func() {
   110  		for {
   111  			_, ok := g.Next()
   112  			if !ok {
   113  				break
   114  			}
   115  			j++
   116  			if j == accept {
   117  				g.Stop()
   118  				break
   119  			}
   120  		}
   121  		c <- true
   122  	}()
   123  	<-c
   124  }
   125  
   126  func TestGenerator_Yield(t *testing.T) {
   127  	var g *generator.Generator
   128  
   129  	supplierC := make(chan any, 100)
   130  	supplierF := func(i int) {
   131  		yield := g.Yield(supplierC)
   132  		if !yield(i) {
   133  			return
   134  		}
   135  		if !yield(i + 10) {
   136  			return
   137  		}
   138  		close(supplierC)
   139  	}
   140  	g = generator.GeneratorWithSupplier(supplierC)
   141  	supplierF(10)
   142  
   143  	for msg := range g.C {
   144  		fmt.Println(msg)
   145  	}
   146  }
   147  
   148  func TestGeneratorVariadicFunc(t *testing.T) {
   149  	g := generator.GeneratorVariadicFunc(func(yield generator.Yield, args ...any) {
   150  		i := (args[0]).(int)
   151  		if !yield(i) {
   152  			return
   153  		}
   154  		if !yield(i + 10) {
   155  			return
   156  		}
   157  	})
   158  
   159  	gen := g(10)
   160  
   161  	for msg := range gen.C {
   162  		fmt.Println(msg)
   163  	}
   164  }
   165  
   166  func TestGeneratorFuncClosure(t *testing.T) {
   167  	g := func(i int) *generator.Generator {
   168  		return generator.GeneratorFunc(func(yield generator.Yield) {
   169  			if !yield(i) {
   170  				return
   171  			}
   172  			if !yield(i + 10) {
   173  				return
   174  			}
   175  		})
   176  	}
   177  
   178  	gen := g(10)
   179  
   180  	for msg := range gen.C {
   181  		fmt.Println(msg)
   182  	}
   183  }
   184  
   185  func TestNewGeneratorFunc(t *testing.T) {
   186  	i := 10
   187  	gen := generator.GeneratorFunc(func(yield generator.Yield) {
   188  		if !yield(i) {
   189  			return
   190  		}
   191  		if !yield(i + 10) {
   192  			return
   193  		}
   194  	})
   195  
   196  	for msg := range gen.C {
   197  		fmt.Println(msg)
   198  	}
   199  }