converterstest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <QtTest/QtTest>
  26. #include <QVariant>
  27. #include <qtprotobuftypes.h>
  28. class QtProtobufConverterTest : public QObject
  29. {
  30. Q_OBJECT
  31. public:
  32. QtProtobufConverterTest()
  33. {
  34. qRegisterProtobufTypes();
  35. }
  36. private slots:
  37. void TestFromTypeConverters();
  38. void TestToTypeConverters();
  39. };
  40. void QtProtobufConverterTest::TestFromTypeConverters()
  41. {
  42. QVariant testVariant;
  43. testVariant.setValue<uint32_t>(42);
  44. QCOMPARE(42, testVariant.value<QtProtobuf::fixed32>()._t);
  45. testVariant.setValue<uint64_t>(43);
  46. QCOMPARE(43, testVariant.value<QtProtobuf::fixed64>()._t);
  47. testVariant.setValue<int32_t>(44);
  48. QCOMPARE(44, testVariant.value<QtProtobuf::sfixed32>()._t);
  49. testVariant.setValue<int64_t>(45);
  50. QCOMPARE(45, testVariant.value<QtProtobuf::sfixed64>()._t);
  51. testVariant.setValue<int32_t>(46);
  52. QCOMPARE(46, testVariant.value<QtProtobuf::int32>()._t);
  53. testVariant.setValue<int64_t>(47);
  54. QCOMPARE(47, testVariant.value<QtProtobuf::int64>()._t);
  55. }
  56. void QtProtobufConverterTest::TestToTypeConverters()
  57. {
  58. bool ok = false;
  59. QVariant testVariant;
  60. testVariant.setValue<QtProtobuf::fixed32>({42});
  61. QCOMPARE(42, testVariant.toUInt(&ok));
  62. QVERIFY(ok);
  63. ok = false;
  64. testVariant.setValue<QtProtobuf::fixed64>({43});
  65. QCOMPARE(43, testVariant.toULongLong(&ok));
  66. QVERIFY(ok);
  67. ok = false;
  68. testVariant.setValue<QtProtobuf::sfixed32>({44});
  69. QCOMPARE(44, testVariant.toInt(&ok));
  70. QVERIFY(ok);
  71. ok = false;
  72. testVariant.setValue<QtProtobuf::sfixed64>({45});
  73. QCOMPARE(45, testVariant.toLongLong(&ok));
  74. QVERIFY(ok);
  75. ok = false;
  76. testVariant.setValue<QtProtobuf::int32>({46});
  77. QCOMPARE(46, testVariant.toInt(&ok));
  78. QVERIFY(ok);
  79. ok = false;
  80. testVariant.setValue<QtProtobuf::int64>({47});
  81. QCOMPARE(47, testVariant.toLongLong(&ok));
  82. QVERIFY(ok);
  83. }
  84. QTEST_MAIN(QtProtobufConverterTest)
  85. #include "converterstest.moc"