1 /*
2 * Copyright (C) 2017 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 <iostream>
18
19 #include <gtest/gtest.h>
20
21 #include "utility/FixedBlockAdapter.h"
22 #include "utility/FixedBlockWriter.h"
23 #include "utility/FixedBlockReader.h"
24
25 #define FIXED_BLOCK_SIZE 47
26 #define TEST_BUFFER_SIZE 103
27
28 // Pass varying sized blocks.
29 // Frames contain a sequential index, which are easily checked.
30 class TestBlockAdapter {
31 public:
TestBlockAdapter()32 TestBlockAdapter()
33 : mTestIndex(0), mLastIndex(0) {
34 }
35
36 ~TestBlockAdapter() = default;
37
fillSequence(int32_t * indexBuffer,int32_t frameCount)38 void fillSequence(int32_t *indexBuffer, int32_t frameCount) {
39 ASSERT_LE(frameCount, TEST_BUFFER_SIZE);
40 for (int i = 0; i < frameCount; i++) {
41 indexBuffer[i] = mLastIndex++;
42 }
43 }
44
checkSequence(const int32_t * indexBuffer,int32_t frameCount)45 int checkSequence(const int32_t *indexBuffer, int32_t frameCount) {
46 // This is equivalent to calling an output callback.
47 for (int i = 0; i < frameCount; i++) {
48 int32_t expected = mTestIndex++;
49 int32_t actual = indexBuffer[i];
50 EXPECT_EQ(expected, actual);
51 if (actual != expected) {
52 return -1;
53 }
54 }
55 return 0;
56 }
57
58 int32_t mTestBuffer[TEST_BUFFER_SIZE];
59 int32_t mTestIndex;
60 int32_t mLastIndex;
61 };
62
63 class TestBlockWriter : public TestBlockAdapter, FixedBlockProcessor {
64 public:
TestBlockWriter()65 TestBlockWriter()
66 : mFixedBlockWriter(*this) {
67 mFixedBlockWriter.open(sizeof(int32_t) * FIXED_BLOCK_SIZE);
68 }
69
~TestBlockWriter()70 ~TestBlockWriter() {
71 mFixedBlockWriter.close();
72 }
73
onProcessFixedBlock(uint8_t * buffer,int32_t numBytes)74 int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override {
75 int32_t frameCount = numBytes / sizeof(int32_t);
76 return checkSequence((int32_t *) buffer, frameCount);
77 }
78
79 // Simulate audio input from a variable sized callback.
testInputWrite(int32_t variableCount)80 AdapterProcessResult testInputWrite(int32_t variableCount) {
81 fillSequence(mTestBuffer, variableCount);
82 int32_t sizeBytes = variableCount * sizeof(int32_t);
83 return mFixedBlockWriter.processVariableBlock((uint8_t *) mTestBuffer, sizeBytes);
84 }
85
86 private:
87 FixedBlockWriter mFixedBlockWriter;
88 };
89
90 class TestBlockReader : public TestBlockAdapter, FixedBlockProcessor {
91 public:
TestBlockReader()92 TestBlockReader()
93 : mFixedBlockReader(*this) {
94 mFixedBlockReader.open(sizeof(int32_t) * FIXED_BLOCK_SIZE);
95 }
96
~TestBlockReader()97 ~TestBlockReader() {
98 mFixedBlockReader.close();
99 }
100
onProcessFixedBlock(uint8_t * buffer,int32_t numBytes)101 int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override {
102 int32_t frameCount = numBytes / sizeof(int32_t);
103 fillSequence((int32_t *) buffer, frameCount);
104 return 0;
105 }
106
107 // Simulate audio output from a variable sized callback.
testOutputRead(int32_t variableCount)108 AdapterProcessResult testOutputRead(int32_t variableCount) {
109 int32_t sizeBytes = variableCount * sizeof(int32_t);
110 auto [result, dataProcessedBytes] =
111 mFixedBlockReader.processVariableBlock((uint8_t *) mTestBuffer, sizeBytes);
112 if (result >= 0) {
113 result = checkSequence((int32_t *)mTestBuffer, variableCount);
114 }
115 return {result, dataProcessedBytes};
116 }
117
118 private:
119 FixedBlockReader mFixedBlockReader;
120 };
121
122
TEST(test_block_adapter,block_adapter_write)123 TEST(test_block_adapter, block_adapter_write) {
124 TestBlockWriter tester;
125 int result = 0;
126 int bytesProcessed = 0;
127 const int numLoops = 1000;
128
129 for (int i = 0; i<numLoops && result == 0; i++) {
130 long r = random();
131 int32_t size = (r % TEST_BUFFER_SIZE);
132 ASSERT_LE(size, TEST_BUFFER_SIZE);
133 ASSERT_GE(size, 0);
134 std::tie(result, bytesProcessed) = tester.testInputWrite(size);
135 ASSERT_GE(bytesProcessed, 0);
136 }
137 ASSERT_EQ(0, result);
138 }
139
TEST(test_block_adapter,block_adapter_read)140 TEST(test_block_adapter, block_adapter_read) {
141 TestBlockReader tester;
142 int result = 0;
143 int bytesProcessed = 0;
144 const int numLoops = 1000;
145
146 for (int i = 0; i < numLoops && result == 0; i++) {
147 long r = random();
148 int32_t size = (r % TEST_BUFFER_SIZE);
149 ASSERT_LE(size, TEST_BUFFER_SIZE);
150 ASSERT_GE(size, 0);
151 std::tie(result, bytesProcessed) = tester.testOutputRead(size);
152 ASSERT_GE(bytesProcessed, 0);
153 }
154 ASSERT_EQ(0, result);
155 };
156
157