From c7fc02a8d015ee943f7a5d559acfc04af9e97138 Mon Sep 17 00:00:00 2001 From: fss Date: Tue, 2 Jul 2024 00:04:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E7=AB=A0=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_tensor.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/test_tensor.cpp b/test/test_tensor.cpp index 13eef0a..b5cdae3 100644 --- a/test/test_tensor.cpp +++ b/test/test_tensor.cpp @@ -43,3 +43,34 @@ TEST(test_tensor, init4) { ASSERT_EQ(*t1.ptr(), 31); delete[] ptr; } + +TEST(test_tensor, index) { + using namespace base; + float* ptr = new float[32]; + auto alloc_cu = base::CPUDeviceAllocatorFactory::get_instance(); + ptr[0] = 31; + tensor::Tensor t1(base::DataType::kDataTypeFp32, 32, false, nullptr, ptr); + void* p1 = t1.ptr(); + p1 += 1; + + float* f1 = t1.ptr(); + f1 += 1; + ASSERT_NE(f1, p1); + delete[] ptr; +} + +TEST(test_tensor, dims_stride) { + using namespace base; + auto alloc_cu = base::CPUDeviceAllocatorFactory::get_instance(); + + tensor::Tensor t1(base::DataType::kDataTypeFp32, 32, 32, 3, true, alloc_cu); + ASSERT_EQ(t1.is_empty(), false); + ASSERT_EQ(t1.get_dim(0), 32); + ASSERT_EQ(t1.get_dim(1), 32); + ASSERT_EQ(t1.get_dim(2), 3); + + const auto& strides = t1.strides(); + ASSERT_EQ(strides.at(0), 32 * 3); + ASSERT_EQ(strides.at(1), 3); + ASSERT_EQ(strides.at(2), 1); +}