From 1d8ceaedbb785ccac935bc8c46488a85efcc3000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Mitrovi=C4=87?= Date: Wed, 24 Apr 2024 11:51:25 +0200 Subject: [PATCH 1/2] add randint for generating unsinged integers of every size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dušan Mitrović --- randint/randint.go | 59 +++++++++++++++++++++++++++++++++++++ randint/randint_test.go | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 randint/randint.go create mode 100644 randint/randint_test.go diff --git a/randint/randint.go b/randint/randint.go new file mode 100644 index 0000000..1f4f9e1 --- /dev/null +++ b/randint/randint.go @@ -0,0 +1,59 @@ +package randint + +import ( + "crypto/rand" + "encoding/binary" +) + +const ( + uint8Len = 1 + uint16Len = 2 + uint32Len = 4 + uint64Len = 8 +) + +func generateBytes(length int) ([]byte, error) { + b := make([]byte, length) + + if _, err := rand.Read(b); err != nil { + return nil, err + } + + return b, nil +} + +func Uint8() (uint8, error) { + b, err := generateBytes(uint8Len) + if err != nil { + return 0, err + } + + return uint8(b[0]), nil +} + +func Uint16() (uint16, error) { + b, err := generateBytes(uint16Len) + if err != nil { + return 0, err + } + + return binary.LittleEndian.Uint16(b), nil +} + +func Uint32() (uint32, error) { + b, err := generateBytes(uint32Len) + if err != nil { + return 0, err + } + + return binary.LittleEndian.Uint32(b), nil +} + +func Uint64() (uint64, error) { + b, err := generateBytes(uint64Len) + if err != nil { + return 0, err + } + + return binary.LittleEndian.Uint64(b), nil +} diff --git a/randint/randint_test.go b/randint/randint_test.go new file mode 100644 index 0000000..2cc8020 --- /dev/null +++ b/randint/randint_test.go @@ -0,0 +1,64 @@ +package randint + +import ( + "testing" + "unsafe" + + "github.com/stretchr/testify/require" +) + +func TestUint8(t *testing.T) { + t.Parallel() + + // arrange + assert := require.New(t) + + // act + v, err := Uint8() + + // assert + assert.NoError(err) + assert.Equal(uint8Len, int(unsafe.Sizeof(v))) +} + +func TestUint16(t *testing.T) { + t.Parallel() + + // arrange + assert := require.New(t) + + // act + v, err := Uint16() + + // assert + assert.NoError(err) + assert.Equal(uint16Len, int(unsafe.Sizeof(v))) +} + +func TestUint32(t *testing.T) { + t.Parallel() + + // arrange + assert := require.New(t) + + // act + v, err := Uint32() + + // assert + assert.NoError(err) + assert.Equal(uint32Len, int(unsafe.Sizeof(v))) +} + +func TestUint64(t *testing.T) { + t.Parallel() + + // arrange + assert := require.New(t) + + // act + v, err := Uint64() + + // assert + assert.NoError(err) + assert.Equal(uint64Len, int(unsafe.Sizeof(v))) +} From 48fb84c5552a1ebc72c92cba14c66913e60c56e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Mitrovi=C4=87?= Date: Wed, 24 Apr 2024 14:12:15 +0200 Subject: [PATCH 2/2] Use native endian MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dušan Mitrović --- randint/randint.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/randint/randint.go b/randint/randint.go index 1f4f9e1..90805cf 100644 --- a/randint/randint.go +++ b/randint/randint.go @@ -37,7 +37,7 @@ func Uint16() (uint16, error) { return 0, err } - return binary.LittleEndian.Uint16(b), nil + return binary.NativeEndian.Uint16(b), nil } func Uint32() (uint32, error) { @@ -46,7 +46,7 @@ func Uint32() (uint32, error) { return 0, err } - return binary.LittleEndian.Uint32(b), nil + return binary.NativeEndian.Uint32(b), nil } func Uint64() (uint64, error) { @@ -55,5 +55,5 @@ func Uint64() (uint64, error) { return 0, err } - return binary.LittleEndian.Uint64(b), nil + return binary.NativeEndian.Uint64(b), nil }