GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/endian.cc Lines: 8 8 100.0 %
Date: 2021-03-17 Branches: 7 18 38.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "logger.h"
25
26
PRAGMA48(GCC diagnostic push)
27
PRAGMA48(GCC diagnostic ignored "-Wshadow")
28
#include <SDL_endian.h>
29
PRAGMA48(GCC diagnostic pop)
30
31
#include "debug.h"
32
33
#ifndef SDL_BIG_ENDIAN
34
#error missing SDL_endian.h
35
#endif  // SDL_BYTEORDER
36
37
namespace
38
{
39
    union ByteOrderData final
40
    {
41
        uint32_t dwordData;
42
        uint8_t byteData[4];
43
    } __attribute__((packed));
44
}  // namespace
45
46
3
TEST_CASE("endian test", "")
47
{
48
    ByteOrderData data;
49
1
    data.byteData[0] = 0x10;
50
1
    data.byteData[1] = 0x20;
51
1
    data.byteData[2] = 0x30;
52
1
    data.byteData[3] = 0x40;
53
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
54
    REQUIRE(data.dwordData == 0x10203040);
55
    logger->log("big endian detected");
56
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
57
58



4
    REQUIRE(data.dwordData == 0x40302010);
59
1
    logger->log("little endian detected");
60
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
61

4
}