File: test_shape.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (84 lines) | stat: -rw-r--r-- 2,253 bytes parent folder | download | duplicates (3)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <gtest/gtest.h>

#include <sstream>

#include <torch/csrc/lazy/core/shape.h>

namespace torch {
namespace lazy {

TEST(ShapeTest, Basic1) {
  auto shape = Shape();

  EXPECT_STREQ(shape.to_string().c_str(), "UNKNOWN_SCALAR[]");
  EXPECT_EQ(shape.scalar_type(), c10::ScalarType::Undefined);
  EXPECT_EQ(shape.dim(), 0);
  EXPECT_TRUE(shape.sizes().empty());
  EXPECT_THROW(shape.size(0), std::out_of_range);
}

TEST(ShapeTest, Basic2) {
  auto shape = Shape(c10::ScalarType::Float, {1, 2, 3});

  EXPECT_EQ(shape.numel(), 6);
  EXPECT_STREQ(shape.to_string().c_str(), "Float[1,2,3]");
  EXPECT_EQ(shape.scalar_type(), c10::ScalarType::Float);
  EXPECT_EQ(shape.dim(), 3);
  EXPECT_EQ(shape.sizes().size(), 3);
  for (int64_t i = 0; i < shape.dim(); i++) {
    EXPECT_EQ(shape.sizes()[i], i + 1);
    EXPECT_EQ(shape.size(i), i + 1);
  }
}

TEST(ShapeTest, Basic3) {
  auto shape = Shape(c10::ScalarType::Float, {});

  EXPECT_STREQ(shape.to_string().c_str(), "Float[]");
  EXPECT_EQ(shape.scalar_type(), c10::ScalarType::Float);
  EXPECT_EQ(shape.dim(), 0);
  // this is surprising, but it's in line with how 0-D tensors behave
  EXPECT_EQ(shape.numel(), 1);
  EXPECT_TRUE(shape.sizes().empty());
  EXPECT_THROW(shape.size(0), std::out_of_range);
}

TEST(ShapeTest, SetScalarType) {
  auto shape = Shape();

  shape.set_scalar_type(c10::ScalarType::Long);
  EXPECT_EQ(shape.scalar_type(), c10::ScalarType::Long);
}

TEST(ShapeTest, SetSize) {
  auto shape1 = Shape();
  EXPECT_THROW(shape1.set_size(0, 0), std::out_of_range);

  auto shape2 = Shape(c10::ScalarType::Float, {1, 2, 3});
  shape2.set_size(0, 3);
  EXPECT_EQ(shape2.sizes()[0], 3);
  EXPECT_EQ(shape2.size(0), 3);
}

TEST(ShapeTest, Equal) {
  auto shape1 = Shape(c10::ScalarType::Float, {});
  auto shape2 = Shape(c10::ScalarType::Float, {1, 2, 3});
  auto shape3 = Shape(c10::ScalarType::Long, {1, 2, 3});
  auto shape4 = Shape(c10::ScalarType::Float, {1, 2, 3});

  EXPECT_FALSE(shape1 == shape2);
  EXPECT_FALSE(shape2 == shape3);
  EXPECT_FALSE(shape1 == shape3);
  EXPECT_TRUE(shape2 == shape2);
}

TEST(ShapeTest, Ostream) {
  auto shape = Shape();
  std::stringstream ss;
  ss << shape;

  EXPECT_EQ(shape.to_string(), ss.str());
}

} // namespace lazy
} // namespace torch