1 #include "WriteTest.h" 2 #include "cppunit/TestCaller.h" 3 #include <DataIO.h> 4 #include <stdio.h> 5 #include <string.h> 6 7 WriteTest::WriteTest(std::string name) : 8 BTestCase(name) 9 { 10 } 11 12 13 14 WriteTest::~WriteTest() 15 { 16 } 17 18 19 void 20 WriteTest::PerformTest(void) 21 { 22 char buf[10]; 23 const char *writeBuf = "ABCDEFG"; 24 25 BMemoryIO mem(buf, 10); 26 ssize_t err; 27 off_t pos; 28 29 NextSubTest(); 30 memset(buf, 0, 10); 31 pos = mem.Position(); 32 err = mem.Write(writeBuf, 7); 33 CPPUNIT_ASSERT(err == 7); // Check how much data we wrote 34 CPPUNIT_ASSERT(strcmp(writeBuf, buf) == 0); // Check if we wrote it correctly 35 CPPUNIT_ASSERT(mem.Position() == pos + err); // Check if Position changed 36 37 NextSubTest(); 38 memset(buf, 0, 10); 39 pos = mem.Position(); 40 err = mem.WriteAt(3, writeBuf, 2); 41 CPPUNIT_ASSERT(err == 2); 42 CPPUNIT_ASSERT(strncmp(buf + 3, writeBuf, 2) == 0); 43 CPPUNIT_ASSERT(mem.Position() == pos); 44 45 NextSubTest(); 46 memset(buf, 0, 10); 47 pos = mem.Position(); 48 err = mem.WriteAt(9, writeBuf, 5); 49 CPPUNIT_ASSERT(err == 1); 50 CPPUNIT_ASSERT(strncmp(buf + 9, writeBuf, 1) == 0); 51 CPPUNIT_ASSERT(mem.Position() == pos); 52 53 NextSubTest(); 54 memset(buf, 0, 10); 55 pos = mem.Position(); 56 err = mem.WriteAt(-10, writeBuf, 5); 57 CPPUNIT_ASSERT(err == 5); 58 } 59 60 61 CppUnit::Test *WriteTest::suite(void) 62 { 63 typedef CppUnit::TestCaller<WriteTest> 64 WriteTestCaller; 65 66 return(new WriteTestCaller("BMemoryIO::Write Test", &WriteTest::PerformTest)); 67 } 68