8
8
9
9
#include < executorch/runtime/core/portable_type/tensor_impl.h>
10
10
11
- #include < executorch/runtime/core/exec_aten/util/tensor_util.h>
12
- #include < executorch/runtime/platform/runtime.h>
13
-
14
11
#include < gtest/gtest.h>
15
12
#include < random>
16
13
14
+ #include < executorch/runtime/core/exec_aten/util/tensor_util.h>
15
+ #include < executorch/runtime/platform/runtime.h>
16
+ #include < executorch/test/utils/DeathTest.h>
17
+
17
18
using namespace ::testing;
18
19
19
20
namespace torch {
@@ -29,7 +30,7 @@ class TensorImplTest : public ::testing::Test {
29
30
void SetUp () override {
30
31
// Since these tests cause ET_LOG to be called, the PAL must be initialized
31
32
// first.
32
- torch::executor:: runtime_init ();
33
+ runtime_init ();
33
34
}
34
35
};
35
36
@@ -370,5 +371,81 @@ TEST_F(TensorImplTest, TestWriteRead) {
370
371
EXPECT_EQ (y[0 ], 22.0 );
371
372
}
372
373
374
+ TEST_F (TensorImplTest, TestInvalidScalarType) {
375
+ SizesType sizes[2 ] = {3 , 2 };
376
+ ET_EXPECT_DEATH (TensorImpl t (static_cast <ScalarType>(-1 ), 2 , sizes), " " );
377
+ }
378
+
379
+ TEST_F (TensorImplTest, TestNegativeDimension) {
380
+ SizesType sizes[2 ] = {3 , 2 };
381
+ ET_EXPECT_DEATH (TensorImpl t (ScalarType::Float, -1 , sizes), " " );
382
+ }
383
+
384
+ TEST_F (TensorImplTest, TestNullSizesNonZeroDim) {
385
+ ET_EXPECT_DEATH (TensorImpl t (ScalarType::Float, 2 , nullptr ), " " );
386
+ }
387
+
388
+ TEST_F (TensorImplTest, TestNonNegativeSizes) {
389
+ SizesType sizes[2 ] = {3 , -2 };
390
+ ET_EXPECT_DEATH (TensorImpl t (ScalarType::Float, 2 , sizes), " " );
391
+ }
392
+
393
+ TEST_F (TensorImplTest, TestEmptyTensor) {
394
+ SizesType sizes[2 ] = {0 , 0 };
395
+ TensorImpl t (ScalarType::Float, 2 , sizes);
396
+ EXPECT_EQ (t.numel (), 0 );
397
+ EXPECT_EQ (t.data (), nullptr );
398
+ }
399
+
400
+ TEST_F (TensorImplTest, TestTensorWithNoElementsButAllocatedMemory) {
401
+ SizesType sizes[2 ] = {0 , 0 };
402
+ float data[1 ] = {1.0 };
403
+ TensorImpl t (ScalarType::Float, 2 , sizes, data);
404
+ EXPECT_EQ (t.numel (), 0 );
405
+ EXPECT_EQ (t.data (), data);
406
+ }
407
+
408
+ TEST_F (TensorImplTest, TestTensorWithShapeButNoMemory) {
409
+ SizesType sizes[2 ] = {3 , 2 };
410
+ TensorImpl t (ScalarType::Float, 2 , sizes);
411
+ EXPECT_GT (t.numel (), 0 );
412
+ EXPECT_EQ (t.data (), nullptr );
413
+ }
414
+
415
+ TEST_F (TensorImplTest, TestNormalTensor) {
416
+ SizesType sizes[2 ] = {3 , 2 };
417
+ float data[6 ] = {1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 };
418
+ TensorImpl t (ScalarType::Float, 2 , sizes, data);
419
+ EXPECT_GT (t.numel (), 0 );
420
+ EXPECT_EQ (t.data (), data);
421
+ }
422
+
423
+ TEST_F (TensorImplTest, TestResizingTensorToZeroAndBack) {
424
+ SizesType sizes[2 ] = {3 , 2 };
425
+ TensorImpl t (
426
+ ScalarType::Float,
427
+ 2 ,
428
+ sizes,
429
+ nullptr ,
430
+ nullptr ,
431
+ nullptr ,
432
+ TensorShapeDynamism::DYNAMIC_BOUND);
433
+
434
+ float data[6 ] = {1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 };
435
+ t.set_data (data);
436
+ EXPECT_GT (t.numel (), 0 );
437
+ EXPECT_EQ (t.data (), data);
438
+
439
+ SizesType zero_sizes[2 ] = {0 , 0 };
440
+ t.set_sizes_contiguous ({zero_sizes, 2 });
441
+ EXPECT_EQ (t.numel (), 0 );
442
+ EXPECT_EQ (t.data (), data);
443
+
444
+ SizesType new_sizes[2 ] = {3 , 2 };
445
+ t.set_sizes_contiguous ({new_sizes, 2 });
446
+ EXPECT_GT (t.numel (), 0 );
447
+ EXPECT_EQ (t.data (), data);
448
+ }
449
+
373
450
} // namespace executor
374
451
} // namespace torch
0 commit comments