| 1 |
/* |
| 2 |
Copyright (C) 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 |
#include <gtest/gtest.h> |
| 31 |
#include <audiofile.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
#include "TestUtilities.h" |
| 39 |
|
| 40 |
struct Miscellaneous |
| 41 |
{ |
| 42 |
int id; |
| 43 |
int type; |
| 44 |
const char *data; |
| 45 |
}; |
| 46 |
|
| 47 |
const Miscellaneous kMiscellaneous[] = |
| 48 |
{ |
| 49 |
{ 1, AF_MISC_COPY, "1806 Ludwig van Beethoven" }, |
| 50 |
{ 2, AF_MISC_NAME, "Violin Concerto in D major" } |
| 51 |
}; |
| 52 |
|
| 53 |
const int kNumMiscellaneous = sizeof (kMiscellaneous) / sizeof (Miscellaneous); |
| 54 |
|
| 55 |
void writeMiscellaneous(int fileFormat, const std::string &testFileName) |
| 56 |
{ |
| 57 |
AFfilesetup setup = afNewFileSetup(); |
| 58 |
afInitChannels(setup, AF_DEFAULT_TRACK, 1); |
| 59 |
afInitFileFormat(setup, fileFormat); |
| 60 |
int *miscIDs = new int[kNumMiscellaneous]; |
| 61 |
for (int i=0; i<kNumMiscellaneous; i++) |
| 62 |
miscIDs[i] = kMiscellaneous[i].id; |
| 63 |
afInitMiscIDs(setup, miscIDs, kNumMiscellaneous); |
| 64 |
delete [] miscIDs; |
| 65 |
for (int i=0; i<kNumMiscellaneous; i++) |
| 66 |
{ |
| 67 |
afInitMiscType(setup, kMiscellaneous[i].id, kMiscellaneous[i].type); |
| 68 |
afInitMiscSize(setup, kMiscellaneous[i].id, strlen(kMiscellaneous[i].data)); |
| 69 |
} |
| 70 |
|
| 71 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 72 |
ASSERT_TRUE(file); |
| 73 |
afFreeFileSetup(setup); |
| 74 |
int result; |
| 75 |
for (int i=0; i<kNumMiscellaneous; i++) |
| 76 |
{ |
| 77 |
result = afWriteMisc(file, kMiscellaneous[i].id, kMiscellaneous[i].data, |
| 78 |
strlen(kMiscellaneous[i].data)); |
| 79 |
EXPECT_EQ(result, strlen(kMiscellaneous[i].data)); |
| 80 |
} |
| 81 |
|
| 82 |
const int16_t samples[] = { 1, 2, 3, 4 }; |
| 83 |
afWriteFrames(file, AF_DEFAULT_TRACK, samples, |
| 84 |
sizeof (samples) / sizeof (samples[0])); |
| 85 |
afCloseFile(file); |
| 86 |
} |
| 87 |
|
| 88 |
void readMiscellaneous(const std::string &testFileName) |
| 89 |
{ |
| 90 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "r", NULL); |
| 91 |
ASSERT_TRUE(file); |
| 92 |
int count = afGetMiscIDs(file, NULL); |
| 93 |
EXPECT_EQ(count, kNumMiscellaneous); |
| 94 |
int miscIDs[kNumMiscellaneous]; |
| 95 |
afGetMiscIDs(file, miscIDs); |
| 96 |
for (int i=0; i<kNumMiscellaneous; i++) |
| 97 |
EXPECT_EQ(miscIDs[i], kMiscellaneous[i].id); |
| 98 |
for (int i=0; i<kNumMiscellaneous; i++) |
| 99 |
{ |
| 100 |
int misctype = afGetMiscType(file, miscIDs[i]); |
| 101 |
EXPECT_EQ(misctype, kMiscellaneous[i].type); |
| 102 |
|
| 103 |
int datasize = afGetMiscSize(file, miscIDs[i]); |
| 104 |
EXPECT_EQ(datasize, strlen(kMiscellaneous[i].data)); |
| 105 |
|
| 106 |
char *data = new char[datasize]; |
| 107 |
afReadMisc(file, miscIDs[i], data, datasize); |
| 108 |
|
| 109 |
EXPECT_TRUE(!memcmp(data, kMiscellaneous[i].data, datasize)); |
| 110 |
|
| 111 |
delete [] data; |
| 112 |
} |
| 113 |
afCloseFile(file); |
| 114 |
} |
| 115 |
|
| 116 |
TEST(Miscellaneous, AIFF) |
| 117 |
{ |
| 118 |
std::string testFileName; |
| 119 |
ASSERT_TRUE(createTemporaryFile("Miscellaneous", &testFileName)); |
| 120 |
writeMiscellaneous(AF_FILE_AIFF, testFileName); |
| 121 |
readMiscellaneous(testFileName); |
| 122 |
::unlink(testFileName.c_str()); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(Miscellaneous, AIFFC) |
| 126 |
{ |
| 127 |
std::string testFileName; |
| 128 |
ASSERT_TRUE(createTemporaryFile("Miscellaneous", &testFileName)); |
| 129 |
writeMiscellaneous(AF_FILE_AIFFC, testFileName); |
| 130 |
readMiscellaneous(testFileName); |
| 131 |
::unlink(testFileName.c_str()); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(Miscellaneous, WAVE) |
| 135 |
{ |
| 136 |
std::string testFileName; |
| 137 |
ASSERT_TRUE(createTemporaryFile("Miscellaneous", &testFileName)); |
| 138 |
writeMiscellaneous(AF_FILE_WAVE, testFileName); |
| 139 |
readMiscellaneous(testFileName); |
| 140 |
::unlink(testFileName.c_str()); |
| 141 |
} |
| 142 |
|
| 143 |
TEST(Miscellaneous, IFF_8SVX) |
| 144 |
{ |
| 145 |
std::string testFileName; |
| 146 |
ASSERT_TRUE(createTemporaryFile("Miscellaneous", &testFileName)); |
| 147 |
writeMiscellaneous(AF_FILE_IFF_8SVX, testFileName); |
| 148 |
readMiscellaneous(testFileName); |
| 149 |
::unlink(testFileName.c_str()); |
| 150 |
} |
| 151 |
|
| 152 |
int main(int argc, char **argv) |
| 153 |
{ |
| 154 |
::testing::InitGoogleTest(&argc, argv); |
| 155 |
return RUN_ALL_TESTS(); |
| 156 |
} |