diff options
author | Johannes Schickel | 2010-02-03 17:03:46 +0000 |
---|---|---|
committer | Johannes Schickel | 2010-02-03 17:03:46 +0000 |
commit | 87534da624016dadb72a24adbee1a92a390c582f (patch) | |
tree | c526563cb39b1860a10cdb20efa163855e4837e6 | |
parent | 5030ba3596ac012273e2e29dbac1b76b843585e9 (diff) | |
download | scummvm-rg350-87534da624016dadb72a24adbee1a92a390c582f.tar.gz scummvm-rg350-87534da624016dadb72a24adbee1a92a390c582f.tar.bz2 scummvm-rg350-87534da624016dadb72a24adbee1a92a390c582f.zip |
Remove code duplication.
svn-id: r47863
-rw-r--r-- | test/sound/raw.h | 235 |
1 files changed, 33 insertions, 202 deletions
diff --git a/test/sound/raw.h b/test/sound/raw.h index e39f725847..33f000566b 100644 --- a/test/sound/raw.h +++ b/test/sound/raw.h @@ -66,16 +66,16 @@ public: return s; } - void test_read_buffer_8_bit_signed_mono() { - const int sampleRate = 11025; - const int time = 2; - +private: + template<typename T> + void readBufferTestTemplate(const int sampleRate, const int time, const bool le, const bool isStereo) { int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false); + Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, le, isStereo); - int16 *buffer = new int16[sampleRate * time]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0); + const int totalSamples = sampleRate * time * (isStereo ? 2 : 1); + int16 *buffer = new int16[totalSamples]; + TS_ASSERT_EQUALS(s->readBuffer(buffer, totalSamples), totalSamples); + TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * totalSamples), 0); TS_ASSERT_EQUALS(s->endOfData(), true); delete[] sine; @@ -83,140 +83,41 @@ public: delete s; } - void test_read_buffer_8_bit_signed_stereo() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, true); - - int16 *buffer = new int16[sampleRate * time * 2]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time * 2), sampleRate * time * 2); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time * 2), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); +public: + void test_read_buffer_8_bit_signed_mono() { + readBufferTestTemplate<int8>(11025, 2, false, false); + } - delete[] sine; - delete[] buffer; - delete s; + void test_read_buffer_8_bit_signed_stereo() { + readBufferTestTemplate<int8>(11025, 2, false, true); } void test_read_buffer_8_bit_unsigned_mono() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<uint8>(sampleRate, time, &sine, false, false); - - int16 *buffer = new int16[sampleRate * time]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<uint8>(11025, 2, false, false); } void test_read_buffer_16_bit_signed_be_mono() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int16>(sampleRate, time, &sine, false, false); - - int16 *buffer = new int16[sampleRate * time]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<int16>(11025, 2, false, false); } void test_read_buffer_16_bit_signed_be_stereo() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int16>(sampleRate, time, &sine, false, true); - - int16 *buffer = new int16[sampleRate * time * 2]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time * 2), sampleRate * time * 2); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time * 2), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<int16>(11025, 2, false, true); } void test_read_buffer_16_bit_unsigned_be_mono() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<uint16>(sampleRate, time, &sine, false, false); - - int16 *buffer = new int16[sampleRate * time]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<uint16>(11025, 2, false, false); } void test_read_buffer_16_bit_signed_le_mono() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int16>(sampleRate, time, &sine, true, false); - - int16 *buffer = new int16[sampleRate * time]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<int16>(11025, 2, true, false); } void test_read_buffer_16_bit_unsigned_le_mono() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<uint16>(sampleRate, time, &sine, true, false); - - int16 *buffer = new int16[sampleRate * time]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<uint16>(11025, 2, true, false); } void test_read_buffer_16_bit_unsigned_le_stereo() { - const int sampleRate = 11025; - const int time = 2; - - int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<uint16>(sampleRate, time, &sine, true, true); - - int16 *buffer = new int16[sampleRate * time * 2]; - TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time * 2), sampleRate * time * 2); - TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time * 2), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - delete[] sine; - delete[] buffer; - delete s; + readBufferTestTemplate<uint16>(11025, 2, true, true); } void test_partial_read() { @@ -336,10 +237,8 @@ public: delete s; } - void test_seek_mono() { - const int sampleRate = 11025; - const int time = 2; - const bool isStereo = false; +private: + void seekTest(const int sampleRate, const int time, const bool isStereo) { const int totalFrames = sampleRate * time * (isStereo ? 2 : 1); int readData = 0, offset = 0; @@ -351,7 +250,7 @@ public: // Seek to 500ms const Audio::Timestamp a(0, 1, 2); - offset = Audio::convertTimeToStreamPos(a, 11025, isStereo).totalNumberOfFrames(); + offset = Audio::convertTimeToStreamPos(a, sampleRate, isStereo).totalNumberOfFrames(); readData = totalFrames - offset; TS_ASSERT_EQUALS(s->seek(a), true); @@ -362,7 +261,7 @@ public: // Seek to 3/4 of a second const Audio::Timestamp b(0, 3, 4); - offset = Audio::convertTimeToStreamPos(b, 11025, isStereo).totalNumberOfFrames(); + offset = Audio::convertTimeToStreamPos(b, sampleRate, isStereo).totalNumberOfFrames(); readData = totalFrames - offset; TS_ASSERT_EQUALS(s->seek(b), true); @@ -377,7 +276,7 @@ public: // Seek to the mid of the stream const Audio::Timestamp c(time * 1000 / 2, 1000); - offset = Audio::convertTimeToStreamPos(c, 11025, isStereo).totalNumberOfFrames(); + offset = Audio::convertTimeToStreamPos(c, sampleRate, isStereo).totalNumberOfFrames(); readData = totalFrames - offset; TS_ASSERT_EQUALS(s->seek(c), true); @@ -388,7 +287,7 @@ public: // Seek to the 1/4th of the last second of the stream const Audio::Timestamp d(time - 1, 1, 4); - offset = Audio::convertTimeToStreamPos(d, 11025, isStereo).totalNumberOfFrames(); + offset = Audio::convertTimeToStreamPos(d, sampleRate, isStereo).totalNumberOfFrames(); readData = totalFrames - offset; TS_ASSERT_EQUALS(s->seek(d), true); @@ -410,80 +309,12 @@ public: delete[] buffer; } - // TODO: Apart from the "isStereo" bit this is exactly the same functionality - // as test_seek_mono. We should consider merging those too. Maybe a template - // would do and then call it from two different tests? - void test_seek_stereo() { - const int sampleRate = 11025; - const int time = 2; - const bool isStereo = true; - const int totalFrames = sampleRate * time * (isStereo ? 2 : 1); - int readData = 0, offset = 0; - - int16 *buffer = new int16[totalFrames]; - Audio::SeekableAudioStream *s = 0; - int16 *sine = 0; - - s = createSineStream<int8>(sampleRate, time, &sine, false, isStereo); - - // Seek to 500ms - const Audio::Timestamp a(0, 1, 2); - offset = Audio::convertTimeToStreamPos(a, 11025, isStereo).totalNumberOfFrames(); - readData = totalFrames - offset; - - TS_ASSERT_EQUALS(s->seek(a), true); - TS_ASSERT_EQUALS(s->endOfData(), false); - TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData); - TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - // Seek to 3/4 of a second - const Audio::Timestamp b(0, 3, 4); - offset = Audio::convertTimeToStreamPos(b, 11025, isStereo).totalNumberOfFrames(); - readData = totalFrames - offset; - - TS_ASSERT_EQUALS(s->seek(b), true); - TS_ASSERT_EQUALS(s->endOfData(), false); - TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData); - TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - // Seek to the start of the stream - TS_ASSERT_EQUALS(s->seek(0), true); - TS_ASSERT_EQUALS(s->endOfData(), false); - - // Seek to the mid of the stream - const Audio::Timestamp c(time * 1000 / 2, 1000); - offset = Audio::convertTimeToStreamPos(c, 11025, isStereo).totalNumberOfFrames(); - readData = totalFrames - offset; - - TS_ASSERT_EQUALS(s->seek(c), true); - TS_ASSERT_EQUALS(s->endOfData(), false); - TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData); - TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - // Seek to the 1/4th of the last second of the stream - const Audio::Timestamp d(time - 1, 1, 4); - offset = Audio::convertTimeToStreamPos(d, 11025, isStereo).totalNumberOfFrames(); - readData = totalFrames - offset; - - TS_ASSERT_EQUALS(s->seek(d), true); - TS_ASSERT_EQUALS(s->endOfData(), false); - TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData); - TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0); - TS_ASSERT_EQUALS(s->endOfData(), true); - - // Try to seek after the end of the stream - TS_ASSERT_EQUALS(s->seek(Audio::Timestamp(time * 1000, 1, 100000)), false); - TS_ASSERT_EQUALS(s->endOfData(), true); - - // Try to seek exactly at the end of the stream - TS_ASSERT_EQUALS(s->seek(Audio::Timestamp(time * 1000, 1000)), true); - TS_ASSERT_EQUALS(s->endOfData(), true); +public: + void test_seek_mono() { + seekTest(11025, 2, false); + } - delete[] sine; - delete s; - delete[] buffer; + void test_seek_stereo() { + seekTest(11025, 2, true); } }; |