File: byte_orderer.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,569 bytes parent folder | download | duplicates (13)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2011  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <dlib/byte_orderer.h>

#include "tester.h"

namespace  
{

    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.byte_orderer");


    class byte_orderer_tester : public tester
    {
    public:
        byte_orderer_tester (
        ) :
            tester ("test_byte_orderer",
                    "Runs tests on the byte_orderer component.")
        {}

        void perform_test (
        )
        {
            byte_orderer bo;

            union data
            {
                unsigned char b[4];
                dlib::uint32 val;
            };

            data a;

            a.val = 1;

            if (bo.host_is_little_endian())
            {
                DLIB_TEST(a.b[0] == 1);
                DLIB_TEST(a.b[1] == 0);
                DLIB_TEST(a.b[2] == 0);
                DLIB_TEST(a.b[3] == 0);

                bo.host_to_big(a.val);

                DLIB_TEST(a.b[0] == 0);
                DLIB_TEST(a.b[1] == 0);
                DLIB_TEST(a.b[2] == 0);
                DLIB_TEST(a.b[3] == 1);

                bo.big_to_host(a.val);

                DLIB_TEST(a.b[0] == 1);
                DLIB_TEST(a.b[1] == 0);
                DLIB_TEST(a.b[2] == 0);
                DLIB_TEST(a.b[3] == 0);

                DLIB_TEST(a.val == 1);
                bo.host_to_network(a.val);
                DLIB_TEST(a.val == 0x01000000);
                bo.network_to_host(a.val);
                DLIB_TEST(a.val == 1);
            }
            else
            {
                DLIB_TEST(a.b[0] == 0);
                DLIB_TEST(a.b[1] == 0);
                DLIB_TEST(a.b[2] == 0);
                DLIB_TEST(a.b[3] == 1);

                bo.host_to_little(a.val);

                DLIB_TEST(a.b[0] == 1);
                DLIB_TEST(a.b[1] == 0);
                DLIB_TEST(a.b[2] == 0);
                DLIB_TEST(a.b[3] == 0);

                bo.little_to_host(a.val);

                DLIB_TEST(a.b[0] == 0);
                DLIB_TEST(a.b[1] == 0);
                DLIB_TEST(a.b[2] == 0);
                DLIB_TEST(a.b[3] == 1);


                DLIB_TEST(a.val == 1);
                bo.network_to_host(a.val);
                DLIB_TEST(a.val == 1);
                bo.host_to_network(a.val);
                DLIB_TEST(a.val == 1);

            }


        }
    } a;

}