/[svn]/libgig/trunk/src/testcases/GigWriteTest.cpp
ViewVC logotype

Contents of /libgig/trunk/src/testcases/GigWriteTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 923 - (show annotations) (download)
Thu Oct 19 19:01:13 2006 UTC (14 years, 3 months ago) by schoenebeck
File size: 11739 byte(s)
* fixed and expanded initial test cases

1 #include "GigWriteTest.h"
2
3 #include <iostream>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "../gig.h"
9 #include "../helper.h"
10
11 CPPUNIT_TEST_SUITE_REGISTRATION(GigWriteTest);
12
13 using namespace std;
14
15 // file name of the Gigasampler file we are going to create for these tests
16 #define TEST_GIG_FILE_NAME "foo.gig"
17
18 // four stupid little sample "waves"
19 // (each having three sample points length, 16 bit depth, mono)
20 int16_t sampleData1[] = { 1, 2, 3 };
21 int16_t sampleData2[] = { 4, 5, 6 };
22 int16_t sampleData3[] = { 7, 8, 9 };
23 int16_t sampleData4[] = { 10,11,12 };
24
25 // 1. Run) print the purpose of this test case first
26 void GigWriteTest::printTestSuiteName() {
27 cout << "\b \nTesting Gigasampler write support: " << flush;
28 }
29
30 // code executed when this test suite is created
31 void GigWriteTest::setUp() {
32 }
33
34 // code executed when this test suite will be destroyed
35 void GigWriteTest::tearDown() {
36 }
37
38
39 /////////////////////////////////////////////////////////////////////////////
40 // The actual test cases (in order) ...
41
42 // 2. Run) create a new Gigasampler file from scratch
43 void GigWriteTest::createNewGigFile() {
44 try {
45 // create an empty Gigasampler file
46 gig::File file;
47 // we give it an internal name, not mandatory though
48 file.pInfo->Name = "Foo Gigasampler File";
49
50 // create four samples
51 gig::Sample* pSample1 = file.AddSample();
52 gig::Sample* pSample2 = file.AddSample();
53 gig::Sample* pSample3 = file.AddSample();
54 gig::Sample* pSample4 = file.AddSample();
55 // give those samples a name (not mandatory)
56 pSample1->pInfo->Name = "Foo Sample 1";
57 pSample2->pInfo->Name = "Foo Sample 2";
58 pSample3->pInfo->Name = "Foo Sample 3";
59 pSample4->pInfo->Name = "Foo Sample 4";
60 // set meta informations for those samples
61 pSample1->Channels = 1; // mono
62 pSample1->BitDepth = 16; // 16 bits
63 pSample1->FrameSize = 16/*bitdepth*/ / 8/*1 byte are 8 bits*/ * 1/*mono*/;
64 pSample1->SamplesPerSecond = 44100;
65 pSample2->Channels = 1; // mono
66 pSample2->BitDepth = 16; // 16 bits
67 pSample2->FrameSize = 16 / 8 * 1;
68 pSample2->SamplesPerSecond = 44100;
69 pSample3->Channels = 1; // mono
70 pSample3->BitDepth = 16; // 16 bits
71 pSample3->FrameSize = 16 / 8 * 1;
72 pSample3->SamplesPerSecond = 44100;
73 pSample4->Channels = 1; // mono
74 pSample4->BitDepth = 16; // 16 bits
75 pSample4->FrameSize = 16 / 8 * 1;
76 pSample4->SamplesPerSecond = 44100;
77 // resize those samples to a length of three sample points
78 // (again: _sample_points_ NOT bytes!) which is the length of our
79 // ficticious samples from above. after the Save() call below we can
80 // then directly write our sample data to disk by using the Write()
81 // method, that is without having to load all the sample data into
82 // RAM. for large instruments / .gig files this is definitely the way
83 // to go
84 pSample1->Resize(3);
85 pSample2->Resize(3);
86 pSample3->Resize(3);
87 pSample4->Resize(3);
88
89 // create four instruments
90 gig::Instrument* pInstrument1 = file.AddInstrument();
91 gig::Instrument* pInstrument2 = file.AddInstrument();
92 gig::Instrument* pInstrument3 = file.AddInstrument();
93 gig::Instrument* pInstrument4 = file.AddInstrument();
94 // give them a name (not mandatory)
95 pInstrument1->pInfo->Name = "Foo Instrument 1";
96 pInstrument2->pInfo->Name = "Foo Instrument 2";
97 pInstrument3->pInfo->Name = "Foo Instrument 3";
98 pInstrument4->pInfo->Name = "Foo Instrument 4";
99
100 // create one region for each instrument
101 // in this example we do not add a dimension, so
102 // every region will have exactly one DimensionRegion
103 // also we assign a sample to each dimension region
104 gig::Region* pRegion = pInstrument1->AddRegion();
105 pRegion->SetSample(pSample1);
106 pRegion->pDimensionRegions[0]->pSample = pSample1;
107
108 pRegion = pInstrument2->AddRegion();
109 pRegion->SetSample(pSample2);
110 pRegion->pDimensionRegions[0]->pSample = pSample2;
111
112 pRegion = pInstrument3->AddRegion();
113 pRegion->SetSample(pSample3);
114 pRegion->pDimensionRegions[0]->pSample = pSample3;
115
116 pRegion = pInstrument4->AddRegion();
117 pRegion->SetSample(pSample4);
118 pRegion->pDimensionRegions[0]->pSample = pSample4;
119
120 // save file ("physically") as of now
121 file.Save(TEST_GIG_FILE_NAME);
122 } catch (RIFF::Exception e) {
123 std::cerr << "\nCould not create a new Gigasampler file from scratch:\n" << std::flush;
124 e.PrintMessage();
125 throw e; // stop further tests
126 }
127 }
128
129 // 3. Run) test if the newly created Gigasampler file exists & can be opened
130 void GigWriteTest::testOpenCreatedGigFile() {
131 // try to open previously created Gigasampler file
132 try {
133 RIFF::File riff(TEST_GIG_FILE_NAME);
134 gig::File file(&riff);
135 } catch (RIFF::Exception e) {
136 std::cerr << "\nCould not open newly created Gigasampler file:\n" << std::flush;
137 e.PrintMessage();
138 throw e; // stop further tests
139 }
140 }
141
142 // 4. Run) test if the articulation informations of the newly created Gigasampler file were correctly written
143 void GigWriteTest::testArticulationsOfCreatedGigFile() {
144 try {
145 // open previously created Gigasampler file
146 RIFF::File riff(TEST_GIG_FILE_NAME);
147 gig::File file(&riff);
148 // check global file informations
149 CPPUNIT_ASSERT(file.pInfo);
150 CPPUNIT_ASSERT(file.pInfo->Name == "Foo Gigasampler File");
151 // check amount of instruments and samples
152 CPPUNIT_ASSERT(file.Instruments == 4);
153 int iInstruments = 0;
154 for (gig::Instrument* pInstrument = file.GetFirstInstrument(); pInstrument; pInstrument = file.GetNextInstrument()) iInstruments++;
155 CPPUNIT_ASSERT(iInstruments == 4);
156 int iSamples = 0;
157 for (gig::Sample* pSample = file.GetFirstSample(); pSample; pSample = file.GetNextSample()) iSamples++;
158 CPPUNIT_ASSERT(iSamples == 4);
159 // check samples' meta informations
160 int iSample = 1;
161 for (gig::Sample* pSample = file.GetFirstSample(); pSample; pSample = file.GetNextSample()) {
162 CPPUNIT_ASSERT(pSample->pInfo);
163 std::string sOughtToBe = "Foo Sample " + ToString(iSample);
164 CPPUNIT_ASSERT(pSample->pInfo->Name == sOughtToBe);
165 CPPUNIT_ASSERT(pSample->GetSize() == 3); // three sample points
166 CPPUNIT_ASSERT(pSample->SamplesTotal == 3); // three sample points
167 CPPUNIT_ASSERT(pSample->Channels == 1); // mono
168 CPPUNIT_ASSERT(pSample->BitDepth == 16); // bit depth 16 bits
169 CPPUNIT_ASSERT(pSample->FrameSize == 16 / 8 * 1);
170 CPPUNIT_ASSERT(pSample->SamplesPerSecond == 44100);
171 iSample++;
172 }
173 // check instruments' meta informations
174 int iInstrument = 1;
175 for (gig::Instrument* pInstrument = file.GetFirstInstrument(); pInstrument; pInstrument = file.GetNextInstrument()) {
176 CPPUNIT_ASSERT(pInstrument->pInfo);
177 std::string sOughtToBe = "Foo Instrument " + ToString(iInstrument);
178 CPPUNIT_ASSERT(pInstrument->pInfo->Name == sOughtToBe);
179 gig::Region* pRegion = pInstrument->GetFirstRegion();
180 CPPUNIT_ASSERT(pRegion);
181 CPPUNIT_ASSERT(pRegion->Dimensions == 0);
182 CPPUNIT_ASSERT(pRegion->DimensionRegions == 1);
183 sOughtToBe = "Foo Sample " + ToString(iInstrument);
184 CPPUNIT_ASSERT(pRegion->GetSample()->pInfo->Name == sOughtToBe);
185 gig::DimensionRegion* pDimensionRegion = pRegion->GetDimensionRegionByValue((uint[8]){0,0,0,0,0,0,0,0});
186 CPPUNIT_ASSERT(pDimensionRegion);
187 CPPUNIT_ASSERT(pDimensionRegion->pSample->pInfo->Name == sOughtToBe);
188 iInstrument++;
189 }
190 } catch (RIFF::Exception e) {
191 std::cerr << "\nThere was an exception while checking the articulation data of the newly created Gigasampler file:\n" << std::flush;
192 e.PrintMessage();
193 throw e; // stop further tests
194 }
195 }
196
197 // 5. Run) try to write sample data to that newly created Gigasampler file
198 void GigWriteTest::testWriteSamples() {
199 try {
200 // open previously created Gigasampler file (in read/write mode)
201 RIFF::File riff(TEST_GIG_FILE_NAME);
202 riff.SetMode(RIFF::stream_mode_read_write);
203 gig::File file(&riff);
204 // until this point we just wrote the articulation data to the .gig file
205 // and prepared the .gig file for writing our 4 example sample data, so
206 // now as the file exists physically and the 'samples' are already
207 // of the correct size we can now write the actual samples' data by
208 // directly writing them to disk
209 gig::Sample* pSample1 = file.GetFirstSample();
210 gig::Sample* pSample2 = file.GetNextSample();
211 gig::Sample* pSample3 = file.GetNextSample();
212 gig::Sample* pSample4 = file.GetNextSample();
213 CPPUNIT_ASSERT(pSample1);
214 pSample1->Write(sampleData1, 3);
215 CPPUNIT_ASSERT(pSample2);
216 pSample2->Write(sampleData2, 3);
217 CPPUNIT_ASSERT(pSample3);
218 pSample3->Write(sampleData3, 3);
219 CPPUNIT_ASSERT(pSample4);
220 pSample4->Write(sampleData4, 3);
221 } catch (RIFF::Exception e) {
222 std::cerr << "\nCould not directly write samples to newly created Gigasampler file:\n" << std::flush;
223 e.PrintMessage();
224 throw e; // stop further tests
225 }
226 }
227
228 // 6. Run) check the previously written samples' data
229 void GigWriteTest::testSamplesData() {
230 try {
231 // open previously created Gigasampler file
232 RIFF::File riff(TEST_GIG_FILE_NAME);
233 gig::File file(&riff);
234 // check samples' meta informations
235 gig::Sample* pSample1 = file.GetFirstSample();
236 gig::Sample* pSample2 = file.GetNextSample();
237 gig::Sample* pSample3 = file.GetNextSample();
238 gig::Sample* pSample4 = file.GetNextSample();
239 CPPUNIT_ASSERT(pSample1);
240 CPPUNIT_ASSERT(pSample2);
241 CPPUNIT_ASSERT(pSample3);
242 CPPUNIT_ASSERT(pSample4);
243 gig::buffer_t sampleBuffer1 = pSample1->LoadSampleData();
244 gig::buffer_t sampleBuffer2 = pSample2->LoadSampleData();
245 gig::buffer_t sampleBuffer3 = pSample3->LoadSampleData();
246 gig::buffer_t sampleBuffer4 = pSample4->LoadSampleData();
247 CPPUNIT_ASSERT(sampleBuffer1.pStart);
248 CPPUNIT_ASSERT(sampleBuffer2.pStart);
249 CPPUNIT_ASSERT(sampleBuffer3.pStart);
250 CPPUNIT_ASSERT(sampleBuffer4.pStart);
251 CPPUNIT_ASSERT(sampleBuffer1.Size == pSample1->FrameSize * 3); // three sample points length
252 CPPUNIT_ASSERT(sampleBuffer2.Size == pSample2->FrameSize * 3); // three sample points length
253 CPPUNIT_ASSERT(sampleBuffer3.Size == pSample3->FrameSize * 3); // three sample points length
254 CPPUNIT_ASSERT(sampleBuffer4.Size == pSample4->FrameSize * 3); // three sample points length
255 // check samples' PCM data
256 CPPUNIT_ASSERT(memcmp(sampleBuffer1.pStart, sampleData1, 3) == 0);
257 CPPUNIT_ASSERT(memcmp(sampleBuffer2.pStart, sampleData2, 3) == 0);
258 CPPUNIT_ASSERT(memcmp(sampleBuffer3.pStart, sampleData3, 3) == 0);
259 CPPUNIT_ASSERT(memcmp(sampleBuffer4.pStart, sampleData4, 3) == 0);
260 } catch (RIFF::Exception e) {
261 std::cerr << "\nThere was an exception while checking the written samples' data:\n" << std::flush;
262 e.PrintMessage();
263 throw e; // stop further tests
264 }
265 }

  ViewVC Help
Powered by ViewVC