bitbucket.org/ai69/amoy@v0.2.3/random_test.go (about)

     1  package amoy
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestFeelLucky(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  		rate float64
    12  		want bool
    13  	}{
    14  		{"never", 0, false},
    15  		{"always", 1, true},
    16  		//{"am i lucky", 0.5, true},
    17  	}
    18  	for _, tt := range tests {
    19  		t.Run(tt.name, func(t *testing.T) {
    20  			if got := FeelLucky(tt.rate); got != tt.want {
    21  				t.Errorf("FeelLucky(%.6f) = %v, want %v", tt.rate, got, tt.want)
    22  			}
    23  		})
    24  	}
    25  }
    26  
    27  func TestRandomTime(t *testing.T) {
    28  	now := time.Now()
    29  	tests := []struct {
    30  		name    string
    31  		start   time.Time
    32  		d       time.Duration
    33  		wantMin time.Time
    34  		wantMax time.Time
    35  	}{
    36  		{"positive", now, Hours(1), now, now.Add(Hours(1))},
    37  		{"negative", now, -Hours(2), now.Add(Hours(-2)), now},
    38  		{"zero", now, 0, now, now},
    39  	}
    40  	for _, tt := range tests {
    41  		t.Run(tt.name, func(t *testing.T) {
    42  			if got := RandomTime(tt.start, tt.d); !(!tt.wantMin.After(got) && !got.After(tt.wantMax)) {
    43  				t.Errorf("RandomTime() = %v, want [%v, %v)", got, tt.wantMin, tt.wantMax)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func TestRandomTimeBetween(t *testing.T) {
    50  	now := time.Now()
    51  	tests := []struct {
    52  		name    string
    53  		start   time.Time
    54  		end     time.Time
    55  		wantMin time.Time
    56  		wantMax time.Time
    57  	}{
    58  		{"before", now, now.Add(Hours(1)), now, now.Add(Hours(1))},
    59  		{"after", now, now.Add(Hours(-1)), now, now},
    60  		{"same", now, now, now, now},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			if got := RandomTimeBetween(tt.start, tt.end); !(!tt.wantMin.After(got) && !got.After(tt.wantMax)) {
    65  				t.Errorf("RandomTimeBetween() = %v, want [%v, %v)", got, tt.wantMin, tt.wantMax)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestRandomDuration(t *testing.T) {
    72  	tests := []struct {
    73  		name    string
    74  		min     time.Duration
    75  		max     time.Duration
    76  		wantMin time.Duration
    77  		wantMax time.Duration
    78  	}{
    79  		{"normal", Hours(1), Hours(2), Hours(1), Hours(2)},
    80  		{"reversed", Hours(4), Hours(2), Hours(2), Hours(2)},
    81  		{"same", Hours(5), Hours(5), Hours(5), Hours(5)},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			if got := RandomDuration(tt.min, tt.max); !(tt.wantMin <= got && got <= tt.wantMax) {
    86  				t.Errorf("RandomDuration() = %v, want [%v, %v)", got, tt.wantMin, tt.wantMax)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestRandomInt(t *testing.T) {
    93  	tests := []struct {
    94  		name string
    95  		min  int
    96  		max  int
    97  	}{
    98  		{"normal", 1, 10},
    99  		{"nearby", 1, 2},
   100  	}
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			if got := RandomInt(tt.min, tt.max); got < tt.min || tt.max <= got {
   104  				t.Errorf("RandomInt(%v, %v) = %v, not included", tt.min, tt.max, got)
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func TestRandomFloat(t *testing.T) {
   111  	tests := []struct {
   112  		name string
   113  		min  float64
   114  		max  float64
   115  	}{
   116  		{"normal", 0, 1},
   117  		{"nearby", 1, 2},
   118  		{"wide", 1, 200},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			if got := RandomFloat(tt.min, tt.max); got < tt.min || tt.max <= got {
   123  				t.Errorf("RandomFloat(%v, %v) = %v, not included", tt.min, tt.max, got)
   124  			}
   125  		})
   126  	}
   127  }
   128  
   129  func TestRandomString(t *testing.T) {
   130  	tests := []struct {
   131  		name   string
   132  		length int
   133  	}{
   134  		// {"zero", 0},
   135  		{"one", 1},
   136  		{"normal", 10},
   137  		{"large", 1024},
   138  	}
   139  	for _, tt := range tests {
   140  		t.Run(tt.name, func(t *testing.T) {
   141  			if got := RandomString(tt.length); len(got) != tt.length {
   142  				t.Errorf("RandomString(%v) = %v, not enough", tt.length, got)
   143  			}
   144  		})
   145  	}
   146  }