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 == B_BAD_VALUE); 58 CPPUNIT_ASSERT(mem.Position() == pos); 59 60 NextSubTest(); 61 memset(buf, 0, 10); 62 BMemoryIO read_only_mem(const_cast<const char*>(buf), 10); 63 pos = read_only_mem.Position(); 64 err = read_only_mem.WriteAt(3, writeBuf, 2); 65 CPPUNIT_ASSERT(err == B_NOT_ALLOWED); 66 CPPUNIT_ASSERT(read_only_mem.Position() == pos); 67 } 68 69 70 CppUnit::Test *WriteTest::suite(void) 71 { 72 typedef CppUnit::TestCaller<WriteTest> 73 WriteTestCaller; 74 75 return(new WriteTestCaller("BMemoryIO::Write Test", &WriteTest::PerformTest)); 76 } 77