Update test to work with the split serializer/deserializer

This commit is contained in:
Dale Glass 2022-06-24 14:25:20 +02:00 committed by HifiExperiments
parent 201c531edb
commit 2cef749183
3 changed files with 59 additions and 39 deletions

View file

@ -15,15 +15,13 @@
const int DataSerializer::DEFAULT_SIZE;
const char DataSerializer::PADDING_CHAR;
QDebug operator<<(QDebug debug, const DataSerializer &ds) {
debug << "{ capacity =" << ds.capacity() << "; length = " << ds.length() << "; pos = " << ds.pos() << "}";
debug << "\n";
QString literal;
static void dumpHex(QDebug &debug, const char*buf, size_t len) {
QString literal;
QString hex;
for(size_t i=0;i<ds.length();i++) {
char c = ds._store[i];
for(size_t i=0;i<len;i++) {
char c = buf[i];
if (std::isalnum(c)) {
literal.append(c);
@ -38,7 +36,7 @@ QDebug operator<<(QDebug debug, const DataSerializer &ds) {
hex.append(hnum + " ");
if ( literal.length() == 16 || (i+1 == ds.length()) ) {
if ( literal.length() == 16 || (i+1 == len) ) {
while( literal.length() < 16 ) {
literal.append(" ");
hex.append(" ");
@ -49,7 +47,24 @@ QDebug operator<<(QDebug debug, const DataSerializer &ds) {
hex.clear();
}
}
}
QDebug operator<<(QDebug debug, const DataSerializer &ser) {
debug << "{ capacity =" << ser.capacity() << "; length = " << ser.length() << "; pos = " << ser.pos() << "}";
debug << "\n";
dumpHex(debug, ser.buffer(), ser.length());
return debug;
}
QDebug operator<<(QDebug debug, const DataDeserializer &des) {
debug << "{ length = " << des.length() << "; pos = " << des.pos() << "}";
debug << "\n";
dumpHex(debug, des.buffer(), des.length());
return debug;
}

View file

@ -103,7 +103,7 @@ class DataSerializer {
*/
DataSerializer(char *externalStore, size_t storeLength) {
_capacity = storeLength;
_length = storeLength;
_length = 0;
_pos = 0;
_storeIsExternal = true;
_store = externalStore;
@ -430,7 +430,7 @@ class DataSerializer {
if ( _capacity < _length + bytes) {
if ( _storeIsExternal ) {
qCritical() << "Serializer trying to write past end of input, writing" << bytes << "bytes for" << type_name << " from position " << _pos << ", length " << _length;
qCritical() << "Serializer trying to write past end of output buffer, writing" << bytes << "bytes for" << type_name << " from position " << _pos << ", length " << _length;
_overflow = true;
return false;
}

View file

@ -20,14 +20,18 @@ void SerializerTests::initTestCase() {
}
void SerializerTests::testCreate() {
SerDes s;
DataSerializer s;
QCOMPARE(s.length(), 0);
QCOMPARE(s.capacity(), SerDes::DEFAULT_SIZE);
QCOMPARE(s.capacity(), DataSerializer::DEFAULT_SIZE);
QCOMPARE(s.isEmpty(), true);
DataDeserializer d(s);
QCOMPARE(d.length(), 0);
}
void SerializerTests::testAdd() {
SerDes s;
DataSerializer s;
s << (qint8)1;
QCOMPARE(s.length(), 1);
QCOMPARE(s.isEmpty(), false);
@ -62,7 +66,7 @@ void SerializerTests::testAdd() {
}
void SerializerTests::testAddAndRead() {
SerDes s;
DataSerializer s;
glm::vec3 v3_a{1.f, 3.1415f, 2.71828f};
glm::vec3 v3_b;
glm::vec4 v4_a{3.1415f, 2.71828f, 1.4142f, 1.6180f};
@ -84,17 +88,17 @@ void SerializerTests::testAddAndRead() {
qint16 i16;
qint32 i32;
s.rewind();
DataDeserializer d(s);
s >> i8;
s >> i16;
s >> i32;
s >> v3_b;
s >> v4_b;
s >> iv2_b;
s >> f_b;
d >> i8;
d >> i16;
d >> i32;
d >> v3_b;
d >> v4_b;
d >> iv2_b;
d >> f_b;
qDebug() << s;
qDebug() << d;
QCOMPARE(i8, (qint8)1);
QCOMPARE(i16, (qint16)0xaabb);
@ -106,22 +110,23 @@ void SerializerTests::testAddAndRead() {
}
void SerializerTests::testReadPastEnd() {
SerDes s;
DataSerializer s;
qint8 i8;
qint16 i16;
s << (qint8)1;
s.rewind();
s >> i8;
QCOMPARE(s.pos(), 1);
s.rewind();
s >> i16;
QCOMPARE(s.pos(), 0);
DataDeserializer d(s);
d >> i8;
QCOMPARE(d.pos(), 1);
d.rewind();
d >> i16;
QCOMPARE(d.pos(), 0);
}
void SerializerTests::benchmarkEncodingDynamicAlloc() {
QBENCHMARK {
SerDes s;
DataSerializer s;
glm::vec3 v3_a{1.f, 3.1415f, 2.71828f};
glm::vec3 v3_b;
glm::vec4 v4_a{3.1415f, 2.71828f, 1.4142f, 1.6180f};
@ -142,7 +147,7 @@ void SerializerTests::benchmarkEncodingStaticAlloc() {
char buf[1024];
QBENCHMARK {
SerDes s(buf, sizeof(buf));
DataSerializer s(buf, sizeof(buf));
glm::vec3 v3_a{1.f, 3.1415f, 2.71828f};
glm::vec3 v3_b;
glm::vec4 v4_a{3.1415f, 2.71828f, 1.4142f, 1.6180f};
@ -161,7 +166,7 @@ void SerializerTests::benchmarkEncodingStaticAlloc() {
void SerializerTests::benchmarkDecoding() {
SerDes s;
DataSerializer s;
qint8 q8 = 1;
qint16 q16 = 0xaabb;
qint32 q32 = 0xccddeeff;
@ -182,13 +187,13 @@ void SerializerTests::benchmarkDecoding() {
QBENCHMARK {
s.rewind();
s >> q8;
s >> q16;
s >> q32;
s >> v3_a;
s >> v4_a;
s >> iv2_a;
DataDeserializer d(s);
d >> q8;
d >> q16;
d >> q32;
d >> v3_a;
d >> v4_a;
d >> iv2_a;
}
}