| 50 | } |
| 51 | |
| 52 | TEST(Tensor, Basic) { |
| 53 | Tensor nd = Empty({1, 2, 3}, DLDataType({kDLFloat, 32, 1}), DLDevice({kDLCPU, 0})); |
| 54 | Shape shape = nd.shape(); |
| 55 | Shape strides = nd.strides(); |
| 56 | EXPECT_EQ(shape.size(), 3); |
| 57 | EXPECT_EQ(shape[0], 1); |
| 58 | EXPECT_EQ(shape[1], 2); |
| 59 | EXPECT_EQ(shape[2], 3); |
| 60 | EXPECT_EQ(strides.size(), 3); |
| 61 | EXPECT_EQ(strides[0], 6); |
| 62 | EXPECT_EQ(strides[1], 3); |
| 63 | EXPECT_EQ(strides[2], 1); |
| 64 | EXPECT_EQ(nd.dtype(), DLDataType({kDLFloat, 32, 1})); |
| 65 | for (int64_t i = 0; i < shape.Product(); ++i) { |
| 66 | reinterpret_cast<float*>(nd.data_ptr())[i] = static_cast<float>(i); |
| 67 | } |
| 68 | |
| 69 | EXPECT_EQ(nd.numel(), 6); |
| 70 | EXPECT_EQ(nd.ndim(), 3); |
| 71 | EXPECT_EQ(nd.data_ptr(), nd.GetDLTensorPtr()->data); |
| 72 | |
| 73 | Any any0 = nd; |
| 74 | Tensor nd2 = any0.as<Tensor>().value(); // NOLINT(bugprone-unchecked-optional-access) |
| 75 | EXPECT_EQ(nd2.dtype(), DLDataType({kDLFloat, 32, 1})); |
| 76 | for (int64_t i = 0; i < shape.Product(); ++i) { |
| 77 | EXPECT_EQ(reinterpret_cast<float*>(nd2.data_ptr())[i], i); |
| 78 | } |
| 79 | |
| 80 | EXPECT_EQ(nd.IsContiguous(), true); |
| 81 | EXPECT_EQ(nd2.use_count(), 3); |
| 82 | |
| 83 | Tensor nd3 = EmptyStrided({2, 3}, {1, 2}, DLDataType({kDLFloat, 32, 1}), DLDevice({kDLCPU, 0})); |
| 84 | Shape shape3 = nd3.shape(); |
| 85 | Shape strides3 = nd3.strides(); |
| 86 | EXPECT_EQ(shape3.size(), 2); |
| 87 | EXPECT_EQ(shape3[0], 2); |
| 88 | EXPECT_EQ(shape3[1], 3); |
| 89 | EXPECT_EQ(strides3.size(), 2); |
| 90 | EXPECT_EQ(strides3[0], 1); |
| 91 | EXPECT_EQ(strides3[1], 2); |
| 92 | } |
| 93 | |
| 94 | TEST(Tensor, EmptyTensorIsContiguous) { |
| 95 | // An empty tensor (any shape dim == 0) is trivially contiguous regardless of |
nothing calls this directly
no test coverage detected