Home | History | Annotate | Download | only in tests
      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:
     32     TestBlockAdapter()
     33             : mTestIndex(0), mLastIndex(0) {
     34     }
     35 
     36     ~TestBlockAdapter() = default;
     37 
     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 
     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:
     65     TestBlockWriter()
     66             : mFixedBlockWriter(*this) {
     67         mFixedBlockWriter.open(sizeof(int32_t) * FIXED_BLOCK_SIZE);
     68     }
     69 
     70     ~TestBlockWriter() {
     71         mFixedBlockWriter.close();
     72     }
     73 
     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.
     80     int32_t 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:
     92     TestBlockReader()
     93             : mFixedBlockReader(*this) {
     94         mFixedBlockReader.open(sizeof(int32_t) * FIXED_BLOCK_SIZE);
     95     }
     96 
     97     ~TestBlockReader() {
     98         mFixedBlockReader.close();
     99     }
    100 
    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.
    108     int32_t testOutputRead(int32_t variableCount) {
    109         int32_t sizeBytes = variableCount * sizeof(int32_t);
    110         int32_t result = mFixedBlockReader.processVariableBlock((uint8_t *) mTestBuffer, sizeBytes);
    111         if (result >= 0) {
    112             result = checkSequence((int32_t *)mTestBuffer, variableCount);
    113         }
    114         return result;
    115     }
    116 
    117 private:
    118     FixedBlockReader   mFixedBlockReader;
    119 };
    120 
    121 
    122 TEST(test_block_adapter, block_adapter_write) {
    123     TestBlockWriter tester;
    124     int result = 0;
    125     const int numLoops = 1000;
    126 
    127     for (int i = 0; i<numLoops && result == 0; i++) {
    128         long r = random();
    129         int32_t size = (r % TEST_BUFFER_SIZE);
    130         ASSERT_LE(size, TEST_BUFFER_SIZE);
    131         ASSERT_GE(size, 0);
    132         result = tester.testInputWrite(size);
    133     }
    134     ASSERT_EQ(0, result);
    135 }
    136 
    137 TEST(test_block_adapter, block_adapter_read) {
    138     TestBlockReader tester;
    139     int result = 0;
    140     const int numLoops = 1000;
    141 
    142     for (int i = 0; i < numLoops && result == 0; i++) {
    143         long r = random();
    144         int32_t size = (r % TEST_BUFFER_SIZE);
    145         ASSERT_LE(size, TEST_BUFFER_SIZE);
    146         ASSERT_GE(size, 0);
    147         result = tester.testOutputRead(size);
    148     }
    149     ASSERT_EQ(0, result);
    150 };
    151 
    152