...

Source file src/github.com/PaesslerAG/gval/random_test.go

Documentation: github.com/PaesslerAG/gval

     1  package gval
     2  
     3  // Courtesy of abrander
     4  // ref: https://gist.github.com/abrander/fa05ae9b181b48ffe7afb12c961b6e90
     5  import (
     6  	"fmt"
     7  	"math/rand"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  var (
    13  	hello  = "hello"
    14  	empty  struct{}
    15  	empty2 *string
    16  
    17  	values = []interface{}{
    18  		-1,
    19  		0,
    20  		12,
    21  		13,
    22  		"",
    23  		"hello",
    24  		&hello,
    25  		nil,
    26  		"nil",
    27  		empty,
    28  		empty2,
    29  		true,
    30  		false,
    31  		time.Now(),
    32  		rune('r'),
    33  		int64(34),
    34  		time.Duration(0),
    35  		"true",
    36  		"false",
    37  		"\ntrue\n",
    38  		"\nfalse\n",
    39  		"12",
    40  		"nil",
    41  		"arg1",
    42  		"arg2",
    43  		int(12),
    44  		int32(12),
    45  		int64(12),
    46  		complex(1.0, 1.0),
    47  		[]byte{0, 0, 0},
    48  		[]int{0, 0, 0},
    49  		[]string{},
    50  		"[]",
    51  		"{}",
    52  		"\"\"",
    53  		"\"12\"",
    54  		"\"hello\"",
    55  		".*",
    56  		"==",
    57  		"!=",
    58  		">",
    59  		">=",
    60  		"<",
    61  		"<=",
    62  		"=~",
    63  		"!~",
    64  		"in",
    65  		"&&",
    66  		"||",
    67  		"^",
    68  		"&",
    69  		"|",
    70  		">>",
    71  		"<<",
    72  		"+",
    73  		"-",
    74  		"*",
    75  		"/",
    76  		"%",
    77  		"**",
    78  		"-",
    79  		"!",
    80  		"~",
    81  		"?",
    82  		":",
    83  		"??",
    84  		"+",
    85  		"-",
    86  		"*",
    87  		"/",
    88  		"%",
    89  		"**",
    90  		"&",
    91  		"|",
    92  		"^",
    93  		">>",
    94  		"<<",
    95  		",",
    96  		"(",
    97  		")",
    98  		"[",
    99  		"]",
   100  		"\n",
   101  		"\000",
   102  	}
   103  )
   104  
   105  const SEED = 1487873697990155515
   106  
   107  func BenchmarkRandom(bench *testing.B) {
   108  	rand.Seed(SEED)
   109  	for i := 0; i < bench.N; i++ {
   110  		num := rand.Intn(3) + 2
   111  		expression := ""
   112  
   113  		for n := 0; n < num; n++ {
   114  			expression += fmt.Sprintf(" %s", getRandom(values))
   115  		}
   116  
   117  		Evaluate(expression, nil)
   118  	}
   119  }
   120  
   121  func getRandom(haystack []interface{}) interface{} {
   122  	i := rand.Intn(len(haystack))
   123  	return haystack[i]
   124  }
   125  

View as plain text