Skip to content

Commit

Permalink
unexport rand (#15)
Browse files Browse the repository at this point in the history
* unexport rand

* gofmt
  • Loading branch information
tac0turtle authored Jul 31, 2019
1 parent 4fbf5ff commit 94017c8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestBackendsGetSetDelete(t *testing.T) {
}

func withDB(t *testing.T, creator dbCreator, fn func(DB)) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db, err := creator(name, dir)
require.Nil(t, err)
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestBackendsNilKeys(t *testing.T) {
}

func TestGoLevelDBBackend(t *testing.T) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
db := NewDB(name, GoLevelDBBackend, "")
defer cleanupDBDir("", name)

Expand All @@ -164,7 +164,7 @@ func TestDBIterator(t *testing.T) {
}

func testDBIterator(t *testing.T, backend DBBackendType) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db := NewDB(name, backend, dir)
defer cleanupDBDir(dir, name)
Expand Down
4 changes: 2 additions & 2 deletions boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestBoltDBNewBoltDB(t *testing.T) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
defer cleanupDBDir(dir, name)

Expand All @@ -21,7 +21,7 @@ func TestBoltDBNewBoltDB(t *testing.T) {
}

func BenchmarkBoltDBRandomReadsWrites(b *testing.B) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
db, err := NewBoltDB(name, "")
if err != nil {
b.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions c_level_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BenchmarkRandomReadsWrites2(b *testing.B) {
for i := 0; i < int(numItems); i++ {
internal[int64(i)] = int64(0)
}
db, err := NewCLevelDB(fmt.Sprintf("test_%x", RandStr(12)), "")
db, err := NewCLevelDB(fmt.Sprintf("test_%x", randStr(12)), "")
if err != nil {
b.Fatal(err.Error())
return
Expand Down Expand Up @@ -88,7 +88,7 @@ func bytes2Int64(buf []byte) int64 {
*/

func TestCLevelDBBackend(t *testing.T) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
// Can't use "" (current directory) or "./" here because levigo.Open returns:
// "Error initializing DB: IO error: test_XXX.db: Invalid argument"
dir := os.TempDir()
Expand All @@ -100,7 +100,7 @@ func TestCLevelDBBackend(t *testing.T) {
}

func TestCLevelDBStats(t *testing.T) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
dir := os.TempDir()
db := NewDB(name, CLevelDBBackend, dir)
defer cleanupDBDir(dir, name)
Expand Down
4 changes: 2 additions & 2 deletions go_level_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestGoLevelDBNewGoLevelDB(t *testing.T) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
defer cleanupDBDir("", name)

// Test we can't open the db twice for writing
Expand All @@ -29,7 +29,7 @@ func TestGoLevelDBNewGoLevelDB(t *testing.T) {
}

func BenchmarkGoLevelDBRandomReadsWrites(b *testing.B) {
name := fmt.Sprintf("test_%x", RandStr(12))
name := fmt.Sprintf("test_%x", randStr(12))
db, err := NewGoLevelDB(name, "")
if err != nil {
b.Fatal(err)
Expand Down
31 changes: 31 additions & 0 deletions test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package db

import "math/rand"

const (
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
)

// Str constructs a random alphanumeric string of given length.
func randStr(length int) string {
chars := []byte{}
MAIN_LOOP:
for {
val := rand.Int63()
for i := 0; i < 10; i++ {
v := int(val & 0x3f) // rightmost 6 bits
if v >= 62 { // only 62 characters in strChars
val >>= 6
continue
} else {
chars = append(chars, strChars[v])
if len(chars) == length {
break MAIN_LOOP
}
val >>= 6
}
}
}

return string(chars)
}
29 changes: 0 additions & 29 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"bytes"
"math/rand"
"os"
)

Expand Down Expand Up @@ -50,31 +49,3 @@ func FileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}

const (
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
)

// Str constructs a random alphanumeric string of given length.
func RandStr(length int) string {
chars := []byte{}
MAIN_LOOP:
for {
val := rand.Int63()
for i := 0; i < 10; i++ {
v := int(val & 0x3f) // rightmost 6 bits
if v >= 62 { // only 62 characters in strChars
val >>= 6
continue
} else {
chars = append(chars, strChars[v])
if len(chars) == length {
break MAIN_LOOP
}
val >>= 6
}
}
}

return string(chars)
}

0 comments on commit 94017c8

Please sign in to comment.