forked from Vonage/gosrvlib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AUTOPR] Automatic updates (Vonage#241)
* Add RandString function --------- Co-authored-by: nicolaasuni-vonage <nicola.asuni@vonage.com>
- Loading branch information
1 parent
9c2e76b
commit 134fa91
Showing
8 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.89.0 | ||
1.90.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package random_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Vonage/gosrvlib/pkg/random" | ||
) | ||
|
||
//nolint:testableexamples | ||
func ExampleRnd_RandUint32() { | ||
r := random.New(nil) | ||
|
||
n := r.RandUint32() | ||
|
||
fmt.Println(n) | ||
} | ||
|
||
//nolint:testableexamples | ||
func ExampleRnd_RandUint64() { | ||
r := random.New(nil) | ||
|
||
n := r.RandUint64() | ||
|
||
fmt.Println(n) | ||
} | ||
|
||
//nolint:testableexamples | ||
func ExampleRnd_RandString() { | ||
r := random.New(nil) | ||
|
||
s, err := r.RandString(16) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package random | ||
|
||
// Option is the interface that allows to set client options. | ||
type Option func(c *Rnd) | ||
|
||
// WithByteToCharMap overwrites the default slice used to map random bytes to characters. | ||
// If cm is empty, then the default character map will be used. | ||
// The maximum cm length is 256, if it is greater than 256, it will be truncated. | ||
func WithByteToCharMap(cm []byte) Option { | ||
switch d := len(cm); { | ||
case d == 0: | ||
cm = []byte(chrMap) | ||
case d > chrMapMaxLen: | ||
cm = cm[:chrMapMaxLen] | ||
} | ||
|
||
return func(c *Rnd) { | ||
c.chrMap = cm | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package random | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWithByteToCharMap(t *testing.T) { | ||
t.Parallel() | ||
|
||
want := []byte(chrMap) | ||
c := &Rnd{} | ||
|
||
WithByteToCharMap(want)(c) | ||
require.Equal(t, want, c.chrMap) | ||
|
||
WithByteToCharMap(nil)(c) | ||
require.Equal(t, want, c.chrMap) | ||
|
||
WithByteToCharMap([]byte{})(c) | ||
require.Equal(t, want, c.chrMap) | ||
|
||
WithByteToCharMap([]byte("0123456789abcdefx"))(c) | ||
require.Len(t, c.chrMap, 17) | ||
|
||
longMap := make([]byte, chrMapMaxLen+1) | ||
|
||
WithByteToCharMap(longMap)(c) | ||
require.Len(t, c.chrMap, chrMapMaxLen) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package random | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func BenchmarkRnd_RandUint32(b *testing.B) { | ||
b.ResetTimer() | ||
|
||
r := New(nil) | ||
|
||
for i := 0; i < b.N; i++ { | ||
_ = r.RandUint32() | ||
} | ||
} | ||
|
||
func BenchmarkRnd_RandUint64(b *testing.B) { | ||
b.ResetTimer() | ||
|
||
r := New(nil) | ||
|
||
for i := 0; i < b.N; i++ { | ||
_ = r.RandUint64() | ||
} | ||
} | ||
|
||
func BenchmarkRnd_RandString(b *testing.B) { | ||
b.ResetTimer() | ||
|
||
r := New(nil) | ||
|
||
for i := 0; i < b.N; i++ { | ||
_, _ = r.RandString(16) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters