Debian Codegraph @

test/VirtualFile.cpp

raw
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 "config.h"
31
32 #include <af_vfs.h>
33 #include <audiofile.h>
34 #include <gtest/gtest.h>
35 #include <limits>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "TestUtilities.h"
42
43 TEST(VirtualFile, Basic)
44 {
45 ASSERT_GE(sizeof (off_t), 8) << "Size of off_t must be at least 8 bytes.";
46 }
47
48 static ssize_t vf_read(AFvirtualfile *vf, void *data, size_t nbytes)
49 {
50 FILE *fp = static_cast<FILE *>(vf->closure);
51 return fread(data, 1, nbytes, fp);
52 }
53
54 static AFfileoffset vf_length(AFvirtualfile *vf)
55 {
56 FILE *fp = static_cast<FILE *>(vf->closure);
57 off_t current = ftello(fp);
58 fseeko(fp, 0, SEEK_END);
59 off_t length = ftello(fp);
60 fseeko(fp, current, SEEK_SET);
61 return length;
62 }
63
64 static ssize_t vf_write(AFvirtualfile *vf, const void *data, size_t nbytes)
65 {
66 FILE *fp = static_cast<FILE *>(vf->closure);
67 return fwrite(data, 1, nbytes, fp);
68 }
69
70 static void vf_close(AFvirtualfile *vf)
71 {
72 FILE *fp = static_cast<FILE *>(vf->closure);
73 fclose(fp);
74 }
75
76 static AFfileoffset vf_seek(AFvirtualfile *vf, AFfileoffset offset, int is_relative)
77 {
78 FILE *fp = static_cast<FILE *>(vf->closure);
79 fseeko(fp, offset, is_relative ? SEEK_CUR : SEEK_SET);
80 return ftello(fp);
81 }
82
83 static AFfileoffset vf_tell(AFvirtualfile *vf)
84 {
85 FILE *fp = static_cast<FILE *>(vf->closure);
86 return ftello(fp);
87 }
88
89 static AFvirtualfile *vf_create(FILE *fp)
90 {
91 AFvirtualfile *vf = af_virtual_file_new();
92 vf->read = vf_read;
93 vf->length = vf_length;
94 vf->write = vf_write;
95 vf->destroy = vf_close;
96 vf->seek = vf_seek;
97 vf->tell = vf_tell;
98 vf->closure = fp;
99 return vf;
100 }
101
102 TEST(VirtualFile, ReadVirtual)
103 {
104 std::string testFileName;
105 ASSERT_TRUE(createTemporaryFile("VirtualFile", &testFileName));
106
107 AFfilesetup setup = afNewFileSetup();
108 afInitFileFormat(setup, AF_FILE_AIFF);
109 afInitChannels(setup, AF_DEFAULT_TRACK, 1);
110
111 AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
112 ASSERT_TRUE(file) << "Could not open virtual file";
113
114 afFreeFileSetup(setup);
115
116 const int16_t samples[] = { 1, 1, 2, 3, 5, 8, 13 };
117 const int numSamples = sizeof (samples) / sizeof (int16_t);
118 ASSERT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, samples, numSamples),
119 numSamples) << "Incorrect number of samples written";
120
121 ASSERT_EQ(afCloseFile(file), 0) << "Error closing virtual file";
122
123 FILE *fp = fopen(testFileName.c_str(), "r");
124 AFvirtualfile *vf = vf_create(fp);
125
126 file = afOpenVirtualFile(vf, "r", NULL);
127 ASSERT_TRUE(file) << "Could not open file";
128
129 int16_t readSamples[numSamples];
130 ASSERT_EQ(afReadFrames(file, AF_DEFAULT_TRACK, readSamples, numSamples),
131 numSamples) << "Incorrect number of samples read";
132
133 for (int i=0; i<numSamples; i++)
134 ASSERT_EQ(samples[i], readSamples[i]);
135
136 ASSERT_EQ(afCloseFile(file), 0) << "Error closing file";
137
138 ASSERT_EQ(::unlink(testFileName.c_str()), 0);
139 }
140
141 TEST(VirtualFile, WriteVirtual)
142 {
143 std::string testFileName;
144 ASSERT_TRUE(createTemporaryFile("VirtualFile", &testFileName));
145
146 FILE *fp = fopen(testFileName.c_str(), "w");
147 AFvirtualfile *vf = vf_create(fp);
148
149 AFfilesetup setup = afNewFileSetup();
150 afInitFileFormat(setup, AF_FILE_AIFF);
151 afInitChannels(setup, AF_DEFAULT_TRACK, 1);
152
153 AFfilehandle file = afOpenVirtualFile(vf, "w", setup);
154 ASSERT_TRUE(file) << "Could not open virtual file";
155
156 afFreeFileSetup(setup);
157
158 const int16_t samples[] = { 1, 1, 2, 3, 5, 8, 13 };
159 const int numSamples = sizeof (samples) / sizeof (int16_t);
160 ASSERT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, samples, numSamples),
161 numSamples) << "Incorrect number of samples written";
162
163 ASSERT_EQ(afCloseFile(file), 0) << "Error closing virtual file";
164
165 file = afOpenFile(testFileName.c_str(), "r", NULL);
166 ASSERT_TRUE(file) << "Could not open file";
167
168 int16_t readSamples[numSamples];
169 ASSERT_EQ(afReadFrames(file, AF_DEFAULT_TRACK, readSamples, numSamples),
170 numSamples) << "Incorrect number of samples read";
171
172 for (int i=0; i<numSamples; i++)
173 ASSERT_EQ(samples[i], readSamples[i]);
174
175 ASSERT_EQ(afCloseFile(file), 0) << "Error closing file";
176
177 ASSERT_EQ(::unlink(testFileName.c_str()), 0);
178 }
179
180 int main(int argc, char **argv)
181 {
182 ::testing::InitGoogleTest(&argc, argv);
183 return RUN_ALL_TESTS();
184 }