1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "EndianUtil.h" 16 #include <string> 17 #include "gtest/gtest.h" 18 19 namespace { TEST(EndianUtilTest,IsBigEndianTest)20 TEST(EndianUtilTest, IsBigEndianTest) 21 { 22 // 测试 IsBigEndian() 方法是否返回正确结果 23 bool ret = EndianUtil::IsBigEndian(); 24 EXPECT_FALSE(ret); // 当前环境为小端序 25 } 26 TEST(EndianUtilTest,ToNetworkEndianTest)27 TEST(EndianUtilTest, ToNetworkEndianTest) 28 { 29 // 测试 ToNetworkEndian() 方法是否正确转换字节序 30 uint32_t value = 0x12345678; 31 uint32_t networkValue = EndianUtil::ToNetworkEndian(value); 32 if (EndianUtil::IsBigEndian()) { 33 EXPECT_EQ(networkValue, value); // 当前为大端环境,不转换 34 } else { 35 EXPECT_EQ(networkValue, 0x78563412); // 在小端序环境下应该转换为 0x78563412 36 } 37 } 38 }