Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include <estd/multi_slice.h> 4 : 5 : #include <gtest/gtest.h> 6 : 7 3 : TEST(multi_slice_Example, Construction_and_operations) 8 : { 9 : // [EXAMPLE_MULTI_SLICE_CONSTRUCTION_AND_OPERATIONS_START] 10 1 : using MS816 = estd::multi_slice<uint8_t, uint16_t const>; 11 : 12 1 : uint8_t u8_array[10U] = {0}; 13 1 : uint16_t const u16_array[10U] = {0}; 14 : 15 : // Creating a multi_slice of size 10 of different types. 16 1 : MS816 u8u16 = MS816(u8_array, u16_array); 17 : 18 : // Getting the size of multi_slice. 19 1 : EXPECT_EQ(10U, u8u16.size()); 20 1 : EXPECT_EQ(10U, u8u16.get<uint8_t>().size()); 21 1 : EXPECT_EQ(10U, u8u16.get<uint16_t const>().size()); 22 : 23 : // Getting data pointer from slice. 24 1 : EXPECT_EQ(u8_array, u8u16.data<uint8_t>()); 25 1 : EXPECT_EQ(u8_array, u8u16.data<uint8_t const>()); 26 1 : EXPECT_EQ(u16_array, u8u16.data<uint16_t>()); 27 1 : EXPECT_EQ(u16_array, u8u16.data<uint16_t const>()); 28 : 29 : // at function returns reference to an item. 30 1 : EXPECT_EQ(&u8_array[2], &u8u16.at<uint8_t>(2)); 31 1 : EXPECT_EQ(&u16_array[5], &u8u16.at<uint16_t>(5)); 32 : 33 : // idx function gives the position of an item. 34 1 : EXPECT_EQ(3U, u8u16.idx(&u8_array[3])); 35 1 : EXPECT_EQ(4U, u8u16.idx(&u16_array[4])); 36 : 37 : // advance function reduces the size by the value passed in the argument. 38 1 : u8u16.advance(6U); 39 1 : EXPECT_EQ(4U, u8u16.size()); 40 : 41 : // advance function adjusts the data pointer. 42 1 : EXPECT_EQ(u8_array + 6U, u8u16.get<uint8_t>().data()); 43 1 : EXPECT_EQ(u16_array + 6U, u8u16.get<uint16_t const>().data()); 44 : 45 : // trim function reduces the size by the value passed in the argument. 46 1 : u8u16.trim(2U); 47 1 : EXPECT_EQ(2U, u8u16.size()); 48 : 49 : // trim function sets the size to zero if passed argument is bigger than the size. 50 1 : u8u16.trim(3); 51 1 : EXPECT_EQ(0U, u8u16.size()); 52 : // [EXAMPLE_MULTI_SLICE_CONSTRUCTION_AND_OPERATIONS_END] 53 1 : }