forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec_test.go
More file actions
61 lines (56 loc) 路 1.81 KB
/
Copy pathcodec_test.go
File metadata and controls
61 lines (56 loc) 路 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package gnet
import (
"encoding/binary"
"math/rand"
"testing"
)
func TestLengthFieldBasedFrameCodec(t *testing.T) {
encoderConfig := EncoderConfig{
ByteOrder: binary.BigEndian,
LengthFieldLength: 3,
LengthAdjustment: 0,
LengthIncludesLengthFieldLength: false,
}
decoderConfig := DecoderConfig{
ByteOrder: binary.BigEndian,
LengthFieldOffset: 0,
LengthFieldLength: 3,
LengthAdjustment: 0,
InitialBytesToStrip: 3,
}
codec := NewLengthFieldBasedFrameCodec(encoderConfig, decoderConfig)
sz := rand.Intn(10) * 64
data := make([]byte, sz)
if _, err := rand.Read(data); err != nil {
panic(err)
}
out, _ := codec.Encode(data)
if string(out[3:]) != string(data) {
t.Fatalf("data don't match with big endian, raw data: %s, encoded data: %s\n", string(data), string(out))
}
encoderConfig.ByteOrder = binary.LittleEndian
decoderConfig.ByteOrder = binary.LittleEndian
codec = NewLengthFieldBasedFrameCodec(encoderConfig, decoderConfig)
sz = rand.Intn(10) * 64
data = make([]byte, sz)
if _, err := rand.Read(data); err != nil {
panic(err)
}
out, _ = codec.Encode(data)
if string(out[3:]) != string(data) {
t.Fatalf("data don't match with little endian, raw data: %s, encoded data: %s\n", string(data), string(out))
}
buf := make([]byte, 3)
rand.Read(buf)
bNum := readUint24(binary.BigEndian, buf)
p := writeUint24(binary.BigEndian, int(bNum))
if string(buf) != string(p) {
t.Fatalf("data don't match with big endian, raw data: %s, recovered data: %s\n", string(buf), string(p))
}
rand.Read(buf)
bNum = readUint24(binary.LittleEndian, buf)
p = writeUint24(binary.LittleEndian, int(bNum))
if string(buf) != string(p) {
t.Fatalf("data don't match with little endian, raw data: %s, recovered data: %s\n", string(buf), string(p))
}
}