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 <memory>
21 
22 #include "audio/Buffer.h"
23 
24 
25 class BufferTest : public testing::Test {
26 public:
27 
SetUp()28     virtual void SetUp() {
29 
30     }
31 
TearDown()32     virtual void TearDown() {
33 
34     }
35 };
36 
37 
TEST_F(BufferTest,saveLoadStereoTest)38 TEST_F(BufferTest, saveLoadStereoTest) {
39     const int BUFFER_SIZE = 32;
40 
41     std::unique_ptr<Buffer> buffer(new Buffer(BUFFER_SIZE, BUFFER_SIZE, true));
42     ASSERT_TRUE(buffer.get() != NULL);
43     int16_t* data = (int16_t*)buffer->getData();
44     ASSERT_TRUE(data != NULL);
45     for (int i = 0; i < BUFFER_SIZE/4; i++) {
46         data[2*i] = i;
47         data[2*i+1] = i;
48     }
49     android::String8 file("/tmp/cts_audio_temp");
50     ASSERT_TRUE(buffer->saveToFile(file));
51     file.append(".r2s");
52     std::unique_ptr<Buffer> bufferRead(Buffer::loadFromFile(file));
53     ASSERT_TRUE(bufferRead.get() != NULL);
54     ASSERT_TRUE(bufferRead->getSize() == (size_t)BUFFER_SIZE);
55     ASSERT_TRUE(bufferRead->isStereo());
56     int16_t* dataRead = (int16_t*)bufferRead->getData();
57     for (int i = 0; i < BUFFER_SIZE/4; i++) {
58         ASSERT_TRUE(data[2*i] == dataRead[2*i]);
59         ASSERT_TRUE(data[2*i+1] == dataRead[2*i+1]);
60     }
61 }
62 
TEST_F(BufferTest,monoLTest)63 TEST_F(BufferTest, monoLTest) {
64     const int BUFFER_SIZE = 8;
65 
66     std::unique_ptr<Buffer> buffer(new Buffer(BUFFER_SIZE, BUFFER_SIZE, true));
67     ASSERT_TRUE(buffer.get() != NULL);
68     int16_t* data = (int16_t*)buffer->getData();
69     ASSERT_TRUE(data != NULL);
70     for (int i = 0; i < BUFFER_SIZE/2; i++) {
71         data[i] = i;
72     }
73     std::unique_ptr<Buffer> bufferl(new Buffer(BUFFER_SIZE/2, BUFFER_SIZE/2, false));
74     ASSERT_TRUE(bufferl.get() != NULL);
75     data = (int16_t*)bufferl->getData();
76     ASSERT_TRUE(data != NULL);
77     for (int i = 0; i < BUFFER_SIZE/4; i++) {
78         data[i] = 2 * i;
79     }
80     buffer->changeToMono(Buffer::EKeepCh0);
81     ASSERT_TRUE((*buffer) == (*bufferl));
82 }
83