Utils_test.cpp revision a0940a569f2bc24b00dc10ce0fa7658b1dc3a3a5
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#include <utils/String8.h>
22#include <utils/Errors.h>
23#include <fcntl.h>
24#include <unistd.h>
25
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/AStringUtils.h>
28#include <media/stagefright/foundation/AUtils.h>
29#include <media/stagefright/Utils.h>
30
31namespace android {
32
33class UtilsTest : public ::testing::Test {
34};
35
36TEST_F(UtilsTest, TestStringUtils) {
37    ASSERT_EQ(AStringUtils::Compare("Audio", "AudioExt", 5, false), 0);
38    ASSERT_EQ(AStringUtils::Compare("Audio", "audiOExt", 5, true), 0);
39    ASSERT_NE(AStringUtils::Compare("Audio", "audioExt", 5, false), 0);
40    ASSERT_NE(AStringUtils::Compare("Audio", "AudiOExt", 5, false), 0);
41
42    ASSERT_LT(AStringUtils::Compare("Audio", "AudioExt", 7, false), 0);
43    ASSERT_LT(AStringUtils::Compare("Audio", "audiOExt", 7, true), 0);
44
45    ASSERT_GT(AStringUtils::Compare("AudioExt", "Audio", 7, false), 0);
46    ASSERT_GT(AStringUtils::Compare("audiOext", "Audio", 7, true), 0);
47
48    ASSERT_LT(AStringUtils::Compare("Audio", "Video", 5, false), 0);
49    ASSERT_LT(AStringUtils::Compare("Audio1", "Audio2", 6, false), 0);
50    ASSERT_LT(AStringUtils::Compare("audio", "VIDEO", 5, true), 0);
51    ASSERT_LT(AStringUtils::Compare("audio1", "AUDIO2", 6, true), 0);
52
53    ASSERT_GT(AStringUtils::Compare("Video", "Audio", 5, false), 0);
54    ASSERT_GT(AStringUtils::Compare("Audio2", "Audio1", 6, false), 0);
55    ASSERT_GT(AStringUtils::Compare("VIDEO", "audio", 5, true), 0);
56    ASSERT_GT(AStringUtils::Compare("AUDIO2", "audio1", 6, true), 0);
57
58    ASSERT_TRUE(AStringUtils::MatchesGlob("AudioA", 5, "AudioB", 5, false));
59    ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 6, "AudioA", 5, false));
60    ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "AudioA", 6, false));
61    ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "audiOB", 5, false));
62    ASSERT_TRUE(AStringUtils::MatchesGlob("AudioA", 5, "audiOB", 5, true));
63    ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 6, "AudioA", 5, true));
64    ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "AudioA", 6, true));
65
66    ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 6, true));
67    ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 6, false));
68    ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 0, true));
69    ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 0, false));
70
71    ASSERT_TRUE(AStringUtils::MatchesGlob("*ring1", 5, "String8", 6, false));
72    ASSERT_TRUE(AStringUtils::MatchesGlob("*ring2", 5, "STRING8", 6, true));
73    ASSERT_FALSE(AStringUtils::MatchesGlob("*ring4", 5, "StRing8", 6, false));
74    ASSERT_FALSE(AStringUtils::MatchesGlob("*ring5", 5, "StrinG8", 6, false));
75    ASSERT_FALSE(AStringUtils::MatchesGlob("*ring8", 5, "String8", 7, false));
76    ASSERT_FALSE(AStringUtils::MatchesGlob("*ring8", 5, "String8", 7, true));
77
78    ASSERT_TRUE(AStringUtils::MatchesGlob("Str*1", 4, "String8", 6, false));
79    ASSERT_TRUE(AStringUtils::MatchesGlob("Str*2", 4, "STRING8", 6, true));
80    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*3", 4, "string8", 6, false));
81    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*4", 4, "StRing8", 6, false));
82    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*5", 4, "AString8", 7, false));
83    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*6", 4, "AString8", 7, true));
84
85    ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ng1", 6, "String8", 6, false));
86    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng2", 6, "string8", 6, false));
87    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng3", 6, "StRing8", 6, false));
88    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng4", 6, "StriNg8", 6, false));
89    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng5", 6, "StrinG8", 6, false));
90    ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ng6", 6, "STRING8", 6, true));
91    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng8", 6, "AString8", 7, false));
92    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng1", 6, "String16", 7, false));
93    ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ing9", 7, "String8", 6, false));
94    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ringA", 8, "String8", 6, false));
95    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng8", 6, "AString8", 7, true));
96    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng1", 6, "String16", 7, true));
97    ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ing9", 7, "STRING8", 6, true));
98    ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ringA", 8, "String8", 6, true));
99
100    ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "bestrestroom", 9, false));
101    ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "bestrestrestroom", 13, false));
102    ASSERT_FALSE(AStringUtils::MatchesGlob("*str*stro", 8, "bestrestrestroom", 14, false));
103    ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str*1", 9, "bestrestrestroom", 14, false));
104    ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "beSTReSTRoom", 9, true));
105    ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "beSTRestreSTRoom", 13, true));
106    ASSERT_FALSE(AStringUtils::MatchesGlob("*str*stro", 8, "bestreSTReSTRoom", 14, true));
107    ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str*1", 9, "bestreSTReSTRoom", 14, true));
108}
109
110TEST_F(UtilsTest, TestDebug) {
111#define LVL(x) (ADebug::Level)(x)
112    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "", LVL(5)), LVL(5));
113    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "   \t  \n ", LVL(2)), LVL(2));
114    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "3", LVL(5)), LVL(3));
115    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "3:*deo", LVL(5)), LVL(3));
116    ASSERT_EQ(ADebug::GetDebugLevelFromString(
117            "video", "\t\n 3 \t\n:\t\n video \t\n", LVL(5)), LVL(3));
118    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "3:*deo,2:vid*", LVL(5)), LVL(2));
119    ASSERT_EQ(ADebug::GetDebugLevelFromString(
120            "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));
121    ASSERT_EQ(ADebug::GetDebugLevelFromString(
122            "audio.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(2));
123    ASSERT_EQ(ADebug::GetDebugLevelFromString(
124            "video.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
125    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
126    ASSERT_EQ(ADebug::GetDebugLevelFromString("omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(4));
127#undef LVL
128}
129
130TEST_F(UtilsTest, TestFourCC) {
131    ASSERT_EQ(FOURCC('s', 't', 'm' , 'u'), 'stmu');
132}
133
134TEST_F(UtilsTest, TestMathTemplates) {
135    ASSERT_EQ(divRound(-10, -4), 3);
136    ASSERT_EQ(divRound(-11, -4), 3);
137    ASSERT_EQ(divRound(-12, -4), 3);
138    ASSERT_EQ(divRound(-13, -4), 3);
139    ASSERT_EQ(divRound(-14, -4), 4);
140
141    ASSERT_EQ(divRound(10, -4), -3);
142    ASSERT_EQ(divRound(11, -4), -3);
143    ASSERT_EQ(divRound(12, -4), -3);
144    ASSERT_EQ(divRound(13, -4), -3);
145    ASSERT_EQ(divRound(14, -4), -4);
146
147    ASSERT_EQ(divRound(-10, 4), -3);
148    ASSERT_EQ(divRound(-11, 4), -3);
149    ASSERT_EQ(divRound(-12, 4), -3);
150    ASSERT_EQ(divRound(-13, 4), -3);
151    ASSERT_EQ(divRound(-14, 4), -4);
152
153    ASSERT_EQ(divRound(10, 4), 3);
154    ASSERT_EQ(divRound(11, 4), 3);
155    ASSERT_EQ(divRound(12, 4), 3);
156    ASSERT_EQ(divRound(13, 4), 3);
157    ASSERT_EQ(divRound(14, 4), 4);
158
159    ASSERT_EQ(divUp(-11, -4), 3);
160    ASSERT_EQ(divUp(-12, -4), 3);
161    ASSERT_EQ(divUp(-13, -4), 4);
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), -2);
168    ASSERT_EQ(divUp(-12, 4), -3);
169    ASSERT_EQ(divUp(-13, 4), -3);
170
171    ASSERT_EQ(divUp(11, 4), 3);
172    ASSERT_EQ(divUp(12, 4), 3);
173    ASSERT_EQ(divUp(13, 4), 4);
174
175    ASSERT_EQ(align(11, 4), 12);
176    ASSERT_EQ(align(12, 4), 12);
177    ASSERT_EQ(align(13, 4), 16);
178    ASSERT_EQ(align(11, 8), 16);
179    ASSERT_EQ(align(11, 2), 12);
180    ASSERT_EQ(align(11, 1), 11);
181
182    ASSERT_EQ(abs(5L), 5L);
183    ASSERT_EQ(abs(-25), 25);
184
185    ASSERT_EQ(min(5.6f, 6.0f), 5.6f);
186    ASSERT_EQ(min(6.0f, 5.6f), 5.6f);
187    ASSERT_EQ(min(-4.3, 8.6), -4.3);
188    ASSERT_EQ(min(8.6, -4.3), -4.3);
189
190    ASSERT_EQ(max(5.6f, 6.0f), 6.0f);
191    ASSERT_EQ(max(6.0f, 5.6f), 6.0f);
192    ASSERT_EQ(max(-4.3, 8.6), 8.6);
193    ASSERT_EQ(max(8.6, -4.3), 8.6);
194
195    ASSERT_EQ(periodicError(124, 100), 24);
196    ASSERT_EQ(periodicError(288, 100), 12);
197    ASSERT_EQ(periodicError(-345, 100), 45);
198    ASSERT_EQ(periodicError(-493, 100), 7);
199    ASSERT_EQ(periodicError(-550, 100), 50);
200    ASSERT_EQ(periodicError(-600, 100), 0);
201}
202
203} // namespace android
204