Debian Codegraph @

pdata/internal/profileid_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11
12 "go.opentelemetry.io/collector/pdata/internal/json"
13 )
14
15 func TestProfileID(t *testing.T) {
16 tid := ProfileID([profileIDSize]byte{})
17 assert.EqualValues(t, [profileIDSize]byte{}, tid)
18 assert.Equal(t, 0, tid.SizeProto())
19
20 b := [profileIDSize]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
21 tid = b
22 assert.EqualValues(t, b, tid)
23 assert.Equal(t, profileIDSize, tid.SizeProto())
24 }
25
26 func TestProfileIDMarshal(t *testing.T) {
27 buf := make([]byte, profileIDSize)
28
29 tid := ProfileID([profileIDSize]byte{})
30 n := tid.MarshalProto(buf)
31 assert.Equal(t, 0, n)
32
33 tid = [profileIDSize]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
34 n = tid.MarshalProto(buf)
35 assert.Equal(t, profileIDSize, n)
36 assert.Equal(t, []byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}, buf[0:profileIDSize])
37 }
38
39 func TestProfileIDUnmarshal(t *testing.T) {
40 buf := [profileIDSize]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
41
42 tid := ProfileID{}
43 err := tid.UnmarshalProto(buf[:])
44 require.NoError(t, err)
45 assert.EqualValues(t, buf, tid)
46
47 err = tid.UnmarshalProto(buf[0:0])
48 require.NoError(t, err)
49 assert.EqualValues(t, [profileIDSize]byte{}, tid)
50
51 err = tid.UnmarshalProto(nil)
52 require.NoError(t, err)
53 assert.EqualValues(t, [profileIDSize]byte{}, tid)
54 }
55
56 func TestProfileIDMarshalAndUnmarshalJSON(t *testing.T) {
57 stream := json.BorrowStream(nil)
58 defer json.ReturnStream(stream)
59 src := ProfileID([profileIDSize]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
60 src.MarshalJSON(stream)
61 require.NoError(t, stream.Error())
62
63 iter := json.BorrowIterator(stream.Buffer())
64 defer json.ReturnIterator(iter)
65 dest := ProfileID{}
66 dest.UnmarshalJSON(iter)
67 require.NoError(t, iter.Error())
68
69 assert.Equal(t, src, dest)
70 }