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

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



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

8
}