Fixed and tested all Stream tests

pull/21/head
Nicolay Korslund 15 years ago
parent eaf93691d5
commit c5316804b5

@ -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

@ -4,6 +4,8 @@
#include <audiere.h>
#include <assert.h>
#include "../stream.h"
namespace Mangle {
namespace Stream {

@ -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

@ -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

@ -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(); }
};

@ -3,6 +3,7 @@
#include <assert.h>
#include "../stream.h"
#include <string.h>
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;

@ -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 <iostream>
@ -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;

@ -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<Stream> StreamPtr;

@ -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:

@ -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;

@ -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";

@ -0,0 +1,18 @@
#include "../servers/file_stream.h"
#include <iostream>
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;
}

@ -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;
}

@ -1,5 +1,5 @@
#include "../servers/memory_stream.h"
#include "ogre_datastream.h"
#include "../clients/ogre_datastream.h"
#include <iostream>
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;

@ -0,0 +1,42 @@
#include <iostream>
#include <string.h>
#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;
}

@ -1,3 +1,3 @@
// This file should include whatever it needs to define the boost/tr1
// shared_ptr<> template.
#include <boost/smart_ptr.h>
#include <boost/smart_ptr.hpp>

Loading…
Cancel
Save