Simplified file structure

This commit is contained in:
2024-08-07 19:39:10 +02:00
parent 1b13539b22
commit 66569446b1
219 changed files with 453 additions and 457 deletions

20
src/sizeof/Signed.go Normal file
View File

@ -0,0 +1,20 @@
package sizeof
import "math"
// Signed tells you how many bytes are needed to encode this signed number.
func Signed(number int64) int {
switch {
case number >= math.MinInt8 && number <= math.MaxInt8:
return 1
case number >= math.MinInt16 && number <= math.MaxInt16:
return 2
case number >= math.MinInt32 && number <= math.MaxInt32:
return 4
default:
return 8
}
}

21
src/sizeof/Signed_test.go Normal file
View File

@ -0,0 +1,21 @@
package sizeof_test
import (
"math"
"testing"
"git.akyoto.dev/cli/q/src/sizeof"
"git.akyoto.dev/go/assert"
)
func TestSigned(t *testing.T) {
assert.Equal(t, sizeof.Signed(0), 1)
assert.Equal(t, sizeof.Signed(math.MinInt8), 1)
assert.Equal(t, sizeof.Signed(math.MaxInt8), 1)
assert.Equal(t, sizeof.Signed(math.MinInt16), 2)
assert.Equal(t, sizeof.Signed(math.MaxInt16), 2)
assert.Equal(t, sizeof.Signed(math.MinInt32), 4)
assert.Equal(t, sizeof.Signed(math.MaxInt32), 4)
assert.Equal(t, sizeof.Signed(math.MinInt64), 8)
assert.Equal(t, sizeof.Signed(math.MaxInt64), 8)
}

20
src/sizeof/Unsigned.go Normal file
View File

@ -0,0 +1,20 @@
package sizeof
import "math"
// Unsigned tells you how many bytes are needed to encode this unsigned number.
func Unsigned(number uint64) int {
switch {
case number <= math.MaxUint8:
return 1
case number <= math.MaxUint16:
return 2
case number <= math.MaxUint32:
return 4
default:
return 8
}
}

View File

@ -0,0 +1,17 @@
package sizeof_test
import (
"math"
"testing"
"git.akyoto.dev/cli/q/src/sizeof"
"git.akyoto.dev/go/assert"
)
func TestUnsigned(t *testing.T) {
assert.Equal(t, sizeof.Unsigned(0), 1)
assert.Equal(t, sizeof.Unsigned(math.MaxUint8), 1)
assert.Equal(t, sizeof.Unsigned(math.MaxUint16), 2)
assert.Equal(t, sizeof.Unsigned(math.MaxUint32), 4)
assert.Equal(t, sizeof.Unsigned(math.MaxUint64), 8)
}