Example #1
0
void DataBufferTF::testDrainData()
{
    DataBuffer buffer;
    char str[] = "just for test";
    buffer.writeBytes(str, sizeof(str)-1);
    int dataLen = buffer.getDataLen();
    buffer.drainData(0);
    CPPUNIT_ASSERT_EQUAL(dataLen, buffer.getDataLen());
    buffer.drainData(sizeof(str)*2);
    CPPUNIT_ASSERT_EQUAL(0, buffer.getDataLen());
}
void DEFAULTPACKETSTREAMERTF::testEncode() {
    MyPacketFactory *factory =  new MyPacketFactory();
    DefaultPacketStreamer *streamer
        = new DefaultPacketStreamer(factory);
    MyPacket packet(16);
    DataBuffer buffer;
    PacketHeader *header = new PacketHeader;
    packet.setPacketHeader(header);
    delete header;
    header = packet.getPacketHeader();
    header->_chid = 1111;
    header->_pcode = 2222;
    CPPUNIT_ASSERT(streamer->encode(&packet, &buffer));
    CPPUNIT_ASSERT_EQUAL(32, (int)buffer.getDataLen());
    buffer.drainData(4);
    CPPUNIT_ASSERT_EQUAL(1111, (int)buffer.readInt32());
    CPPUNIT_ASSERT_EQUAL(2222, (int)buffer.readInt32());
    CPPUNIT_ASSERT_EQUAL(16, (int)buffer.readInt32());


    MyPacket packet1(100, false);
    CPPUNIT_ASSERT(!streamer->encode(&packet1, &buffer));
    CPPUNIT_ASSERT_EQUAL(16, (int)buffer.getDataLen());
    delete streamer;
    delete factory;
}
void DEFAULTPACKETSTREAMERTF::testGetPacketInfo() {
    DataBuffer buffer;
    PacketHeader header;
    DefaultPacketStreamer streamer(NULL);
    bool broken;
    buffer.writeInt32(ANET_PACKET_FLAG);
    buffer.writeInt32(1111);
    buffer.writeInt32(2222);
    buffer.writeInt32(-1);    
    buffer.writeBytes("data for test", 16);
    CPPUNIT_ASSERT(!streamer.getPacketInfo(&buffer, &header, &broken));
    CPPUNIT_ASSERT_EQUAL(1111, (int)header._chid);
    CPPUNIT_ASSERT_EQUAL(2222, (int)header._pcode);
    CPPUNIT_ASSERT_EQUAL(-1, (int)header._dataLen);
    

    CPPUNIT_ASSERT(broken);

    buffer.drainData(buffer.getDataLen());
    streamer._existPacketHeader = false;
    CPPUNIT_ASSERT(!streamer.getPacketInfo(&buffer, &header, &broken));

    
}