binapigen: fix union size
[govpp.git] / binapigen / generator_test.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
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:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package binapigen
16
17 import (
18         "fmt"
19         "git.fd.io/govpp.git/binapigen/vppapi"
20         . "github.com/onsi/gomega"
21         "testing"
22 )
23
24 func TestGoModule(t *testing.T) {
25         const expected = "git.fd.io/govpp.git/binapi"
26
27         impPath, err := resolveImportPath("../binapi")
28         if err != nil {
29                 t.Fatalf("unexpected error: %v", err)
30         }
31         if impPath != expected {
32                 t.Fatalf("expected: %q, got: %q", expected, impPath)
33         }
34 }
35
36 func TestBinapiTypeSizes(t *testing.T) {
37         tests := []struct {
38                 name    string
39                 input   string
40                 expsize int
41         }{
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},
48         }
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)
54                         }
55                 })
56         }
57 }
58
59 func TestBinapiUnionSizes(t *testing.T) {
60         RegisterTestingT(t)
61         tests := []struct {
62                 testName string
63                 input    *Union
64                 expsize  int
65         }{
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}}},
86                                 }},
87                                 {typ: "union", fields: []*typeTestData{
88                                         {typ: "struct", fields: []*typeTestData{{value: U32}}},
89                                 }},
90                                 {typ: "union", fields: []*typeTestData{
91                                         {typ: "struct", fields: []*typeTestData{{value: U64}}},
92                                 }},
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}}},
103                                 }},
104                         }}.getUnion("union6"), expsize: 7},
105         }
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))
110                 })
111         }
112 }
113
114 // Typed data used for union size evaluation testing.
115 type typeTestData struct {
116         typ    string
117         value  string
118         fields []*typeTestData
119 }
120
121 func (t typeTestData) getUnion(name string) *Union {
122         return &Union{
123                 UnionType: vppapi.UnionType{Name: name},
124                 Fields:    t.getUnionFields(name),
125         }
126 }
127
128 func (t typeTestData) getUnionFields(parentName string) (fields []*Field) {
129         for i, field := range t.fields {
130                 var (
131                         dataType   string
132                         aliasType  *Alias
133                         enumType   *Enum
134                         structType *Struct
135                         unionType  *Union
136                 )
137                 switch field.typ {
138                 case "alias":
139                         aliasType = &Alias{AliasType: vppapi.AliasType{Name: fmt.Sprintf("%s_alias_%d", parentName, i), Type: field.value}}
140                 case "enum":
141                         enumType = &Enum{EnumType: vppapi.EnumType{Name: fmt.Sprintf("%s_enum_%d", parentName, i), Type: field.value}}
142                 case "struct":
143                         structType = &Struct{Fields: field.getUnionFields(fmt.Sprintf("%s_struct_%d", parentName, i))}
144                 case "union":
145                         unionType = field.getUnion(parentName)
146                 default:
147                         dataType = field.value
148                 }
149                 fields = append(fields, &Field{
150                         Field:      vppapi.Field{Name: fmt.Sprintf("%s_field_%d", parentName, i), Type: dataType},
151                         TypeAlias:  aliasType,
152                         TypeEnum:   enumType,
153                         TypeStruct: structType,
154                         TypeUnion:  unionType,
155                 })
156         }
157         return fields
158 }