add num package

This commit is contained in:
siddontang 2014-09-11 13:23:43 +08:00
parent 66c517a7a9
commit 8c8a794873
4 changed files with 512 additions and 0 deletions

37
num/bytes.go Normal file
View File

@ -0,0 +1,37 @@
package num
import (
"encoding/binary"
)
//all are bigendian format
func BytesToUint16(b []byte) uint16 {
return binary.BigEndian.Uint16(b)
}
func Uint16ToBytes(u uint16) []byte {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, u)
return buf
}
func BytesToUint32(b []byte) uint32 {
return binary.BigEndian.Uint32(b)
}
func Uint32ToBytes(u uint32) []byte {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, u)
return buf
}
func BytesToUint64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
func Uint64ToBytes(u uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, u)
return buf
}

161
num/cmp.go Normal file
View File

@ -0,0 +1,161 @@
package num
func MinUint(a uint, b uint) uint {
if a > b {
return b
} else {
return a
}
}
func MaxUint(a uint, b uint) uint {
if a > b {
return a
} else {
return b
}
}
func MinInt(a int, b int) int {
if a > b {
return b
} else {
return a
}
}
func MaxInt(a int, b int) int {
if a > b {
return a
} else {
return b
}
}
func MinUint8(a uint8, b uint8) uint8 {
if a > b {
return b
} else {
return a
}
}
func MaxUint8(a uint8, b uint8) uint8 {
if a > b {
return a
} else {
return b
}
}
func MinInt8(a int8, b int8) int8 {
if a > b {
return b
} else {
return a
}
}
func MaxInt8(a int8, b int8) int8 {
if a > b {
return a
} else {
return b
}
}
func MinUint16(a uint16, b uint16) uint16 {
if a > b {
return b
} else {
return a
}
}
func MaxUint16(a uint16, b uint16) uint16 {
if a > b {
return a
} else {
return b
}
}
func MinInt16(a int16, b int16) int16 {
if a > b {
return b
} else {
return a
}
}
func MaxInt16(a int16, b int16) int16 {
if a > b {
return a
} else {
return b
}
}
func MinUint32(a uint32, b uint32) uint32 {
if a > b {
return b
} else {
return a
}
}
func MaxUint32(a uint32, b uint32) uint32 {
if a > b {
return a
} else {
return b
}
}
func MinInt32(a int32, b int32) int32 {
if a > b {
return b
} else {
return a
}
}
func MaxInt32(a int32, b int32) int32 {
if a > b {
return a
} else {
return b
}
}
func MinUint64(a uint64, b uint64) uint64 {
if a > b {
return b
} else {
return a
}
}
func MaxUint64(a uint64, b uint64) uint64 {
if a > b {
return a
} else {
return b
}
}
func MinInt64(a int64, b int64) int64 {
if a > b {
return b
} else {
return a
}
}
func MaxInt64(a int64, b int64) int64 {
if a > b {
return a
} else {
return b
}
}

197
num/num_test.go Normal file
View File

@ -0,0 +1,197 @@
package num
import (
"testing"
)
func testMin(t *testing.T, v1 interface{}, v2 interface{}, v interface{}) {
var c interface{}
switch i1 := v1.(type) {
case int:
c = MinInt(i1, v2.(int))
case int8:
c = MinInt8(i1, v2.(int8))
case int16:
c = MinInt16(i1, v2.(int16))
case int32:
c = MinInt32(i1, v2.(int32))
case int64:
c = MinInt64(i1, v2.(int64))
case uint:
c = MinUint(i1, v2.(uint))
case uint8:
c = MinUint8(i1, v2.(uint8))
case uint16:
c = MinUint16(i1, v2.(uint16))
case uint32:
c = MinUint32(i1, v2.(uint32))
case uint64:
c = MinUint64(i1, v2.(uint64))
default:
t.Fatalf("invalid type %T", t)
}
if c != v {
t.Fatalf("invalid %v(%T) != %v(%T)", c, c, v, v)
}
}
func TestMin(t *testing.T) {
testMin(t, int(1), int(2), int(1))
testMin(t, int(1), int(1), int(1))
testMin(t, int8(1), int8(2), int8(1))
testMin(t, int8(1), int8(1), int8(1))
testMin(t, int16(1), int16(2), int16(1))
testMin(t, int16(1), int16(1), int16(1))
testMin(t, int32(1), int32(2), int32(1))
testMin(t, int32(1), int32(1), int32(1))
testMin(t, int64(1), int64(2), int64(1))
testMin(t, int64(1), int64(1), int64(1))
testMin(t, uint(1), uint(2), uint(1))
testMin(t, uint(1), uint(1), uint(1))
testMin(t, uint8(1), uint8(2), uint8(1))
testMin(t, uint8(1), uint8(1), uint8(1))
testMin(t, uint16(1), uint16(2), uint16(1))
testMin(t, uint16(1), uint16(1), uint16(1))
testMin(t, uint32(1), uint32(2), uint32(1))
testMin(t, uint32(1), uint32(1), uint32(1))
testMin(t, uint64(1), uint64(2), uint64(1))
testMin(t, uint64(1), uint64(1), uint64(1))
}
func testMax(t *testing.T, v1 interface{}, v2 interface{}, v interface{}) {
var c interface{}
switch i1 := v1.(type) {
case int:
c = MaxInt(i1, v2.(int))
case int8:
c = MaxInt8(i1, v2.(int8))
case int16:
c = MaxInt16(i1, v2.(int16))
case int32:
c = MaxInt32(i1, v2.(int32))
case int64:
c = MaxInt64(i1, v2.(int64))
case uint:
c = MaxUint(i1, v2.(uint))
case uint8:
c = MaxUint8(i1, v2.(uint8))
case uint16:
c = MaxUint16(i1, v2.(uint16))
case uint32:
c = MaxUint32(i1, v2.(uint32))
case uint64:
c = MaxUint64(i1, v2.(uint64))
default:
t.Fatalf("invalid type %T", t)
}
if c != v {
t.Fatalf("invalid %v(%T) != %v(%T)", c, c, v, v)
}
}
func TestMax(t *testing.T) {
testMax(t, int(1), int(2), int(2))
testMax(t, int(1), int(1), int(1))
testMax(t, int8(1), int8(2), int8(2))
testMax(t, int8(1), int8(1), int8(1))
testMax(t, int16(1), int16(2), int16(2))
testMax(t, int16(1), int16(1), int16(1))
testMax(t, int32(1), int32(2), int32(2))
testMax(t, int32(1), int32(1), int32(1))
testMax(t, int64(1), int64(2), int64(2))
testMax(t, int64(1), int64(1), int64(1))
testMax(t, uint(1), uint(2), uint(2))
testMax(t, uint(1), uint(1), uint(1))
testMax(t, uint8(1), uint8(2), uint8(2))
testMax(t, uint8(1), uint8(1), uint8(1))
testMax(t, uint16(1), uint16(2), uint16(2))
testMax(t, uint16(1), uint16(1), uint16(1))
testMax(t, uint32(1), uint32(2), uint32(2))
testMax(t, uint32(1), uint32(1), uint32(1))
testMax(t, uint64(1), uint64(2), uint64(2))
testMax(t, uint64(1), uint64(1), uint64(1))
}
func TestBytes(t *testing.T) {
if BytesToUint64(Uint64ToBytes(1)) != 1 {
t.Fatal("convert fail")
}
if BytesToUint32(Uint32ToBytes(1)) != 1 {
t.Fatal("convert fail")
}
if BytesToUint16(Uint16ToBytes(1)) != 1 {
t.Fatal("convert fail")
}
}
func TestStr(t *testing.T) {
if v, err := StringToUint64(Uint64ToString(1)); err != nil {
t.Fatal(err)
} else if v != 1 {
t.Fatal(v)
}
if v, err := StringToUint32(Uint32ToString(1)); err != nil {
t.Fatal(err)
} else if v != 1 {
t.Fatal(v)
}
if v, err := StringToUint16(Uint16ToString(1)); err != nil {
t.Fatal(err)
} else if v != 1 {
t.Fatal(v)
}
if v, err := StringToUint8(Uint8ToString(1)); err != nil {
t.Fatal(err)
} else if v != 1 {
t.Fatal(v)
}
if v, err := StringToInt64(Int64ToString(-1)); err != nil {
t.Fatal(err)
} else if v != -1 {
t.Fatal(v)
}
if v, err := StringToInt32(Int32ToString(-1)); err != nil {
t.Fatal(err)
} else if v != -1 {
t.Fatal(v)
}
if v, err := StringToInt16(Int16ToString(-1)); err != nil {
t.Fatal(err)
} else if v != -1 {
t.Fatal(v)
}
if v, err := StringToInt8(Int8ToString(-1)); err != nil {
t.Fatal(err)
} else if v != -1 {
t.Fatal(v)
}
}

117
num/str.go Normal file
View File

@ -0,0 +1,117 @@
package num
import (
"strconv"
)
func StringToUint(s string) (uint, error) {
if v, err := strconv.ParseUint(s, 10, 0); err != nil {
return 0, err
} else {
return uint(v), nil
}
}
func StringToUint8(s string) (uint8, error) {
if v, err := strconv.ParseUint(s, 10, 8); err != nil {
return 0, err
} else {
return uint8(v), nil
}
}
func StringToUint16(s string) (uint16, error) {
if v, err := strconv.ParseUint(s, 10, 16); err != nil {
return 0, err
} else {
return uint16(v), nil
}
}
func StringToUint32(s string) (uint32, error) {
if v, err := strconv.ParseUint(s, 10, 32); err != nil {
return 0, err
} else {
return uint32(v), nil
}
}
func StringToUint64(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 64)
}
func StringToInt(s string) (int, error) {
if v, err := strconv.ParseInt(s, 10, 0); err != nil {
return 0, err
} else {
return int(v), nil
}
}
func StringToInt8(s string) (int8, error) {
if v, err := strconv.ParseInt(s, 10, 8); err != nil {
return 0, err
} else {
return int8(v), nil
}
}
func StringToInt16(s string) (int16, error) {
if v, err := strconv.ParseInt(s, 10, 16); err != nil {
return 0, err
} else {
return int16(v), nil
}
}
func StringToInt32(s string) (int32, error) {
if v, err := strconv.ParseInt(s, 10, 32); err != nil {
return 0, err
} else {
return int32(v), nil
}
}
func StringToInt64(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
func IntToString(v int) string {
return strconv.FormatInt(int64(v), 10)
}
func Int8ToString(v int8) string {
return strconv.FormatInt(int64(v), 10)
}
func Int16ToString(v int16) string {
return strconv.FormatInt(int64(v), 10)
}
func Int32ToString(v int32) string {
return strconv.FormatInt(int64(v), 10)
}
func Int64ToString(v int64) string {
return strconv.FormatInt(int64(v), 10)
}
func UintToString(v uint) string {
return strconv.FormatUint(uint64(v), 10)
}
func Uint8ToString(v uint8) string {
return strconv.FormatUint(uint64(v), 10)
}
func Uint16ToString(v uint16) string {
return strconv.FormatUint(uint64(v), 10)
}
func Uint32ToString(v uint32) string {
return strconv.FormatUint(uint64(v), 10)
}
func Uint64ToString(v uint64) string {
return strconv.FormatUint(uint64(v), 10)
}