1 /*
2 * Copyright (C) 2016 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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "C2UtilTest"
19
20 #include <gtest/gtest.h>
21
22 #define __C2_GENERATE_GLOBAL_VARS__
23 #include <_C2MacroUtils.h>
24 #include <C2Enum.h>
25
26 /** \file
27 * Tests for vndk/util.
28 */
29
30 /* --------------------------------------- _C2MacroUtils --------------------------------------- */
31
32 static_assert(0 == _C2_ARGC(), "should be 0");
33 static_assert(1 == _C2_ARGC(1), "should be 1");
34 static_assert(2 == _C2_ARGC(1, 2), "should be 2");
35 static_assert(64 == _C2_ARGC(
36 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
37 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
38 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64), "should be 64");
39
40 static_assert(0 == _C2_ARGC(,), "should be 0");
41 static_assert(1 == _C2_ARGC(1,), "should be 1");
42 static_assert(2 == _C2_ARGC(1, 2,), "should be 2");
43 static_assert(64 == _C2_ARGC(
44 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
45 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
46 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,), "should be 64");
47
48 /* ---------------------------------------- _C2EnumUtils ---------------------------------------- */
49
50 class C2UtilTest : public ::testing::Test {
51 };
52
53 C2ENUM(Enum1, uint32_t,
54 Enum1Value1,
55 Enum1Value2,
56 Enum1Value4 = Enum1Value2 + 2,
57 );
58
59 C2ENUM_CUSTOM_PREFIX(Enum2, uint32_t, "Enum",
60 Enum2Value1,
61 Enum2Value2,
62 Enum2Value4 = Enum1Value2 + 2,
63 );
64
65 C2ENUM_CUSTOM_NAMES(Enum3, uint8_t,
66 ({ { "value1", Enum3Value1 },
67 { "value2", Enum3Value2 },
68 { "value4", Enum3Value4 },
69 { "invalid", Invalid } }),
70 Enum3Value1,
71 Enum3Value2,
72 Enum3Value4 = Enum3Value2 + 2,
73 Invalid,
74 );
75
TEST_F(C2UtilTest,EnumUtilsTest)76 TEST_F(C2UtilTest, EnumUtilsTest) {
77 std::vector<std::pair<C2String, Enum3>> pairs ( { { "value1", Enum3Value1 },
78 { "value2", Enum3Value2 },
79 { "value4", Enum3Value4 },
80 { "invalid", Invalid } });
81 Enum3 e3;
82 C2FieldDescriptor::namedValuesFor(e3);
83
84 // upper case
85 EXPECT_EQ("yes", _C2EnumUtils::camelCaseToDashed("YES"));
86 EXPECT_EQ("no", _C2EnumUtils::camelCaseToDashed("NO"));
87 EXPECT_EQ("yes-no", _C2EnumUtils::camelCaseToDashed("YES_NO"));
88 EXPECT_EQ("yes-no", _C2EnumUtils::camelCaseToDashed("YES__NO"));
89 EXPECT_EQ("a2dp", _C2EnumUtils::camelCaseToDashed("A2DP"));
90 EXPECT_EQ("mp2-ts", _C2EnumUtils::camelCaseToDashed("MP2_TS"));
91 EXPECT_EQ("block-2d", _C2EnumUtils::camelCaseToDashed("BLOCK_2D"));
92 EXPECT_EQ("mpeg-2-ts", _C2EnumUtils::camelCaseToDashed("MPEG_2_TS"));
93 EXPECT_EQ("_hidden-value", _C2EnumUtils::camelCaseToDashed("_HIDDEN_VALUE"));
94 EXPECT_EQ("__hidden-value2", _C2EnumUtils::camelCaseToDashed("__HIDDEN_VALUE2"));
95 EXPECT_EQ("__hidden-value-2", _C2EnumUtils::camelCaseToDashed("__HIDDEN_VALUE_2"));
96
97 // camel case
98 EXPECT_EQ("yes", _C2EnumUtils::camelCaseToDashed("Yes"));
99 EXPECT_EQ("no", _C2EnumUtils::camelCaseToDashed("No"));
100 EXPECT_EQ("yes-no", _C2EnumUtils::camelCaseToDashed("YesNo"));
101 EXPECT_EQ("yes-no", _C2EnumUtils::camelCaseToDashed("Yes_No"));
102 EXPECT_EQ("mp2-ts", _C2EnumUtils::camelCaseToDashed("MP2Ts"));
103 EXPECT_EQ("block-2d", _C2EnumUtils::camelCaseToDashed("Block2D"));
104 EXPECT_EQ("mpeg-2-ts", _C2EnumUtils::camelCaseToDashed("Mpeg2ts"));
105 EXPECT_EQ("_hidden-value", _C2EnumUtils::camelCaseToDashed("_HiddenValue"));
106 EXPECT_EQ("__hidden-value-2", _C2EnumUtils::camelCaseToDashed("__HiddenValue2"));
107
108 // mixed case
109 EXPECT_EQ("mp2t-s", _C2EnumUtils::camelCaseToDashed("MP2T_s"));
110 EXPECT_EQ("block-2d", _C2EnumUtils::camelCaseToDashed("Block_2D"));
111 EXPECT_EQ("block-2-d", _C2EnumUtils::camelCaseToDashed("Block2_D"));
112 EXPECT_EQ("mpeg-2-ts", _C2EnumUtils::camelCaseToDashed("Mpeg_2ts"));
113 EXPECT_EQ("mpeg-2-ts", _C2EnumUtils::camelCaseToDashed("Mpeg_2_TS"));
114 EXPECT_EQ("_hidden-value", _C2EnumUtils::camelCaseToDashed("_Hidden__VALUE"));
115 EXPECT_EQ("__hidden-value-2", _C2EnumUtils::camelCaseToDashed("__HiddenValue_2"));
116 EXPECT_EQ("_2", _C2EnumUtils::camelCaseToDashed("_2"));
117 EXPECT_EQ("__23", _C2EnumUtils::camelCaseToDashed("__23"));
118 }
119
120