github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/rand/rand_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rand
    18  
    19  import (
    20  	"math/rand"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  const (
    26  	maxRangeTestCount = 500
    27  	testStringLength  = 32
    28  )
    29  
    30  func TestString(t *testing.T) {
    31  	valid := "bcdfghjklmnpqrstvwxz2456789"
    32  	for _, l := range []int{0, 1, 2, 10, 123} {
    33  		s := String(l)
    34  		if len(s) != l {
    35  			t.Errorf("expected string of size %d, got %q", l, s)
    36  		}
    37  		for _, c := range s {
    38  			if !strings.ContainsRune(valid, c) {
    39  				t.Errorf("expected valid characters, got %v", c)
    40  			}
    41  		}
    42  	}
    43  }
    44  
    45  // Confirm that panic occurs on invalid input.
    46  func TestRangePanic(t *testing.T) {
    47  	defer func() {
    48  		if err := recover(); err == nil {
    49  			t.Errorf("Panic didn't occur!")
    50  		}
    51  	}()
    52  	// Should result in an error...
    53  	Intn(0)
    54  }
    55  
    56  func TestIntn(t *testing.T) {
    57  	// 0 is invalid.
    58  	for _, max := range []int{1, 2, 10, 123} {
    59  		inrange := Intn(max)
    60  		if inrange < 0 || inrange > max {
    61  			t.Errorf("%v out of range (0,%v)", inrange, max)
    62  		}
    63  	}
    64  }
    65  
    66  func TestPerm(t *testing.T) {
    67  	Seed(5)
    68  	rand.Seed(5)
    69  	for i := 1; i < 20; i++ {
    70  		actual := Perm(i)
    71  		expected := rand.Perm(i)
    72  		for j := 0; j < i; j++ {
    73  			if actual[j] != expected[j] {
    74  				t.Errorf("Perm call result is unexpected")
    75  			}
    76  		}
    77  	}
    78  }
    79  
    80  func TestIntnRange(t *testing.T) {
    81  	// 0 is invalid.
    82  	for min, max := range map[int]int{1: 2, 10: 123, 100: 500} {
    83  		for i := 0; i < maxRangeTestCount; i++ {
    84  			inrange := IntnRange(min, max)
    85  			if inrange < min || inrange >= max {
    86  				t.Errorf("%v out of range (%v,%v)", inrange, min, max)
    87  			}
    88  		}
    89  	}
    90  }
    91  
    92  func TestInt63nRange(t *testing.T) {
    93  	// 0 is invalid.
    94  	for min, max := range map[int64]int64{1: 2, 10: 123, 100: 500} {
    95  		for i := 0; i < maxRangeTestCount; i++ {
    96  			inrange := Int63nRange(min, max)
    97  			if inrange < min || inrange >= max {
    98  				t.Errorf("%v out of range (%v,%v)", inrange, min, max)
    99  			}
   100  		}
   101  	}
   102  }
   103  
   104  func BenchmarkRandomStringGeneration(b *testing.B) {
   105  	b.ResetTimer()
   106  	var s string
   107  	for i := 0; i < b.N; i++ {
   108  		s = String(testStringLength)
   109  	}
   110  	b.StopTimer()
   111  	if len(s) == 0 {
   112  		b.Fatal(s)
   113  	}
   114  }