From c5316804b566cd905a5ecf621eb8fdc6c4a42c20 Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Fri, 1 Jan 2010 16:39:11 +0100 Subject: [PATCH] Fixed and tested all Stream tests --- .../sources/{memsource.h => stream_source.h} | 10 ++--- stream/clients/audiere_file.h | 2 + stream/clients/ogre_datastream.h | 6 +-- stream/filters/slice_stream.h | 14 +++---- stream/servers/file_stream.h | 2 +- stream/servers/memory_stream.h | 5 ++- stream/servers/std_stream.h | 6 +-- stream/stream.h | 6 +-- stream/tests/Makefile | 16 ++++--- stream/tests/audiere_client_test.cpp | 4 +- ...buffer_test.cpp => buffer_filter_test.cpp} | 4 +- stream/tests/file_server_test.cpp | 18 ++++++++ ...memory_test.cpp => memory_server_test.cpp} | 4 +- stream/tests/ogre_client_test.cpp | 6 +-- stream/tests/slice_filter_test.cpp | 42 +++++++++++++++++++ tools/shared_ptr.h | 2 +- 16 files changed, 109 insertions(+), 38 deletions(-) rename sound/sources/{memsource.h => stream_source.h} (81%) rename stream/tests/{buffer_test.cpp => buffer_filter_test.cpp} (92%) create mode 100644 stream/tests/file_server_test.cpp rename stream/tests/{memory_test.cpp => memory_server_test.cpp} (89%) create mode 100644 stream/tests/slice_filter_test.cpp diff --git a/sound/sources/memsource.h b/sound/sources/stream_source.h similarity index 81% rename from sound/sources/memsource.h rename to sound/sources/stream_source.h index 72624b899..278f701e8 100644 --- a/sound/sources/memsource.h +++ b/sound/sources/stream_source.h @@ -1,5 +1,5 @@ -#ifndef MANGLE_SOUND_MEMSOURCE_H -#define MANGLE_SOUND_MEMSOURCE_H +#ifndef MANGLE_SOUND_STREAMSOURCE_H +#define MANGLE_SOUND_STREAMSOURCE_H #include "../source.h" @@ -38,9 +38,9 @@ class Stream2Samples : public SampleSource size_t tell() const { return inp->tell(); } size_t size() const { return inp->size(); } bool eof() const { return inp->eof(); } - void *getPtr() const { return inp->getPtr(); } - void *getPtr(size_t size) const { return inp->getPtr(size); } - void *getPtr(size_t pos, size_t size) const { return inp->getPtr(pos, size); } + const void *getPtr() { return inp->getPtr(); } + const void *getPtr(size_t size) { return inp->getPtr(size); } + const void *getPtr(size_t pos, size_t size) { return inp->getPtr(pos, size); } }; }} // namespaces diff --git a/stream/clients/audiere_file.h b/stream/clients/audiere_file.h index ec67fd1b7..670b36ffa 100644 --- a/stream/clients/audiere_file.h +++ b/stream/clients/audiere_file.h @@ -4,6 +4,8 @@ #include #include +#include "../stream.h" + namespace Mangle { namespace Stream { diff --git a/stream/clients/ogre_datastream.h b/stream/clients/ogre_datastream.h index 23ab94fd4..5369ed11a 100644 --- a/stream/clients/ogre_datastream.h +++ b/stream/clients/ogre_datastream.h @@ -14,7 +14,7 @@ namespace Stream { to make your own modifications if you're working with newer (or older) versions. */ -class MangleDataStream : public Ogre::DataStream +class Mangle2OgreStream : public Ogre::DataStream { StreamPtr inp; @@ -27,11 +27,11 @@ class MangleDataStream : public Ogre::DataStream public: /// Constructor without name - MangleDataStream(StreamPtr _inp) + Mangle2OgreStream(StreamPtr _inp) : inp(_inp) { init(); } /// Constructor for a named data stream - MangleDataStream(const Ogre::String &name, StreamPtr _inp) + Mangle2OgreStream(const Ogre::String &name, StreamPtr _inp) : inp(_inp), Ogre::DataStream(name) { init(); } // Only implement the DataStream functions we have to implement diff --git a/stream/filters/slice_stream.h b/stream/filters/slice_stream.h index a24a66e16..d792b57d6 100644 --- a/stream/filters/slice_stream.h +++ b/stream/filters/slice_stream.h @@ -21,7 +21,7 @@ class SliceStream : public Stream assert(src->isSeekable); // Make sure we can actually fit inside the source stream - assert(src->size() <= offset+length); + assert(src->size() >= offset+length); isSeekable = true; hasPosition = true; @@ -50,13 +50,13 @@ class SliceStream : public Stream if(pos > length) pos = length; } - bool eof() { return pos == length; } - size_t tell() { return pos; } - size_t size() { return length; } + bool eof() const { return pos == length; } + size_t tell() const { return pos; } + size_t size() const { return length; } - void *getPtr() { return getPtr(0, length); } - void *getPtr(size_t size) { return getPtr(pos, size); } - void *getPtr(size_t pos, size_t size) + const void *getPtr() { return getPtr(0, length); } + const void *getPtr(size_t size) { return getPtr(pos, size); } + const void *getPtr(size_t pos, size_t size) { // Boundry checks on pos and size. Bounding the size is // important even when getting pointers, as the source stream diff --git a/stream/servers/file_stream.h b/stream/servers/file_stream.h index bb4df4d38..012cb3a78 100644 --- a/stream/servers/file_stream.h +++ b/stream/servers/file_stream.h @@ -17,7 +17,7 @@ class FileStream : public StdStream FileStream(const std::string &name) : StdStream(&file) { - file.open(name, ios::binary); + file.open(name.c_str(), std::ios::binary); } ~FileStream() { file.close(); } }; diff --git a/stream/servers/memory_stream.h b/stream/servers/memory_stream.h index 23842e09b..d77779878 100644 --- a/stream/servers/memory_stream.h +++ b/stream/servers/memory_stream.h @@ -3,6 +3,7 @@ #include #include "../stream.h" +#include namespace Mangle { namespace Stream { @@ -71,8 +72,8 @@ class MemoryStream : public Stream bool eof() const { return pos == length; } /// Get the base pointer to the entire buffer - const void *getPtr() const { return data; } - const void *getPtr(size_t size) const { return ((char*)data)+pos; } + const void *getPtr() { return data; } + const void *getPtr(size_t size) { return ((char*)data)+pos; } const void *getPtr(size_t pos, size_t size) { if(pos > length) pos = length; diff --git a/stream/servers/std_stream.h b/stream/servers/std_stream.h index 0e91ebb78..d4b56a9ae 100644 --- a/stream/servers/std_stream.h +++ b/stream/servers/std_stream.h @@ -1,5 +1,5 @@ -#ifndef MANGLE_STREAM_FILESERVER_H -#define MANGLE_STREAM_FILESERVER_H +#ifndef MANGLE_STREAM_STDIOSERVER_H +#define MANGLE_STREAM_STDIOSERVER_H #include "../stream.h" #include @@ -41,7 +41,7 @@ class StdStream : public Stream { // Use the standard iostream size hack, terrible as it is. std::streampos pos = inf->tellg(); - inf->seekg(0, ios_base::end); + inf->seekg(0, std::ios::end); size_t res = inf->tellg(); inf->seekg(pos); return res; diff --git a/stream/stream.h b/stream/stream.h index 704abc88a..309b0b33a 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -58,15 +58,15 @@ class Stream /// Return a pointer to the entire stream. This function (and the /// other getPtr() variants below) should only be implemented for /// memory-based streams where using them would be an optimization. - virtual void *getPtr() const { assert(0); } + virtual const void *getPtr() { assert(0); } /// Get a pointer to a memory region of 'size' bytes from the /// current position. - virtual void *getPtr(size_t size) const { assert(0); } + virtual const void *getPtr(size_t size) { assert(0); } /// Get a pointer to a memory region of 'size' bytes starting from /// position 'pos' - virtual void *getPtr(size_t pos, size_t size) const { assert(0); } + virtual const void *getPtr(size_t pos, size_t size) { assert(0); } }; typedef boost::shared_ptr StreamPtr; diff --git a/stream/tests/Makefile b/stream/tests/Makefile index c86a7397f..d95908afb 100644 --- a/stream/tests/Makefile +++ b/stream/tests/Makefile @@ -1,21 +1,27 @@ GCC=g++ -I../ -all: ogre_client_test dummy_test audiere_client_test +all: ogre_client_test audiere_client_test memory_server_test buffer_filter_test file_server_test slice_filter_test I_OGRE=$(shell pkg-config --cflags OGRE) L_OGRE=$(shell pkg-config --libs OGRE) L_AUDIERE=-laudiere -ogre_client_test: ogre_client_test.cpp ../stream.h ../clients/iwrapper.h ../clients/ogre_datastream.h +ogre_client_test: ogre_client_test.cpp ../stream.h ../clients/ogre_datastream.h $(GCC) $< -o $@ $(I_OGRE) $(L_OGRE) -audiere_client_test: audiere_client_test.cpp ../stream.h ../clients/iwrapper.h ../clients/audiere_file.h ../clients/audiere_file.cpp +audiere_client_test: audiere_client_test.cpp ../stream.h ../clients/audiere_file.h ../clients/audiere_file.cpp $(GCC) $< -o $@ ../clients/audiere_file.cpp $(L_AUDIERE) -memory_test: memory_test.cpp ../stream.h ../servers/memory_stream.h +file_server_test: file_server_test.cpp ../stream.h ../servers/file_stream.h ../servers/std_stream.h $(GCC) $< -o $@ -buffer_test: buffer_test.cpp ../stream.h ../servers/memory_stream.h ../filters/buffer_stream.h +memory_server_test: memory_server_test.cpp ../stream.h ../servers/memory_stream.h + $(GCC) $< -o $@ + +buffer_filter_test: buffer_filter_test.cpp ../stream.h ../servers/memory_stream.h ../filters/buffer_stream.h + $(GCC) $< -o $@ + +slice_filter_test: slice_filter_test.cpp ../stream.h ../servers/memory_stream.h ../filters/slice_stream.h $(GCC) $< -o $@ clean: diff --git a/stream/tests/audiere_client_test.cpp b/stream/tests/audiere_client_test.cpp index 01f548714..dd4dfe4ad 100644 --- a/stream/tests/audiere_client_test.cpp +++ b/stream/tests/audiere_client_test.cpp @@ -11,8 +11,8 @@ int main() { char str[12]; memset(str, 0, 12); - Stream *inp = new MemoryStream("hello world", 11); - FilePtr p(new AudiereFile(inp, true)); + StreamPtr inp(new MemoryStream("hello world", 11)); + FilePtr p(new AudiereFile(inp)); cout << "pos=" << p->tell() << endl; p->read(str, 2); cout << "2 bytes: " << str << endl; diff --git a/stream/tests/buffer_test.cpp b/stream/tests/buffer_filter_test.cpp similarity index 92% rename from stream/tests/buffer_test.cpp rename to stream/tests/buffer_filter_test.cpp index 24bb7e070..b6f43e99b 100644 --- a/stream/tests/buffer_test.cpp +++ b/stream/tests/buffer_filter_test.cpp @@ -8,8 +8,8 @@ using namespace std; int main() { - Stream *orig = new MemoryStream("hello world", 11); - Stream *inp = new BufferStream(orig); + StreamPtr orig (new MemoryStream("hello world", 11)); + StreamPtr inp (new BufferStream(orig)); cout << "Size: " << inp->size() << endl; cout << "Pos: " << inp->tell() << "\nSeeking...\n"; diff --git a/stream/tests/file_server_test.cpp b/stream/tests/file_server_test.cpp new file mode 100644 index 000000000..1c2505158 --- /dev/null +++ b/stream/tests/file_server_test.cpp @@ -0,0 +1,18 @@ +#include "../servers/file_stream.h" +#include + +using namespace Mangle::Stream; +using namespace std; + +int main() +{ + StreamPtr inp(new FileStream("file_server_test.cpp")); + + char buf[21]; + buf[20] = 0; + cout << "pos=" << inp->tell() << " eof=" << inp->eof() << endl; + inp->read(buf, 20); + cout << "First 20 bytes: " << buf << endl; + cout << "pos=" << inp->tell() << " eof=" << inp->eof() << endl; + return 0; +} diff --git a/stream/tests/memory_test.cpp b/stream/tests/memory_server_test.cpp similarity index 89% rename from stream/tests/memory_test.cpp rename to stream/tests/memory_server_test.cpp index 7e7f34075..b9f21b9b1 100644 --- a/stream/tests/memory_test.cpp +++ b/stream/tests/memory_server_test.cpp @@ -8,7 +8,7 @@ using namespace std; int main() { - Stream *inp = new MemoryStream("hello world", 11); + Stream* inp = new MemoryStream("hello world\0", 12); cout << "Size: " << inp->size() << endl; cout << "Pos: " << inp->tell() << "\nSeeking...\n"; @@ -35,6 +35,8 @@ int main() cout << "Result: " << data << endl; cout << "Eof: " << inp->eof() << endl; cout << "Pos: " << inp->tell() << endl; + + cout << "Entire stream from pointer: " << (char*)inp->getPtr() << endl; return 0; } diff --git a/stream/tests/ogre_client_test.cpp b/stream/tests/ogre_client_test.cpp index 530113362..a89589d7c 100644 --- a/stream/tests/ogre_client_test.cpp +++ b/stream/tests/ogre_client_test.cpp @@ -1,5 +1,5 @@ #include "../servers/memory_stream.h" -#include "ogre_datastream.h" +#include "../clients/ogre_datastream.h" #include using namespace Mangle::Stream; @@ -8,8 +8,8 @@ using namespace std; int main() { - Stream *inp = new MemoryStream("hello world", 11); - DataStreamPtr p(new MangleDataStream("hello", inp, true)); + StreamPtr inp(new MemoryStream("hello world", 11)); + DataStreamPtr p(new Mangle2OgreStream("hello", inp)); cout << "Name: " << p->getName() << endl; cout << "As string: " << p->getAsString() << endl; cout << "pos=" << p->tell() << " eof=" << p->eof() << endl; diff --git a/stream/tests/slice_filter_test.cpp b/stream/tests/slice_filter_test.cpp new file mode 100644 index 000000000..ff384c1bb --- /dev/null +++ b/stream/tests/slice_filter_test.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include "../filters/slice_stream.h" +#include "../servers/memory_stream.h" + +using namespace Mangle::Stream; +using namespace std; + +void test(StreamPtr inp) +{ + cout << "Size: " << inp->size() << endl; + cout << "Pos: " << inp->tell() << "\nSeeking...\n"; + char data[6]; + memset(data, 0, 6); + cout << "Reading " << inp->read(data, 6) << " bytes\n"; + cout << "Result: " << data << endl; + cout << "Pos: " << inp->tell() << endl; + cout << "Eof: " << inp->eof() << endl; + inp->seek(2); + cout << "Seeking:\nPos: " << inp->tell() << endl; + cout << "Eof: " << inp->eof() << endl; + cout << "Reading " << inp->read(data, 6) << " bytes\n"; + cout << "Result: " << data << endl; + cout << "Pos: " << inp->tell() << endl; + cout << "Eof: " << inp->eof() << endl; + cout << "Entire stream as pointer: " << (char*)inp->getPtr() << endl; +} + +int main() +{ + StreamPtr orig (new MemoryStream("hello\0world\0", 12)); + StreamPtr slice1 (new SliceStream(orig,0,6)); + StreamPtr slice2 (new SliceStream(orig,6,6)); + + cout << "\nSlice 1:\n--------\n"; + test(slice1); + cout << "\nSlice 2:\n--------\n"; + test(slice2); + + return 0; +} diff --git a/tools/shared_ptr.h b/tools/shared_ptr.h index 00af6084b..da0b399bd 100644 --- a/tools/shared_ptr.h +++ b/tools/shared_ptr.h @@ -1,3 +1,3 @@ // This file should include whatever it needs to define the boost/tr1 // shared_ptr<> template. -#include +#include