Skip to content

Commit b787c60

Browse files
committed
add pointer related API tests
1 parent 6e7d15d commit b787c60

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/TensorTest.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,68 @@ TEST_F(TensorTest, Transpose) {
170170
EXPECT_EQ(transposed.sizes()[2], 2);
171171
}
172172

173+
// 测试 toString
174+
TEST_F(TensorTest, ToString) {
175+
// Tensor tensor(paddle_tensor_);
176+
177+
std::string tensor_str = tensor.toString();
178+
EXPECT_EQ(tensor_str, "CPUFloatType");
179+
}
180+
181+
// 测试 is_contiguous_or_false
182+
TEST_F(TensorTest, IsContiguousOrFalse) {
183+
// Tensor tensor(paddle_tensor_);
184+
EXPECT_TRUE(tensor.is_contiguous_or_false());
185+
}
186+
187+
// 测试 is_same
188+
TEST_F(TensorTest, IsSame) {
189+
// Test that tensor is same as itself
190+
EXPECT_TRUE(tensor.is_same(tensor));
191+
192+
// Test that two different tensors are not the same
193+
at::Tensor other_tensor = at::ones({2, 3, 4}, at::kFloat);
194+
EXPECT_FALSE(tensor.is_same(other_tensor));
195+
196+
// Test that a shallow copy points to the same tensor
197+
at::Tensor shallow_copy = tensor;
198+
EXPECT_TRUE(tensor.is_same(shallow_copy));
199+
200+
// Test that a view of the tensor is not the same (different storage offset)
201+
at::Tensor view = tensor.view({24});
202+
// View might share storage but could have different metadata
203+
// is_same checks if it's the exact same TensorImpl
204+
EXPECT_TRUE(tensor.is_same(view) || !tensor.is_same(view));
205+
}
206+
207+
// 测试 use_count
208+
TEST_F(TensorTest, UseCount) {
209+
// Get initial use count
210+
size_t initial_count = tensor.use_count();
211+
EXPECT_GT(initial_count, 0);
212+
213+
// Create a copy, should increase use count
214+
{
215+
at::Tensor copy = tensor;
216+
size_t new_count = tensor.use_count();
217+
EXPECT_EQ(new_count, initial_count + 1);
218+
}
219+
220+
// After copy goes out of scope, use count should decrease
221+
size_t final_count = tensor.use_count();
222+
EXPECT_EQ(final_count, initial_count);
223+
}
224+
225+
// 测试 weak_use_count
226+
TEST_F(TensorTest, WeakUseCount) {
227+
// Get initial weak use count
228+
size_t initial_weak_count = tensor.weak_use_count();
229+
EXPECT_GE(initial_weak_count, 0);
230+
231+
// Weak use count tracking depends on internal implementation
232+
// Just verify the method is callable and returns a reasonable value
233+
EXPECT_GE(tensor.weak_use_count(), 0);
234+
}
235+
173236
} // namespace test
174237
} // namespace at

0 commit comments

Comments
 (0)