From baf6b53431bbb1b5a630fd153fe3df350e3d16fd Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Wed, 3 Feb 2010 10:36:14 +0000 Subject: Add some (lame) first version of RawStream unit tests. Currently all it does it, checking, whether readBuffer works for 8bit and 16bit samples. svn-id: r47849 --- test/sound/raw.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 test/sound/raw.h (limited to 'test') diff --git a/test/sound/raw.h b/test/sound/raw.h new file mode 100644 index 0000000000..bedee869ec --- /dev/null +++ b/test/sound/raw.h @@ -0,0 +1,82 @@ +#include + +#include "sound/decoders/raw.h" + +#include "common/stream.h" +#include "common/endian.h" + +#include +#include + +class RawStreamTestSuite : public CxxTest::TestSuite +{ +public: + template + T *createSine(int sampleRate, int time) { + T *sine = (T *)malloc(sizeof(T) * time * sampleRate); + + const T maxValue = std::numeric_limits::max(); + + for (int i = 0; i < time * sampleRate; ++i) + sine[i] = (T)(sin((double)i / sampleRate * 2 * PI) * maxValue); + + // Convert to BE + if (sizeof(T) == 2) { + for (int i = 0; i < time * sampleRate; ++i) + WRITE_BE_UINT16(&sine[i], sine[i]); + } + + return sine; + } + + template + Audio::SeekableAudioStream *createSineStream(int sampleRate, int time, const T **sineP) { + T *sine = createSine(sampleRate, time); + + Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(T) * sampleRate * time, DisposeAfterUse::YES); + Audio::SeekableAudioStream *s = Audio::makeRawStream(sD, sampleRate, (sizeof(T) == 2) ? Audio::FLAG_16BITS : 0); + + if (sineP) + *sineP = sine; + + return s; + } + + void test_read_buffer_8_bit_mono() { + const int sampleRate = 11025; + const int time = 2; + + const int8 *sine = 0; + Audio::SeekableAudioStream *s = createSineStream(sampleRate, time, &sine); + + int16 *buffer = new int16[sampleRate * time]; + TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); + + for (int i = 0; i < sampleRate * time; ++i) + TS_ASSERT_EQUALS(buffer[i], sine[i] << 8); + + TS_ASSERT_EQUALS(s->endOfData(), true); + + delete[] buffer; + delete s; + } + + void test_read_buffer_16_bit_mono() { + const int sampleRate = 11025; + const int time = 2; + + const int16 *sine = 0; + Audio::SeekableAudioStream *s = createSineStream(sampleRate, time, &sine); + + int16 *buffer = new int16[sampleRate * time]; + TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); + + for (int i = 0; i < sampleRate * time; ++i) + TS_ASSERT_EQUALS(buffer[i], (int16)READ_BE_UINT16(&sine[i])); + + TS_ASSERT_EQUALS(s->endOfData(), true); + + delete[] buffer; + delete s; + } +}; -- cgit v1.2.3