fix nasty names

This commit is contained in:
2025-06-19 20:07:55 +01:00
parent 3b0fd72290
commit b29ad5bd07
9 changed files with 58 additions and 58 deletions

View File

@@ -8,7 +8,7 @@ import (
"lukechampine.com/frand"
)
func TestUint16Codec(t *testing.T) {
func TestUint16(t *testing.T) {
// Helper function to generate random 16-bit integers
generateRandomUint16 := func() uint16 {
return uint16(frand.Intn(math.MaxUint16)) // math.MaxUint16 == 65535

View File

@@ -8,14 +8,14 @@ import (
// MaxUint24 is the maximum value of a 24-bit unsigned integer: 2^24 - 1.
const MaxUint24 uint32 = 1<<24 - 1
// Uint24Codec is a codec for encoding and decoding 24-bit unsigned integers.
type Uint24Codec struct {
// Uint24 is a codec for encoding and decoding 24-bit unsigned integers.
type Uint24 struct {
value uint32
}
// SetUint24 sets the value as a 24-bit unsigned integer.
// If the value exceeds the maximum allowable value for 24 bits, it returns an error.
func (c *Uint24Codec) SetUint24(value uint32) error {
func (c *Uint24) SetUint24(value uint32) error {
if value > MaxUint24 {
return errors.New("value exceeds 24-bit range")
}
@@ -24,13 +24,13 @@ func (c *Uint24Codec) SetUint24(value uint32) error {
}
// Uint24 gets the value as a 24-bit unsigned integer.
func (c *Uint24Codec) Uint24() uint32 {
func (c *Uint24) Uint24() uint32 {
return c.value
}
// SetInt sets the value as an int, converting it to a 24-bit unsigned integer.
// If the value is out of the 24-bit range, it returns an error.
func (c *Uint24Codec) SetInt(value int) error {
func (c *Uint24) SetInt(value int) error {
if value < 0 || uint32(value) > MaxUint24 {
return errors.New("value exceeds 24-bit range")
}
@@ -39,13 +39,13 @@ func (c *Uint24Codec) SetInt(value int) error {
}
// Int gets the value as an int, converted from the 24-bit unsigned integer.
func (c *Uint24Codec) Int() int {
func (c *Uint24) Int() int {
return int(c.value)
}
// MarshalWrite encodes the 24-bit unsigned integer and writes it directly to the provided io.Writer.
// The encoding uses 3 bytes in BigEndian order.
func (c *Uint24Codec) MarshalWrite(w io.Writer) error {
func (c *Uint24) MarshalWrite(w io.Writer) error {
if c.value > MaxUint24 {
return errors.New("value exceeds 24-bit range")
}
@@ -61,7 +61,7 @@ func (c *Uint24Codec) MarshalWrite(w io.Writer) error {
}
// UnmarshalRead reads 3 bytes directly from the provided io.Reader and decodes it into a 24-bit unsigned integer.
func (c *Uint24Codec) UnmarshalRead(r io.Reader) error {
func (c *Uint24) UnmarshalRead(r io.Reader) error {
// Read 3 bytes directly from the reader
var buf [3]byte
_, err := io.ReadFull(r, buf[:]) // Ensure exactly 3 bytes are read

View File

@@ -5,7 +5,7 @@ import (
"testing"
)
func TestUint24Codec(t *testing.T) {
func TestUint24(t *testing.T) {
tests := []struct {
name string
value uint32
@@ -19,7 +19,7 @@ func TestUint24Codec(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
codec := new(Uint24Codec)
codec := new(Uint24)
// Test SetUint24
err := codec.SetUint24(tt.value)
@@ -53,14 +53,14 @@ func TestUint24Codec(t *testing.T) {
}
// Decode from the buffer
decodedCodec := new(Uint24Codec)
if err := decodedCodec.UnmarshalRead(buf); err != nil {
decoded := new(Uint24)
if err := decoded.UnmarshalRead(buf); err != nil {
t.Fatalf("UnmarshalRead failed: %v", err)
}
// Validate decoded value
if decodedCodec.Uint24() != tt.value {
t.Errorf("Decoded value mismatch: got %d, expected %d", decodedCodec.Uint24(), tt.value)
if decoded.Uint24() != tt.value {
t.Errorf("Decoded value mismatch: got %d, expected %d", decoded.Uint24(), tt.value)
}
})
}

View File

@@ -5,38 +5,38 @@ import (
"io"
)
// Uint32Codec is a codec for encoding and decoding 32-bit unsigned integers.
type Uint32Codec struct {
// Uint32 is a codec for encoding and decoding 32-bit unsigned integers.
type Uint32 struct {
value uint32
}
// SetUint32 sets the value as a uint32.
func (c *Uint32Codec) SetUint32(value uint32) {
func (c *Uint32) SetUint32(value uint32) {
c.value = value
}
// Uint32 gets the value as a uint32.
func (c *Uint32Codec) Uint32() uint32 {
func (c *Uint32) Uint32() uint32 {
return c.value
}
// SetInt sets the value as an int, converting it to uint32.
// Values outside the range of uint32 (04294967295) will be truncated.
func (c *Uint32Codec) SetInt(value int) {
func (c *Uint32) SetInt(value int) {
c.value = uint32(value)
}
// Int gets the value as an int, converted from uint32.
func (c *Uint32Codec) Int() int {
func (c *Uint32) Int() int {
return int(c.value)
}
// MarshalWrite writes the uint32 value to the provided writer in BigEndian order.
func (c *Uint32Codec) MarshalWrite(w io.Writer) error {
func (c *Uint32) MarshalWrite(w io.Writer) error {
return binary.Write(w, binary.BigEndian, c.value)
}
// UnmarshalRead reads a uint32 value from the provided reader in BigEndian order.
func (c *Uint32Codec) UnmarshalRead(r io.Reader) error {
func (c *Uint32) UnmarshalRead(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &c.value)
}

View File

@@ -8,7 +8,7 @@ import (
"lukechampine.com/frand"
)
func TestUint32Codec(t *testing.T) {
func TestUint32(t *testing.T) {
// Helper function to generate random 32-bit integers
generateRandomUint32 := func() uint32 {
return uint32(frand.Intn(math.MaxUint32)) // math.MaxUint32 == 4294967295
@@ -20,7 +20,7 @@ func TestUint32Codec(t *testing.T) {
randomInt := int(randomUint32)
// Create a new codec
codec := new(Uint32Codec)
codec := new(Uint32)
// Test UInt32 setter and getter
codec.SetUint32(randomUint32)
@@ -48,14 +48,14 @@ func TestUint32Codec(t *testing.T) {
bufDec := bytes.NewBuffer(encoded)
// Decode back the value
decodedCodec := new(Uint32Codec)
err = decodedCodec.UnmarshalRead(bufDec)
decoded := new(Uint32)
err = decoded.UnmarshalRead(bufDec)
if err != nil {
t.Fatalf("UnmarshalRead failed: %v", err)
}
if decodedCodec.Uint32() != randomUint32 {
t.Fatalf("Decoded value mismatch: got %d, expected %d", decodedCodec.Uint32(), randomUint32)
if decoded.Uint32() != randomUint32 {
t.Fatalf("Decoded value mismatch: got %d, expected %d", decoded.Uint32(), randomUint32)
}
// Compare encoded bytes to ensure correctness

View File

@@ -8,12 +8,12 @@ import (
// MaxUint40 is the maximum value of a 40-bit unsigned integer: 2^40 - 1.
const MaxUint40 uint64 = 1<<40 - 1
// Uint40Codec is a codec for encoding and decoding 40-bit unsigned integers.
type Uint40Codec struct{ value uint64 }
// Uint40 is a codec for encoding and decoding 40-bit unsigned integers.
type Uint40 struct{ value uint64 }
// SetUint40 sets the value as a 40-bit unsigned integer.
// If the value exceeds the maximum allowable value for 40 bits, it returns an error.
func (c *Uint40Codec) SetUint40(value uint64) error {
func (c *Uint40) SetUint40(value uint64) error {
if value > MaxUint40 {
return errors.New("value exceeds 40-bit range")
}
@@ -22,11 +22,11 @@ func (c *Uint40Codec) SetUint40(value uint64) error {
}
// Uint40 gets the value as a 40-bit unsigned integer.
func (c *Uint40Codec) Uint40() uint64 { return c.value }
func (c *Uint40) Uint40() uint64 { return c.value }
// SetInt sets the value as an int, converting it to a 40-bit unsigned integer.
// If the value is out of the 40-bit range, it returns an error.
func (c *Uint40Codec) SetInt(value int) error {
func (c *Uint40) SetInt(value int) error {
if value < 0 || uint64(value) > MaxUint40 {
return errors.New("value exceeds 40-bit range")
}
@@ -36,11 +36,11 @@ func (c *Uint40Codec) SetInt(value int) error {
// Int gets the value as an int, converted from the 40-bit unsigned integer.
// Note: If the value exceeds the int range, it will be truncated.
func (c *Uint40Codec) Int() int { return int(c.value) }
func (c *Uint40) Int() int { return int(c.value) }
// MarshalWrite encodes the 40-bit unsigned integer and writes it to the provided writer.
// The encoding uses 5 bytes in BigEndian order.
func (c *Uint40Codec) MarshalWrite(w io.Writer) (err error) {
func (c *Uint40) MarshalWrite(w io.Writer) (err error) {
if c.value > MaxUint40 {
return errors.New("value exceeds 40-bit range")
}
@@ -57,7 +57,7 @@ func (c *Uint40Codec) MarshalWrite(w io.Writer) (err error) {
}
// UnmarshalRead reads 5 bytes from the provided reader and decodes it into a 40-bit unsigned integer.
func (c *Uint40Codec) UnmarshalRead(r io.Reader) (err error) {
func (c *Uint40) UnmarshalRead(r io.Reader) (err error) {
// Buffer for the 5 bytes
buf := make([]byte, 5)
_, err = r.Read(buf)

View File

@@ -5,8 +5,8 @@ import (
"testing"
)
func TestUint40Codec(t *testing.T) {
// Test cases for Uint40Codec
func TestUint40(t *testing.T) {
// Test cases for Uint40
tests := []struct {
name string
value uint64
@@ -20,7 +20,7 @@ func TestUint40Codec(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
codec := new(Uint40Codec)
codec := new(Uint40)
// Test SetUint40
err := codec.SetUint40(tt.value)
@@ -54,14 +54,14 @@ func TestUint40Codec(t *testing.T) {
}
// Decode from the buffer
decodedCodec := new(Uint40Codec)
if err = decodedCodec.UnmarshalRead(buf); err != nil {
decoded := new(Uint40)
if err = decoded.UnmarshalRead(buf); err != nil {
t.Fatalf("UnmarshalRead failed: %v", err)
}
// Validate decoded value
if decodedCodec.Uint40() != tt.value {
t.Errorf("Decoded value mismatch: got %d, expected %d", decodedCodec.Uint40(), tt.value)
if decoded.Uint40() != tt.value {
t.Errorf("Decoded value mismatch: got %d, expected %d", decoded.Uint40(), tt.value)
}
})
}

View File

@@ -5,38 +5,38 @@ import (
"io"
)
// Uint64Codec is a codec for encoding and decoding 64-bit unsigned integers.
type Uint64Codec struct {
// Uint64 is a codec for encoding and decoding 64-bit unsigned integers.
type Uint64 struct {
value uint64
}
// SetUint64 sets the value as a uint64.
func (c *Uint64Codec) SetUint64(value uint64) {
func (c *Uint64) SetUint64(value uint64) {
c.value = value
}
// Uint64 gets the value as a uint64.
func (c *Uint64Codec) Uint64() uint64 {
func (c *Uint64) Uint64() uint64 {
return c.value
}
// SetInt sets the value as an int, converting it to uint64.
// Values outside the range of uint64 are truncated.
func (c *Uint64Codec) SetInt(value int) {
func (c *Uint64) SetInt(value int) {
c.value = uint64(value)
}
// Int gets the value as an int, converted from uint64. May truncate if the value exceeds the range of int.
func (c *Uint64Codec) Int() int {
func (c *Uint64) Int() int {
return int(c.value)
}
// MarshalWrite writes the uint64 value to the provided writer in BigEndian order.
func (c *Uint64Codec) MarshalWrite(w io.Writer) error {
func (c *Uint64) MarshalWrite(w io.Writer) error {
return binary.Write(w, binary.BigEndian, c.value)
}
// UnmarshalRead reads a uint64 value from the provided reader in BigEndian order.
func (c *Uint64Codec) UnmarshalRead(r io.Reader) error {
func (c *Uint64) UnmarshalRead(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &c.value)
}

View File

@@ -8,7 +8,7 @@ import (
"lukechampine.com/frand"
)
func TestUint64Codec(t *testing.T) {
func TestUint64(t *testing.T) {
// Helper function to generate random 64-bit integers
generateRandomUint64 := func() uint64 {
return frand.Uint64n(math.MaxUint64) // math.MaxUint64 == 18446744073709551615
@@ -20,7 +20,7 @@ func TestUint64Codec(t *testing.T) {
randomInt := int(randomUint64)
// Create a new codec
codec := new(Uint64Codec)
codec := new(Uint64)
// Test UInt64 setter and getter
codec.SetUint64(randomUint64)
@@ -48,14 +48,14 @@ func TestUint64Codec(t *testing.T) {
bufDec := bytes.NewBuffer(encoded)
// Decode back the value
decodedCodec := new(Uint64Codec)
err = decodedCodec.UnmarshalRead(bufDec)
decoded := new(Uint64)
err = decoded.UnmarshalRead(bufDec)
if err != nil {
t.Fatalf("UnmarshalRead failed: %v", err)
}
if decodedCodec.Uint64() != randomUint64 {
t.Fatalf("Decoded value mismatch: got %d, expected %d", decodedCodec.Uint64(), randomUint64)
if decoded.Uint64() != randomUint64 {
t.Fatalf("Decoded value mismatch: got %d, expected %d", decoded.Uint64(), randomUint64)
}
// Compare encoded bytes to ensure correctness