From f84b6ba4e4b56283ee3c4027600ecd832836aed8 Mon Sep 17 00:00:00 2001 From: Rafal Walczyna Date: Wed, 3 Feb 2021 15:48:06 +0100 Subject: [PATCH] [ML][Common] Add unit tests to TensorsData::GetTensorRawData [==========] 17 tests from 2 test suites ran. (120 ms total) [ PASSED ] 17 tests. [Verification] Built successful. Tested on TM1 Change-Id: Ia73add4cab4500de078ce45933ca68f952975e4e Signed-off-by: Rafal Walczyna --- src/ml/ut/tensor_raw_data.cc | 198 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) diff --git a/src/ml/ut/tensor_raw_data.cc b/src/ml/ut/tensor_raw_data.cc index 6f2f795..abf4977 100644 --- a/src/ml/ut/tensor_raw_data.cc +++ b/src/ml/ut/tensor_raw_data.cc @@ -365,3 +365,201 @@ TEST_F(MlTensorRawDataTest, SetTensorRawData8) { ASSERT_FALSE(res) << "SetTensorRawData should fail"; ASSERT_EQ(res.error_code(), ErrorCode::INVALID_VALUES_ERR) << "Invalid error code"; } + +class MlTensorRawDataTestGet : public testing::Test { + public: + MlTensorRawDataTestGet() : tensors_info_manager_(&tensors_data_manager_) { + } + virtual void SetUp() override { + auto tensors_info = tensors_info_manager_.CreateTensorsInfo(); + unsigned int dim[] = {3, 2, 2, 2}; + tensors_info->AddTensorInfo("", ML_TENSOR_TYPE_UINT8, dim); + unsigned int dim_big[] = {5, 4, 3, 2}; + tensors_info->AddTensorInfo("", ML_TENSOR_TYPE_UINT8, dim_big); + tensors_data_ = tensors_info_manager_.CreateTensorsData(tensors_info); + + unsigned int location[] = {0, 0, 0, 0}; + unsigned int size[] = {3, 2, 2, 2}; + uint8_t arr_in[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; + TensorRawData raw_data_set{arr_in, 24}; + PlatformResult res = tensors_data_->SetTensorRawData(0, location, size, raw_data_set); + ASSERT_TRUE(res) << "SetTensorRawData failed"; + + unsigned int location_big[] = {0, 0, 0, 0}; + unsigned int size_big[] = {5, 4, 3, 2}; + + uint8_t arr_in_big[120] = {}; + for (int i = 0; i < 120; i++) { + arr_in_big[i] = i + 1; + } + + raw_data_set.data = arr_in_big; + raw_data_set.size_in_bytes = 120; + res = tensors_data_->SetTensorRawData(1, location_big, size_big, raw_data_set); + ASSERT_TRUE(res) << "SetTensorRawData failed"; + } + + virtual void TearDown() override { + } + + TensorsDataManager tensors_data_manager_; + TensorsInfoManager tensors_info_manager_; + TensorsData* tensors_data_; +}; + +// 1, 2, 3 +// 4, 5, 6 +// +// 7, 8, 9 +// 10, 11, 12 +// ------------ +// 13, 14, 15 +// 16, 17, 18 +// +// 19, 20, 21 +// 22, 23, 24 + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData1) { + ScopeLogger("GetTensorRawData1"); + + unsigned int location_get[4] = {0, 0, 0, 0}; + unsigned int size_get[4] = {1, 1, 1, 1}; + unsigned int expected_dim[4] = {1, 1, 1, 1}; + size_t expected_size = 1; + uint8_t expected_arr[1] = {1}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(0, location_get, size_get, &raw_data_get); + ASSERT_TRUE(res) << "GetTensorRawData failed"; + + for (int i = 0; i < ML_TENSOR_RANK_LIMIT; i++) { + EXPECT_EQ(raw_data_get.shape[i], expected_dim[i]) << "Invalid value at dim " << i; + } + EXPECT_EQ(raw_data_get.size_in_bytes, expected_size); + for (size_t i = 0; i < expected_size; i++) { + EXPECT_EQ(raw_data_get.data[i], expected_arr[i]) << "Mismatch at pos " << i; + } +} + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData2) { + ScopeLogger("GetTensorRawData2"); + + unsigned int location_get[4] = {0, 0, 1, 0}; + unsigned int size_get[4] = {3, 2, 2, 2}; + unsigned int expected_dim[4] = {3, 2, 1, 2}; + size_t expected_size = 12; + uint8_t expected_arr[12] = {7, 8, 9, 10, 11, 12, 19, 20, 21, 22, 23, 24}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(0, location_get, size_get, &raw_data_get); + ASSERT_TRUE(res) << "GetTensorRawData failed"; + + for (int i = 0; i < ML_TENSOR_RANK_LIMIT; i++) { + EXPECT_EQ(raw_data_get.shape[i], expected_dim[i]) << "Invalid value at dim " << i; + } + EXPECT_EQ(raw_data_get.size_in_bytes, expected_size); + for (size_t i = 0; i < expected_size; i++) { + EXPECT_EQ(raw_data_get.data[i], expected_arr[i]) << "Mismatch at pos " << i; + } +} + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData3) { + ScopeLogger("GetTensorRawData3"); + + unsigned int location_get[4] = {1, 1, 1, 1}; + unsigned int size_get[4] = {2, 2, 2, 2}; + unsigned int expected_dim[4] = {2, 1, 1, 1}; + size_t expected_size = 2; + uint8_t expected_arr[2] = {23, 24}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(0, location_get, size_get, &raw_data_get); + ASSERT_TRUE(res) << "GetTensorRawData failed"; + + for (int i = 0; i < ML_TENSOR_RANK_LIMIT; i++) { + EXPECT_EQ(raw_data_get.shape[i], expected_dim[i]) << "Invalid value at dim " << i; + } + EXPECT_EQ(raw_data_get.size_in_bytes, expected_size); + for (size_t i = 0; i < expected_size; i++) { + EXPECT_EQ(raw_data_get.data[i], expected_arr[i]) << "Mismatch at pos " << i; + } +} + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData4) { + ScopeLogger("GetTensorRawData4"); + + unsigned int location_get[4] = {2, 1, 0, 1}; + unsigned int size_get[4] = {3, 2, 2, 2}; + unsigned int expected_dim[4] = {1, 1, 2, 1}; + size_t expected_size = 2; + uint8_t expected_arr[2] = {18, 24}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(0, location_get, size_get, &raw_data_get); + ASSERT_TRUE(res) << "GetTensorRawData failed"; + + for (int i = 0; i < ML_TENSOR_RANK_LIMIT; i++) { + EXPECT_EQ(raw_data_get.shape[i], expected_dim[i]) << "Invalid value at dim " << i; + } + EXPECT_EQ(raw_data_get.size_in_bytes, expected_size); + for (size_t i = 0; i < expected_size; i++) { + EXPECT_EQ(raw_data_get.data[i], expected_arr[i]) << "Mismatch at pos " << i; + } +} + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData5) { + ScopeLogger("GetTensorRawData5"); + + unsigned int location_get[4] = {3, 2, 1, 0}; + unsigned int size_get[4] = {1, 1, 2, 2}; + unsigned int expected_dim[4] = {1, 1, 2, 2}; + size_t expected_size = 4; + uint8_t expected_arr[4] = {34, 54, 94, 114}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(1, location_get, size_get, &raw_data_get); + ASSERT_TRUE(res) << "GetTensorRawData failed"; + + for (int i = 0; i < ML_TENSOR_RANK_LIMIT; i++) { + EXPECT_EQ(raw_data_get.shape[i], expected_dim[i]) << "Invalid value at dim " << i; + } + EXPECT_EQ(raw_data_get.size_in_bytes, expected_size); + for (size_t i = 0; i < expected_size; i++) { + EXPECT_EQ(raw_data_get.data[i], expected_arr[i]) << "Mismatch at pos " << i; + } +} + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData6) { + ScopeLogger("GetTensorRawData6"); + + unsigned int location_get[4] = {4, 1, 0, 1}; + unsigned int size_get[4] = {5, 4, 3, 2}; + unsigned int expected_dim[4] = {1, 3, 3, 1}; + size_t expected_size = 9; + uint8_t expected_arr[9] = {70, 75, 80, 90, 95, 100, 110, 115, 120}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(1, location_get, size_get, &raw_data_get); + ASSERT_TRUE(res) << "GetTensorRawData failed"; + + for (int i = 0; i < ML_TENSOR_RANK_LIMIT; i++) { + EXPECT_EQ(raw_data_get.shape[i], expected_dim[i]) << "Invalid value at dim " << i; + } + EXPECT_EQ(raw_data_get.size_in_bytes, expected_size); + for (size_t i = 0; i < expected_size; i++) { + EXPECT_EQ(raw_data_get.data[i], expected_arr[i]) << "Mismatch at pos " << i; + } +} + +TEST_F(MlTensorRawDataTestGet, GetTensorRawData7) { + ScopeLogger("GetTensorRawData7"); + + unsigned int location_get[4] = {4, 1, 0, 1}; + unsigned int size_get[4] = {5, 4, 3, 2}; + + TensorRawData raw_data_get; + PlatformResult res = tensors_data_->GetTensorRawData(0, location_get, size_get, &raw_data_get); + ASSERT_FALSE(res) << "GetTensorRawData should fail"; + ASSERT_EQ(res.error_code(), ErrorCode::INVALID_VALUES_ERR) << "Invalid error code"; +} -- 2.7.4