converterstest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <gtest/gtest.h>
  26. #include <QVariant>
  27. #include <qtprotobuftypes.h>
  28. namespace QtProtobuf {
  29. namespace tests {
  30. class ConverterTest : public ::testing::Test
  31. {
  32. public:
  33. ConverterTest() = default;
  34. static void SetUpTestCase() {
  35. QtProtobuf::qRegisterProtobufTypes();
  36. }
  37. };
  38. TEST_F(ConverterTest, TestFromTypeConverters)
  39. {
  40. QVariant testVariant;
  41. testVariant.setValue<uint32_t>(42);
  42. EXPECT_EQ(42, testVariant.value<fixed32>()._t);
  43. testVariant.setValue<uint64_t>(43);
  44. EXPECT_EQ(43, testVariant.value<fixed64>()._t);
  45. testVariant.setValue<int32_t>(44);
  46. EXPECT_EQ(44, testVariant.value<sfixed32>()._t);
  47. testVariant.setValue<int64_t>(45);
  48. EXPECT_EQ(45, testVariant.value<sfixed64>()._t);
  49. testVariant.setValue<int32_t>(46);
  50. EXPECT_EQ(46, testVariant.value<int32>()._t);
  51. testVariant.setValue<int64_t>(47);
  52. EXPECT_EQ(47, testVariant.value<int64>()._t);
  53. }
  54. TEST_F(ConverterTest, TestToTypeConverters)
  55. {
  56. bool ok = false;
  57. QVariant testVariant;
  58. testVariant.setValue<fixed32>({42});
  59. EXPECT_EQ(42, testVariant.toUInt(&ok));
  60. EXPECT_TRUE(ok);
  61. ok = false;
  62. testVariant.setValue<fixed64>({43});
  63. EXPECT_EQ(43, testVariant.toULongLong(&ok));
  64. EXPECT_TRUE(ok);
  65. ok = false;
  66. testVariant.setValue<sfixed32>({44});
  67. EXPECT_EQ(44, testVariant.toInt(&ok));
  68. EXPECT_TRUE(ok);
  69. ok = false;
  70. testVariant.setValue<sfixed64>({45});
  71. EXPECT_EQ(45, testVariant.toLongLong(&ok));
  72. EXPECT_TRUE(ok);
  73. ok = false;
  74. testVariant.setValue<int32>({46});
  75. EXPECT_EQ(46, testVariant.toInt(&ok));
  76. EXPECT_TRUE(ok);
  77. ok = false;
  78. testVariant.setValue<int64>({47});
  79. EXPECT_EQ(47, testVariant.toLongLong(&ok));
  80. EXPECT_TRUE(ok);
  81. }
  82. }
  83. }