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 using namespace std; 19 20 namespace { TEST(EndianUtilTest,IsBigEndianTest)21 TEST(EndianUtilTest, IsBigEndianTest) 22 { 23 // 测试 IsBigEndian() 方法是否返回正确结果 24 bool ret = EndianUtil::IsBigEndian(); 25 EXPECT_FALSE(ret); // 当前环境为小端序 26 } 27 TEST(EndianUtilTest,ToNetworkEndianTest)28 TEST(EndianUtilTest, ToNetworkEndianTest) 29 { 30 // 测试 ToNetworkEndian() 方法是否正确转换字节序 31 uint32_t value = 0x12345678; 32 uint32_t networkValue = EndianUtil::ToNetworkEndian(value); 33 if (EndianUtil::IsBigEndian()) { 34 EXPECT_EQ(networkValue, value); // 当前为大端环境,不转换 35 } else { 36 EXPECT_EQ(networkValue, 0x78563412); // 在小端序环境下应该转换为 0x78563412 37 } 38 } 39 }