1 /*
2 * Copyright 2014 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 "Utils_test"
19
20 #include <gtest/gtest.h>
21
22 #include <media/stagefright/foundation/ADebug.h>
23 #include <media/stagefright/foundation/AStringUtils.h>
24 #include <media/stagefright/foundation/AUtils.h>
25 #include <media/stagefright/foundation/ByteUtils.h> // for FOURCC
26
27 namespace android {
28
29 class UtilsTest : public ::testing::Test {
30 };
31
TEST_F(UtilsTest,TestStringUtils)32 TEST_F(UtilsTest, TestStringUtils) {
33 ASSERT_EQ(AStringUtils::Compare("Audio", "AudioExt", 5, false), 0);
34 ASSERT_EQ(AStringUtils::Compare("Audio", "audiOExt", 5, true), 0);
35 ASSERT_NE(AStringUtils::Compare("Audio", "audioExt", 5, false), 0);
36 ASSERT_NE(AStringUtils::Compare("Audio", "AudiOExt", 5, false), 0);
37
38 ASSERT_LT(AStringUtils::Compare("Audio", "AudioExt", 7, false), 0);
39 ASSERT_LT(AStringUtils::Compare("Audio", "audiOExt", 7, true), 0);
40
41 ASSERT_GT(AStringUtils::Compare("AudioExt", "Audio", 7, false), 0);
42 ASSERT_GT(AStringUtils::Compare("audiOext", "Audio", 7, true), 0);
43
44 ASSERT_LT(AStringUtils::Compare("Audio", "Video", 5, false), 0);
45 ASSERT_LT(AStringUtils::Compare("Audio1", "Audio2", 6, false), 0);
46 ASSERT_LT(AStringUtils::Compare("audio", "VIDEO", 5, true), 0);
47 ASSERT_LT(AStringUtils::Compare("audio1", "AUDIO2", 6, true), 0);
48
49 ASSERT_GT(AStringUtils::Compare("Video", "Audio", 5, false), 0);
50 ASSERT_GT(AStringUtils::Compare("Audio2", "Audio1", 6, false), 0);
51 ASSERT_GT(AStringUtils::Compare("VIDEO", "audio", 5, true), 0);
52 ASSERT_GT(AStringUtils::Compare("AUDIO2", "audio1", 6, true), 0);
53
54 ASSERT_TRUE(AStringUtils::MatchesGlob("AudioA", 5, "AudioB", 5, false));
55 ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 6, "AudioA", 5, false));
56 ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "AudioA", 6, false));
57 ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "audiOB", 5, false));
58 ASSERT_TRUE(AStringUtils::MatchesGlob("AudioA", 5, "audiOB", 5, true));
59 ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 6, "AudioA", 5, true));
60 ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "AudioA", 6, true));
61
62 ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 6, true));
63 ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 6, false));
64 ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 0, true));
65 ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 0, false));
66
67 ASSERT_TRUE(AStringUtils::MatchesGlob("*ring1", 5, "String8", 6, false));
68 ASSERT_TRUE(AStringUtils::MatchesGlob("*ring2", 5, "STRING8", 6, true));
69 ASSERT_FALSE(AStringUtils::MatchesGlob("*ring4", 5, "StRing8", 6, false));
70 ASSERT_FALSE(AStringUtils::MatchesGlob("*ring5", 5, "StrinG8", 6, false));
71 ASSERT_FALSE(AStringUtils::MatchesGlob("*ring8", 5, "String8", 7, false));
72 ASSERT_FALSE(AStringUtils::MatchesGlob("*ring8", 5, "String8", 7, true));
73
74 ASSERT_TRUE(AStringUtils::MatchesGlob("Str*1", 4, "String8", 6, false));
75 ASSERT_TRUE(AStringUtils::MatchesGlob("Str*2", 4, "STRING8", 6, true));
76 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*3", 4, "string8", 6, false));
77 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*4", 4, "StRing8", 6, false));
78 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*5", 4, "AString8", 7, false));
79 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*6", 4, "AString8", 7, true));
80
81 ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ng1", 6, "String8", 6, false));
82 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng2", 6, "string8", 6, false));
83 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng3", 6, "StRing8", 6, false));
84 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng4", 6, "StriNg8", 6, false));
85 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng5", 6, "StrinG8", 6, false));
86 ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ng6", 6, "STRING8", 6, true));
87 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng8", 6, "AString8", 7, false));
88 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng1", 6, "String16", 7, false));
89 ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ing9", 7, "String8", 6, false));
90 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ringA", 8, "String8", 6, false));
91 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng8", 6, "AString8", 7, true));
92 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng1", 6, "String16", 7, true));
93 ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ing9", 7, "STRING8", 6, true));
94 ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ringA", 8, "String8", 6, true));
95
96 ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "bestrestroom", 9, false));
97 ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "bestrestrestroom", 13, false));
98 ASSERT_FALSE(AStringUtils::MatchesGlob("*str*stro", 8, "bestrestrestroom", 14, false));
99 ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str*1", 9, "bestrestrestroom", 14, false));
100 ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "beSTReSTRoom", 9, true));
101 ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "beSTRestreSTRoom", 13, true));
102 ASSERT_FALSE(AStringUtils::MatchesGlob("*str*stro", 8, "bestreSTReSTRoom", 14, true));
103 ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str*1", 9, "bestreSTReSTRoom", 14, true));
104 }
105
TEST_F(UtilsTest,TestDebug)106 TEST_F(UtilsTest, TestDebug) {
107 #define LVL(x) (ADebug::Level)(x)
108 ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "", LVL(5)), LVL(5));
109 ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", " \t \n ", LVL(2)), LVL(2));
110 ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3", LVL(5)), LVL(3));
111 ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3:*deo", LVL(5)), LVL(3));
112 ASSERT_EQ(ADebug::GetLevelFromSettingsString(
113 "video", "\t\n 3 \t\n:\t\n video \t\n", LVL(5)), LVL(3));
114 ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3:*deo,2:vid*", LVL(5)), LVL(2));
115 ASSERT_EQ(ADebug::GetLevelFromSettingsString(
116 "avideo", "\t\n 3 \t\n:\t\n avideo \t\n,\t\n 2 \t\n:\t\n video \t\n", LVL(5)), LVL(3));
117 ASSERT_EQ(ADebug::GetLevelFromSettingsString(
118 "audio.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(2));
119 ASSERT_EQ(ADebug::GetLevelFromSettingsString(
120 "video.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
121 ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
122 ASSERT_EQ(ADebug::GetLevelFromSettingsString("omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(4));
123 #undef LVL
124 }
125
TEST_F(UtilsTest,TestFourCC)126 TEST_F(UtilsTest, TestFourCC) {
127 ASSERT_EQ(FOURCC('s', 't', 'm' , 'u'), 'stmu');
128 }
129
TEST_F(UtilsTest,TestMathTemplates)130 TEST_F(UtilsTest, TestMathTemplates) {
131 ASSERT_EQ(divRound(-10, -4), 3);
132 ASSERT_EQ(divRound(-11, -4), 3);
133 ASSERT_EQ(divRound(-12, -4), 3);
134 ASSERT_EQ(divRound(-13, -4), 3);
135 ASSERT_EQ(divRound(-14, -4), 4);
136
137 ASSERT_EQ(divRound(10, -4), -3);
138 ASSERT_EQ(divRound(11, -4), -3);
139 ASSERT_EQ(divRound(12, -4), -3);
140 ASSERT_EQ(divRound(13, -4), -3);
141 ASSERT_EQ(divRound(14, -4), -4);
142
143 ASSERT_EQ(divRound(-10, 4), -3);
144 ASSERT_EQ(divRound(-11, 4), -3);
145 ASSERT_EQ(divRound(-12, 4), -3);
146 ASSERT_EQ(divRound(-13, 4), -3);
147 ASSERT_EQ(divRound(-14, 4), -4);
148
149 ASSERT_EQ(divRound(10, 4), 3);
150 ASSERT_EQ(divRound(11, 4), 3);
151 ASSERT_EQ(divRound(12, 4), 3);
152 ASSERT_EQ(divRound(13, 4), 3);
153 ASSERT_EQ(divRound(14, 4), 4);
154
155 ASSERT_EQ(divUp(-11, -4), 3);
156 ASSERT_EQ(divUp(-12, -4), 3);
157 ASSERT_EQ(divUp(-13, -4), 4);
158
159 ASSERT_EQ(divUp(11, -4), -2);
160 ASSERT_EQ(divUp(12, -4), -3);
161 ASSERT_EQ(divUp(13, -4), -3);
162
163 ASSERT_EQ(divUp(-11, 4), -2);
164 ASSERT_EQ(divUp(-12, 4), -3);
165 ASSERT_EQ(divUp(-13, 4), -3);
166
167 ASSERT_EQ(divUp(11, 4), 3);
168 ASSERT_EQ(divUp(12, 4), 3);
169 ASSERT_EQ(divUp(13, 4), 4);
170
171 ASSERT_EQ(align(11, 4), 12);
172 ASSERT_EQ(align(12, 4), 12);
173 ASSERT_EQ(align(13, 4), 16);
174 ASSERT_EQ(align(11, 8), 16);
175 ASSERT_EQ(align(11, 2), 12);
176 ASSERT_EQ(align(11, 1), 11);
177
178 ASSERT_EQ(abs(5L), 5L);
179 ASSERT_EQ(abs(-25), 25);
180
181 ASSERT_EQ(min(5.6f, 6.0f), 5.6f);
182 ASSERT_EQ(min(6.0f, 5.6f), 5.6f);
183 ASSERT_EQ(min(-4.3, 8.6), -4.3);
184 ASSERT_EQ(min(8.6, -4.3), -4.3);
185
186 ASSERT_EQ(max(5.6f, 6.0f), 6.0f);
187 ASSERT_EQ(max(6.0f, 5.6f), 6.0f);
188 ASSERT_EQ(max(-4.3, 8.6), 8.6);
189 ASSERT_EQ(max(8.6, -4.3), 8.6);
190
191 ASSERT_FALSE(isInRange(-43, 86u, -44));
192 ASSERT_TRUE(isInRange(-43, 87u, -43));
193 ASSERT_TRUE(isInRange(-43, 88u, -1));
194 ASSERT_TRUE(isInRange(-43, 89u, 0));
195 ASSERT_TRUE(isInRange(-43, 90u, 46));
196 ASSERT_FALSE(isInRange(-43, 91u, 48));
197 ASSERT_FALSE(isInRange(-43, 92u, 50));
198
199 ASSERT_FALSE(isInRange(43, 86u, 42));
200 ASSERT_TRUE(isInRange(43, 87u, 43));
201 ASSERT_TRUE(isInRange(43, 88u, 44));
202 ASSERT_TRUE(isInRange(43, 89u, 131));
203 ASSERT_FALSE(isInRange(43, 90u, 133));
204 ASSERT_FALSE(isInRange(43, 91u, 135));
205
206 ASSERT_FALSE(isInRange(43u, 86u, 42u));
207 ASSERT_TRUE(isInRange(43u, 85u, 43u));
208 ASSERT_TRUE(isInRange(43u, 84u, 44u));
209 ASSERT_TRUE(isInRange(43u, 83u, 125u));
210 ASSERT_FALSE(isInRange(43u, 82u, 125u));
211 ASSERT_FALSE(isInRange(43u, 81u, 125u));
212
213 ASSERT_FALSE(isInRange(-43, ~0u, 43));
214 ASSERT_FALSE(isInRange(-43, ~0u, 44));
215 ASSERT_FALSE(isInRange(-43, ~0u, ~0));
216 ASSERT_FALSE(isInRange(-43, ~0u, 41));
217 ASSERT_FALSE(isInRange(-43, ~0u, 40));
218
219 ASSERT_FALSE(isInRange(43u, ~0u, 43u));
220 ASSERT_FALSE(isInRange(43u, ~0u, 41u));
221 ASSERT_FALSE(isInRange(43u, ~0u, 40u));
222 ASSERT_FALSE(isInRange(43u, ~0u, ~0u));
223
224 ASSERT_FALSE(isInRange(-43, 86u, -44, 0u));
225 ASSERT_FALSE(isInRange(-43, 86u, -44, 1u));
226 ASSERT_FALSE(isInRange(-43, 86u, -44, 2u));
227 ASSERT_FALSE(isInRange(-43, 86u, -44, ~0u));
228 ASSERT_TRUE(isInRange(-43, 87u, -43, 0u));
229 ASSERT_TRUE(isInRange(-43, 87u, -43, 1u));
230 ASSERT_TRUE(isInRange(-43, 87u, -43, 86u));
231 ASSERT_TRUE(isInRange(-43, 87u, -43, 87u));
232 ASSERT_FALSE(isInRange(-43, 87u, -43, 88u));
233 ASSERT_FALSE(isInRange(-43, 87u, -43, ~0u));
234 ASSERT_TRUE(isInRange(-43, 88u, -1, 0u));
235 ASSERT_TRUE(isInRange(-43, 88u, -1, 45u));
236 ASSERT_TRUE(isInRange(-43, 88u, -1, 46u));
237 ASSERT_FALSE(isInRange(-43, 88u, -1, 47u));
238 ASSERT_FALSE(isInRange(-43, 88u, -1, ~3u));
239 ASSERT_TRUE(isInRange(-43, 90u, 46, 0u));
240 ASSERT_TRUE(isInRange(-43, 90u, 46, 1u));
241 ASSERT_FALSE(isInRange(-43, 90u, 46, 2u));
242 ASSERT_FALSE(isInRange(-43, 91u, 48, 0u));
243 ASSERT_FALSE(isInRange(-43, 91u, 48, 2u));
244 ASSERT_FALSE(isInRange(-43, 91u, 48, ~6u));
245 ASSERT_FALSE(isInRange(-43, 92u, 50, 0u));
246 ASSERT_FALSE(isInRange(-43, 92u, 50, 1u));
247
248 ASSERT_FALSE(isInRange(43u, 86u, 42u, 0u));
249 ASSERT_FALSE(isInRange(43u, 86u, 42u, 1u));
250 ASSERT_FALSE(isInRange(43u, 86u, 42u, 2u));
251 ASSERT_FALSE(isInRange(43u, 86u, 42u, ~0u));
252 ASSERT_TRUE(isInRange(43u, 87u, 43u, 0u));
253 ASSERT_TRUE(isInRange(43u, 87u, 43u, 1u));
254 ASSERT_TRUE(isInRange(43u, 87u, 43u, 86u));
255 ASSERT_TRUE(isInRange(43u, 87u, 43u, 87u));
256 ASSERT_FALSE(isInRange(43u, 87u, 43u, 88u));
257 ASSERT_FALSE(isInRange(43u, 87u, 43u, ~0u));
258 ASSERT_TRUE(isInRange(43u, 88u, 60u, 0u));
259 ASSERT_TRUE(isInRange(43u, 88u, 60u, 70u));
260 ASSERT_TRUE(isInRange(43u, 88u, 60u, 71u));
261 ASSERT_FALSE(isInRange(43u, 88u, 60u, 72u));
262 ASSERT_FALSE(isInRange(43u, 88u, 60u, ~3u));
263 ASSERT_TRUE(isInRange(43u, 90u, 132u, 0u));
264 ASSERT_TRUE(isInRange(43u, 90u, 132u, 1u));
265 ASSERT_FALSE(isInRange(43u, 90u, 132u, 2u));
266 ASSERT_FALSE(isInRange(43u, 91u, 134u, 0u));
267 ASSERT_FALSE(isInRange(43u, 91u, 134u, 2u));
268 ASSERT_FALSE(isInRange(43u, 91u, 134u, ~6u));
269 ASSERT_FALSE(isInRange(43u, 92u, 136u, 0u));
270 ASSERT_FALSE(isInRange(43u, 92u, 136u, 1u));
271
272 ASSERT_EQ(periodicError(124, 100), 24);
273 ASSERT_EQ(periodicError(288, 100), 12);
274 ASSERT_EQ(periodicError(-345, 100), 45);
275 ASSERT_EQ(periodicError(-493, 100), 7);
276 ASSERT_EQ(periodicError(-550, 100), 50);
277 ASSERT_EQ(periodicError(-600, 100), 0);
278 }
279
280 } // namespace android
281