1 // Copyright 2019 The Amber Authors.
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 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,ExtensionInstance)23 TEST_F(AmberScriptParserTest, ExtensionInstance) {
24 std::string in = "INSTANCE_EXTENSION VK_KHR_storage_buffer_storage_class";
25
26 Parser parser;
27 Result r = parser.Parse(in);
28 ASSERT_TRUE(r.IsSuccess()) << r.Error();
29
30 auto script = parser.GetScript();
31 auto ext = script->GetRequiredInstanceExtensions();
32 ASSERT_EQ(1U, ext.size());
33 EXPECT_EQ("VK_KHR_storage_buffer_storage_class", ext[0]);
34 }
35
TEST_F(AmberScriptParserTest,ExtensionInstanceMulti)36 TEST_F(AmberScriptParserTest, ExtensionInstanceMulti) {
37 std::string in = R"(
38 INSTANCE_EXTENSION VK_KHR_storage_buffer_storage_class
39 INSTANCE_EXTENSION VK_KHR_variable_pointers)";
40
41 Parser parser;
42 Result r = parser.Parse(in);
43 ASSERT_TRUE(r.IsSuccess()) << r.Error();
44
45 auto script = parser.GetScript();
46 auto ext = script->GetRequiredInstanceExtensions();
47 ASSERT_EQ(2U, ext.size());
48 EXPECT_EQ("VK_KHR_storage_buffer_storage_class", ext[0]);
49 EXPECT_EQ("VK_KHR_variable_pointers", ext[1]);
50 }
51
TEST_F(AmberScriptParserTest,ExtensionInstanceMissingName)52 TEST_F(AmberScriptParserTest, ExtensionInstanceMissingName) {
53 std::string in = "INSTANCE_EXTENSION";
54
55 Parser parser;
56 Result r = parser.Parse(in);
57 ASSERT_FALSE(r.IsSuccess());
58
59 EXPECT_EQ("1: INSTANCE_EXTENSION missing name", r.Error());
60 }
61
TEST_F(AmberScriptParserTest,ExtensionInstanceInvalidName)62 TEST_F(AmberScriptParserTest, ExtensionInstanceInvalidName) {
63 std::string in = "INSTANCE_EXTENSION 1234";
64
65 Parser parser;
66 Result r = parser.Parse(in);
67 ASSERT_FALSE(r.IsSuccess());
68
69 EXPECT_EQ("1: INSTANCE_EXTENSION invalid name: 1234", r.Error());
70 }
71
TEST_F(AmberScriptParserTest,ExtensionInstanceExtraParams)72 TEST_F(AmberScriptParserTest, ExtensionInstanceExtraParams) {
73 std::string in = "INSTANCE_EXTENSION VK_KHR_variable_pointers EXTRA";
74
75 Parser parser;
76 Result r = parser.Parse(in);
77 ASSERT_FALSE(r.IsSuccess());
78
79 EXPECT_EQ("1: extra parameters after INSTANCE_EXTENSION command: EXTRA",
80 r.Error());
81 }
82
TEST_F(AmberScriptParserTest,ExtensionDevice)83 TEST_F(AmberScriptParserTest, ExtensionDevice) {
84 std::string in = "DEVICE_EXTENSION VK_KHR_get_physical_device_properties2";
85
86 Parser parser;
87 Result r = parser.Parse(in);
88 ASSERT_TRUE(r.IsSuccess()) << r.Error();
89
90 auto script = parser.GetScript();
91 auto ext = script->GetRequiredDeviceExtensions();
92 ASSERT_EQ(1U, ext.size());
93 EXPECT_EQ("VK_KHR_get_physical_device_properties2", ext[0]);
94 }
95
TEST_F(AmberScriptParserTest,ExtensionDeviceMulti)96 TEST_F(AmberScriptParserTest, ExtensionDeviceMulti) {
97 std::string in = R"(
98 DEVICE_EXTENSION VK_KHR_get_physical_device_properties2
99 DEVICE_EXTENSION VK_KHR_external_memory)";
100
101 Parser parser;
102 Result r = parser.Parse(in);
103 ASSERT_TRUE(r.IsSuccess()) << r.Error();
104
105 auto script = parser.GetScript();
106 auto ext = script->GetRequiredDeviceExtensions();
107 ASSERT_EQ(2U, ext.size());
108 EXPECT_EQ("VK_KHR_get_physical_device_properties2", ext[0]);
109 EXPECT_EQ("VK_KHR_external_memory", ext[1]);
110 }
111
TEST_F(AmberScriptParserTest,ExtensionDeviceMissingName)112 TEST_F(AmberScriptParserTest, ExtensionDeviceMissingName) {
113 std::string in = "DEVICE_EXTENSION";
114
115 Parser parser;
116 Result r = parser.Parse(in);
117 ASSERT_FALSE(r.IsSuccess());
118 EXPECT_EQ("1: DEVICE_EXTENSION missing name", r.Error());
119 }
120
TEST_F(AmberScriptParserTest,ExtensionDeviceInvalidName)121 TEST_F(AmberScriptParserTest, ExtensionDeviceInvalidName) {
122 std::string in = "DEVICE_EXTENSION 1234";
123
124 Parser parser;
125 Result r = parser.Parse(in);
126 ASSERT_FALSE(r.IsSuccess());
127 EXPECT_EQ("1: DEVICE_EXTENSION invalid name: 1234", r.Error());
128 }
129
TEST_F(AmberScriptParserTest,ExtensionDeviceExtraParams)130 TEST_F(AmberScriptParserTest, ExtensionDeviceExtraParams) {
131 std::string in = "DEVICE_EXTENSION VK_KHR_external_memory EXTRA";
132
133 Parser parser;
134 Result r = parser.Parse(in);
135 ASSERT_FALSE(r.IsSuccess());
136 EXPECT_EQ("1: extra parameters after DEVICE_EXTENSION command: EXTRA",
137 r.Error());
138 }
139
140 } // namespace amberscript
141 } // namespace amber
142