implement svg interface in login and signup
This commit is contained in:
39
util/util.go
39
util/util.go
@@ -60,6 +60,15 @@ func GenerateRandomUInt64() (uint64, error) {
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func GenerateRandomInt() (int, error) {
|
||||
randBytes, err := RandomBytes(8)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
val := int(binary.LittleEndian.Uint64(randBytes) & 0x7FFFFFFFFFFFFFFF) // Ensure it's positive
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func GenerateRandomNonRepeatingUint64(listLen int) ([]uint64, error) {
|
||||
if listLen > int(1)<<32 {
|
||||
return nil, errors.New("list length must be less than 2^32")
|
||||
@@ -80,6 +89,26 @@ func GenerateRandomNonRepeatingUint64(listLen int) ([]uint64, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func GenerateRandomNonRepeatingInt(listLen int) ([]int, error) {
|
||||
if listLen > int(1)<<31 {
|
||||
return nil, errors.New("list length must be less than 2^31")
|
||||
}
|
||||
listSet := make(hashset.Set[int])
|
||||
for {
|
||||
if listSet.Size() == listLen {
|
||||
break
|
||||
}
|
||||
randNum, err := GenerateRandomInt()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
listSet.Add(randNum)
|
||||
}
|
||||
|
||||
data := listSet.ToSlice()
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func XorLists(l0 []uint64, l1 []uint64) ([]uint64, error) {
|
||||
if len(l0) != len(l1) {
|
||||
return nil, errors.New(fmt.Sprintf("list len mismatch %d, %d", len(l0), len(l1)))
|
||||
@@ -222,3 +251,13 @@ func Choice[T any](items []T) T {
|
||||
r.Seed(time.Now().UnixNano()) // Seed the random number generator
|
||||
return items[r.Intn(len(items))]
|
||||
}
|
||||
|
||||
// GenerateRandomString creates a random string of a specified length.
|
||||
func GenerateRandomString(length int) string {
|
||||
charset := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
b := make([]rune, length)
|
||||
for i := range b {
|
||||
b[i] = Choice[rune](charset)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,14 @@ func TestGenerateRandomNonRepeatingUint64(t *testing.T) {
|
||||
assert.Equal(t, len(randNumbs), arrLen)
|
||||
}
|
||||
|
||||
func TestGenerateRandomNonRepeatingInt(t *testing.T) {
|
||||
arrLen := 100000
|
||||
randNumbs, err := GenerateRandomNonRepeatingInt(arrLen)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, len(randNumbs), arrLen)
|
||||
}
|
||||
|
||||
func TestEncodeDecode(t *testing.T) {
|
||||
testArr := []uint64{1, 2, 3, 4, 5, 6}
|
||||
testEncode := EncodeBase64Str(testArr)
|
||||
|
||||
Reference in New Issue
Block a user