1 /*
2  * Copyright (C) 2012 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 #include <gtest/gtest.h>
18 #include <utils/String8.h>
19 
20 #include <audio/AudioSignalFactory.h>
21 #include <SignalProcessingInterface.h>
22 #include <SignalProcessingImpl.h>
23 #include <task/TaskAll.h>
24 
25 class SignalProcessingInterfaceTest : public testing::Test {
26 protected:
27     SignalProcessingImpl* mSp;
28 
29 protected:
SetUp()30     virtual void SetUp() {
31         mSp = new SignalProcessingImpl();
32         ASSERT_TRUE(mSp != NULL);
33         ASSERT_TRUE(mSp->init(SignalProcessingImpl::MAIN_PROCESSING_SCRIPT));
34     }
35 
TearDown()36     virtual void TearDown() {
37         delete mSp;
38         mSp = NULL;
39     }
40 };
41 
TEST_F(SignalProcessingInterfaceTest,InitTest)42 TEST_F(SignalProcessingInterfaceTest, InitTest) {
43     // SetUp do all the work, nothing to do
44 }
45 
TEST_F(SignalProcessingInterfaceTest,EchoTest)46 TEST_F(SignalProcessingInterfaceTest, EchoTest) {
47     android::String8 functionName("echo");
48     int nInputs = 4;
49     int nOutputs = 4;
50     bool inputTypes[4] = { true, true, false, false };
51     bool outputTypes[4] = { true, true, false, false };
52 
53     android::sp<Buffer> in0(new Buffer(160000, 160000, true));
54     char* data0 = in0->getData();
55     for (size_t i = 0; i < in0->getSize(); i++) {
56         data0[i] = i;
57     }
58     android::sp<Buffer> in1(new Buffer(8, 8, false));
59     char* data1 = in1->getData();
60     for (size_t i = 0; i < in1->getSize(); i++) {
61         data1[i] = i;
62     }
63     TaskCase::Value in2(1.0f);
64     TaskCase::Value in3((int64_t)100);
65     void* inputs[4] = { &in0, &in1, &in2, &in3 };
66 
67     android::sp<Buffer> out0(new Buffer(160000, 160000, true));
68     char* outdata0 = out0->getData();
69     for (size_t i = 0; i < out0->getSize(); i++) {
70         outdata0[i] = 0xaa;
71     }
72     android::sp<Buffer> out1(new Buffer(8, 8, false));
73     char* outdata1 = out1->getData();
74     for (size_t i = 0; i < out1->getSize(); i++) {
75         outdata1[i] = 0xbb;
76     }
77     TaskCase::Value out2(-1.0f);
78     TaskCase::Value out3((int64_t)1000);
79     void *outputs[4] = { &out0, &out1, &out2, &out3 };
80 
81     ASSERT_TRUE(mSp->run( functionName,
82             nInputs, inputTypes, inputs,
83             nOutputs, outputTypes, outputs) == TaskGeneric::EResultOK);
84     ASSERT_TRUE(*(in0.get()) == *(out0.get()));
85     ASSERT_TRUE(*(in1.get()) == *(out1.get()));
86     ASSERT_TRUE(in2 == out2);
87     ASSERT_TRUE(in3 == out3);
88 }
89 
TEST_F(SignalProcessingInterfaceTest,intsumTest)90 TEST_F(SignalProcessingInterfaceTest, intsumTest) {
91     android::String8 functionName("intsum");
92     int nInputs = 2;
93     int nOutputs = 1;
94     bool inputTypes[2] = { false, false };
95     bool outputTypes[1] = { false };
96 
97     TaskCase::Value in0((int64_t)10);
98     TaskCase::Value in1((int64_t)100);
99     void* inputs[2] = { &in0, &in1 };
100 
101     TaskCase::Value out0((int64_t)0);
102     void *outputs[1] = { &out0 };
103 
104     ASSERT_TRUE(mSp->run( functionName,
105             nInputs, inputTypes, inputs,
106             nOutputs, outputTypes, outputs) == TaskGeneric::EResultOK);
107     ASSERT_TRUE(out0.getInt64() == (in0.getInt64() + in1.getInt64()));
108 }
109 
110 // two instances of python processing processes should work
TEST_F(SignalProcessingInterfaceTest,TwoInstanceTest)111 TEST_F(SignalProcessingInterfaceTest, TwoInstanceTest) {
112     SignalProcessingImpl* sp2 = new SignalProcessingImpl();
113     ASSERT_TRUE(sp2 != NULL);
114     ASSERT_TRUE(sp2->init(SignalProcessingImpl::MAIN_PROCESSING_SCRIPT));
115 
116     android::String8 functionName("intsum");
117     int nInputs = 2;
118     int nOutputs = 1;
119     bool inputTypes[2] = { false, false };
120     bool outputTypes[1] = { false };
121 
122     TaskCase::Value in0((int64_t)10);
123     TaskCase::Value in1((int64_t)100);
124     void* inputs[2] = { &in0, &in1 };
125 
126     TaskCase::Value out0((int64_t)0);
127     void *outputs[1] = { &out0 };
128 
129     ASSERT_TRUE(mSp->run( functionName,
130             nInputs, inputTypes, inputs,
131             nOutputs, outputTypes, outputs) == TaskGeneric::EResultOK);
132     ASSERT_TRUE(out0.getInt64() == (in0.getInt64() + in1.getInt64()));
133     out0.setInt64(0);
134     ASSERT_TRUE(sp2->run( functionName,
135                 nInputs, inputTypes, inputs,
136                 nOutputs, outputTypes, outputs) == TaskGeneric::EResultOK);
137     ASSERT_TRUE(out0.getInt64() == (in0.getInt64() + in1.getInt64()));
138     delete sp2;
139 }
140 
141 // test to run processing/example.py
TEST_F(SignalProcessingInterfaceTest,exampleTest)142 TEST_F(SignalProcessingInterfaceTest, exampleTest) {
143     android::String8 functionName("example");
144     int nInputs = 8;
145     int nOutputs = 4;
146     bool inputTypes[8] = { true, true, true, true, false, false, false, false };
147     bool outputTypes[4] = { true, true, false, false };
148 
149     android::sp<Buffer> in0(new Buffer(16, 16, true));
150     char* data0 = in0->getData();
151     for (size_t i = 0; i < in0->getSize(); i++) {
152         data0[i] = i;
153     }
154     android::sp<Buffer> in1(new Buffer(16, 16, true));
155     char* data1 = in1->getData();
156     for (size_t i = 0; i < in1->getSize(); i++) {
157         data1[i] = i;
158     }
159     android::sp<Buffer> in2(new Buffer(8, 8, false));
160     char* data2 = in2->getData();
161     for (size_t i = 0; i < in2->getSize(); i++) {
162         data2[i] = i;
163     }
164     android::sp<Buffer> in3(new Buffer(8, 8, false));
165     char* data3 = in3->getData();
166     for (size_t i = 0; i < in3->getSize(); i++) {
167         data3[i] = i;
168     }
169     TaskCase::Value in4((int64_t)100);
170     TaskCase::Value in5((int64_t)100);
171     TaskCase::Value in6(1.0f);
172     TaskCase::Value in7(1.0f);
173     void* inputs[8] = { &in0, &in1, &in2, &in3, &in4, &in5, &in6, &in7 };
174 
175     android::sp<Buffer> out0(new Buffer(16, 16, true));
176     char* outdata0 = out0->getData();
177     for (size_t i = 0; i < out0->getSize(); i++) {
178         outdata0[i] = 0xaa;
179     }
180     android::sp<Buffer> out1(new Buffer(8, 8, false));
181     char* outdata1 = out1->getData();
182     for (size_t i = 0; i < out1->getSize(); i++) {
183         outdata1[i] = 0xbb;
184     }
185     TaskCase::Value out2((int64_t)1000);
186     TaskCase::Value out3(-1.0f);
187     void *outputs[4] = { &out0, &out1, &out2, &out3 };
188 
189     ASSERT_TRUE(mSp->run( functionName,
190             nInputs, inputTypes, inputs,
191             nOutputs, outputTypes, outputs) == TaskGeneric::EResultOK);
192     ASSERT_TRUE(*(in0.get()) == *(out0.get()));
193     ASSERT_TRUE(*(in2.get()) == *(out1.get()));
194     ASSERT_TRUE(in4 == out2);
195     ASSERT_TRUE(in6 == out3);
196 }
197