home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / tests / Util / MemBufferTest.cc < prev    next >
C/C++ Source or Header  |  2000-06-03  |  4KB  |  168 lines

  1. //
  2. //  $Id: MemBufferTest.cc,v 1.1.1.1 2000/06/02 22:22:58 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include <stdio.h>
  7. #include "../Test.h"
  8. #include "Util/MemBuffer.h"
  9. #include "TestDataStream.h"
  10.  
  11.  
  12. namespace Util
  13. {
  14.     class MemBufferTest: public Test
  15.     {
  16.     public:
  17.         virtual const char* name() const { return "MemBufferTest"; }
  18.  
  19.         virtual void runTest()
  20.         {
  21.             testCopy();
  22.             testSimpleLock();
  23.             testAllocatingLock();
  24.             testAllocation();
  25.             testResize();
  26.             testRelease();
  27.             testSerialization();
  28.         }
  29.  
  30.         void testCopy()
  31.         {
  32.             MemBuffer buff1, buff2;
  33.  
  34.             buff2.allocate(10);
  35.             testAssert(buff1.size() == 0 && buff2.size() == 10);
  36.  
  37.             buff1 = buff2;
  38.             testAssert(buff1.size() == 10 && buff2.size() == 0);
  39.         }
  40.  
  41.         void testSimpleLock()
  42.         {
  43.             MemBuffer buff;
  44.  
  45.             testAssert(buff.size() == 0);
  46.             testAssert(!buff.isLocked());
  47.  
  48.             testAssert(buff.lock() == 0);
  49.             testAssert(!buff.isLocked());
  50.  
  51.             buff.unlock();
  52.         }
  53.  
  54.         void testAllocatingLock()
  55.         {
  56.             MemBuffer buff;
  57.  
  58.             testAssert(buff.size() == 0);
  59.             testAssert(!buff.isLocked());
  60.  
  61.             testAssert(buff.lock(10) != 0);
  62.             testAssert(buff.size() == 10);
  63.             testAssert(buff.isLocked());
  64.  
  65.             testAssert(buff.lock(5) != 0);
  66.             testAssert(buff.size() == 10);
  67.             testAssert(buff.isLocked());
  68.  
  69.             buff.unlock();
  70.             buff.unlock();
  71.  
  72.             testAssert(buff.lock(10) == buff.lock(5));
  73.             buff.unlock();
  74.             buff.unlock();
  75.  
  76.             testAssert(buff.lock(10) == buff.lock());
  77.             buff.unlock();
  78.             buff.unlock();
  79.  
  80.             testAssert(!buff.isLocked());
  81.         }
  82.  
  83.         void testAllocation()
  84.         {
  85.             MemBuffer buff;
  86.             testAssert(buff.isNull());
  87.  
  88.             testAssert(buff.allocate(10));
  89.             testAssert(buff.size() == 10);
  90.             testAssert(!buff.isNull());
  91.  
  92.             testAssert(buff.lock() != 0);
  93.             testAssert(buff.isLocked());
  94.  
  95.             buff.unlock();
  96.             testAssert(!buff.isLocked());
  97.  
  98.             testAssert(buff.lock() == buff.lock());
  99.             // unlock not here intentionally - free() must take care of it.
  100.  
  101.             testAssert(buff.isLocked());
  102.             buff.free();
  103.             testAssert(!buff.isLocked());
  104.             testAssert(buff.isNull());
  105.         }
  106.  
  107.         void testResize()
  108.         {
  109.             MemBuffer buff;
  110.  
  111.             testAssert(buff.allocate(10));
  112.             testAssert(buff.size() == 10);
  113.  
  114.             testAssert(buff.resize(20));
  115.             testAssert(buff.size() == 20);
  116.         }
  117.  
  118.         void testRelease()
  119.         {
  120.             MemBuffer buff;
  121.  
  122.             testAssert(buff.allocate(10));
  123.             testAssert(buff.size() == 10);
  124.  
  125.             VoidHand handle = buff.release();
  126.             testAssert(handle != 0);
  127.             testAssert(buff.isNull());
  128.             testAssert(buff.size() == 0);
  129.  
  130.             MemBuffer buff2(handle);
  131.             testAssert(!buff2.isNull());
  132.             testAssert(buff2.size() == 10);
  133.         }
  134.  
  135.         void testSerialization()
  136.         {
  137.             TestDataStream stream;
  138.             MemBuffer buff1, buff2;
  139.  
  140.             buff1.serialize(stream);
  141.             testAssert(stream._size == 2 && stream._position == 2);
  142.  
  143.             stream.reset();
  144.             buff2.restore(stream);
  145.             testAssert(buff2.isNull());
  146.  
  147.             StrCopy((char*)buff1.lock(10), "Hello!");
  148.  
  149.             stream.reset();
  150.             buff1.serialize(stream);
  151.             testAssert(stream._size == 12 && stream._position == 12);
  152.             testAssert(StrCompare(stream._buffer+2, (char*)buff1.lock()) == 0);
  153.  
  154.             stream.reset();
  155.             buff2.restore(stream);
  156.  
  157.             testAssert(buff2.size() == 10);
  158.             testAssert(StrCompare((char*)buff1.lock(), (char*)buff2.lock()) == 0);
  159.         }
  160.     };
  161. }
  162.  
  163. Test& GetMemBufferTest()
  164. {
  165.     static Util::MemBufferTest test;
  166.     return test;
  167. }
  168.