2016-12-15 12:03:02 +01:00
|
|
|
package goutils
|
|
|
|
|
|
|
|
import (
|
2017-05-18 18:54:23 +02:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
2016-12-15 12:03:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ****************************** TESTS ********************************************
|
|
|
|
|
|
|
|
func TestRandomSeed(t *testing.T) {
|
|
|
|
|
|
|
|
// count, start, end, letters, numbers := 5, 0, 0, true, true
|
|
|
|
random := rand.New(rand.NewSource(10))
|
|
|
|
out := "3ip9v"
|
|
|
|
|
|
|
|
// Test 1: Simulating RandomAlphaNumeric(count int)
|
2017-05-18 18:54:23 +02:00
|
|
|
if x, _ := RandomSeed(5, 0, 0, true, true, nil, random); x != out {
|
2016-12-15 12:03:02 +01:00
|
|
|
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, true, true, nil, random, x, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 2: Simulating RandomAlphabetic(count int)
|
|
|
|
out = "MBrbj"
|
|
|
|
|
2017-05-18 18:54:23 +02:00
|
|
|
if x, _ := RandomSeed(5, 0, 0, true, false, nil, random); x != out {
|
2016-12-15 12:03:02 +01:00
|
|
|
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, true, false, nil, random, x, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 3: Simulating RandomNumeric(count int)
|
|
|
|
out = "88935"
|
|
|
|
|
2017-05-18 18:54:23 +02:00
|
|
|
if x, _ := RandomSeed(5, 0, 0, false, true, nil, random); x != out {
|
2016-12-15 12:03:02 +01:00
|
|
|
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, false, true, nil, random, x, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 4: Simulating RandomAscii(count int)
|
|
|
|
out = "H_I;E"
|
|
|
|
|
2017-05-18 18:54:23 +02:00
|
|
|
if x, _ := RandomSeed(5, 32, 127, false, false, nil, random); x != out {
|
2016-12-15 12:03:02 +01:00
|
|
|
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 32, 127, false, false, nil, random, x, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 5: Simulating RandomSeed(...) with custom chars
|
2017-05-18 18:54:23 +02:00
|
|
|
chars := []rune{'1', '2', '3', 'a', 'b', 'c'}
|
2016-12-15 12:03:02 +01:00
|
|
|
out = "2b2ca"
|
|
|
|
|
2017-05-18 18:54:23 +02:00
|
|
|
if x, _ := RandomSeed(5, 0, 0, false, false, chars, random); x != out {
|
2016-12-15 12:03:02 +01:00
|
|
|
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, false, false, chars, random, x, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ****************************** EXAMPLES ********************************************
|
|
|
|
|
|
|
|
func ExampleRandomSeed() {
|
|
|
|
|
2017-05-18 18:54:23 +02:00
|
|
|
var seed int64 = 10 // If you change this seed #, the random sequence below will change
|
|
|
|
random := rand.New(rand.NewSource(seed))
|
|
|
|
chars := []rune{'1', '2', '3', 'a', 'b', 'c'}
|
2016-12-15 12:03:02 +01:00
|
|
|
|
2017-05-18 18:54:23 +02:00
|
|
|
rand1, _ := RandomSeed(5, 0, 0, true, true, nil, random) // RandomAlphaNumeric (Alphabets and numbers possible)
|
|
|
|
rand2, _ := RandomSeed(5, 0, 0, true, false, nil, random) // RandomAlphabetic (Only alphabets)
|
|
|
|
rand3, _ := RandomSeed(5, 0, 0, false, true, nil, random) // RandomNumeric (Only numbers)
|
|
|
|
rand4, _ := RandomSeed(5, 32, 127, false, false, nil, random) // RandomAscii (Alphabets, numbers, and other ASCII chars)
|
|
|
|
rand5, _ := RandomSeed(5, 0, 0, true, true, chars, random) // RandomSeed with custom characters
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
fmt.Println(rand1)
|
|
|
|
fmt.Println(rand2)
|
|
|
|
fmt.Println(rand3)
|
|
|
|
fmt.Println(rand4)
|
|
|
|
fmt.Println(rand5)
|
2017-05-18 18:54:23 +02:00
|
|
|
// Output:
|
2016-12-15 12:03:02 +01:00
|
|
|
// 3ip9v
|
|
|
|
// MBrbj
|
|
|
|
// 88935
|
|
|
|
// H_I;E
|
|
|
|
// 2b2ca
|
|
|
|
}
|