Debian Codegraph @

test/Marker.cpp

raw
1 /*
2 Audio File Library
3 Copyright (C) 2012, Michael Pruett <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <audiofile.h>
21 #include <gtest/gtest.h>
22 #include <stdint.h>
23 #include <unistd.h>
24
25 #include "TestUtilities.h"
26
27 static void testMarkers(int fileFormat, bool supportsComments)
28 {
29 std::string testFileName;
30 ASSERT_TRUE(createTemporaryFile("Marker", &testFileName));
31
32 AFfilesetup setup = afNewFileSetup();
33 afInitFileFormat(setup, fileFormat);
34 afInitChannels(setup, AF_DEFAULT_TRACK, 1);
35 afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
36
37 const int markerIDs[] = { 1, 2, 3, 5, 8 };
38 const int numMarkers = sizeof (markerIDs) / sizeof (int);
39 const char * const markerNames[numMarkers] =
40 {
41 "one",
42 "two",
43 "three",
44 "five",
45 "eight"
46 };
47 const char * const markerComments[numMarkers] =
48 {
49 "comment one",
50 "comment two",
51 "comment three",
52 "comment five",
53 "comment eight"
54 };
55 const int markerPositions[numMarkers] = { 1, 2, 3, 5, 8 };
56 afInitMarkIDs(setup, AF_DEFAULT_TRACK, markerIDs, numMarkers);
57 for (int i=0; i<numMarkers; i++)
58 afInitMarkName(setup, AF_DEFAULT_TRACK, markerIDs[i], markerNames[i]);
59
60 for (int i=0; i<numMarkers; i++)
61 afInitMarkComment(setup, AF_DEFAULT_TRACK, markerIDs[i], markerComments[i]);
62
63 AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
64 ASSERT_TRUE(file) << "Could not open test file for writing";
65 afFreeFileSetup(setup);
66
67 const int frameCount = 10;
68 int16_t frames[frameCount] = {0};
69
70 EXPECT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, frames, frameCount),
71 frameCount);
72
73 for (int i=0; i<numMarkers; i++)
74 afSetMarkPosition(file, AF_DEFAULT_TRACK, markerIDs[i], markerPositions[i]);
75
76 ASSERT_EQ(afCloseFile(file), 0);
77
78 file = afOpenFile(testFileName.c_str(), "r", NULL);
79 ASSERT_TRUE(file) << "Could not open test file for reading";
80 EXPECT_EQ(afGetFileFormat(file, NULL), fileFormat);
81
82 ASSERT_EQ(afGetMarkIDs(file, AF_DEFAULT_TRACK, NULL), numMarkers);
83 int readMarkerIDs[numMarkers];
84 afGetMarkIDs(file, AF_DEFAULT_TRACK, readMarkerIDs);
85 for (int i=0; i<numMarkers; i++)
86 EXPECT_EQ(markerIDs[i], readMarkerIDs[i]);
87
88 for (int i=0; i<numMarkers; i++)
89 EXPECT_TRUE(!strcmp(afGetMarkName(file, AF_DEFAULT_TRACK, markerIDs[i]),
90 markerNames[i]));
91
92 for (int i=0; i<numMarkers; i++)
93 {
94 const char *comment = afGetMarkComment(file, AF_DEFAULT_TRACK,
95 markerIDs[i]);
96 if (supportsComments)
97 EXPECT_TRUE(!strcmp(comment, markerComments[i]));
98 }
99
100 for (int i=0; i<numMarkers; i++)
101 EXPECT_EQ(afGetMarkPosition(file, AF_DEFAULT_TRACK, markerIDs[i]),
102 markerPositions[i]);
103
104 ASSERT_EQ(afCloseFile(file), 0);
105
106 ASSERT_EQ(::unlink(testFileName.c_str()), 0);
107 }
108
109 TEST(Marker, AIFF)
110 {
111 testMarkers(AF_FILE_AIFF, false);
112 }
113
114 TEST(Marker, AIFC)
115 {
116 testMarkers(AF_FILE_AIFFC, false);
117 }
118
119 TEST(Marker, WAVE)
120 {
121 testMarkers(AF_FILE_WAVE, true);
122 }
123
124 static void testUnsupported(int fileFormat)
125 {
126 std::string testFileName;
127 ASSERT_TRUE(createTemporaryFile("Marker", &testFileName));
128
129 AFerrfunc errorHandler = afSetErrorHandler(NULL);
130
131 AFfilesetup setup = afNewFileSetup();
132 afInitFileFormat(setup, fileFormat);
133
134 const int markerIDs[] = { 1 };
135 const int numMarkers = sizeof (markerIDs) / sizeof (int);
136 const char * const markerNames[numMarkers] = { "name" };
137 const char * const markerComments[numMarkers] = { "comment" };
138 afInitMarkIDs(setup, AF_DEFAULT_TRACK, markerIDs, numMarkers);
139 for (int i=0; i<numMarkers; i++)
140 afInitMarkName(setup, AF_DEFAULT_TRACK, markerIDs[i], markerNames[i]);
141
142 for (int i=0; i<numMarkers; i++)
143 afInitMarkComment(setup, AF_DEFAULT_TRACK, markerIDs[i], markerComments[i]);
144
145 AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
146 ASSERT_FALSE(file) << "Did not expect opening test file for writing to succeed";
147 afFreeFileSetup(setup);
148
149 afSetErrorHandler(errorHandler);
150
151 ASSERT_EQ(::unlink(testFileName.c_str()), 0);
152 }
153
154 TEST(Unsupported, Raw) { testUnsupported(AF_FILE_RAWDATA); }
155 TEST(Unsupported, NeXT) { testUnsupported(AF_FILE_NEXTSND); }
156 TEST(Unsupported, IRCAM) { testUnsupported(AF_FILE_IRCAM); }
157 TEST(Unsupported, AVR) { testUnsupported(AF_FILE_AVR); }
158 TEST(Unsupported, IFF) { testUnsupported(AF_FILE_IFF_8SVX); }
159 TEST(Unsupported, SampleVision) { testUnsupported(AF_FILE_SAMPLEVISION); }
160 TEST(Unsupported, VOC) { testUnsupported(AF_FILE_VOC); }
161 TEST(Unsupported, NIST) { testUnsupported(AF_FILE_NIST_SPHERE); }
162 TEST(Unsupported, CAF) { testUnsupported(AF_FILE_CAF); }
163 TEST(Unsupported, FLAC) { testUnsupported(AF_FILE_FLAC); }
164
165 int main(int argc, char **argv)
166 {
167 ::testing::InitGoogleTest(&argc, argv);
168 return RUN_ALL_TESTS();
169 }