Skip to content

Commit

Permalink
👔 up: update some for gen random chars
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 13, 2023
1 parent d10983d commit da9fd86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 4 additions & 5 deletions strutil/gensn.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,15 @@ func DateSNV2(prefix string, extBase ...int) string {
// micro datetime
nt := time.Now()
bs = nt.AppendFormat(bs, "20060102150405.000000")
bs = bs[:14+pl] // prefix + yyyyMMddHHmmss
ext := bs[15+pl:] // 6bit micro + 5bit rand

// rand 10000 - 99999
rs := rand.New(rand.NewSource(nt.UnixNano()))
ext = strconv.AppendInt(ext, 10000+rs.Int63n(89999), 10)
// 6bit micro + 5bit rand
ext := strconv.AppendInt(bs[16+pl:], 10000+rs.Int63n(89999), 10)

base := basefn.FirstOr(extBase, 16)
// convert ext to base
bs = append(bs[:15+pl], strconv.FormatInt(SafeInt64(string(ext)), base)...)
// prefix + yyyyMMddHHmmss + ext(convert to base)
bs = append(bs[:14+pl], strconv.FormatInt(SafeInt64(string(ext)), base)...)

return string(bs)
}
22 changes: 21 additions & 1 deletion strutil/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"fmt"
"math/rand"
"runtime"
"testing"
"time"

Expand All @@ -27,6 +28,11 @@ func TestRandomCharsV2(t *testing.T) {
assert.Len(t, str, 6)
keyMp[str] = true
}

if runtime.GOOS == "windows" {
t.Skip("skip test unique on windows")
return
}
assert.Len(t, keyMp, 10)
}

Expand All @@ -41,6 +47,10 @@ func TestRandomCharsV2_issues121(t *testing.T) {
keyMp[str] = true
}

if runtime.GOOS == "windows" {
t.Skip("skip test unique on windows")
return
}
assert.Len(t, keyMp, 10)
}

Expand All @@ -54,6 +64,11 @@ func TestRandomCharsV3(t *testing.T) {
assert.Len(t, str, 4)
keyMp[str] = true
}

if runtime.GOOS == "windows" {
t.Skip("skip test unique on windows")
return
}
assert.Len(t, keyMp, size)
}

Expand All @@ -67,10 +82,15 @@ func TestRandWithTpl(t *testing.T) {
assert.Len(t, str, 4)
keyMp[str] = true
}
assert.Len(t, keyMp, size)

assert.NotEmpty(t, strutil.RandWithTpl(8, ""))
assert.NotEmpty(t, strutil.RandWithTpl(8, strutil.AlphaBet1))

if runtime.GOOS == "windows" {
t.Skip("skip test unique on windows")
return
}
assert.Len(t, keyMp, size)
}

func TestRandomBytes(t *testing.T) {
Expand Down

0 comments on commit da9fd86

Please sign in to comment.