Prechádzať zdrojové kódy

Complete deserialization of simple lists types

- Add deserialization for IntList/FloatList/DoubleList
- Implement tests
Alexey Edelev 6 rokov pred
rodič
commit
313beb2951

+ 31 - 3
src/lib/protobufobject.h

@@ -380,11 +380,11 @@ protected:
     void deserializeUserType(int userType, QByteArray::const_iterator& it, QVariant &newValue)
     {
         if (userType == qMetaTypeId<IntList>()) {
-              //TODO: implement
+            newValue = deserializeVarintListType<int>(it);
         } else if(userType == qMetaTypeId<FloatList>()) {
-              //TODO: implement
+            newValue = deserializeListType<float>(it);
         } else if(userType == qMetaTypeId<DoubleList>()) {
-              //TODO: implement
+            newValue = deserializeListType<double>(it);
         } else {
             auto value = reinterpret_cast<ProtobufObjectPrivate *>(QMetaType::create(userType));
             value->deserializePrivate(deserializeLengthDelimited(it));
@@ -398,6 +398,34 @@ protected:
     QByteArray deserializeListType(QByteArray::const_iterator& it) {
         return deserializeLengthDelimited(it);
     }
+
+    template <typename V,
+              typename std::enable_if_t<std::is_floating_point<V>::value
+                                        || std::is_same<V, unsigned int>::value
+                                        || std::is_same<V, qulonglong>::value, int> = 0>
+    QVariant deserializeListType(QByteArray::const_iterator& it) {
+        QList<V> out;
+        unsigned int count = deserializeVarint<unsigned int>(it).toUInt() / sizeof(V);
+        for (int i = 0; i < count; i++) {
+            QVariant variant = deserializeFixed<V>(it);
+            out.append(variant.value<V>());
+        }
+        return QVariant::fromValue(out);
+    }
+
+    template <typename V,
+              typename std::enable_if_t<std::is_same<V, int>::value, int> = 0>
+    QVariant deserializeVarintListType(QByteArray::const_iterator& it) {
+        QList<V> out;
+        unsigned int count = deserializeVarint<unsigned int>(it).toUInt();
+        QByteArray::const_iterator lastVarint = it + count;
+        while(it != lastVarint) {
+            QVariant variant = deserializeVarint<V>(it);
+            out.append(variant.value<V>());
+        }
+        return QVariant::fromValue(out);
+    }
+
 public:
     virtual QByteArray serializePrivate() = 0;
     virtual void deserializePrivate(const QByteArray &data) = 0;

+ 27 - 0
tests/deserializationtest.cpp

@@ -34,6 +34,9 @@
 #include "complexmessage.h"
 #include "repeatedstringmessage.h"
 #include "repeatedbytesmessage.h"
+#include "repeateddoublemessage.h"
+#include "repeatedfloatmessage.h"
+#include "repeatedintmessage.h"
 
 using namespace qtprotobufnamespace::tests;
 using namespace qtprotobuf::tests;
@@ -271,3 +274,27 @@ TEST_F(DeserializationTest, RepeatedBytesMessageTest)
                                                              QByteArray::fromHex("eaeaeaeaea"),
                                                              QByteArray::fromHex("010203040506")}));
 }
+
+TEST_F(DeserializationTest, RepeatedFloatMessageTest)
+{
+    RepeatedFloatMessage test;
+    test.deserialize(QByteArray::fromHex("0a14cdcccc3e9a99993f0000003f3333b33f9a99193f"));
+    ASSERT_EQ(5, test.testRepeatedFloat().count());
+    ASSERT_TRUE(test.testRepeatedFloat() == FloatList({0.4f, 1.2f, 0.5f, 1.4f, 0.6f}));
+}
+
+TEST_F(DeserializationTest, RepeatedDoubleMessageTest)
+{
+    RepeatedDoubleMessage test;
+    test.deserialize(QByteArray::fromHex("0a289a9999999999b93f9a9999999999c93f333333333333d33f9a9999999999d93f000000000000e03f"));
+    ASSERT_EQ(5, test.testRepeatedDouble().count());
+    ASSERT_TRUE(test.testRepeatedDouble() == DoubleList({0.1, 0.2, 0.3, 0.4, 0.5}));
+}
+
+TEST_F(DeserializationTest, RepeatedIntMessageTest)
+{
+    RepeatedIntMessage test;
+    test.deserialize(QByteArray::fromHex("0a0702a00606080a0c"));
+    ASSERT_EQ(6, test.testRepeatedInt().count());
+    ASSERT_TRUE(test.testRepeatedInt() == IntList({1, 400, 3, 4, 5, 6}));
+}

+ 1 - 1
tests/serializationtest.cpp

@@ -458,7 +458,7 @@ TEST_F(SerializationTest, ComplexTypeSerializeTest)
 TEST_F(SerializationTest, RepeatedIntMessageTest)
 {
     RepeatedIntMessage test;
-    test.setTestRepeatedInt({1,400,3,4,5,6});
+    test.setTestRepeatedInt({1, 400, 3, 4, 5, 6});
     QByteArray result = test.serialize();
 //    qDebug() << "result " << result.toHex();
     ASSERT_TRUE(result == QByteArray::fromHex("0a0702a00606080a0c"));