home *** CD-ROM | disk | FTP | other *** search
- //
- // $Id: MemBufferTest.cc,v 1.1.1.1 2000/06/02 22:22:58 sergey Exp $
- //
-
- #include <Pilot.h>
- #include <stdio.h>
- #include "../Test.h"
- #include "Util/MemBuffer.h"
- #include "TestDataStream.h"
-
-
- namespace Util
- {
- class MemBufferTest: public Test
- {
- public:
- virtual const char* name() const { return "MemBufferTest"; }
-
- virtual void runTest()
- {
- testCopy();
- testSimpleLock();
- testAllocatingLock();
- testAllocation();
- testResize();
- testRelease();
- testSerialization();
- }
-
- void testCopy()
- {
- MemBuffer buff1, buff2;
-
- buff2.allocate(10);
- testAssert(buff1.size() == 0 && buff2.size() == 10);
-
- buff1 = buff2;
- testAssert(buff1.size() == 10 && buff2.size() == 0);
- }
-
- void testSimpleLock()
- {
- MemBuffer buff;
-
- testAssert(buff.size() == 0);
- testAssert(!buff.isLocked());
-
- testAssert(buff.lock() == 0);
- testAssert(!buff.isLocked());
-
- buff.unlock();
- }
-
- void testAllocatingLock()
- {
- MemBuffer buff;
-
- testAssert(buff.size() == 0);
- testAssert(!buff.isLocked());
-
- testAssert(buff.lock(10) != 0);
- testAssert(buff.size() == 10);
- testAssert(buff.isLocked());
-
- testAssert(buff.lock(5) != 0);
- testAssert(buff.size() == 10);
- testAssert(buff.isLocked());
-
- buff.unlock();
- buff.unlock();
-
- testAssert(buff.lock(10) == buff.lock(5));
- buff.unlock();
- buff.unlock();
-
- testAssert(buff.lock(10) == buff.lock());
- buff.unlock();
- buff.unlock();
-
- testAssert(!buff.isLocked());
- }
-
- void testAllocation()
- {
- MemBuffer buff;
- testAssert(buff.isNull());
-
- testAssert(buff.allocate(10));
- testAssert(buff.size() == 10);
- testAssert(!buff.isNull());
-
- testAssert(buff.lock() != 0);
- testAssert(buff.isLocked());
-
- buff.unlock();
- testAssert(!buff.isLocked());
-
- testAssert(buff.lock() == buff.lock());
- // unlock not here intentionally - free() must take care of it.
-
- testAssert(buff.isLocked());
- buff.free();
- testAssert(!buff.isLocked());
- testAssert(buff.isNull());
- }
-
- void testResize()
- {
- MemBuffer buff;
-
- testAssert(buff.allocate(10));
- testAssert(buff.size() == 10);
-
- testAssert(buff.resize(20));
- testAssert(buff.size() == 20);
- }
-
- void testRelease()
- {
- MemBuffer buff;
-
- testAssert(buff.allocate(10));
- testAssert(buff.size() == 10);
-
- VoidHand handle = buff.release();
- testAssert(handle != 0);
- testAssert(buff.isNull());
- testAssert(buff.size() == 0);
-
- MemBuffer buff2(handle);
- testAssert(!buff2.isNull());
- testAssert(buff2.size() == 10);
- }
-
- void testSerialization()
- {
- TestDataStream stream;
- MemBuffer buff1, buff2;
-
- buff1.serialize(stream);
- testAssert(stream._size == 2 && stream._position == 2);
-
- stream.reset();
- buff2.restore(stream);
- testAssert(buff2.isNull());
-
- StrCopy((char*)buff1.lock(10), "Hello!");
-
- stream.reset();
- buff1.serialize(stream);
- testAssert(stream._size == 12 && stream._position == 12);
- testAssert(StrCompare(stream._buffer+2, (char*)buff1.lock()) == 0);
-
- stream.reset();
- buff2.restore(stream);
-
- testAssert(buff2.size() == 10);
- testAssert(StrCompare((char*)buff1.lock(), (char*)buff2.lock()) == 0);
- }
- };
- }
-
- Test& GetMemBufferTest()
- {
- static Util::MemBufferTest test;
- return test;
- }
-