1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <linux/swab.h>
20 
21 // This test makes sure that references to all of the kernel swab
22 // macros/inline functions that are exported work properly.
23 // Verifies that any kernel header updates do not break these macros.
TEST(linux_swab,smoke)24 TEST(linux_swab, smoke) {
25   EXPECT_EQ(0x3412U, __swab16(0x1234));
26   EXPECT_EQ(0x78563412U, __swab32(0x12345678U));
27   EXPECT_EQ(0xbaefcdab78563412ULL, __swab64(0x12345678abcdefbaULL));
28 
29   __u16 bval16 = 0x1234;
30   EXPECT_EQ(0x3412U, __swab16p(&bval16));
31   __u32 bval32 = 0x12345678U;
32   EXPECT_EQ(0x78563412U, __swab32p(&bval32));
33   __u64 bval64 = 0x12345678abcdefbaULL;
34   EXPECT_EQ(0xbaefcdab78563412ULL, __swab64p(&bval64));
35 
36   __u16 sval16 = 0x1234;
37   __swab16s(&sval16);
38   EXPECT_EQ(0x3412U, sval16);
39   __u32 sval32 = 0x12345678U;
40   __swab32s(&sval32);
41   EXPECT_EQ(0x78563412U, sval32);
42   __u64 sval64 = 0x12345678abcdefbaULL;
43   __swab64s(&sval64);
44   EXPECT_EQ(0xbaefcdab78563412ULL, sval64);
45 
46   EXPECT_EQ(0x56781234U, __swahw32(0x12345678U));
47   EXPECT_EQ(0x34127856U, __swahb32(0x12345678U));
48 
49   __u32 hval32 = 0x12345678U;
50   EXPECT_EQ(0x56781234U, __swahw32p(&hval32));
51   hval32 = 0x12345678U;
52   EXPECT_EQ(0x34127856U, __swahb32p(&hval32));
53 
54   __u32 hsval32 = 0x12345678U;
55   __swahw32s(&hsval32);
56   EXPECT_EQ(0x56781234U, hsval32);
57   hsval32 = 0x12345678U;
58   __swahb32s(&hsval32);
59   EXPECT_EQ(0x34127856U, hsval32);
60 }
61