File: endian.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (89 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#define BOOST_TEST_MODULE endian
#include <boost/test/unit_test.hpp>

#include <cstdint>
#include "odil/endian.h"

BOOST_AUTO_TEST_CASE(ToLittleEndian16)
{
    uint16_t const input = 0x1234;
    std::string const expected("\x34\x12");
    BOOST_REQUIRE_EQUAL(
        odil::host_to_little_endian(input),
        *reinterpret_cast<uint16_t const *>(&expected[0])
    );
}

BOOST_AUTO_TEST_CASE(ToLittleEndian32)
{
    uint32_t const input = 0x12345678;
    std::string const expected("\x78\x56\x34\x12");
    BOOST_REQUIRE_EQUAL(
        odil::host_to_little_endian(input),
        *reinterpret_cast<uint32_t const *>(&expected[0])
    );
}

BOOST_AUTO_TEST_CASE(ToBigEndian16)
{
    uint16_t const input = 0x1234;
    std::string const expected("\x12\x34");
    BOOST_REQUIRE_EQUAL(
        odil::host_to_big_endian(input),
        *reinterpret_cast<uint16_t const *>(&expected[0])
    );
}

BOOST_AUTO_TEST_CASE(ToBigEndian32)
{
    uint32_t const input = 0x12345678;
    std::string const expected("\x12\x34\x56\x78");
    BOOST_REQUIRE_EQUAL(
        odil::host_to_big_endian(input),
        *reinterpret_cast<uint32_t const *>(&expected[0])
    );
}

BOOST_AUTO_TEST_CASE(FromLittleEndian16)
{
    std::string const input("\x34\x12");
    uint16_t const expected = 0x1234;
    BOOST_REQUIRE_EQUAL(
        odil::little_endian_to_host(
            *reinterpret_cast<uint16_t const *>(&input[0])),
        expected
    );
}

BOOST_AUTO_TEST_CASE(FromLittleEndian32)
{
    std::string const input("\x78\x56\x34\x12");
    uint32_t const expected = 0x12345678;
    BOOST_REQUIRE_EQUAL(
        odil::little_endian_to_host(
            *reinterpret_cast<uint32_t const *>(&input[0])),
        expected
    );
}

BOOST_AUTO_TEST_CASE(FromBigEndian16)
{
    std::string const input("\x12\x34");
    uint16_t const expected = 0x1234;
    BOOST_REQUIRE_EQUAL(
        odil::big_endian_to_host(
            *reinterpret_cast<uint16_t const *>(&input[0])),
        expected
    );
}

BOOST_AUTO_TEST_CASE(FromBigEndian32)
{
    std::string const input("\x12\x34\x56\x78");
    uint32_t const expected = 0x12345678;
    BOOST_REQUIRE_EQUAL(
        odil::big_endian_to_host(
            *reinterpret_cast<uint32_t const *>(&input[0])),
        expected
    );
}