bool SerializeAttachment(Grid& grid, TAttachment& attachment, typename geometry_traits<TElem>::iterator iterBegin, typename geometry_traits<TElem>::iterator iterEnd, BinaryBuffer& out) { if(!grid.has_attachment<TElem>(attachment)) return false; // copy data Grid::AttachmentAccessor<TElem, TAttachment> aa(grid, attachment); typedef typename TAttachment::ValueType ValueType; // write a magic number at the beginning and at the end. int magicNumber = 8304548; out.write((char*)&magicNumber, sizeof(int)); //TODO: remove the following test code. // test: write a number-value to check whether it is send correctly /* number tNum = 1247.001234; out.write((char*)&tNum, sizeof(number)); */ for(; iterBegin != iterEnd; ++iterBegin) { out.write((char*)&aa[*iterBegin], sizeof(ValueType)); } out.write((char*)&magicNumber, sizeof(int)); return true; }
void ZlibTest::testZlibCompressBinary() { bool isOk; unsigned long length; unsigned long num, num1 = 0, num2 = 1640; std::string str, str1, str2, str3("hello"); BinaryBuffer compressBuffer; unsigned long contentSize = 5 * DEF_SizeOfLong + str1.length() + str2.length() + str3.length(); CPPUNIT_ASSERT_MESSAGE("Bad binary buffer size", contentSize == 25); { size_t lgSize = DEF_SizeOfLong + DEF_SizeOfLong; BinaryBuffer binaryBuffer; char *buf = (char *)::malloc(lgSize); char *pos = buf; // Store some numbers SetLgBuf(pos, num1); pos += DEF_SizeOfLong; SetLgBuf(pos, num2); pos += DEF_SizeOfLong; binaryBuffer.write(buf, lgSize); // Store a string length = (unsigned long)str1.length(); SetLgBuf(buf, length); binaryBuffer.write(buf, DEF_SizeOfLong); binaryBuffer.write(str1.c_str(), length); // Store a string length = (unsigned long)str2.length(); SetLgBuf(buf, length); binaryBuffer.write(buf, DEF_SizeOfLong); binaryBuffer.write(str2.c_str(), length); // Store a string length = (unsigned long)str3.length(); SetLgBuf(buf, length); binaryBuffer.write(buf, DEF_SizeOfLong); binaryBuffer.write(str3.c_str(), length); // Zlib compression length = (unsigned long)binaryBuffer.getOccupancy(); CPPUNIT_ASSERT_MESSAGE("Bad binary buffer occupancy", length == contentSize); SetLgBuf(buf, length); // Reset binary buffer compressBuffer.reset(); compressBuffer.write(buf, DEF_SizeOfLong); isOk = ZlibCompress(compressBuffer, (const char *)binaryBuffer.getBuffer(), length); CPPUNIT_ASSERT_MESSAGE("Bad zlib binary compression", isOk); ::free(buf); } { // Zlib deflate const char *pos = static_cast<const char*>(compressBuffer.getBuffer()); size_t size = compressBuffer.getOccupancy(); CPPUNIT_ASSERT_MESSAGE("Bad compress buffer occupancy", size == 26); length = GetLgBuf(pos); pos += DEF_SizeOfLong; CPPUNIT_ASSERT_MESSAGE("Bad binary buffer occupancy", length == contentSize); char *uncompr = NULL; isOk = ZlibDeflate(uncompr, length, (void *)pos, size - DEF_SizeOfLong); CPPUNIT_ASSERT_MESSAGE("Bad zlib binary delate", isOk); pos = uncompr; // Get the numbers num = GetLgBuf(pos); pos += DEF_SizeOfLong; CPPUNIT_ASSERT_MESSAGE("Bad number 1", num == num1); num = GetLgBuf(pos); pos += DEF_SizeOfLong; CPPUNIT_ASSERT_MESSAGE("Bad number 2", num == num2); // Get string length = GetLgBuf(pos); pos += DEF_SizeOfLong; str.assign(pos, length); pos += length; CPPUNIT_ASSERT_MESSAGE("Bad string 1", str == str1); // Get string length = GetLgBuf(pos); pos += DEF_SizeOfLong; str.assign(pos, length); pos += length; CPPUNIT_ASSERT_MESSAGE("Bad string 2", str == str2); // Get string length = GetLgBuf(pos); pos += DEF_SizeOfLong; str.assign(pos, length); pos += length; CPPUNIT_ASSERT_MESSAGE("Bad string 3", str == str3); ::free(uncompr); } }