b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
0
|
// test_endian.c |
98e5939 |
Jamozed |
2021-11-27 17:53:46 |
1
|
// Endian unit test for libutil |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
2
|
// Copyright (C) 2021, Jakob Wakeling |
7f427d9 |
Jamozed |
2022-03-06 12:55:13 |
3
|
// MIT Licence |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
4
|
|
bd9fc8b |
Jamozed |
2021-01-27 14:44:30 |
5
|
#include "../endian.h" |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
6
|
#include "unit.h" |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
7
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
8
|
#include <stdint.h> |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
9
|
#include <stdio.h> |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
10
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
11
|
int main(void) { |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
12
|
{ |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
13
|
ASSERT("G00T00 BSWAP16", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
14
|
BSWAP16(0xFF00u) == 0x00FFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
15
|
ASSERT("G00T02 BSWAP32", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
16
|
BSWAP32(0xFF00FF00u) == 0x00FF00FFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
17
|
ASSERT("G00T04 BSWAP64", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
18
|
BSWAP64(0xFF00FF00FF00FF00) == 0x00FF00FF00FF00FF); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
19
|
} |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
20
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
21
|
{ |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
22
|
uint8_t a16[2] = { 0xFF, 0x00 }; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
23
|
uint8_t a32[4] = { 0xFF, 0x00, 0x00, 0x00 }; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
24
|
uint8_t a64[8] = { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
25
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
26
|
ASSERT("G01T00 LD16LE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
27
|
LD16LE(a16) == 0x00FFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
28
|
ASSERT("G01T01 LD16BE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
29
|
LD16BE(a16) == 0xFF00u); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
30
|
ASSERT("G01T02 LD32LE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
31
|
LD32LE(a32) == 0x000000FFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
32
|
ASSERT("G01T03 LD32BE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
33
|
LD32BE(a32) == 0xFF000000u); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
34
|
ASSERT("G01T04 LD64LE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
35
|
LD64LE(a64) == 0x00000000000000FFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
36
|
ASSERT("G01T05 LD64BE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
37
|
LD64BE(a64) == 0xFF00000000000000u); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
38
|
} |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
39
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
40
|
{ |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
41
|
uint16_t a16 = 0x00FFu; uint32_t a32 = 0x000000FFu; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
42
|
uint64_t a64 = 0x00000000000000FFu; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
43
|
uint8_t b16[2]; uint8_t b32[4]; uint8_t b64[8]; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
44
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
45
|
ST16LE(b16, a16); ASSERT("G02T00 ST16LE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
46
|
b16[0] == 0xFFu && b16[1] == 0x00u); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
47
|
ST16BE(b16, a16); ASSERT("G02T01 ST16BE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
48
|
b16[0] == 0x00u && b16[1] == 0xFFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
49
|
ST32LE(b32, a32); ASSERT("G02T02 ST32LE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
50
|
b32[0] == 0xFFu && b32[1] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
51
|
b32[2] == 0x00u && b32[3] == 0x00u); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
52
|
ST32BE(b32, a32); ASSERT("G02T03 ST32BE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
53
|
b32[0] == 0x00u && b32[1] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
54
|
b32[2] == 0x00u && b32[3] == 0xFFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
55
|
ST64LE(b64, a64); ASSERT("G02T04 ST64LE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
56
|
b64[0] == 0xFFu && b64[1] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
57
|
b64[2] == 0x00u && b64[3] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
58
|
b64[4] == 0x00u && b64[5] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
59
|
b64[6] == 0x00u && b64[7] == 0x00u); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
60
|
ST64BE(b64, a64); ASSERT("G02T05 ST64BE", |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
61
|
b64[0] == 0x00u && b64[1] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
62
|
b64[2] == 0x00u && b64[3] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
63
|
b64[4] == 0x00u && b64[5] == 0x00u && |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
64
|
b64[6] == 0x00u && b64[7] == 0xFFu); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
65
|
} |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
66
|
|
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
67
|
printf("%d of %d tests passed\n", testspassed, testsrun); |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
68
|
return testsfailed; |
b9d4a23 |
Jamozed |
2021-01-26 13:11:04 |
69
|
} |
|
|
|
70
|
|