This commit is contained in:
Akvicor 2024-01-19 02:02:30 +08:00
parent 87e29cadb2
commit 0237aa52e0
4 changed files with 71 additions and 17 deletions

View File

@ -12,70 +12,70 @@ func TestBitSet(t *testing.T) {
b := uint(0)
BitSet(&b8, 0, true)
if b8 != 1 {
t.Errorf("need %d got %d", b8, 1)
t.Errorf("need %d got %d", 1, b8)
}
BitSet(&b16, 0, true)
if b16 != 1 {
t.Errorf("need %d got %d", b16, 1)
t.Errorf("need %d got %d", 1, b16)
}
BitSet(&b32, 0, true)
if b32 != 1 {
t.Errorf("need %d got %d", b32, 1)
t.Errorf("need %d got %d", 1, b32)
}
BitSet(&b64, 0, true)
if b64 != 1 {
t.Errorf("need %d got %d", b64, 1)
t.Errorf("need %d got %d", 1, b64)
}
BitSet(&b, 0, true)
if b != 1 {
t.Errorf("need %d got %d", b, 1)
t.Errorf("need %d got %d", 1, b)
}
BitSet(&b8, 7, true)
if b8 != 129 {
t.Errorf("need %d got %d", b8, 129)
t.Errorf("need %d got %d", 129, b8)
}
BitSet(&b16, 15, true)
if b16 != 32769 {
t.Errorf("need %d got %d", b16, 32769)
t.Errorf("need %d got %d", 32769, b16)
}
BitSet(&b32, 31, true)
if b32 != 2147483649 {
t.Errorf("need %d got %d", b32, 2147483649)
t.Errorf("need %d got %d", 2147483649, b32)
}
BitSet(&b64, 63, true)
if b64 != 9223372036854775809 {
t.Errorf("need %d got %d", b64, uint64(9223372036854775809))
t.Errorf("need %d got %d", uint64(9223372036854775809), b64)
}
BitSet(&b, 1, true)
if b != 3 {
t.Errorf("need %d got %d", b, 3)
t.Errorf("need %d got %d", 3, b)
}
BitSet(&b8, 0, false)
if b8 != 128 {
t.Errorf("need %d got %d", b8, 128)
t.Errorf("need %d got %d", 128, b8)
}
BitSet(&b16, 0, false)
if b16 != 32768 {
t.Errorf("need %d got %d", b16, 32768)
t.Errorf("need %d got %d", 32768, b16)
}
BitSet(&b32, 0, false)
if b32 != 2147483648 {
t.Errorf("need %d got %d", b32, 2147483648)
t.Errorf("need %d got %d", 2147483648, b32)
}
BitSet(&b64, 0, false)
if b64 != 9223372036854775808 {
t.Errorf("need %d got %d", b64, uint64(9223372036854775809))
t.Errorf("need %d got %d", uint64(9223372036854775809), b64)
}
BitSet(&b, 0, false)
if b != 2 {
t.Errorf("need %d got %d", b, 2)
t.Errorf("need %d got %d", 2, b)
}
e := "e"
BitSet(&e, 0, true)
if e != "e" {
t.Errorf("need %s got %s", e, "e")
t.Errorf("need %s got %s", "e", e)
}
}

2
go.mod
View File

@ -1,3 +1,3 @@
module git.viry.cc/gomod/util
go 1.15
go 1.19

26
json.go Normal file
View File

@ -0,0 +1,26 @@
package util
import "encoding/json"
type JSONResult struct {
result []byte
err error
}
func NewJSONResult(v any) *JSONResult {
res := &JSONResult{}
res.result, res.err = json.Marshal(v)
return res
}
func (j *JSONResult) Bytes() []byte {
return j.result
}
func (j *JSONResult) String() string {
return string(j.result)
}
func (j *JSONResult) Error() error {
return j.err
}

28
json_test.go Normal file
View File

@ -0,0 +1,28 @@
package util
import (
"bytes"
"testing"
)
func TestJSON(t *testing.T) {
type TestStruct struct {
Name string `json:"name"`
Age int `json:"age"`
}
test1 := TestStruct{
Name: "Akvicor",
Age: 17,
}
result := `{"name":"Akvicor","age":17}`
res := NewJSONResult(&test1)
if res.Error() != nil {
t.Errorf("%v", res.Error())
}
if res.String() != result {
t.Errorf("need %s got %s", result, res.String())
}
if !bytes.Equal(res.Bytes(), []byte(result)) {
t.Errorf("need %v got %v", []byte(result), res.Bytes())
}
}