| 1 |
/* |
| 2 |
Copyright (C) 2003, 2011, 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 |
/* |
| 31 |
This program tests seeking within an audio file. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <audiofile.h> |
| 35 |
#include <gtest/gtest.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string> |
| 38 |
#include <unistd.h> |
| 39 |
|
| 40 |
#include "TestUtilities.h" |
| 41 |
|
| 42 |
TEST(Seek, Seek) |
| 43 |
{ |
| 44 |
std::string testFileName; |
| 45 |
ASSERT_TRUE(createTemporaryFile("Seek", &testFileName)); |
| 46 |
|
| 47 |
const int kFrameCount = 2000; |
| 48 |
const int kPadFrameCount = kFrameCount + 5; |
| 49 |
const int kDataLength = kFrameCount * sizeof (int16_t); |
| 50 |
|
| 51 |
int16_t data[kFrameCount]; |
| 52 |
int16_t readData[kPadFrameCount]; |
| 53 |
|
| 54 |
AFfilesetup setup = afNewFileSetup(); |
| 55 |
ASSERT_TRUE(setup); |
| 56 |
|
| 57 |
afInitFileFormat(setup, AF_FILE_AIFF); |
| 58 |
afInitChannels(setup, AF_DEFAULT_TRACK, 1); |
| 59 |
|
| 60 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 61 |
ASSERT_TRUE(file) << "could not open file for writing"; |
| 62 |
|
| 63 |
afFreeFileSetup(setup); |
| 64 |
|
| 65 |
/* Initialize data to a nontrivial test pattern. */ |
| 66 |
for (int i=0; i<kFrameCount; i++) |
| 67 |
{ |
| 68 |
if ((i%2) != 0) |
| 69 |
data[i] = i; |
| 70 |
else |
| 71 |
data[i] = -i; |
| 72 |
} |
| 73 |
|
| 74 |
ASSERT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, data, kFrameCount), |
| 75 |
kFrameCount); |
| 76 |
|
| 77 |
afCloseFile(file); |
| 78 |
|
| 79 |
file = afOpenFile(testFileName.c_str(), "r", AF_NULL_FILESETUP); |
| 80 |
ASSERT_TRUE(file) << "Could not open file for reading"; |
| 81 |
|
| 82 |
/* |
| 83 |
For each position in the file, seek to that position and |
| 84 |
read to the end of the file, checking that the data read |
| 85 |
matches the data written. |
| 86 |
*/ |
| 87 |
for (int i=0; i<kFrameCount; i++) |
| 88 |
{ |
| 89 |
memset(readData, 0, kDataLength); |
| 90 |
|
| 91 |
AFfileoffset currentposition = afSeekFrame(file, AF_DEFAULT_TRACK, i); |
| 92 |
ASSERT_EQ(currentposition, i) << "Incorrect seek position"; |
| 93 |
|
| 94 |
AFframecount framesread = afReadFrames(file, AF_DEFAULT_TRACK, |
| 95 |
readData + i, kPadFrameCount); |
| 96 |
ASSERT_EQ(framesread, kFrameCount - i) << |
| 97 |
"Incorrect number of frames read"; |
| 98 |
|
| 99 |
ASSERT_TRUE(!memcmp(data + i, readData + i, |
| 100 |
framesread * sizeof (int16_t))) << |
| 101 |
"Error in data read"; |
| 102 |
} |
| 103 |
|
| 104 |
afCloseFile(file); |
| 105 |
|
| 106 |
ASSERT_EQ(::unlink(testFileName.c_str()), 0); |
| 107 |
} |
| 108 |
|
| 109 |
int main (int argc, char **argv) |
| 110 |
{ |
| 111 |
::testing::InitGoogleTest(&argc, argv); |
| 112 |
return RUN_ALL_TESTS(); |
| 113 |
} |