blob: 0db230f3ec56dd59b48b97ef52420f378a80de11 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "rtc_base/byte_order.h"
12
pbosc7c26a02017-01-02 08:42:32 -080013#include <stdint.h>
pbos7eb0e232017-01-02 07:32:25 -080014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17namespace rtc {
18
19// Test memory set functions put values into memory in expected order.
20TEST(ByteOrderTest, TestSet) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020021 uint8_t buf[8] = {0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022 Set8(buf, 0, 0xfb);
23 Set8(buf, 1, 0x12);
24 EXPECT_EQ(0xfb, buf[0]);
25 EXPECT_EQ(0x12, buf[1]);
26 SetBE16(buf, 0x1234);
27 EXPECT_EQ(0x12, buf[0]);
28 EXPECT_EQ(0x34, buf[1]);
29 SetLE16(buf, 0x1234);
30 EXPECT_EQ(0x34, buf[0]);
31 EXPECT_EQ(0x12, buf[1]);
32 SetBE32(buf, 0x12345678);
33 EXPECT_EQ(0x12, buf[0]);
34 EXPECT_EQ(0x34, buf[1]);
35 EXPECT_EQ(0x56, buf[2]);
36 EXPECT_EQ(0x78, buf[3]);
37 SetLE32(buf, 0x12345678);
38 EXPECT_EQ(0x78, buf[0]);
39 EXPECT_EQ(0x56, buf[1]);
40 EXPECT_EQ(0x34, buf[2]);
41 EXPECT_EQ(0x12, buf[3]);
42 SetBE64(buf, UINT64_C(0x0123456789abcdef));
43 EXPECT_EQ(0x01, buf[0]);
44 EXPECT_EQ(0x23, buf[1]);
45 EXPECT_EQ(0x45, buf[2]);
46 EXPECT_EQ(0x67, buf[3]);
47 EXPECT_EQ(0x89, buf[4]);
48 EXPECT_EQ(0xab, buf[5]);
49 EXPECT_EQ(0xcd, buf[6]);
50 EXPECT_EQ(0xef, buf[7]);
51 SetLE64(buf, UINT64_C(0x0123456789abcdef));
52 EXPECT_EQ(0xef, buf[0]);
53 EXPECT_EQ(0xcd, buf[1]);
54 EXPECT_EQ(0xab, buf[2]);
55 EXPECT_EQ(0x89, buf[3]);
56 EXPECT_EQ(0x67, buf[4]);
57 EXPECT_EQ(0x45, buf[5]);
58 EXPECT_EQ(0x23, buf[6]);
59 EXPECT_EQ(0x01, buf[7]);
60}
61
62// Test memory get functions get values from memory in expected order.
63TEST(ByteOrderTest, TestGet) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020064 uint8_t buf[8];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 buf[0] = 0x01u;
66 buf[1] = 0x23u;
67 buf[2] = 0x45u;
68 buf[3] = 0x67u;
69 buf[4] = 0x89u;
70 buf[5] = 0xabu;
71 buf[6] = 0xcdu;
72 buf[7] = 0xefu;
73 EXPECT_EQ(0x01u, Get8(buf, 0));
74 EXPECT_EQ(0x23u, Get8(buf, 1));
75 EXPECT_EQ(0x0123u, GetBE16(buf));
76 EXPECT_EQ(0x2301u, GetLE16(buf));
77 EXPECT_EQ(0x01234567u, GetBE32(buf));
78 EXPECT_EQ(0x67452301u, GetLE32(buf));
79 EXPECT_EQ(UINT64_C(0x0123456789abcdef), GetBE64(buf));
80 EXPECT_EQ(UINT64_C(0xefcdab8967452301), GetLE64(buf));
81}
82
83} // namespace rtc