-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits_test.go
More file actions
87 lines (71 loc) · 2.43 KB
/
bits_test.go
File metadata and controls
87 lines (71 loc) · 2.43 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package dvb
import (
"dvb-go/pkg/utils"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSliceU8toU16LE(t *testing.T) {
t.Parallel()
t.Run("safe", func(t *testing.T) {
assert.Equal(t, []uint16{0b11111111_00000000}, sliceU8toU16LE([]uint8{0b00000000, 0b11111111}))
assert.Equal(t, []uint16{0b11111111_00000000, 0b11111111_00000000}, sliceU8toU16LE([]uint8{0b0000000, 0b11111111, 0b00000000, 0b11111111}))
// assert.Equal(t, []uint16{0b11111111_00000000}, uint8ToUint16LE([]byte{0b11111111}))
})
t.Run("unsafe", func(t *testing.T) {
assert.Equal(t, []uint16{0b11111111_00000000}, sliceU8toU16LEUnsafe([]uint8{0b00000000, 0b11111111}))
assert.Equal(t, []uint16{0b11111111_00000000, 0b11111111_00000000}, sliceU8toU16LEUnsafe([]uint8{0b0000000, 0b11111111, 0b00000000, 0b11111111}))
// assert.Equal(t, []uint16{0b11111111_00000000}, uint8ToUint16Unsafe([]byte{0b11111111}))
})
}
func BenchmarkSliceU8toU16(b *testing.B) {
sliceSize := 10
b.Run("safe", func(b *testing.B) {
data := utils.RandomU8Slice(sliceSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = sliceU8toU16LE(data)
}
})
b.Run("unsafe", func(b *testing.B) {
data := utils.RandomU8Slice(sliceSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = sliceU8toU16LEUnsafe(data)
}
})
}
func TestU16toU8(t *testing.T) {
t.Parallel()
u8 := U16toU8LE(uint16(0b00000000_11111111))
assert.Equal(t, []byte{0xFF, 0x00}, u8)
}
func TestSliceU16toU8LE(t *testing.T) {
t.Parallel()
t.Run("safe", func(t *testing.T) {
assert.Equal(t, []uint8{0b00000000, 0b11111111}, sliceU16toU8LE([]uint16{0b11111111_00000000}))
assert.Equal(t, []uint8{0b11111111, 0b00000000}, sliceU16toU8LE([]uint16{0b00000000_11111111}))
})
t.Run("unsafe", func(t *testing.T) {
assert.Equal(t, []uint8{0b00000000, 0b11111111}, sliceU16toU8Unsafe([]uint16{0b11111111_00000000}))
assert.Equal(t, []uint8{0b11111111, 0b00000000}, sliceU16toU8Unsafe([]uint16{0b00000000_11111111}))
})
}
func BenchmarkSliceU16toU8(b *testing.B) {
sliceSize := 10
b.Run("safe", func(b *testing.B) {
data := utils.RandomU16Slice(sliceSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// _ = sliceU16toU8([]uint16{0b00000000_11111111})
_ = sliceU16toU8LE(data)
}
})
b.Run("unsafe", func(b *testing.B) {
data := utils.RandomU16Slice(sliceSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// _ = sliceU16toU8Unsafe([]uint16{0b00000000_11111111})
_ = sliceU16toU8Unsafe(data)
}
})
}