2 * Copyright 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <gtest/gtest.h>
20 #define ARRAYLEN(x) (sizeof(x) / sizeof(x[0]))
22 TEST(ParserTest, normalizeTypeDeclaration) {
28 { "const unsigned int", " const unsigned\tint\n" },
29 { "char* const**", "char *const* *" },
31 const size_t kDataSize = ARRAYLEN(kData);
32 for (size_t n = 0; n < kDataSize; ++n) {
34 std::string text = "When parsing '";
35 text += kData[n].input;
38 result = normalizeTypeDeclaration(kData[n].input);
39 EXPECT_STREQ(kData[n].expected, result.c_str()) << text;
43 TEST(ParserTest, parseTypeDeclaration) {
47 const char* expectedType;
48 const char* expectedError;
50 { "const", false, NULL, "Missing type name" },
51 { "const const", false, NULL, "Missing type name" },
52 { "foo", true, "foo", NULL },
53 { "void", true, "void", NULL },
54 { "const foo", true, "const foo", NULL },
55 { "foo *", true, "foo*", NULL },
56 { "char foo", true, "char foo", NULL },
57 { "\tunsigned \t int\n", true, "unsigned int", NULL },
58 { "const * char", false, NULL, "Unexpected '*' before type name" },
59 { "const char * ", true, "const char*", NULL },
60 { "const void*const * *", true, "const void* const**", NULL },
62 const size_t kDataSize = ARRAYLEN(kData);
63 for (size_t n = 0; n < kDataSize; ++n) {
64 std::string varname, vartype, error;
65 std::string text = "When parsing '";
66 text += kData[n].input;
69 EXPECT_EQ(kData[n].expected,
70 parseTypeDeclaration(kData[n].input,
73 if (kData[n].expected) {
74 EXPECT_STREQ(kData[n].expectedType, vartype.c_str()) << text;
76 EXPECT_STREQ(kData[n].expectedError, error.c_str()) << text;
81 TEST(ParserTest, parseParameterDeclaration) {
85 const char* expectedType;
86 const char* expectedVariable;
87 const char* expectedError;
89 { "foo", false, NULL, NULL, "Missing variable name" },
90 { "const", false, NULL, NULL, "Missing type name" },
91 { "const foo", false, NULL, NULL, "Missing variable name" },
92 { "const const", false, NULL, NULL, "Missing type name" },
93 { "char foo", true, "char", "foo", NULL },
94 { "unsigned int\t bar\n", true, "unsigned int", "bar", NULL },
95 { "const * char foo", false, NULL, NULL, "Unexpected '*' before type name" },
96 { "const char * foo", true, "const char*", "foo", NULL },
97 { "const void*const *data", true, "const void* const*", "data", NULL },
98 { "char foo const", false, NULL, NULL, "Extra 'const' after variable name" },
99 { "int bar*", false, NULL, NULL, "Extra '*' after variable name" },
101 const size_t kDataSize = ARRAYLEN(kData);
102 for (size_t n = 0; n < kDataSize; ++n) {
103 std::string varname, vartype, error;
104 std::string text = "When parsing '";
105 text += kData[n].input;
108 EXPECT_EQ(kData[n].expected,
109 parseParameterDeclaration(kData[n].input,
113 if (kData[n].expected) {
114 EXPECT_STREQ(kData[n].expectedType, vartype.c_str()) << text;
115 EXPECT_STREQ(kData[n].expectedVariable, varname.c_str()) << text;
117 EXPECT_STREQ(kData[n].expectedError, error.c_str()) << text;