2 Star 0 Fork 0

Admin/bd_sender

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
slot_test.go 7.28 KB
一键复制 编辑 原始数据 按行查看 历史
钟林峰 提交于 2019-03-06 15:41 +08:00 . add tests
package main
import (
"reflect"
"testing"
"github.com/go-test/deep"
)
func Test_bytify(t *testing.T) {
type args struct {
bs string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
// TODO: Add test cases.
{
name: "empty convert to empty",
args: args{
bs: "",
},
want: []byte{},
},
{
name: "convert to byte all zero",
args: args{
bs: "00000000",
},
want: []byte{0},
},
{
name: "convert to byte no zero",
args: args{
bs: "00000001",
},
want: []byte{0x01},
},
{
name: "convert to byte with non aligned full 0",
args: args{
bs: "000",
},
want: []byte{0},
},
{
name: "convert to byte with non aligned",
args: args{
bs: "001",
},
want: []byte{0x20},
},
{
name: "convert to multiple byte with non aligned",
args: args{
bs: "00000000001",
},
want: []byte{0x00, 0x20},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := bytify(tt.args.bs)
if (err != nil) != tt.wantErr {
t.Errorf("bytify() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("bytify() = %v, want %v", got, tt.want)
}
})
}
}
func TestSlot_Encode(t *testing.T) {
type args struct {
data string
}
head := []byte{0x01, 0x02, 0x03, 0x04}
tests := []struct {
name string
s *Slot
args args
want []byte
wantErr bool
}{
// TODO: Add test cases.
{
name: "empty slot encode to empty",
s: &Slot{
head: head,
items: []item{},
},
args: args{
data: "{}",
},
want: head,
},
{
name: "encode float item",
s: &Slot{
head: head,
items: []item{
item{
Kind: "JSON",
Type: "FLOAT",
Path: "x",
Width: 4,
Max: 16,
Min: 0,
},
},
},
args: args{
data: "{\"x\":1}",
},
want: append(head, []byte{0x10}...),
},
{
name: "encode bool item",
s: &Slot{
items: []item{
item{
Kind: "JSON",
Type: "BOOL",
Path: "x",
Width: 1,
},
},
},
args: args{
data: "{\"x\":true}",
},
want: []byte{0x80},
},
{
name: "encode mixed items",
s: &Slot{
items: []item{
item{
Kind: "JSON",
Type: "FLOAT",
Path: "x",
Width: 4,
Max: 16,
Min: 0,
},
item{
Kind: "JSON",
Type: "BOOL",
Path: "y",
Width: 1,
},
},
},
args: args{
data: "{\"x\":1,\"y\":true}",
},
want: []byte{0x18},
},
{
name: "many items",
s: &Slot{
head: head,
items: []item{
item{
Kind: "JSON",
Type: "FLOAT",
Path: "a",
Width: 4,
Max: 16,
Min: 0,
},
item{
Kind: "JSON",
Type: "FLOAT",
Path: "b",
Width: 4,
Max: 16,
Min: 0,
},
item{
Kind: "JSON",
Type: "FLOAT",
Path: "c",
Width: 7,
Max: 16,
Min: 0,
},
{
Kind: "JSON",
Type: "FLOAT",
Path: "d",
Width: 11,
Max: 100,
Min: 0,
},
},
},
args: args{
data: `{"a":1,"b":1,"c":1,"d":43}`,
},
want: []byte{},
},
{
name: "sibo items",
s: &Slot{
head: head,
items: []item{
{
Type: "FLOAT",
Kind: "JSON",
Path: "a",
Min: 0,
Max: 4,
Width: 9,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "b",
Min: 0,
Max: 120,
Width: 7,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "c",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "d",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "e",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "f",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "g",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "h",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "i",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "j",
Min: 0,
Max: 600,
Width: 10,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "k",
Min: 0,
Max: 250,
Width: 8,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "l",
Min: 0,
Max: 10,
Width: 7,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "m",
Min: 0,
Max: 120,
Width: 7,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "n",
Min: 0,
Max: 1600,
Width: 11,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "o",
Min: 0,
Max: 70000,
Width: 17,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "p",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "q",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "r",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "s",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "t",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "u",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "v",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "w",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "x",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "y",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "z",
Min: 0,
Max: 100,
Width: 14,
},
{
Type: "FLOAT",
Kind: "JSON",
Path: "A",
Min: 0,
Max: 100,
Width: 14,
},
},
},
args: args{
data: `{"a":2,"b":60,"c":301,"d":302,"e":303,"f":304,"g":305,"h":306,"i":307,"j":308,"k":125,"l":5,"m":60,"n":800,"o":35000,"p":50,"q":50,"r":50,"s":50,"t":50,"u":50,"v":50,"w":50,"x":50,"y":50,"z":50,"A":50}`,
},
want: []byte{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.s.Encode(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Slot.Encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := deep.Equal(got, tt.want); diff != nil {
t.Errorf("Slot.Encode() = %v, want %v, diff = %v", got, tt.want, diff)
}
})
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shipnet_masterclock/bd_sender.git
git@gitee.com:shipnet_masterclock/bd_sender.git
shipnet_masterclock
bd_sender
bd_sender
master

搜索帮助