1 // Copyright (c) 2020 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
19 "git.fd.io/govpp.git/binapigen/vppapi"
20 . "github.com/onsi/gomega"
24 func TestGoModule(t *testing.T) {
25 const expected = "git.fd.io/govpp.git/binapi"
27 impPath, err := resolveImportPath("../binapi")
29 t.Fatalf("unexpected error: %v", err)
31 if impPath != expected {
32 t.Fatalf("expected: %q, got: %q", expected, impPath)
36 func TestBinapiTypeSizes(t *testing.T) {
42 {name: "basic1", input: "u8", expsize: 1},
43 {name: "basic2", input: "i8", expsize: 1},
44 {name: "basic3", input: "u16", expsize: 2},
45 {name: "basic4", input: "i32", expsize: 4},
46 {name: "string", input: "string", expsize: 1},
47 {name: "invalid1", input: "x", expsize: 0},
49 for _, test := range tests {
50 t.Run(test.name, func(t *testing.T) {
51 size := getSizeOfBinapiBaseType(test.input, 1)
52 if size != test.expsize {
53 t.Errorf("expected %d, got %d", test.expsize, size)
59 func TestBinapiUnionSizes(t *testing.T) {
66 {testName: "union_alias", input: typeTestData{
67 typ: "union", fields: []*typeTestData{{typ: "alias", value: U16},
68 }}.getUnion("union1"), expsize: 2},
69 {testName: "union_enum", input: typeTestData{
70 typ: "union", fields: []*typeTestData{{typ: "enum", value: U32},
71 }}.getUnion("union2"), expsize: 4},
72 {testName: "union_struct", input: typeTestData{
73 typ: "union", fields: []*typeTestData{
74 {typ: "struct", fields: []*typeTestData{{value: U8}, {value: U16}, {value: U32}}},
75 }}.getUnion("union3"), expsize: 7},
76 {testName: "union_structs", input: typeTestData{
77 typ: "union", fields: []*typeTestData{
78 {typ: "struct", fields: []*typeTestData{{value: U8}, {value: BOOL}}},
79 {typ: "struct", fields: []*typeTestData{{value: U16}, {value: U32}}},
80 {typ: "struct", fields: []*typeTestData{{value: U32}, {value: U64}}},
81 }}.getUnion("union4"), expsize: 12},
82 {testName: "union_unions", input: typeTestData{
83 typ: "union", fields: []*typeTestData{
84 {typ: "union", fields: []*typeTestData{
85 {typ: "struct", fields: []*typeTestData{{value: STRING}}},
87 {typ: "union", fields: []*typeTestData{
88 {typ: "struct", fields: []*typeTestData{{value: U32}}},
90 {typ: "union", fields: []*typeTestData{
91 {typ: "struct", fields: []*typeTestData{{value: U64}}},
93 }}.getUnion("union5"), expsize: 8},
94 {testName: "union_combined", input: typeTestData{
95 typ: "union", fields: []*typeTestData{
96 {typ: "alias", value: U8},
97 {typ: "enum", value: U16},
98 {typ: "struct", fields: []*typeTestData{{value: U8}, {value: U16}, {value: U32}}}, // <-
99 {typ: "union", fields: []*typeTestData{
100 {typ: "alias", value: U16},
101 {typ: "enum", value: U16},
102 {typ: "struct", fields: []*typeTestData{{value: U32}}},
104 }}.getUnion("union6"), expsize: 7},
106 for _, test := range tests {
107 t.Run(test.testName, func(t *testing.T) {
108 size := getUnionSize(test.input)
109 Expect(size).To(Equal(test.expsize))
114 // Typed data used for union size evaluation testing.
115 type typeTestData struct {
118 fields []*typeTestData
121 func (t typeTestData) getUnion(name string) *Union {
123 UnionType: vppapi.UnionType{Name: name},
124 Fields: t.getUnionFields(name),
128 func (t typeTestData) getUnionFields(parentName string) (fields []*Field) {
129 for i, field := range t.fields {
139 aliasType = &Alias{AliasType: vppapi.AliasType{Name: fmt.Sprintf("%s_alias_%d", parentName, i), Type: field.value}}
141 enumType = &Enum{EnumType: vppapi.EnumType{Name: fmt.Sprintf("%s_enum_%d", parentName, i), Type: field.value}}
143 structType = &Struct{Fields: field.getUnionFields(fmt.Sprintf("%s_struct_%d", parentName, i))}
145 unionType = field.getUnion(parentName)
147 dataType = field.value
149 fields = append(fields, &Field{
150 Field: vppapi.Field{Name: fmt.Sprintf("%s_field_%d", parentName, i), Type: dataType},
151 TypeAlias: aliasType,
153 TypeStruct: structType,
154 TypeUnion: unionType,