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
|
package nlenc
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestBytesString(t *testing.T) {
tests := []struct {
s string
b []byte
}{
{
s: "foo",
b: []byte{'f', 'o', 'o', 0x00},
},
{
s: "nl80211",
b: []byte{'n', 'l', '8', '0', '2', '1', '1', 0x00},
},
{
s: "TASKSTATS",
b: []byte{'T', 'A', 'S', 'K', 'S', 'T', 'A', 'T', 'S', 0x00},
},
}
for _, tt := range tests {
t.Run(tt.s, func(t *testing.T) {
s := String(Bytes(tt.s))
if want, got := tt.s, s; want != got {
t.Fatalf("unexpected string:\n- want: %q\n- got: %q", want, got)
}
})
}
}
func TestStringTrailingNull(t *testing.T) {
const want = "hello world"
// Buffer has many trailing NULL bytes which should all be removed.
var b [64]byte
copy(b[:], want)
if diff := cmp.Diff(want, String(b[:])); diff != "" {
t.Fatalf("unexpected string (-want +got):\n%s", diff)
}
}
|