1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include <stdint.h>
18 #include <gtest/gtest.h>
19 
20 #include "Log.h"
21 #include "StringUtil.h"
22 #include "task/TaskAll.h"
23 
24 
25 class TaskCaseTest : public testing::Test {
26 public:
27     TaskCase* mTaskCase;
SetUp()28     virtual void SetUp() {
29         mTaskCase = new TaskCase();
30         ASSERT_TRUE(mTaskCase != NULL);
31     }
32 
TearDown()33     virtual void TearDown() {
34         delete mTaskCase;
35     }
36 };
37 
38 
TEST_F(TaskCaseTest,DataMapTest)39 TEST_F(TaskCaseTest, DataMapTest) {
40     android::sp<Buffer> buffer1(new Buffer(4, 4, true));
41     android::sp<Buffer> buffer2(new Buffer(4, 4, true));
42     android::sp<Buffer> buffer3(new Buffer(4, 4, true));
43     android::sp<Buffer> buffer4(new Buffer(4, 4, true));
44 
45     const android::String8 BUFFER1("buffer1");
46     const android::String8 BUFFER2("buffer2");
47     const android::String8 BUFFER3("buffer3");
48     const android::String8 BUFFER4("buffer4");
49     ASSERT_TRUE(mTaskCase->registerBuffer(BUFFER1, buffer1));
50     ASSERT_TRUE(mTaskCase->registerBuffer(BUFFER2, buffer2));
51     ASSERT_TRUE(mTaskCase->registerBuffer(BUFFER3, buffer3));
52     ASSERT_TRUE(mTaskCase->registerBuffer(BUFFER4, buffer4));
53 
54     android::sp<Buffer> buffer1f = mTaskCase->findBuffer(BUFFER1);
55     //LOGI("buffer1 %x, buffer1f %x", &buffer1, buffer1f);
56     ASSERT_TRUE(buffer1.get() == buffer1f.get());
57     const android::String8 NO_SUCH_BUFFER("no_such_buffer");
58     buffer1f = mTaskCase->findBuffer(NO_SUCH_BUFFER);
59     ASSERT_TRUE(buffer1f.get() == NULL);
60     const android::String8 RE("buffer[1-2]");
61     std::list<TaskCase::BufferPair>* list = mTaskCase->findAllBuffers(RE);
62     ASSERT_TRUE(list != NULL);
63     ASSERT_TRUE(((list->front().second.get() == buffer1.get()) &&
64                     (list->back().second.get() == buffer2.get())) ||
65                 ((list->front().second.get() == buffer2.get()) &&
66                     (list->back().second.get() == buffer1.get())));
67     delete list;
68 }
69 
TEST_F(TaskCaseTest,ValueMapTest)70 TEST_F(TaskCaseTest, ValueMapTest) {
71     TaskCase::Value val1(1.0f);
72     TaskCase::Value val2(2.0f);
73     TaskCase::Value val3((int64_t)1);
74     TaskCase::Value val4((int64_t)2);
75     TaskCase::Value val2_copy(2.0f);
76     ASSERT_TRUE(!(val1 == val2));
77     ASSERT_TRUE(!(val2 == val3));
78     ASSERT_TRUE(val2 == val2_copy);
79     ASSERT_TRUE(val1.getDouble() == 1.0f);
80     ASSERT_TRUE(val3.getInt64() == 1);
81     const android::String8 V1("v1");
82     const android::String8 V2("v2");
83     const android::String8 V3("v3");
84     const android::String8 V4("v4");
85     const android::String8 V5("v5");
86     ASSERT_TRUE(mTaskCase->registerValue(V1, val1));
87     ASSERT_TRUE(mTaskCase->registerValue(V2, val2));
88     ASSERT_TRUE(mTaskCase->registerValue(V3, val3));
89     ASSERT_TRUE(mTaskCase->registerValue(V4, val4));
90 
91     TaskCase::Value valRead;
92     ASSERT_TRUE(mTaskCase->findValue(V4, valRead));
93     ASSERT_TRUE(valRead.getInt64() == 2);
94     TaskCase::Value val4_2((int64_t)3);
95     ASSERT_TRUE(mTaskCase->updateValue(V4, val4_2));
96     ASSERT_TRUE(mTaskCase->findValue(V4, valRead));
97     ASSERT_TRUE(valRead.getInt64() == 3);
98     ASSERT_TRUE(!mTaskCase->updateValue(V5, val4));
99     ASSERT_TRUE(!mTaskCase->findValue(V5, valRead));
100 
101     const android::String8 RE("v[2-3]");
102     std::list<TaskCase::ValuePair>* list = mTaskCase->findAllValues(RE);
103     ASSERT_TRUE(list != NULL);
104     ASSERT_TRUE(((list->front().second == val2) && (list->back().second == val3)) ||
105                 ((list->front().second == val3) && (list->back().second == val4)));
106     delete list;
107 }
108 
TEST_F(TaskCaseTest,IndexMapTest)109 TEST_F(TaskCaseTest, IndexMapTest) {
110     Buffer buffer1(4, 4, true);
111     Buffer buffer2(4, 4, true);
112     Buffer buffer3(4, 4, true);
113     Buffer buffer4(4, 4, true);
114 
115     int i = 0;
116     int j = 1;
117     const android::String8 I("i");
118     const android::String8 J("j");
119     const android::String8 K("k");
120     ASSERT_TRUE(mTaskCase->registerIndex(I));
121     ASSERT_TRUE(mTaskCase->registerIndex(J));
122     ASSERT_TRUE(mTaskCase->updateIndex(I, i));
123     ASSERT_TRUE(mTaskCase->updateIndex(J, j));
124     int i_read, j_read, k_read;
125     ASSERT_TRUE(mTaskCase->findIndex(I, i_read));
126     ASSERT_TRUE(mTaskCase->findIndex(J, j_read));
127     ASSERT_TRUE(!mTaskCase->findIndex(K, k_read));
128     ASSERT_TRUE(i == i_read);
129     ASSERT_TRUE(j == j_read);
130     //TODO add findAll test
131 }
132 
TEST_F(TaskCaseTest,VarTranslateTest)133 TEST_F(TaskCaseTest, VarTranslateTest) {
134     const android::String8 I("i");
135     const android::String8 J("j");
136     const android::String8 K("k");
137     ASSERT_TRUE(mTaskCase->registerIndex(I, 1));
138     ASSERT_TRUE(mTaskCase->registerIndex(J, 2));
139     ASSERT_TRUE(mTaskCase->registerIndex(K, 3));
140 
141     android::String8 orig1("hello_$i_$j");
142     android::String8 result1;
143     ASSERT_TRUE(mTaskCase->translateVarName(orig1, result1));
144     ASSERT_TRUE(StringUtil::compare(result1, "hello_1_2") == 0);
145 
146     android::String8 orig2("hello_$i_$j_$k_there");
147     android::String8 result2;
148     ASSERT_TRUE(mTaskCase->translateVarName(orig2, result2));
149     ASSERT_TRUE(StringUtil::compare(result2, "hello_1_2_3_there") == 0);
150 
151     // should fail as there is no such var
152     android::String8 orig3("$n");
153     android::String8 result3;
154     ASSERT_TRUE(!mTaskCase->translateVarName(orig3, result3));
155 
156     android::String8 orig4("hello_there");
157     android::String8 result4;
158     ASSERT_TRUE(mTaskCase->translateVarName(orig4, result4));
159     ASSERT_TRUE(StringUtil::compare(result4, "hello_there") == 0);
160 }
161