Debian Codegraph @

test/PCMMapping.cpp

raw
1 /*
2 Copyright (C) 2010, 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 <audiofile.h>
31 #include <gtest/gtest.h>
32 #include <limits>
33 #include <stdint.h>
34 #include <unistd.h>
35
36 #include "TestUtilities.h"
37
38 class PCMMappingTest : public testing::Test
39 {
40 protected:
41 virtual void SetUp()
42 {
43 ASSERT_TRUE(createTemporaryFile("PCMMapping", &m_testFileName));
44 }
45 virtual void TearDown()
46 {
47 ::unlink(m_testFileName.c_str());
48 }
49
50 AFfilehandle createTestFile(int sampleFormat, int sampleWidth)
51 {
52 AFfilesetup setup = afNewFileSetup();
53 afInitFileFormat(setup, AF_FILE_AIFFC);
54 afInitChannels(setup, AF_DEFAULT_TRACK, 1);
55 afInitSampleFormat(setup, AF_DEFAULT_TRACK, sampleFormat, sampleWidth);
56 AFfilehandle file = afOpenFile(m_testFileName.c_str(), "w", setup);
57 afFreeFileSetup(setup);
58 return file;
59 }
60 AFfilehandle openTestFile()
61 {
62 AFfilehandle file = afOpenFile(m_testFileName.c_str(), "r", AF_NULL_FILESETUP);
63 return file;
64 }
65
66 private:
67 std::string m_testFileName;
68 };
69
70 TEST_F(PCMMappingTest, Float)
71 {
72 AFfilehandle file = createTestFile(AF_SAMPFMT_FLOAT, 32);
73 const float data[] = { -1, 0, 1 };
74 const int frameCount = sizeof (data) / sizeof (data[0]);
75 AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, data, frameCount);
76 ASSERT_EQ(framesWritten, frameCount);
77 afCloseFile(file);
78 file = openTestFile();
79 ASSERT_TRUE(file != NULL);
80 double slope = 2, intercept = 2, minClip = 0, maxClip = 4;
81 afSetVirtualPCMMapping(file, AF_DEFAULT_TRACK, slope, intercept, minClip, maxClip);
82 float readData[frameCount];
83 AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, frameCount);
84 ASSERT_EQ(framesRead, frameCount);
85 afCloseFile(file);
86 const float expectedData[] = { 0, 2, 4 };
87 for (int i=0; i<frameCount; i++)
88 EXPECT_EQ(readData[i], expectedData[i]);
89 }
90
91 TEST_F(PCMMappingTest, Double)
92 {
93 AFfilehandle file = createTestFile(AF_SAMPFMT_DOUBLE, 64);
94 const double data[] = { -1, 0, 1 };
95 const int frameCount = sizeof (data) / sizeof (data[0]);
96 AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, data, frameCount);
97 ASSERT_EQ(framesWritten, frameCount);
98 afCloseFile(file);
99 file = openTestFile();
100 ASSERT_TRUE(file != NULL);
101 double slope = 2, intercept = 2, minClip = 0, maxClip = 4;
102 afSetVirtualPCMMapping(file, AF_DEFAULT_TRACK, slope, intercept, minClip, maxClip);
103 double readData[frameCount];
104 AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, frameCount);
105 ASSERT_EQ(framesRead, frameCount);
106 afCloseFile(file);
107 const double expectedData[] = { 0, 2, 4 };
108 for (int i=0; i<frameCount; i++)
109 EXPECT_EQ(readData[i], expectedData[i]);
110 }
111
112 int main(int argc, char **argv)
113 {
114 ::testing::InitGoogleTest(&argc, argv);
115 return RUN_ALL_TESTS();
116 }