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 129 /** 130 * @tc.name: ReadUInt8Test001 131 * @tc.desc: test the ReadUInt8 function of DataBuf 132 * @tc.type: FUNC 133 */ 134 HWTEST_F(DataBufTest, ReadUInt8Test001, TestSize.Level3) 135 { 136 GTEST_LOG_(INFO) << "DataBufTest: ReadUInt8Test001 start"; 137 size_t size = 2; 138 size_t offset = 4; 139 DataBuf dataBuf(size); 140 uint8_t ret = dataBuf.ReadUInt8(offset); 141 ASSERT_EQ(ret, 0); 142 GTEST_LOG_(INFO) << "DataBufTest: ReadUInt8Test001 end"; 143 } 144 145 /** 146 * @tc.name: ReadUInt32Test001 147 * @tc.desc: test the ReadUInt32 function of DataBuf 148 * @tc.type: FUNC 149 */ 150 HWTEST_F(DataBufTest, ReadUInt32Test001, TestSize.Level3) 151 { 152 GTEST_LOG_(INFO) << "DataBufTest: ReadUInt32Test001 start"; 153 size_t size = 2; 154 size_t offset = 4; 155 ByteOrder byteOrder = littleEndian; 156 DataBuf dataBuf(size); 157 uint32_t ret = dataBuf.ReadUInt32(offset, byteOrder); 158 ASSERT_EQ(ret, 0); 159 GTEST_LOG_(INFO) << "DataBufTest: ReadUInt32Test001 end"; 160 } 161 162 /** 163 * @tc.name: CmpBytesTest001 164 * @tc.desc: test the CmpBytes function of DataBuf 165 * @tc.type: FUNC 166 */ 167 HWTEST_F(DataBufTest, CmpBytesTest001, TestSize.Level3) 168 { 169 GTEST_LOG_(INFO) << "DataBufTest: CmpBytesTest001 start"; 170 size_t size = 2; 171 size_t offset = 4; 172 const void *buf = nullptr; 173 size_t bufsize = 8; 174 DataBuf dataBuf(size); 175 int ret = dataBuf.CmpBytes(offset, buf, bufsize); 176 ASSERT_EQ(ret, -1); 177 GTEST_LOG_(INFO) << "DataBufTest: CmpBytesTest001 end"; 178 } 179 180 /** 181 * @tc.name: CDataTest001 182 * @tc.desc: test the CData function of DataBuf 183 * @tc.type: FUNC 184 */ 185 HWTEST_F(DataBufTest, CDataTest001, TestSize.Level3) 186 { 187 GTEST_LOG_(INFO) << "DataBufTest: CDataTest001 start"; 188 size_t size = 2; 189 size_t offset = 4; 190 DataBuf dataBuf(size); 191 const byte *ret = dataBuf.CData(offset); 192 ASSERT_EQ(ret, nullptr); 193 GTEST_LOG_(INFO) << "DataBufTest: CDataTest001 end"; 194 } 195 } // namespace Media 196 } // namespace OHOS