/*
 * MIT License
 *
 * Copyright (c) 2022 Alexey Edelev <semlanik@gmail.com>
 *
 * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "serializationmaptypes.h"

#include "basicmessages.qpb.h"
#include "mapmessages.qpb.h"

#include <QtTest/QtTest>

using namespace qtprotobufnamespace::tests;

void QtProtobufMapTypesSerializationTest::SimpleFixed32StringMapSerializeTest()
{
    SimpleFixed32StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "3a0a0d0a000000120374656e3a0e0d0f00000012076669667465656e3a110d2a000000120a666f757274792074776f");
}

void QtProtobufMapTypesSerializationTest::SimpleSFixed32StringMapSerializeTest()
{
    SimpleSFixed32StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "4a170dd6ffffff12106d696e757320666f757274792074776f4a0a0d0a000000120374656e4a0e0d0f00000012076669667465656e");
}

void QtProtobufMapTypesSerializationTest::SimpleInt32StringMapSerializeTest()
{
    SimpleInt32StringMapMessage test;
    test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "1a1108f6ffffff0f12096d696e75732074656e1a0b080f12076669667465656e1a0e082a120a666f757274792074776f");
}

void QtProtobufMapTypesSerializationTest::SimpleSInt32StringMapSerializeTest()
{
    SimpleSInt32StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "0a14085312106d696e757320666f757274792074776f0a070814120374656e0a0b081e12076669667465656e");
}

void QtProtobufMapTypesSerializationTest::SimpleUInt32StringMapSerializeTest()
{
    SimpleUInt32StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "2a07080a120374656e2a0b080f12076669667465656e2a0e082a120a666f757274792074776f");
}

void QtProtobufMapTypesSerializationTest::SimpleFixed64StringMapSerializeTest()
{
    SimpleFixed64StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "420e090a00000000000000120374656e4212090f0000000000000012076669667465656e4215092a00000000000000120a666f757274792074776f");
}

void QtProtobufMapTypesSerializationTest::SimpleSFixed64StringMapSerializeTest()
{
    SimpleSFixed64StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "521b09d6ffffffffffffff12106d696e757320666f757274792074776f520e090a00000000000000120374656e5212090f0000000000000012076669667465656e");
}

void QtProtobufMapTypesSerializationTest::SimpleInt64StringMapSerializeTest()
{
    SimpleInt64StringMapMessage test;
    test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "221608f6ffffffffffffffff0112096d696e75732074656e220b080f12076669667465656e220e082a120a666f757274792074776f");
}

void QtProtobufMapTypesSerializationTest::SimpleSInt64StringMapSerializeTest()
{
    SimpleSInt64StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());
    QCOMPARE(result.toHex().toStdString().c_str(),
                 "1214085312106d696e757320666f757274792074776f12070814120374656e120b081e12076669667465656e");
}

void QtProtobufMapTypesSerializationTest::SimpleUInt64StringMapSerializeTest()
{
    SimpleUInt64StringMapMessage test;
    test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "3207080a120374656e320b080f12076669667465656e320e082a120a666f757274792074776f");
}

void QtProtobufMapTypesSerializationTest::SimpleStringStringMapSerializeTest()
{
    SimpleStringStringMapMessage test;
    test.setMapField({{"ben", "ten"}, {"what is the answer?", "fourty two"}, {"sweet", "fifteen"}});
    QByteArray result = test.serialize(m_serializer.get());

    QCOMPARE(result.toHex().toStdString().c_str(),
                 "6a0a0a0362656e120374656e6a100a05737765657412076669667465656e6a210a13776861742069732074686520616e737765723f120a666f757274792074776f");
}

QTEST_MAIN(QtProtobufMapTypesSerializationTest)
