aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_basictypeconv.cpp
blob: 10ab37ee67da38b10becfa81bf4cd8970020d291 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2021 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>

#include <jau/test/catch2_ext.hpp>

#include <jau/basic_types.hpp>

static constexpr inline bool VERBOSE = false;

using namespace jau::int_literals;

/**
 * Test private impl namespace
 */
namespace test_impl {
    template<class Dummy_type>
    constexpr bool isLittleEndian2_impl(std::enable_if_t<jau::has_endian_little_v<Dummy_type>, bool> = true) noexcept {
        return true;
    }

    template<class Dummy_type>
    constexpr bool isLittleEndian2_impl(std::enable_if_t<!jau::has_endian_little_v<Dummy_type>, bool> = true) noexcept {
        return false;
    }
}

/**
 * Just demonstrating usage of our type-traits
 * in a convenient API manner w/o requiring to add the dummy template type.
 */
constexpr bool isLittleEndian2() noexcept {
    return test_impl::isLittleEndian2_impl<bool>();
}


TEST_CASE( "Endianess Test 00", "[endian]" ) {
    fprintf(stderr, "********************************************************************************\n");
    fprintf(stderr, "is_builtin_bit_cast_available: %d\n", jau::is_builtin_bit_cast_available());
    fprintf(stderr, "endian: %s\n", jau::to_string(jau::endian::native).c_str());
    fprintf(stderr, "********************************************************************************\n");

    const bool cpp_is_little =
        #if BYTE_ORDER == LITTLE_ENDIAN
            true;
        #else
            false;
        #endif
    const bool cpp_is_big =
        #if BYTE_ORDER == BIG_ENDIAN
            true;
        #else
            false;
        #endif
    const bool is_little = jau::endian::little == jau::endian::native;
    const bool is_big = jau::endian::big == jau::endian::native;
    REQUIRE( cpp_is_little == is_little );
    REQUIRE( cpp_is_little == jau::isLittleEndian() );
    REQUIRE( cpp_is_big == is_big );
    REQUIRE( is_little == isLittleEndian2());
}

template<typename Value_type>
static void print(const Value_type a) {
    const uint8_t * pa = reinterpret_cast<const uint8_t *>(&a);
    for(std::size_t i=0; i<sizeof(Value_type); i++) {
        fprintf(stderr, "a[%zu] 0x%X, ", i, pa[i]);
    }
}

template<typename Value_type>
static bool compare(const Value_type a, const Value_type b) {
    const uint8_t * pa = reinterpret_cast<const uint8_t *>(&a);
    const uint8_t * pb = reinterpret_cast<const uint8_t *>(&b);
    bool res = true;
    for(std::size_t i=0; i<sizeof(Value_type) && res; i++) {
        res = pa[i] == pb[i];
        if( !res ) {
            fprintf(stderr, "pa[%zu] 0x%X != pb[%zu] 0x%X\n", i, pa[i], i, pb[i]);
        }
    }
    return res;
}

template<typename Value_type>
static void test_byteorder(const Value_type v_cpu,
                           const Value_type v_le,
                           const Value_type v_be)
{
    if( VERBOSE ) {
        fprintf(stderr, "test_byteorder: sizeof %zu; platform littleEndian %d", sizeof(Value_type), jau::isLittleEndian());
        fprintf(stderr, "\ncpu: %s: ", jau::to_hexstring(v_cpu).c_str()); print(v_cpu);
        fprintf(stderr, "\nle_: %s: ", jau::to_hexstring(v_le).c_str()); print(v_le);
        fprintf(stderr, "\nbe_: %s: ", jau::to_hexstring(v_be).c_str()); print(v_be);
        fprintf(stderr, "\n");
    }
    {
        Value_type r1_le = jau::bswap(v_be);
        REQUIRE( r1_le == v_le );
        Value_type r1_be = jau::bswap(v_le);
        REQUIRE( r1_be == v_be );
    }
    {
        #if BYTE_ORDER == LITTLE_ENDIAN
            REQUIRE( compare(v_le, v_cpu) == true );
            Value_type r1_cpu = jau::bswap(v_be);
            REQUIRE( r1_cpu == v_cpu );
        #else
            REQUIRE( compare(v_be, v_cpu) == true );
            Value_type r1_cpu = jau::bswap(v_le);
            REQUIRE( r1_cpu == v_cpu );
        #endif
    }
    {
        Value_type r1_cpu = jau::le_to_cpu(v_le);
        Value_type r2_cpu = jau::be_to_cpu(v_be);
        REQUIRE( r1_cpu == v_cpu );
        REQUIRE( r2_cpu == v_cpu );
    }
}

static uint16_t compose(const uint8_t n1, const uint8_t n2) {
    uint16_t dest;
    uint8_t * p_dest = reinterpret_cast<uint8_t*>(&dest);
    p_dest[0] = n1;
    p_dest[1] = n2;
    return dest;
}
static uint32_t compose(const uint8_t n1, const uint8_t n2, const uint8_t n3, const uint8_t n4) {
    uint32_t dest;
    uint8_t * p_dest = reinterpret_cast<uint8_t*>(&dest);
    p_dest[0] = n1;
    p_dest[1] = n2;
    p_dest[2] = n3;
    p_dest[3] = n4;
    return dest;
}
static uint64_t compose(const uint8_t n1, const uint8_t n2, const uint8_t n3, const uint8_t n4,
                        const uint8_t n5, const uint8_t n6, const uint8_t n7, const uint8_t n8) {
    uint64_t dest;
    uint8_t * p_dest = reinterpret_cast<uint8_t*>(&dest);
    p_dest[0] = n1;
    p_dest[1] = n2;
    p_dest[2] = n3;
    p_dest[3] = n4;
    p_dest[4] = n5;
    p_dest[5] = n6;
    p_dest[6] = n7;
    p_dest[7] = n8;
    return dest;
}

template<typename Value_type>
static Value_type compose(const uint8_t lowest_value, const bool little_endian) {
    Value_type dest;
    uint8_t * p_dest = reinterpret_cast<uint8_t*>(&dest);
    uint8_t byte_value = lowest_value;
    if( little_endian ) {
        for(size_t i=0; i<sizeof(dest); i++, byte_value++) {
            p_dest[i] = byte_value;
        }
    } else {
        for(ssize_t i=sizeof(dest)-1; i>=0; i--, byte_value++) {
            p_dest[i] = byte_value;
        }
    }
    return dest;
}

TEST_CASE( "Integer Type Byte Order Test 01", "[byteorder][bswap]" ) {
    {
        uint16_t cpu = 0x3210U;
        uint16_t le = compose(0x10, 0x32); // stream: 1032
        uint16_t be = compose(0x32, 0x10); // stream: 3210
        test_byteorder(cpu, le, be);
    }
    {
        uint32_t cpu = 0x76543210U;
        uint32_t le = compose(0x10, 0x32, 0x54, 0x76); // stream: 10325476
        uint32_t be = compose(0x76, 0x54, 0x32, 0x10); // stream: 76543210
        test_byteorder(cpu, le, be);
    }
    {
        uint64_t cpu = 0xfedcba9876543210ULL;
        uint64_t le = compose(0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe); // stream: 1032547698badcfe
        uint64_t be = compose(0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10); // stream: fedcba9876543210
        test_byteorder(cpu, le, be);
    }
    {
        jau::uint128_t le = compose<jau::uint128_t>(0x01, true /* little_endian */);
        jau::uint128_t be = compose<jau::uint128_t>(0x01, false /* little_endian */);
        jau::uint128_t cpu = jau::isLittleEndian() ? le : be;
        test_byteorder(cpu, le, be);
    }
    {
        jau::uint192_t le = compose<jau::uint192_t>(0x01, true /* little_endian */);
        jau::uint192_t be = compose<jau::uint192_t>(0x01, false /* little_endian */);
        jau::uint192_t cpu = jau::isLittleEndian() ? le : be;
        test_byteorder(cpu, le, be);
    }
    {
        jau::uint256_t le = compose<jau::uint256_t>(0x01, true /* little_endian */);
        jau::uint256_t be = compose<jau::uint256_t>(0x01, false /* little_endian */);
        jau::uint256_t cpu = jau::isLittleEndian() ? le : be;
        test_byteorder(cpu, le, be);
    }
}

template<typename Value_type>
static void test_value_cpu(const Value_type v1, const Value_type v2, const Value_type v3) {
    uint8_t buffer[3 * sizeof(Value_type)];
    jau::put_value(buffer, sizeof(Value_type)*0, v1);
    jau::put_value(buffer, sizeof(Value_type)*1, v2);
    jau::put_value(buffer, sizeof(Value_type)*2, v3);
    const Value_type r1 = jau::get_value<Value_type>(buffer, sizeof(Value_type)*0);
    const Value_type r2 = jau::get_value<Value_type>(buffer, sizeof(Value_type)*1);
    const Value_type r3 = jau::get_value<Value_type>(buffer, sizeof(Value_type)*2);
    REQUIRE( r1 == v1);
    REQUIRE( r2 == v2);
    REQUIRE( r3 == v3);
}

TEST_CASE( "Integer Get/Put in CPU Byte Order Test 02", "[byteorder][get][put]" ) {
    {
        uint8_t a = 0x01, b = 0x11, c = 0xff;
        test_value_cpu(a, b, c);
    }
    {
        uint16_t a = 0x0123, b = 0x1122, c = 0xffee;
        test_value_cpu(a, b, c);
    }
    {
        uint32_t a = 0x01234567U, b = 0x11223344U, c = 0xffeeddccU;
        test_value_cpu(a, b, c);
    }
    {
        uint64_t a = 0x0123456789abcdefULL, b = 0x1122334455667788ULL, c = 0xffeeddcc99887766ULL;
        test_value_cpu(a, b, c);
    }
    {
        jau::uint128_t a = compose<jau::uint128_t>(0x01, jau::isLittleEndian());
        jau::uint128_t b = compose<jau::uint128_t>(0x20, jau::isLittleEndian());
        jau::uint128_t c = compose<jau::uint128_t>(0x40, jau::isLittleEndian());
        test_value_cpu(a, b, c);
    }
    {
        jau::uint192_t a = compose<jau::uint192_t>(0x01, jau::isLittleEndian());
        jau::uint192_t b = compose<jau::uint192_t>(0x20, jau::isLittleEndian());
        jau::uint192_t c = compose<jau::uint192_t>(0x40, jau::isLittleEndian());
        test_value_cpu(a, b, c);
    }
    {
        jau::uint256_t a = compose<jau::uint256_t>(0x01, jau::isLittleEndian());
        jau::uint256_t b = compose<jau::uint256_t>(0x20, jau::isLittleEndian());
        jau::uint256_t c = compose<jau::uint256_t>(0x40, jau::isLittleEndian());
        test_value_cpu(a, b, c);
    }
}

template<typename Value_type>
static void test_value_littlebig(const Value_type v_cpu, const Value_type v_le, const Value_type v_be) {
    if( VERBOSE ) {
        fprintf(stderr, "test_value_littlebig: sizeof %zu; platform littleEndian %d", sizeof(Value_type), jau::isLittleEndian());
        fprintf(stderr, "\ncpu: %s: ", jau::to_hexstring(v_cpu).c_str()); print(v_cpu);
        fprintf(stderr, "\nle_: %s: ", jau::to_hexstring(v_le).c_str()); print(v_le);
        fprintf(stderr, "\nbe_: %s: ", jau::to_hexstring(v_be).c_str()); print(v_be);
        fprintf(stderr, "\n");
    }
    uint8_t buffer[2 * sizeof(Value_type)];

    jau::put_value(buffer, sizeof(Value_type)*0, v_cpu, true /* little_endian */);
    jau::put_value(buffer, sizeof(Value_type)*1, v_cpu, false /* little_endian */);

    const Value_type rle_raw = jau::get_value<Value_type>(buffer, sizeof(Value_type)*0);
    const Value_type rle_cpu = jau::get_value<Value_type>(buffer, sizeof(Value_type)*0, true /* little_endian */);
    REQUIRE( rle_raw == v_le);
    REQUIRE( rle_cpu == v_cpu);

    const Value_type rbe_raw = jau::get_value<Value_type>(buffer, sizeof(Value_type)*1);
    const Value_type rbe_cpu = jau::get_value<Value_type>(buffer, sizeof(Value_type)*1, false /* little_endian */);
    REQUIRE( rbe_raw == v_be);
    REQUIRE( rbe_cpu == v_cpu);
}

TEST_CASE( "Integer Get/Put in explicit Byte Order Test 03", "[byteorder][get][put]" ) {
    {
        uint16_t cpu = 0x3210U;
        uint16_t le = compose(0x10, 0x32); // stream: 1032
        uint16_t be = compose(0x32, 0x10); // stream: 3210
        test_value_littlebig(cpu, le, be);
    }
    {
        uint32_t cpu = 0x76543210U;
        uint32_t le = compose(0x10, 0x32, 0x54, 0x76); // stream: 10325476
        uint32_t be = compose(0x76, 0x54, 0x32, 0x10); // stream: 76543210
        test_value_littlebig(cpu, le, be);
    }
    {
        uint64_t cpu = 0xfedcba9876543210ULL;
        uint64_t le = compose(0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe); // stream: 1032547698badcfe
        uint64_t be = compose(0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10); // stream: fedcba9876543210
        test_value_littlebig(cpu, le, be);
    }
    {
        jau::uint128_t le = compose<jau::uint128_t>(0x01, true /* little_endian */);
        jau::uint128_t be = compose<jau::uint128_t>(0x01, false /* little_endian */);
        jau::uint128_t cpu = jau::isLittleEndian() ? le : be;
        test_value_littlebig(cpu, le, be);
    }
    {
        jau::uint192_t le = compose<jau::uint192_t>(0x01, true /* little_endian */);
        jau::uint192_t be = compose<jau::uint192_t>(0x01, false /* little_endian */);
        jau::uint192_t cpu = jau::isLittleEndian() ? le : be;
        test_value_littlebig(cpu, le, be);
    }
    {
        jau::uint256_t le = compose<jau::uint256_t>(0x01, true /* little_endian */);
        jau::uint256_t be = compose<jau::uint256_t>(0x01, false /* little_endian */);
        jau::uint256_t cpu = jau::isLittleEndian() ? le : be;
        test_value_littlebig(cpu, le, be);
    }
}

TEST_CASE( "HexString from and to byte vector conversion - Test 04", "[hexstring]" ) {
    const std::vector<uint8_t> lalaSink1 = { 0x1a, 0x1b, 0x2a, 0x2b, 0xff };
    {
        const std::string value_s0 = "1a1b2a2bff";
        const std::string value_s1 = jau::bytesHexString(lalaSink1.data(), 0, lalaSink1.size(), true /* lsbFirst */);
        std::vector<uint8_t> lalaSink2;
        jau::hexStringBytes(lalaSink2, value_s1, true /* lsbFirst */, false);
        const std::string value_s2 = jau::bytesHexString(lalaSink2.data(), 0, lalaSink2.size(), true /* lsbFirst */);
        REQUIRE( value_s0 == value_s1 );
        REQUIRE( value_s0 == value_s2 );
        // Assert.assertArrayEquals(lalaSink1, lalaSink2);
    }
    {
        const std::string value_s0 = "0xff2b2a1b1a";
        const std::string value_s1 = jau::bytesHexString(lalaSink1.data(), 0, lalaSink1.size(), false /* lsbFirst */);
        std::vector<uint8_t> lalaSink2;
        jau::hexStringBytes(lalaSink2, value_s1, false /* lsbFirst */, true);
        const std::string value_s2 = jau::bytesHexString(lalaSink2.data(), 0, lalaSink2.size(), false /* lsbFirst */);
        REQUIRE( value_s0 == value_s1 );
        REQUIRE( value_s0 == value_s2 );
        // Assert.assertArrayEquals(lalaSink1, lalaSink2);
    }
}

TEST_CASE( "Integer Type Test Test 05", "[integer][type]" ) {
    REQUIRE( 3_i8 == (int8_t)3 );
    REQUIRE( 3_u8 == (uint8_t)3 );

    REQUIRE( 3_i16 == (int16_t)3 );
    REQUIRE( 3_u16 == (uint16_t)3 );

    REQUIRE( 3_i32 == (int32_t)3 );
    REQUIRE( 3_u32 == (uint32_t)3 );

    REQUIRE( 3_i64 == (int64_t)3 );
    REQUIRE( 3_u64 == (uint64_t)3 );

    REQUIRE( 3_iz == (ssize_t)3 );
    REQUIRE( 3_uz == (size_t)3 );

    REQUIRE( 3_inz == (jau::snsize_t)3 );
    REQUIRE( 3_unz == (jau::nsize_t)3 );
}