1//
2// Copyright (C) 2024 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package main
18
19import (
20	"berberis/cpp_types"
21	"berberis/vulkan_xml"
22	"testing"
23)
24
25func TestAreBaseTypesDeclared(t *testing.T) {
26	foo_type := vulkan_xml.ExtendedStruct(cpp_types.StructType("Foo", []cpp_types.FieldInfo{
27		cpp_types.Field("i", cpp_types.UInt32TType)}), false, "")
28	bar_type := vulkan_xml.ExtendedStruct(cpp_types.StructType("Bar", []cpp_types.FieldInfo{
29		cpp_types.Field("i", cpp_types.UInt32TType),
30		cpp_types.Field("j", cpp_types.UInt64TType)}), false, "")
31	declared_types := make(map[string]cpp_types.Type)
32	if areBaseTypesDeclared(foo_type, declared_types) {
33		t.Error("Directly referred type is not declared, but areBaseTypesDeclared reports it as declared")
34	}
35	if areBaseTypesDeclared(bar_type, declared_types) {
36		t.Error("Directly referred type Bar is not declared, but areBaseTypesDeclared reports it as declared")
37	}
38	if areBaseTypesDeclared(cpp_types.ArrayType(foo_type, 1), declared_types) {
39		t.Error("Directly referred array of type Foo is not declared, but areBaseTypesDeclared reports it as declared")
40	}
41	if areBaseTypesDeclared(cpp_types.ArrayType(bar_type, 1), declared_types) {
42		t.Error("Directly referred array o type Bar is not declared, but areBaseTypesDeclared reports it as declared")
43	}
44	if areBaseTypesDeclared(cpp_types.PointerType(foo_type), declared_types) {
45		t.Error("Pointer for non-opaque struct is not declared, but areBaseTypesDeclared reports it as declared")
46	}
47	if !areBaseTypesDeclared(cpp_types.PointerType(cpp_types.OpaqueType("struct Baz")), declared_types) {
48		t.Error("Pointer for opaque struct must always be considered declared, but areBaseTypesDeclared reports it as not declared")
49	}
50	declared_types["struct Foo"] = foo_type
51	declared_types["struct Bar"] = bar_type
52	if !areBaseTypesDeclared(foo_type, declared_types) {
53		t.Error("Directly referred defined types are declared, but areBaseTypesDeclared reports it as not declared")
54	}
55	if !areBaseTypesDeclared(bar_type, declared_types) {
56		t.Error("Directly referred defined types are declared, but areBaseTypesDeclared reports it as not declared")
57	}
58	if !areBaseTypesDeclared(cpp_types.ArrayType(foo_type, 1), declared_types) {
59		t.Error("Directly referred arrays of defined types are considred declared, but areBaseTypesDeclared reports them as not declared")
60	}
61	if !areBaseTypesDeclared(cpp_types.ArrayType(bar_type, 1), declared_types) {
62		t.Error("Directly referred arrays of defined types are considred declared, but areBaseTypesDeclared reports them as not declared")
63	}
64	if !areBaseTypesDeclared(cpp_types.PointerType(bar_type), declared_types) {
65		t.Error("Indirectly referred types are considered declared, but areBaseTypesDeclared reports them as not declared")
66	}
67}
68