github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/queues/testgen/testgen.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"text/template"
     7  )
     8  
     9  const Header = `package goqueuestest
    10  // DO NOT EDIT, GENERATED CODE
    11  import "testing"
    12  
    13  `
    14  
    15  const testTemplate = `
    16  // {{.Name}}
    17  
    18  func Test{{.Name}}(t *testing.T) {
    19  	{{.Type}}Test(t, {{.Ctor}})
    20  	queuePTest(t, {{.Ctor}})
    21  	queueP2Test(t, {{.Ctor}})
    22  }
    23  `
    24  const benchTemplate = "func Benchmark{{.Name}}_{{.Bench}}x{{.Routines}}(b *testing.B) { runParallelBench(b, {{.Routines}}, {{.Ctor}}, bench{{.Bench}})}\n"
    25  
    26  type queue struct {
    27  	Name, Ctor, Type string
    28  }
    29  
    30  type bench struct {
    31  	queue
    32  	Bench string
    33  	Routines int
    34  }
    35  
    36  var queues = []queue{
    37  	{"CFifo", "NewChanFifo(benchIterations)", "fifo"},
    38  	{"ZLifo", "NewZLifo()", "lifo"},
    39  	{"ZFifo", "NewZFifo()", "fifo"},
    40  	// {"ZcFifo", "NewZFifoFreechan()", "fifo"},
    41  	// {"ZrFifo", "NewZFifoFreering()", "fifo"},
    42  	{"ScLifo", "NewScLifo()", "lifo"},
    43  	{"ScFifo", "NewScFifo()", "fifo"},
    44  	{"SmLifo", "NewSmLifo()", "lifo"},
    45  	{"SmFifo", "NewSmFifo()", "fifo"},
    46  	{"RcLifo", "NewRcLifo()", "lifo"},
    47  	{"RcFifo", "NewRcFifo()", "fifo"},
    48  	{"RmLifo", "NewRmLifo()", "lifo"},
    49  	{"RmFifo", "NewRmFifo()", "fifo"},
    50  	//{"PcLifo", "NewPcLifo()", "lifo"},
    51  	//{"PcFifo", "NewPcFifo()", "fifo"},
    52  	//{"PmLifo", "NewPmLifo()", "lifo"},
    53  	//{"PmFifo", "NewPmFifo()", "fifo"},
    54  	//{"LcLifo", "NewListCLifo()", "lifo"},
    55  	//{"LcFifo", "NewListCFifo()", "fifo"},
    56  	//{"LmLifo", "NewListLifo()", "lifo"},
    57  	//{"LmFifo", "NewListFifo()", "fifo"},
    58  }
    59  
    60  var routines = []int { 1, 3, 5, 10, 50, 100 }
    61  var benchs = []string { "ANRN", "A1R1", "A1R2", "A2R1" }
    62  
    63  func main() {
    64  	tTest := template.Must(template.New("testset").Parse(testTemplate))
    65  	tBench := template.Must(template.New("benchmark").Parse(benchTemplate))
    66  
    67  	fmt.Printf(Header)
    68  	for _, q := range queues {
    69  		tTest.Execute(os.Stdout, q)
    70  		for _, benchName := range benchs {
    71  			for _, routines := range routines {
    72  				var b bench
    73  				b.Name = q.Name
    74  				b.Ctor = q.Ctor
    75  				b.Type = q.Type
    76  				b.Bench = benchName
    77  				b.Routines = routines
    78  				tBench.Execute(os.Stdout, b)
    79  			}
    80  		}
    81  	}
    82  }