-
Notifications
You must be signed in to change notification settings - Fork 3
add pointer related API tests #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.cppcoveringis_same,use_count,weak_use_count,is_contiguous_or_false,toString, andprint. - Extended
FileManergerwithcaptureStdout()to redirectstd::coutoutput into the result file. - Cleaned up
TensorTest.cppby removing an incorrectly-placed duplicateSymSizetest 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.
| 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(); |
Copilot
AI
Feb 9, 2026
There was a problem hiding this comment.
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.
| // 保存原来的 cout buffer | ||
| std::streambuf* old_cout_buf = std::cout.rdbuf(); | ||
|
|
||
| // 创建一个 stringstream 来捕获输出 | ||
| std::stringstream captured_output; | ||
|
|
||
| // 重定向 cout 到 stringstream | ||
| std::cout.rdbuf(captured_output.rdbuf()); | ||
|
|
Copilot
AI
Feb 9, 2026
There was a problem hiding this comment.
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).
| 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 |
Copilot
AI
Feb 9, 2026
There was a problem hiding this comment.
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).
| // 保存原来的 cout buffer | ||
| std::streambuf* old_cout_buf = std::cout.rdbuf(); | ||
|
|
||
| // 创建一个 stringstream 来捕获输出 | ||
| std::stringstream captured_output; | ||
|
|
||
| // 重定向 cout 到 stringstream | ||
| std::cout.rdbuf(captured_output.rdbuf()); | ||
|
|
Copilot
AI
Feb 9, 2026
There was a problem hiding this comment.
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.
新增



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