|
12 | 12 | #include <executorch/runtime/platform/runtime.h>
|
13 | 13 | #include <executorch/test/utils/DeathTest.h>
|
14 | 14 | #include <cmath>
|
| 15 | +#include <cstring> |
15 | 16 | #include <limits>
|
16 | 17 |
|
17 | 18 | using namespace ::testing;
|
18 | 19 | using executorch::aten::ScalarType;
|
19 | 20 | using executorch::aten::Tensor;
|
20 | 21 | using executorch::runtime::extract_scalar_tensor;
|
| 22 | +using executorch::runtime::kTensorDimensionLimit; |
| 23 | +using executorch::runtime::kTensorShapeStringSizeLimit; |
| 24 | +using executorch::runtime::Span; |
| 25 | +using executorch::runtime::tensor_shape_to_c_string; |
| 26 | +using executorch::runtime::internal::kMaximumPrintableTensorShapeElement; |
21 | 27 | using executorch::runtime::testing::TensorFactory;
|
22 | 28 |
|
23 | 29 | class TensorUtilTest : public ::testing::Test {
|
@@ -605,3 +611,74 @@ TEST_F(TensorUtilTest, SameShapesDifferentDimOrder) {
|
605 | 611 | EXPECT_FALSE(tensors_have_same_dim_order(a, c, b));
|
606 | 612 | EXPECT_FALSE(tensors_have_same_dim_order(c, b, a));
|
607 | 613 | }
|
| 614 | + |
| 615 | +TEST_F(TensorUtilTest, TensorShapeToCStringBasic) { |
| 616 | + std::array<executorch::aten::SizesType, 3> sizes = {123, 456, 789}; |
| 617 | + auto str = tensor_shape_to_c_string( |
| 618 | + Span<const executorch::aten::SizesType>(sizes.data(), sizes.size())); |
| 619 | + EXPECT_STREQ(str.data(), "(123, 456, 789)"); |
| 620 | + |
| 621 | + std::array<executorch::aten::SizesType, 1> one_size = {1234567890}; |
| 622 | + str = tensor_shape_to_c_string(Span<const executorch::aten::SizesType>( |
| 623 | + one_size.data(), one_size.size())); |
| 624 | + EXPECT_STREQ(str.data(), "(1234567890)"); |
| 625 | +} |
| 626 | + |
| 627 | +TEST_F(TensorUtilTest, TensorShapeToCStringNegativeItems) { |
| 628 | + std::array<executorch::aten::SizesType, 4> sizes = {-1, -3, -2, 4}; |
| 629 | + auto str = tensor_shape_to_c_string( |
| 630 | + Span<const executorch::aten::SizesType>(sizes.data(), sizes.size())); |
| 631 | + EXPECT_STREQ(str.data(), "(ERR, ERR, ERR, 4)"); |
| 632 | + |
| 633 | + std::array<executorch::aten::SizesType, 1> one_size = {-1234567890}; |
| 634 | + str = tensor_shape_to_c_string(Span<const executorch::aten::SizesType>( |
| 635 | + one_size.data(), one_size.size())); |
| 636 | + if constexpr (std::numeric_limits<executorch::aten::SizesType>::is_signed) { |
| 637 | + EXPECT_STREQ(str.data(), "(ERR)"); |
| 638 | + } else { |
| 639 | + EXPECT_EQ(str.data(), "(" + std::to_string(one_size[0]) + ")"); |
| 640 | + } |
| 641 | +} |
| 642 | +TEST_F(TensorUtilTest, TensorShapeToCStringMaximumElement) { |
| 643 | + std::array<executorch::aten::SizesType, 3> sizes = { |
| 644 | + 123, std::numeric_limits<executorch::aten::SizesType>::max(), 789}; |
| 645 | + auto str = tensor_shape_to_c_string( |
| 646 | + Span<const executorch::aten::SizesType>(sizes.data(), sizes.size())); |
| 647 | + std::ostringstream expected; |
| 648 | + expected << '('; |
| 649 | + for (const auto elem : sizes) { |
| 650 | + expected << elem << ", "; |
| 651 | + } |
| 652 | + auto expected_str = expected.str(); |
| 653 | + expected_str.pop_back(); |
| 654 | + expected_str.back() = ')'; |
| 655 | + EXPECT_EQ(str.data(), expected_str); |
| 656 | +} |
| 657 | + |
| 658 | +TEST_F(TensorUtilTest, TensorShapeToCStringMaximumLength) { |
| 659 | + std::array<executorch::aten::SizesType, kTensorDimensionLimit> sizes; |
| 660 | + std::fill(sizes.begin(), sizes.end(), kMaximumPrintableTensorShapeElement); |
| 661 | + |
| 662 | + auto str = tensor_shape_to_c_string( |
| 663 | + Span<const executorch::aten::SizesType>(sizes.data(), sizes.size())); |
| 664 | + |
| 665 | + std::ostringstream expected; |
| 666 | + expected << '(' << kMaximumPrintableTensorShapeElement; |
| 667 | + for (int ii = 0; ii < kTensorDimensionLimit - 1; ++ii) { |
| 668 | + expected << ", " << kMaximumPrintableTensorShapeElement; |
| 669 | + } |
| 670 | + expected << ')'; |
| 671 | + auto expected_str = expected.str(); |
| 672 | + |
| 673 | + EXPECT_EQ(expected_str, str.data()); |
| 674 | +} |
| 675 | + |
| 676 | +TEST_F(TensorUtilTest, TensorShapeToCStringExceedsDimensionLimit) { |
| 677 | + std::array<executorch::aten::SizesType, kTensorDimensionLimit + 1> sizes; |
| 678 | + std::fill(sizes.begin(), sizes.end(), kMaximumPrintableTensorShapeElement); |
| 679 | + |
| 680 | + auto str = tensor_shape_to_c_string( |
| 681 | + Span<const executorch::aten::SizesType>(sizes.data(), sizes.size())); |
| 682 | + |
| 683 | + EXPECT_STREQ(str.data(), "(ERR: tensor ndim exceeds limit, can't happen)"); |
| 684 | +} |
0 commit comments