Skip to content

Conversation

@youge325
Copy link
Contributor

@youge325 youge325 commented Jan 17, 2026

新增 is_same(other) use_count() weak_use_count() is_contiguous_or_false() toString() print() 接口测试
image
image
image

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Torch/Paddle parity tests for pointer-/storage-related tensor APIs and extends the test output helper to support capturing print() output.

Changes:

  • Added test/TensorUtilTest.cpp covering is_same, use_count, weak_use_count, is_contiguous_or_false, toString, and print.
  • Extended FileManerger with captureStdout() to redirect std::cout output into the result file.
  • Cleaned up TensorTest.cpp by removing an incorrectly-placed duplicate SymSize test block.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
test/TensorUtilTest.cpp New test suite for pointer-related tensor APIs and print() output capture.
test/TensorTest.cpp Removes a misplaced duplicate SymSize test snippet.
src/file_manager.h Adds captureStdout(std::function<void()>) declaration.
src/file_manager.cpp Implements captureStdout() by redirecting std::cout to an internal buffer and writing it to the file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +31 to +37
TEST_F(TensorUtilTest, ToString) {
auto file_name = g_custom_param.get();
FileManerger file(file_name);
file.createFile();
std::string tensor_str = tensor.toString();
file << tensor_str << " ";
file.saveFile();
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tests in this file write to the same result filename (g_custom_param.get() == executable name) and each test calls createFile(), which truncates/removes the file. When the test binary runs with RUN_ALL_TESTS(), only the last test's output will remain, so the earlier API checks (e.g., toString, is_same, use_count) won’t be reflected in the compared result file. Consider consolidating these checks into a single TEST_F, or switch to a per-test output filename scheme and update the result comparison workflow accordingly.

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +90
// 保存原来的 cout buffer
std::streambuf* old_cout_buf = std::cout.rdbuf();

// 创建一个 stringstream 来捕获输出
std::stringstream captured_output;

// 重定向 cout 到 stringstream
std::cout.rdbuf(captured_output.rdbuf());

Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

captureStdout uses std::stringstream but file_manager.cpp does not include <sstream>, which will fail to compile on standard-conforming toolchains. Add the missing header (and include <stdexcept> explicitly if relying on std::runtime_error).

Copilot uses AI. Check for mistakes.
Comment on lines +74 to +95
void FileManerger::captureStdout(std::function<void()> func) {
std::unique_lock<std::shared_mutex> lock(mutex_);

if (!file_stream_.is_open()) {
throw std::runtime_error(
"File stream is not open. Call createFile() first.");
}

// 保存原来的 cout buffer
std::streambuf* old_cout_buf = std::cout.rdbuf();

// 创建一个 stringstream 来捕获输出
std::stringstream captured_output;

// 重定向 cout 到 stringstream
std::cout.rdbuf(captured_output.rdbuf());

try {
// 执行函数
func();

// 恢复 cout
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

captureStdout holds mutex_ with a unique_lock while executing the provided callback. If the callback calls any FileManerger method (e.g., operator<<, saveFile()), it will attempt to lock mutex_ again and deadlock. Consider releasing the lock before invoking func() and only locking around access to file_stream_ (or document/enforce that the callback must not call back into FileManerger).

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +90
// 保存原来的 cout buffer
std::streambuf* old_cout_buf = std::cout.rdbuf();

// 创建一个 stringstream 来捕获输出
std::stringstream captured_output;

// 重定向 cout 到 stringstream
std::cout.rdbuf(captured_output.rdbuf());

Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redirecting std::cout via std::cout.rdbuf(...) is a process-global side effect and is not made safe by the per-instance mutex_. If tests (or other code) run concurrently, output from other threads/tests may be captured or disrupted. Consider guarding the redirection with a single global/static mutex (and keep the critical section as small as possible) or using a dedicated logging/capture mechanism that avoids global std::cout redirection.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant