move pkg/util/rand
This commit is contained in:
@@ -5,19 +5,11 @@ licenses(["notice"])
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["rand.go"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["rand_test.go"],
|
||||
library = ":go_default_library",
|
||||
srcs = ["doc.go"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
|
21
pkg/util/rand/doc.go
Normal file
21
pkg/util/rand/doc.go
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package rand needed for godep.
|
||||
// TODO genericapiserver remove this empty package. Godep fails without this because heapster relies
|
||||
// on this package. This will allow us to start splitting packages, but will force
|
||||
// heapster to update on their next kube rebase.
|
||||
package rand
|
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package rand provides utilities related to randomization.
|
||||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var rng = struct {
|
||||
sync.Mutex
|
||||
rand *rand.Rand
|
||||
}{
|
||||
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
|
||||
}
|
||||
|
||||
// Intn generates an integer in range [0,max).
|
||||
// By design this should panic if input is invalid, <= 0.
|
||||
func Intn(max int) int {
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
return rng.rand.Intn(max)
|
||||
}
|
||||
|
||||
// IntnRange generates an integer in range [min,max).
|
||||
// By design this should panic if input is invalid, <= 0.
|
||||
func IntnRange(min, max int) int {
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
return rng.rand.Intn(max-min) + min
|
||||
}
|
||||
|
||||
// IntnRange generates an int64 integer in range [min,max).
|
||||
// By design this should panic if input is invalid, <= 0.
|
||||
func Int63nRange(min, max int64) int64 {
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
return rng.rand.Int63n(max-min) + min
|
||||
}
|
||||
|
||||
// Seed seeds the rng with the provided seed.
|
||||
func Seed(seed int64) {
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
|
||||
rng.rand = rand.New(rand.NewSource(seed))
|
||||
}
|
||||
|
||||
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n)
|
||||
// from the default Source.
|
||||
func Perm(n int) []int {
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
return rng.rand.Perm(n)
|
||||
}
|
||||
|
||||
// We omit vowels from the set of available characters to reduce the chances
|
||||
// of "bad words" being formed.
|
||||
var alphanums = []rune("bcdfghjklmnpqrstvwxz0123456789")
|
||||
|
||||
// String generates a random alphanumeric string, without vowels, which is n
|
||||
// characters long. This will panic if n is less than zero.
|
||||
func String(length int) string {
|
||||
b := make([]rune, length)
|
||||
for i := range b {
|
||||
b[i] = alphanums[Intn(len(alphanums))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
maxRangeTestCount = 500
|
||||
)
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
valid := "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
for _, l := range []int{0, 1, 2, 10, 123} {
|
||||
s := String(l)
|
||||
if len(s) != l {
|
||||
t.Errorf("expected string of size %d, got %q", l, s)
|
||||
}
|
||||
for _, c := range s {
|
||||
if !strings.ContainsRune(valid, c) {
|
||||
t.Errorf("expected valid charaters, got %v", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Confirm that panic occurs on invalid input.
|
||||
func TestRangePanic(t *testing.T) {
|
||||
defer func() {
|
||||
if err := recover(); err == nil {
|
||||
t.Errorf("Panic didn't occur!")
|
||||
}
|
||||
}()
|
||||
// Should result in an error...
|
||||
Intn(0)
|
||||
}
|
||||
|
||||
func TestIntn(t *testing.T) {
|
||||
// 0 is invalid.
|
||||
for _, max := range []int{1, 2, 10, 123} {
|
||||
inrange := Intn(max)
|
||||
if inrange < 0 || inrange > max {
|
||||
t.Errorf("%v out of range (0,%v)", inrange, max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPerm(t *testing.T) {
|
||||
Seed(5)
|
||||
rand.Seed(5)
|
||||
for i := 1; i < 20; i++ {
|
||||
actual := Perm(i)
|
||||
expected := rand.Perm(i)
|
||||
for j := 0; j < i; j++ {
|
||||
if actual[j] != expected[j] {
|
||||
t.Errorf("Perm call result is unexpected")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntnRange(t *testing.T) {
|
||||
// 0 is invalid.
|
||||
for min, max := range map[int]int{1: 2, 10: 123, 100: 500} {
|
||||
for i := 0; i < maxRangeTestCount; i++ {
|
||||
inrange := IntnRange(min, max)
|
||||
if inrange < min || inrange >= max {
|
||||
t.Errorf("%v out of range (%v,%v)", inrange, min, max)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt63nRange(t *testing.T) {
|
||||
// 0 is invalid.
|
||||
for min, max := range map[int64]int64{1: 2, 10: 123, 100: 500} {
|
||||
for i := 0; i < maxRangeTestCount; i++ {
|
||||
inrange := Int63nRange(min, max)
|
||||
if inrange < min || inrange >= max {
|
||||
t.Errorf("%v out of range (%v,%v)", inrange, min, max)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -12,7 +12,7 @@ go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["slice.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = ["//pkg/util/rand:go_default_library"],
|
||||
deps = ["//vendor:k8s.io/apimachinery/pkg/util/rand"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
|
@@ -20,7 +20,7 @@ package slice
|
||||
import (
|
||||
"sort"
|
||||
|
||||
utilrand "k8s.io/kubernetes/pkg/util/rand"
|
||||
utilrand "k8s.io/apimachinery/pkg/util/rand"
|
||||
)
|
||||
|
||||
// CopyStrings copies the contents of the specified string slice
|
||||
|
Reference in New Issue
Block a user