File: ByteOrderValuesTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (179 lines) | stat: -rw-r--r-- 5,704 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Test Suite for geos::io::ByteOrderValues

// tut
#include <tut/tut.hpp>
// geos
#include <geos/io/ByteOrderValues.h>
#include <geos/constants.h> // for int64

namespace tut {
//
// Test Group
//

// dummy data, not used
struct test_byteordervalues_data {
};

typedef test_group<test_byteordervalues_data> group;
typedef group::object object;

group test_byteordervalues_group("geos::io::ByteOrderValues");


//
// Test Cases
//

// 1 - Read/write an int
template<>
template<>
void object::test<1>
()
{
    using geos::io::ByteOrderValues;

    unsigned char buf[4];
    int in = 1;
    int out;

    ByteOrderValues::putInt(in, buf, ByteOrderValues::ENDIAN_BIG);
    ensure("putInt big endian[0]", buf[0] == 0);
    ensure("putInt big endian[1]", buf[1] == 0);
    ensure("putInt big endian[2]", buf[2] == 0);
    ensure("putInt big endian[3]", buf[3] == 1);

    out = ByteOrderValues::getInt(buf,
                                  geos::io::ByteOrderValues::ENDIAN_BIG);
    ensure_equals("getInt big endian", out, in);

    ByteOrderValues::putInt(1, buf, geos::io::ByteOrderValues::ENDIAN_LITTLE);
    ensure("putInt little endian[0]", buf[0] == 1);
    ensure("putInt little endian[1]", buf[1] == 0);
    ensure("putInt little endian[2]", buf[2] == 0);
    ensure("putInt little endian[3]", buf[3] == 0);

    out = ByteOrderValues::getInt(buf,
                                  ByteOrderValues::ENDIAN_LITTLE);
    ensure_equals("getInt little endian", out, in);
}

// 2 - Read/write a double
template<>
template<>
void object::test<2>
()
{
    using geos::io::ByteOrderValues;

    unsigned char buf[8];
    double in = 2;
    double out;

    ByteOrderValues::putDouble(in, buf,
                               ByteOrderValues::ENDIAN_BIG);
    ensure("putDouble big endian[0]", buf[0] == 64);
    ensure("putDouble big endian[1]", buf[1] == 0);
    ensure("putDouble big endian[2]", buf[2] == 0);
    ensure("putDouble big endian[3]", buf[3] == 0);
    ensure("putDouble big endian[4]", buf[4] == 0);
    ensure("putDouble big endian[5]", buf[5] == 0);
    ensure("putDouble big endian[6]", buf[6] == 0);
    ensure("putDouble big endian[7]", buf[7] == 0);

    out = ByteOrderValues::getDouble(buf,
                                     ByteOrderValues::ENDIAN_BIG);
    ensure_equals("getDouble big endian", out, in);

    ByteOrderValues::putDouble(in, buf,
                               ByteOrderValues::ENDIAN_LITTLE);
    ensure("putDouble little endian[0]", buf[0] == 0);
    ensure("putDouble little endian[1]", buf[1] == 0);
    ensure("putDouble little endian[2]", buf[2] == 0);
    ensure("putDouble little endian[3]", buf[3] == 0);
    ensure("putDouble little endian[4]", buf[4] == 0);
    ensure("putDouble little endian[5]", buf[5] == 0);
    ensure("putDouble little endian[6]", buf[6] == 0);
    ensure("putDouble little endian[7]", buf[7] == 64);

    out = ByteOrderValues::getDouble(buf,
                                     ByteOrderValues::ENDIAN_LITTLE);
    ensure_equals("getDouble little endian", out, in);
}

// 3 - Read/write a long
template<>
template<>
void object::test<3>
()
{
    using geos::io::ByteOrderValues;

    unsigned char buf[8];
    long in = 2;
    long out;

    ByteOrderValues::putLong(in, buf,
                             ByteOrderValues::ENDIAN_BIG);
    ensure("putLong big endian[0]", buf[0] == 0);
    ensure("putLong big endian[1]", buf[1] == 0);
    ensure("putLong big endian[2]", buf[2] == 0);
    ensure("putLong big endian[3]", buf[3] == 0);
    ensure("putLong big endian[4]", buf[4] == 0);
    ensure("putLong big endian[5]", buf[5] == 0);
    ensure("putLong big endian[6]", buf[6] == 0);
    ensure("putLong big endian[7]", buf[7] == 2);

    out = static_cast<long>(ByteOrderValues::getLong(buf, ByteOrderValues::ENDIAN_BIG));
    ensure_equals("getLong big endian", out, in);

    ByteOrderValues::putLong(in, buf,
                             ByteOrderValues::ENDIAN_LITTLE);
    ensure("putLong little endian[0]", buf[0] == 2);
    ensure("putLong little endian[1]", buf[1] == 0);
    ensure("putLong little endian[2]", buf[2] == 0);
    ensure("putLong little endian[3]", buf[3] == 0);
    ensure("putLong little endian[4]", buf[4] == 0);
    ensure("putLong little endian[5]", buf[5] == 0);
    ensure("putLong little endian[6]", buf[6] == 0);
    ensure("putLong little endian[7]", buf[7] == 0);

    out = static_cast<long>(ByteOrderValues::getLong(buf, ByteOrderValues::ENDIAN_LITTLE));
    ensure_equals("getLong little endian", out, in);
}

// 4 - Read/write an unsigned int
template<>
template<>
void object::test<4>()
{
    using geos::io::ByteOrderValues;

    unsigned char buf[4];
    uint32_t in = 3210000003;
    uint32_t out;

    ByteOrderValues::putUnsigned(in, buf, ByteOrderValues::ENDIAN_BIG);
    ensure("putUnsigned big endian[0]", buf[0] == 0xbf);
    ensure("putUnsigned big endian[1]", buf[1] == 0x54);
    ensure("putUnsigned big endian[2]", buf[2] == 0xb6);
    ensure("putUnsigned big endian[3]", buf[3] == 0x83);

    out = ByteOrderValues::getUnsigned(buf,
                                  geos::io::ByteOrderValues::ENDIAN_BIG);
    ensure_equals("getInt big endian", out, in);

    ByteOrderValues::putUnsigned(in, buf, geos::io::ByteOrderValues::ENDIAN_LITTLE);
    ensure("putUnsigned little endian[0]", buf[0] == 0x83);
    ensure("putUnsigned little endian[1]", buf[1] == 0xb6);
    ensure("putUnsigned little endian[2]", buf[2] == 0x54);
    ensure("pUtnsigned little endian[3]", buf[3] == 0xbf);

    out = ByteOrderValues::getUnsigned(buf,
                                  ByteOrderValues::ENDIAN_LITTLE);
    ensure_equals("getInt little endian", out, in);
}

} // namespace tut