Publishing R3
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / tensor_desc_test.cpp
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #include <ie_layouts.h>
7 #include <ie_blob.h>
8 #include <gtest/gtest.h>
9 #include <random>
10 #include <chrono>
11
12 #include "mock_allocator.hpp"
13
14 #include <cpp/ie_cnn_net_reader.h>
15 #include <gmock/gmock-spec-builders.h>
16
17 #ifdef WIN32
18 #define UNUSED
19 #else
20 #define UNUSED  __attribute__((unused))
21 #endif
22
23 using namespace ::testing;
24 using namespace std;
25 using namespace InferenceEngine;
26
27 class TensorDescTests: public ::testing::Test {
28 protected:
29     virtual void TearDown() {
30     }
31
32     virtual void SetUp() {
33     }
34
35 public:
36
37 };
38
39 TEST_F(TensorDescTests, CreateBlobWithIncorrectLayout) {
40     ASSERT_THROW(make_shared_blob<float>(Precision::FP32, Layout::NC, {1, 3, 32}), details::InferenceEngineException);
41 }
42
43 TEST_F(TensorDescTests, CreateEmptyBlob) {
44     Blob::Ptr blob = make_shared_blob<float>(Precision::FP32);
45
46     ASSERT_EQ(blob->getTensorDesc().getLayout(), Layout::NCHW);
47 }
48
49 TEST_F(TensorDescTests, CreateBlockedBlob) {
50     TensorDesc desc(Precision::FP32, {1, 4, 2, 1}, {{1, 2, 2, 1, 2}, {0, 1, 2, 3, 1}});
51     float data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
52     Blob::Ptr blockedBlob = make_shared_blob<float>(desc, data);
53     Blob::Ptr nchwBlob = make_shared_blob<float>({Precision::FP32, {1, 4, 2, 1}, Layout::NCHW}, data);
54     ASSERT_NE(blockedBlob->getTensorDesc().offset(5), nchwBlob->getTensorDesc().offset(5));
55     ASSERT_EQ(6, blockedBlob->getTensorDesc().offset(5));
56     ASSERT_EQ(5, nchwBlob->getTensorDesc().offset(5));
57     ASSERT_EQ(Layout::NCHW, nchwBlob->layout());
58     ASSERT_EQ(Layout::BLOCKED, blockedBlob->layout());
59 }
60
61 TEST_F(TensorDescTests, CompareNHWCandNCHWLayouts) {
62     TensorDesc descNCHW(Precision::FP32, {1, 3, 4, 2}, Layout::NCHW);
63     TensorDesc descNHWC(Precision::FP32, {1, 3, 4, 2}, Layout::NHWC);
64     SizeVector nchw = {0, 1, 2, 3};
65     SizeVector nhwc = {0, 2, 3, 1};
66
67     ASSERT_NE(descNCHW, descNHWC);
68     ASSERT_NE(descNCHW.getBlockingDesc(), descNHWC.getBlockingDesc());
69     ASSERT_NE(descNCHW.getBlockingDesc().getOrder(), descNHWC.getBlockingDesc().getOrder());
70     ASSERT_EQ(descNCHW.getBlockingDesc().getOrder(), nchw);
71     ASSERT_EQ(descNHWC.getBlockingDesc().getOrder(), nhwc);
72 }