PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/staging/src/k8s.io/apimachinery/pkg/util/rand/rand_test.go

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