converterstest.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 CoverterTest : public ::testing::Test
  31. {
  32. public:
  33. CoverterTest() = default;
  34. };
  35. TEST_F(CoverterTest, TestFromTypeConverters)
  36. {
  37. QVariant testVariant;
  38. testVariant.setValue<uint32_t>(42);
  39. ASSERT_EQ(42, testVariant.value<fixed32>()._t);
  40. testVariant.setValue<uint64_t>(43);
  41. ASSERT_EQ(43, testVariant.value<fixed64>()._t);
  42. testVariant.setValue<int32_t>(44);
  43. ASSERT_EQ(44, testVariant.value<sfixed32>()._t);
  44. testVariant.setValue<int64_t>(45);
  45. ASSERT_EQ(45, testVariant.value<sfixed64>()._t);
  46. testVariant.setValue<int32_t>(46);
  47. ASSERT_EQ(46, testVariant.value<int32>()._t);
  48. testVariant.setValue<int64_t>(47);
  49. ASSERT_EQ(47, testVariant.value<int64>()._t);
  50. }
  51. TEST_F(CoverterTest, TestToTypeConverters)
  52. {
  53. bool ok = false;
  54. QVariant testVariant;
  55. testVariant.setValue<fixed32>({42});
  56. ASSERT_EQ(42, testVariant.toUInt(&ok));
  57. ASSERT_TRUE(ok);
  58. ok = false;
  59. testVariant.setValue<fixed64>({43});
  60. ASSERT_EQ(43, testVariant.toULongLong(&ok));
  61. ASSERT_TRUE(ok);
  62. ok = false;
  63. testVariant.setValue<sfixed32>({44});
  64. ASSERT_EQ(44, testVariant.toInt(&ok));
  65. ASSERT_TRUE(ok);
  66. ok = false;
  67. testVariant.setValue<sfixed64>({45});
  68. ASSERT_EQ(45, testVariant.toLongLong(&ok));
  69. ASSERT_TRUE(ok);
  70. ok = false;
  71. testVariant.setValue<int32>({46});
  72. ASSERT_EQ(46, testVariant.toInt(&ok));
  73. ASSERT_TRUE(ok);
  74. ok = false;
  75. testVariant.setValue<int64>({47});
  76. ASSERT_EQ(47, testVariant.toLongLong(&ok));
  77. ASSERT_TRUE(ok);
  78. }
  79. }
  80. }