Skip to content

Commit

Permalink
Add hex value
Browse files Browse the repository at this point in the history
  • Loading branch information
zacscoding committed Aug 12, 2021
1 parent 1cd1c99 commit 5fcafcc
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ Float64() float64
Float64Range(min, max float64) float64
ShuffleInts(a []int)
RandomInt(i []int) int
HexUint8() string
HexUint16() string
HexUint32() string
HexUint64() string
HexUint128() string
HexUint256() string
```

### String
Expand Down
117 changes: 117 additions & 0 deletions number.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,57 @@ func randomUint(r *rand.Rand, u []uint) uint {
return u[r.Intn(size)]
}

// HexUint8 will generate a random uint8 hex value with "0x" prefix
func HexUint8() string { return hexUint(globalFaker.Rand, 8) }

// HexUint8 will generate a random uint8 hex value with "0x" prefix
func (f *Faker) HexUint8() string { return hexUint(f.Rand, 8) }

// HexUint16 will generate a random uint16 hex value with "0x" prefix
func HexUint16() string { return hexUint(globalFaker.Rand, 16) }

// HexUint16 will generate a random uint16 hex value with "0x" prefix
func (f *Faker) HexUint16() string { return hexUint(f.Rand, 16) }

// HexUint32 will generate a random uint32 hex value with "0x" prefix
func HexUint32() string { return hexUint(globalFaker.Rand, 32) }

// HexUint32 will generate a random uint32 hex value with "0x" prefix
func (f *Faker) HexUint32() string { return hexUint(f.Rand, 32) }

// HexUint64 will generate a random uint64 hex value with "0x" prefix
func HexUint64() string { return hexUint(globalFaker.Rand, 64) }

// HexUint64 will generate a random uint64 hex value with "0x" prefix
func (f *Faker) HexUint64() string { return hexUint(f.Rand, 64) }

// HexUint128 will generate a random uint128 hex value with "0x" prefix
func HexUint128() string { return hexUint(globalFaker.Rand, 128) }

// HexUint128 will generate a random uint128 hex value with "0x" prefix
func (f *Faker) HexUint128() string { return hexUint(f.Rand, 128) }

// HexUint256 will generate a random uint256 hex value with "0x" prefix
func HexUint256() string { return hexUint(globalFaker.Rand, 256) }

// HexUint256 will generate a random uint256 hex value with "0x" prefix
func (f *Faker) HexUint256() string { return hexUint(f.Rand, 256) }

func hexUint(r *rand.Rand, bitSize int) string {
digits := []byte("0123456789abcdef")
hexLen := (bitSize >> 2) + 2
if hexLen <= 2 {
return "0x"
}

s := make([]byte, hexLen)
s[0], s[1] = '0', 'x'
for i := 2; i < hexLen; i++ {
s[i] = digits[r.Intn(16)]
}
return string(s)
}

func addNumberLookup() {
AddFuncLookup("number", Info{
Display: "Number",
Expand Down Expand Up @@ -393,4 +444,70 @@ func addNumberLookup() {
return ints, nil
},
})

AddFuncLookup("hexuint8", Info{
Display: "HexUint8",
Category: "number",
Description: "Random uint8 hex value",
Example: "0x87",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return hexUint(r, 8), nil
},
})

AddFuncLookup("hexuint16", Info{
Display: "HexUint16",
Category: "number",
Description: "Random uint16 hex value",
Example: "0x8754",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return hexUint(r, 16), nil
},
})

AddFuncLookup("hexuint32", Info{
Display: "HexUint32",
Category: "number",
Description: "Random uint32 hex value",
Example: "0x87546957",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return hexUint(r, 32), nil
},
})

AddFuncLookup("hexuint64", Info{
Display: "HexUint64",
Category: "number",
Description: "Random uint64 hex value",
Example: "0x875469578e51b5e5",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return hexUint(r, 64), nil
},
})

AddFuncLookup("hexuint128", Info{
Display: "HexUint128",
Category: "number",
Description: "Random uint128 hex value",
Example: "0x875469578e51b5e56c95b64681d147a1",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return hexUint(r, 128), nil
},
})

AddFuncLookup("hexuint256", Info{
Display: "HexUint256",
Category: "number",
Description: "Random uint256 hex value",
Example: "0x875469578e51b5e56c95b64681d147a12cde48a4f417231b0c486abbc263e48d",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return hexUint(r, 256), nil
},
})
}
216 changes: 216 additions & 0 deletions number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,219 @@ func BenchmarkRandomUint(b *testing.B) {
}
})
}

func ExampleHexUint8() {
Seed(11)
fmt.Println(HexUint8())
// Output: 0x87
}

func ExampleFaker_HexUint8() {
f := New(11)
fmt.Println(f.HexUint8())
// Output: 0x87
}

func BenchmarkHexUint8(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
HexUint8()
}
})

b.Run("Faker math", func(b *testing.B) {
f := New(0)

for i := 0; i < b.N; i++ {
f.HexUint8()
}
})

b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()

for i := 0; i < b.N; i++ {
f.HexUint8()
}
})
}

func ExampleHexUint16() {
Seed(11)
fmt.Println(HexUint16())
// Output: 0x8754
}

func ExampleFaker_HexUint16() {
f := New(11)
fmt.Println(f.HexUint16())
// Output: 0x8754
}

func BenchmarkHexUint16(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
HexUint16()
}
})

b.Run("Faker math", func(b *testing.B) {
f := New(0)

for i := 0; i < b.N; i++ {
f.HexUint16()
}
})

b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()

for i := 0; i < b.N; i++ {
f.HexUint16()
}
})
}

func ExampleHexUint32() {
Seed(11)
fmt.Println(HexUint32())
// Output: 0x87546957
}

func ExampleFaker_HexUint32() {
f := New(11)
fmt.Println(f.HexUint32())
// Output: 0x87546957
}

func BenchmarkHexUint32(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
HexUint32()
}
})

b.Run("Faker math", func(b *testing.B) {
f := New(0)

for i := 0; i < b.N; i++ {
f.HexUint32()
}
})

b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()

for i := 0; i < b.N; i++ {
f.HexUint32()
}
})
}

func ExampleHexUint64() {
Seed(11)
fmt.Println(HexUint64())
// Output: 0x875469578e51b5e5
}

func ExampleFaker_HexUint64() {
f := New(11)
fmt.Println(f.HexUint64())
// Output: 0x875469578e51b5e5
}

func BenchmarkHexUint64(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
HexUint64()
}
})

b.Run("Faker math", func(b *testing.B) {
f := New(0)

for i := 0; i < b.N; i++ {
f.HexUint64()
}
})

b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()

for i := 0; i < b.N; i++ {
f.HexUint64()
}
})
}

func ExampleHexUint128() {
Seed(11)
fmt.Println(HexUint128())
// Output: 0x875469578e51b5e56c95b64681d147a1
}

func ExampleFaker_HexUint128() {
f := New(11)
fmt.Println(f.HexUint128())
// Output: 0x875469578e51b5e56c95b64681d147a1
}

func BenchmarkHexUint128(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
HexUint128()
}
})

b.Run("Faker math", func(b *testing.B) {
f := New(0)

for i := 0; i < b.N; i++ {
f.HexUint128()
}
})

b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()

for i := 0; i < b.N; i++ {
f.HexUint128()
}
})
}

func ExampleHexUint256() {
Seed(11)
fmt.Println(HexUint256())
// Output: 0x875469578e51b5e56c95b64681d147a12cde48a4f417231b0c486abbc263e48d
}

func ExampleFaker_HexUint256() {
f := New(11)
fmt.Println(f.HexUint256())
// Output: 0x875469578e51b5e56c95b64681d147a12cde48a4f417231b0c486abbc263e48d
}

func BenchmarkHexUint256(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
HexUint256()
}
})

b.Run("Faker math", func(b *testing.B) {
f := New(0)

for i := 0; i < b.N; i++ {
f.HexUint256()
}
})

b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()

for i := 0; i < b.N; i++ {
f.HexUint256()
}
})
}

0 comments on commit 5fcafcc

Please sign in to comment.