File: test_byte_order.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (100 lines) | stat: -rw-r--r-- 3,689 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2020 Wildfire Games.
 *
 * 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 "lib/self_test.h"

#include "lib/byte_order.h"

#include <cstring>

class TestByteOrder : public CxxTest::TestSuite
{
public:
	void test_conversion()
	{
		const u32 x = 0x01234567u;
		u8 LS_byte;
		memcpy(&LS_byte, &x, 1);
		// little endian
		if(LS_byte == 0x67)
		{
			TS_ASSERT_EQUALS(to_le16(0x0123u), 0x0123u);
			TS_ASSERT_EQUALS(to_le32(0x01234567u), 0x01234567u);
			TS_ASSERT_EQUALS(to_le64(0x0123456789ABCDEFull), 0x0123456789ABCDEFull);

			TS_ASSERT_EQUALS(to_be16(0x0123u), 0x2301u);
			TS_ASSERT_EQUALS(to_be32(0x01234567u), 0x67452301u);
			TS_ASSERT_EQUALS(to_be64(0x0123456789ABCDEFull), 0xEFCDAB8967452301ull);
		}
		// big endian
		else if(LS_byte == 0x01)
		{
			TS_ASSERT_EQUALS(to_le16(0x0123u), 0x2301u);
			TS_ASSERT_EQUALS(to_le32(0x01234567u), 0x67452301u);
			TS_ASSERT_EQUALS(to_le64(0x0123456789ABCDEFull), 0xEFCDAB8967452301ull);

			TS_ASSERT_EQUALS(to_be16(0x0123u), 0x0123u);
			TS_ASSERT_EQUALS(to_be32(0x01234567u), 0x01234567u);
			TS_ASSERT_EQUALS(to_be64(0x0123456789ABCDEFull), 0x0123456789ABCDEFull);
		}
		else
			TS_FAIL("endian determination failed");

		// note: no need to test read_?e* / write_?e* - they are
		// trivial wrappers on top of to_?e*.
	}

	void test_movzx()
	{
		const u8 d1[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
		const u8 d2[] = { 0x43, 0x12, 0x23, 0xA4 };

		TS_ASSERT_EQUALS(movzx_le64(d1, 1), 0x01ull);
		TS_ASSERT_EQUALS(movzx_le64(d1, 2), 0x0201ull);
		TS_ASSERT_EQUALS(movzx_le64(d1, 8), 0x0807060504030201ull);
		TS_ASSERT_EQUALS(movzx_le64(d2, 4), 0xA4231243ull);
		TS_ASSERT_EQUALS(movzx_le64(d2+3, 1), 0xA4ull);

		TS_ASSERT_EQUALS(movzx_be64(d1, 1), 0x01ull);
		TS_ASSERT_EQUALS(movzx_be64(d1, 2), 0x0102ull);
		TS_ASSERT_EQUALS(movzx_be64(d1, 8), 0x0102030405060708ull);
		TS_ASSERT_EQUALS(movzx_be64(d2, 4), 0x431223A4ull);
		TS_ASSERT_EQUALS(movzx_be64(d2+3, 1), 0xA4ull);
	}

	void test_movsx()
	{
		const u8 d1[] = { 0x09, 0xFE };
		const u8 d2[] = { 0xD9, 0x2C, 0xDD, 0x8F };
		const u8 d3[] = { 0x92, 0x26, 0x88, 0xF1, 0x35, 0xAC, 0x01, 0x83 };

		TS_ASSERT_EQUALS(movsx_le64(d1, 1), (i64)0x09ull);
		TS_ASSERT_EQUALS(movsx_le64(d1, 2), (i64)0xFFFFFFFFFFFFFE09ull);
		TS_ASSERT_EQUALS(movsx_le64(d2, 4), (i64)0xFFFFFFFF8FDD2CD9ull);
		TS_ASSERT_EQUALS(movsx_le64(d3, 8), (i64)0x8301AC35F1882692ull);

		TS_ASSERT_EQUALS(movsx_be64(d1, 1), (i64)0x09ull);
		TS_ASSERT_EQUALS(movsx_be64(d1, 2), (i64)0x00000000000009FEull);
		TS_ASSERT_EQUALS(movsx_be64(d2, 4), (i64)0xFFFFFFFFD92CDD8Full);
		TS_ASSERT_EQUALS(movsx_be64(d3, 8), (i64)0x922688F135AC0183ull);
	}
};