| 1 |
/* |
| 2 |
Copyright (C) 2012, Michael Pruett. All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with or without |
| 5 |
modification, are permitted provided that the following conditions |
| 6 |
are met: |
| 7 |
|
| 8 |
1. Redistributions of source code must retain the above copyright |
| 9 |
notice, this list of conditions and the following disclaimer. |
| 10 |
|
| 11 |
2. Redistributions in binary form must reproduce the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer in the |
| 13 |
documentation and/or other materials provided with the distribution. |
| 14 |
|
| 15 |
3. The name of the author may not be used to endorse or promote products |
| 16 |
derived from this software without specific prior written permission. |
| 17 |
|
| 18 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 |
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
#include <unistd.h> |
| 32 |
|
| 33 |
#include <gtest/gtest.h> |
| 34 |
#include <audiofile.h> |
| 35 |
|
| 36 |
TEST(Pipe, Pipe) |
| 37 |
{ |
| 38 |
const int kFrameCount = 500; |
| 39 |
const int kSampleCount = 2 * kFrameCount; |
| 40 |
|
| 41 |
int16_t data[kSampleCount]; |
| 42 |
int16_t readData[kSampleCount]; |
| 43 |
|
| 44 |
for (int i=0; i<kSampleCount; i++) |
| 45 |
data[i] = 3*i + 2; |
| 46 |
|
| 47 |
AFfilesetup setup = afNewFileSetup(); |
| 48 |
ASSERT_TRUE(setup); |
| 49 |
|
| 50 |
afInitFileFormat(setup, AF_FILE_RAWDATA); |
| 51 |
afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); |
| 52 |
afInitChannels(setup, AF_DEFAULT_TRACK, 2); |
| 53 |
afInitRate(setup, AF_DEFAULT_TRACK, 44100); |
| 54 |
|
| 55 |
int pipefd[2]; |
| 56 |
ASSERT_GE(::pipe(pipefd), 0); |
| 57 |
|
| 58 |
AFfilehandle handle; |
| 59 |
|
| 60 |
// Write into pipe. |
| 61 |
handle = afOpenFD(pipefd[1], "w", setup); |
| 62 |
ASSERT_TRUE(handle); |
| 63 |
AFframecount framesWritten; |
| 64 |
framesWritten = afWriteFrames(handle, AF_DEFAULT_TRACK, data, kFrameCount); |
| 65 |
ASSERT_EQ(framesWritten, kFrameCount); |
| 66 |
ASSERT_EQ(afCloseFile(handle), 0); |
| 67 |
|
| 68 |
// Read from pipe. |
| 69 |
handle = afOpenFD(pipefd[0], "r", setup); |
| 70 |
ASSERT_TRUE(handle); |
| 71 |
AFframecount framesRead; |
| 72 |
framesRead = afReadFrames(handle, AF_DEFAULT_TRACK, readData, kFrameCount); |
| 73 |
ASSERT_EQ(framesRead, kFrameCount); |
| 74 |
ASSERT_TRUE(!::memcmp(readData, data, kFrameCount * sizeof (int16_t))) << |
| 75 |
"Data read does not match data written"; |
| 76 |
ASSERT_EQ(afCloseFile(handle), 0); |
| 77 |
|
| 78 |
afFreeFileSetup(setup); |
| 79 |
} |
| 80 |
|
| 81 |
int main(int argc, char **argv) |
| 82 |
{ |
| 83 |
::testing::InitGoogleTest(&argc, argv); |
| 84 |
return RUN_ALL_TESTS(); |
| 85 |
} |