forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshape.cpp
More file actions
71 lines (54 loc) · 1.92 KB
/
shape.cpp
File metadata and controls
71 lines (54 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "openvino/core/shape.hpp"
#include <gtest/gtest.h>
#include <memory>
#include "openvino/core/shape_util.hpp"
using namespace std;
using namespace ov;
TEST(shape, test_shape_size) {
ASSERT_EQ(1, shape_size(ov::Shape{}));
ASSERT_EQ(2 * 3 * 5, shape_size(Shape{2, 3, 5}));
}
TEST(shape, test_shape_strides) {
ASSERT_EQ(Strides{}, row_major_strides(Shape{}));
ASSERT_EQ(Strides{1}, row_major_strides(Shape{3}));
ASSERT_EQ((Strides{7, 1}), row_major_strides(Shape{2, 7}));
ASSERT_EQ((Strides{84, 12, 1}), row_major_strides(Shape{5, 7, 12}));
}
TEST(shape, at) {
const auto shape = ov::Shape{100, 200, 5, 6, 7};
EXPECT_EQ(shape.at(2), 5);
EXPECT_EQ(shape.at(0), 100);
EXPECT_EQ(shape.at(1), 200);
EXPECT_EQ(shape.at(4), 7);
EXPECT_EQ(shape.at(-3), 5);
EXPECT_EQ(shape.at(-5), 100);
EXPECT_EQ(shape.at(-4), 200);
EXPECT_EQ(shape.at(-1), 7);
}
TEST(shape, subscribe_operator) {
const auto shape = ov::Shape{100, 200, 5, 6, 7};
EXPECT_EQ(shape[2], 5);
EXPECT_EQ(shape[0], 100);
EXPECT_EQ(shape[1], 200);
EXPECT_EQ(shape[4], 7);
EXPECT_EQ(shape[-3], 5);
EXPECT_EQ(shape[-5], 100);
EXPECT_EQ(shape[-4], 200);
EXPECT_EQ(shape[-1], 7);
}
TEST(shape, at_throw_exception) {
auto shape = ov::Shape{1, 2, 3, 4, 5, 6, 7};
EXPECT_THROW(shape.at(7), ov::Exception);
EXPECT_THROW(shape.at(1000), ov::Exception);
EXPECT_THROW(shape.at(-8), ov::Exception);
EXPECT_THROW(shape.at(-80000), ov::Exception);
}
TEST(shape, shape_size_overflow) {
constexpr auto max = std::numeric_limits<size_t>::max();
EXPECT_EQ(ov::util::shape_size_safe({1, 2, 3, 4, 5, 6, 7}).value(), (2 * 3 * 4 * 5 * 6 * 7));
EXPECT_EQ(ov::util::shape_size_safe({3, max / 4, 2}), std::nullopt);
EXPECT_EQ(ov::util::shape_size_safe({3, max / 4}).value(), (3 * (max / 4)));
}