| 1 |
/* |
| 2 |
Copyright (C) 2010-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 writing and reading audio files with more |
| 32 |
than 2^24 frames. |
| 33 |
|
| 34 |
This program serves as a regression test for a bug in which |
| 35 |
writing certain audio file formats with more than 2^24 frames |
| 36 |
would produce files with incorrect sizes. |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <audiofile.h> |
| 40 |
#include <gtest/gtest.h> |
| 41 |
#include <stdint.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <string> |
| 45 |
|
| 46 |
#include "TestUtilities.h" |
| 47 |
|
| 48 |
void testLargeFile(int fileFormat) |
| 49 |
{ |
| 50 |
std::string testFileName; |
| 51 |
ASSERT_TRUE(createTemporaryFile("ChannelMatrix", &testFileName)); |
| 52 |
|
| 53 |
AFfilesetup setup = afNewFileSetup(); |
| 54 |
afInitFileFormat(setup, fileFormat); |
| 55 |
afInitChannels(setup, AF_DEFAULT_TRACK, 1); |
| 56 |
afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); |
| 57 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 58 |
afFreeFileSetup(setup); |
| 59 |
ASSERT_TRUE(file) << "Could not open file for writing"; |
| 60 |
|
| 61 |
const int bufferFrameCount = 32768; |
| 62 |
int16_t *data = new int16_t[bufferFrameCount]; |
| 63 |
const AFframecount frameCount = 0x1000007; |
| 64 |
AFframecount framesWritten = 0; |
| 65 |
while (framesWritten < frameCount) |
| 66 |
{ |
| 67 |
AFframecount framesToWrite = bufferFrameCount; |
| 68 |
if (framesToWrite > frameCount - framesWritten) |
| 69 |
framesToWrite = frameCount - framesWritten; |
| 70 |
int dataValue = framesWritten % 32749; |
| 71 |
for (int i=0; i<bufferFrameCount; i++, dataValue++) |
| 72 |
data[i] = dataValue % 32749; |
| 73 |
ASSERT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, data, framesToWrite), |
| 74 |
framesToWrite); |
| 75 |
framesWritten += framesToWrite; |
| 76 |
} |
| 77 |
ASSERT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), frameCount) << |
| 78 |
"Incorrect frame count for file"; |
| 79 |
afCloseFile(file); |
| 80 |
|
| 81 |
file = afOpenFile(testFileName.c_str(), "r", AF_NULL_FILESETUP); |
| 82 |
ASSERT_TRUE(file) << "Could not open file for reading"; |
| 83 |
ASSERT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), frameCount) << |
| 84 |
"Incorrect frame count for file opened for reading"; |
| 85 |
ASSERT_EQ(afGetTrackBytes(file, AF_DEFAULT_TRACK), |
| 86 |
frameCount * sizeof (int16_t)) << |
| 87 |
"Incorrect byte size in file opened for reading"; |
| 88 |
|
| 89 |
AFframecount framesRead = 0; |
| 90 |
while (framesRead < frameCount) |
| 91 |
{ |
| 92 |
AFframecount framesToRead = bufferFrameCount; |
| 93 |
if (framesToRead > frameCount - framesRead) |
| 94 |
framesToRead = frameCount - framesRead; |
| 95 |
afReadFrames(file, AF_DEFAULT_TRACK, data, framesToRead); |
| 96 |
bool valid = true; |
| 97 |
int dataValue = framesRead % 32749; |
| 98 |
for (int i=0; i<framesToRead; i++, dataValue++) |
| 99 |
if (data[i] != dataValue % 32749) |
| 100 |
{ |
| 101 |
valid = false; |
| 102 |
break; |
| 103 |
} |
| 104 |
ASSERT_TRUE(valid) << "Data read does not match data written"; |
| 105 |
framesRead += framesToRead; |
| 106 |
} |
| 107 |
delete [] data; |
| 108 |
afCloseFile(file); |
| 109 |
ASSERT_EQ(::unlink(testFileName.c_str()), 0); |
| 110 |
} |
| 111 |
|
| 112 |
TEST(Large, AIFF) |
| 113 |
{ |
| 114 |
testLargeFile(AF_FILE_AIFF); |
| 115 |
} |
| 116 |
|
| 117 |
TEST(Large, AIFFC) |
| 118 |
{ |
| 119 |
testLargeFile(AF_FILE_AIFFC); |
| 120 |
} |
| 121 |
|
| 122 |
TEST(Large, NeXT) |
| 123 |
{ |
| 124 |
testLargeFile(AF_FILE_NEXTSND); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(Large, WAVE) |
| 128 |
{ |
| 129 |
testLargeFile(AF_FILE_WAVE); |
| 130 |
} |
| 131 |
|
| 132 |
TEST(Large, IRCAM) |
| 133 |
{ |
| 134 |
testLargeFile(AF_FILE_IRCAM); |
| 135 |
} |
| 136 |
|
| 137 |
TEST(Large, AVR) |
| 138 |
{ |
| 139 |
testLargeFile(AF_FILE_AVR); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(Large, CAF) |
| 143 |
{ |
| 144 |
testLargeFile(AF_FILE_CAF); |
| 145 |
} |
| 146 |
|
| 147 |
int main (int argc, char **argv) |
| 148 |
{ |
| 149 |
::testing::InitGoogleTest(&argc, argv); |
| 150 |
return RUN_ALL_TESTS(); |
| 151 |
} |