common: Disable enum type stream operators for Qt >= 5.14
[quassel.git] / tests / common / typestest.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <cstdint>
22
23 #include <QByteArray>
24 #include <QDataStream>
25 #include <QObject>
26
27 #include "testglobal.h"
28 #include "types.h"
29
30 using namespace ::testing;
31
32 class EnumHolder
33 {
34     Q_GADGET
35
36 public:
37     enum class Enum16 : uint16_t {};
38     enum class Enum32 : uint32_t {};
39
40     enum class EnumQt16 : uint16_t {};
41     Q_ENUM(EnumQt16)
42     enum class EnumQt32 : uint32_t {};
43     Q_ENUM(EnumQt32)
44 };
45
46 // Verify that enums are (de)serialized as their underlying type
47 TEST(TypesTest, enumSerialization)
48 {
49     QByteArray data;
50     QDataStream out(&data, QIODevice::WriteOnly);
51
52     // Serialize
53     out << EnumHolder::Enum16(0xabcd);
54     ASSERT_THAT(data.size(), Eq(2));
55     out << EnumHolder::Enum32(0x123456);
56     ASSERT_THAT(data.size(), Eq(6));
57     out << EnumHolder::EnumQt16(0x4321);
58     ASSERT_THAT(data.size(), Eq(8));
59     out << EnumHolder::Enum32(0xfedcba);
60     ASSERT_THAT(data.size(), Eq(12));
61     ASSERT_THAT(out.status(), Eq(QDataStream::Status::Ok));
62
63     // Deserialize
64     QDataStream in(data);
65     EnumHolder::Enum16 enum16;
66     EnumHolder::Enum32 enum32;
67     EnumHolder::EnumQt16 enumQt16;
68     EnumHolder::EnumQt32 enumQt32;
69     in >> enum16  >> enum32 >> enumQt16 >> enumQt32;
70     ASSERT_THAT(in.status(), Eq(QDataStream::Status::Ok));
71     EXPECT_TRUE(in.atEnd());
72
73     EXPECT_THAT((int)enum16, Eq(0xabcd));
74     EXPECT_THAT((int)enum32, Eq(0x123456));
75     EXPECT_THAT((int)enumQt16, Eq(0x4321));
76     EXPECT_THAT((int)enumQt32, Eq(0xfedcba));
77 }
78
79 #include "typestest.moc"