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 16 #include <gtest/gtest.h> 17 #include "data_buf.h" 18 19 using namespace testing::ext; 20 using namespace testing; 21 using namespace OHOS::Media; 22 23 namespace OHOS { 24 namespace Media { 25 class DataBufTest : public testing::Test { 26 public: DataBufTest()27 DataBufTest() {} ~DataBufTest()28 ~DataBufTest() override {} 29 }; 30 31 /** 32 * @tc.name: DataBufTest_Write001 33 * @tc.desc: 验证DataBuf的write_uint8函数 34 * @tc.type: FUNC 35 */ 36 HWTEST_F(DataBufTest, DataBufTest_Write001, TestSize.Level3) 37 { 38 DataBuf dataBuf(10); 39 dataBuf.WriteUInt8(0, 123); 40 EXPECT_EQ(dataBuf.ReadUInt8(0), 123); 41 } 42 43 /** 44 * @tc.name: DataBufTest_GetUShort001 45 * @tc.desc: Validate the GetUShort function of DataBuf 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(DataBufTest, DataBufTest_GetUShort001, TestSize.Level3) 49 { 50 // Define test data 51 byte buf[2] = {0x01, 0x02}; 52 53 // Test the littleEndian case 54 uint16_t result = GetUShort(buf, littleEndian); 55 ASSERT_EQ(result, 0x0201); 56 57 // Test the bigEndian case 58 result = GetUShort(buf, bigEndian); 59 ASSERT_EQ(result, 0x0102); 60 } 61 62 /** 63 * @tc.name: DataBufTest_US2Data001 64 * @tc.desc: Validate the US2Data function of DataBuf 65 * @tc.type: FUNC 66 */ 67 HWTEST_F(DataBufTest, DataBufTest_US2Data001, TestSize.Level3) 68 { 69 // Define test data 70 byte buf[2]; 71 uint16_t value = 0x0201; 72 73 // Test the littleEndian case 74 US2Data(buf, value, littleEndian); 75 ASSERT_EQ(buf[0], 0x01); 76 ASSERT_EQ(buf[1], 0x02); 77 78 // Test the bigEndian case 79 US2Data(buf, value, bigEndian); 80 ASSERT_EQ(buf[0], 0x02); 81 ASSERT_EQ(buf[1], 0x01); 82 } 83 84 /** 85 * @tc.name: DataBufTest_UL2Data001 86 * @tc.desc: Validate the UL2Data function of DataBuf 87 * @tc.type: FUNC 88 */ 89 HWTEST_F(DataBufTest, DataBufTest_UL2Data001, TestSize.Level3) 90 { 91 // Define test data 92 byte buf[4]; 93 uint32_t value = 0x04030201; 94 95 // Test the littleEndian case 96 UL2Data(buf, value, littleEndian); 97 ASSERT_EQ(buf[0], 0x01); 98 ASSERT_EQ(buf[1], 0x02); 99 ASSERT_EQ(buf[2], 0x03); 100 ASSERT_EQ(buf[3], 0x04); 101 102 // Test the bigEndian case 103 UL2Data(buf, value, bigEndian); 104 ASSERT_EQ(buf[0], 0x04); 105 ASSERT_EQ(buf[1], 0x03); 106 ASSERT_EQ(buf[2], 0x02); 107 ASSERT_EQ(buf[3], 0x01); 108 } 109 110 /** 111 * @tc.name: DataBufTest_GetULong001 112 * @tc.desc: Validate the GetULong function of DataBuf 113 * @tc.type: FUNC 114 */ 115 HWTEST_F(DataBufTest, DataBufTest_GetULong001, TestSize.Level3) 116 { 117 // Define test data 118 byte buf[4] = {0x01, 0x02, 0x03, 0x04}; 119 120 // Test the littleEndian case 121 uint32_t result = GetULong(buf, littleEndian); 122 ASSERT_EQ(result, 0x04030201); 123 124 // Test the bigEndian case 125 result = GetULong(buf, bigEndian); 126 ASSERT_EQ(result, 0x01020304); 127 } 128 } // namespace Media 129 } // namespace OHOS