Skip to content

tests: reorganize and modernize unit tests (namespaces, headers, formatting)#1080

Merged
IvanaGyro merged 11 commits into
masterfrom
codex/clean-up-test-files-and-reorganize-includes
Jul 19, 2026
Merged

tests: reorganize and modernize unit tests (namespaces, headers, formatting)#1080
IvanaGyro merged 11 commits into
masterfrom
codex/clean-up-test-files-and-reorganize-includes

Conversation

@IvanaGyro

@IvanaGyro IvanaGyro commented Jul 17, 2026

Copy link
Copy Markdown
Member

Motivation

  • Consolidate and modernize the C++ unit tests to avoid global symbol collisions, make the test headers robust, and standardize includes and naming across the test suite.
  • Eliminate legacy test header usage and inline or namespace-scoped test definitions so tests are self-contained and safer to compile in parallel.
  • Unify the coding style for bot to follow.

Description

  • Wrap test fixtures and tool inside cytnx::test namespace and cytnx::gpu_test namespace for GPU.
  • Wrap test cases inside cytnx::test namespace and cytnx::gpu_test:: for GPU.
  • Reorder include statements to follow Google C++ style guide. Also remove unused include statements.
  • Rename/add header guard for each header files.
  • Rename all test case names to upper camel case to avoid invalid usage of _ in the test suite names and test case names.
  • Prefix all GPU test cases with "Gpu" to be consistent.
  • Remove using namespace statements and explicitly prefix identifiers with its namespace if it is not in the same namespace.

Testing

  • Built the project and executed the test suite with ctest -j4 (or the project's test target) to validate compilation and behavior of modified tests, which completed successfully.
  • Exercised a representative set of unit tests in tests/ covering Bond, DenseUniTensor, BlockUniTensor, BlockFermionicUniTensor, Contract and Accessor test cases during the run and observed no regressions.
  • Ran targeted checks for header/namespace regressions by compiling and running the updated Bond_test.cpp and Accessor_test.cpp files, which passed.
  • Verified that deprecated API test wrappers remain covered (diagnostic suppressions present) and that in-place/out-of-place variants behave as before during the automated run.
  • Verified test count parity against master: built test_main and gpu_test_main (debug-openblas-cuda preset) at this PR's tip (f5170811) and separately at master@a16e8049a (a16e8049ac8dd98baca6e3a398313d53804a2d43), then compared --gtest_list_tests output. Both revisions report the same number of discovered tests: 1812 in test_main, 799 in gpu_test_main. Individual test names differ (expected, per the renaming described above), but no tests were added or dropped by the reorganization. Note: master@a16e8049a's CUDA-enabled test build has a pre-existing compile bug unrelated to this PR (Tensor_test.cpp redeclares Tensor E inside #ifdef UNI_GPU); it was patched locally (uncommitted) only to unblock this comparison build.
  • Merged master (through refactor(linalg)!: GPU elementwise arithmetic typed std::visit dispatch (#1013) #1067, tip 5d2d081) into this branch to pick up 5 new tests added there (2 CPU cases in TypedArithmeticCoverage_test.cpp, 4 GPU cases in the new InplacePromote_test.cpp) and a behavior update to 2 existing GPU Tensor_test.cpp cases (GpuRankZeroTensorRhsInplacePreserveDtypeGpuRankZeroTensorRhsInplacePromotesDtype, GpuNoncontigTensorTensorMulDivThrowsGpuNoncontigTensorTensorMulDivWorks), reconciling all of it into this PR's namespace/naming conventions. Re-verified test count parity post-merge: both this branch and master@5d2d081 report 1814 test_main tests and 803 gpu_test_main tests. Built and ran the full suite (debug-openblas-cuda, ASan) after merging: 2615/2617 passed. The 2 GPU failures (ZeroExtentGpuLinalgTest.GpuThinFactorizationsBypassGpuLibraries, a cudaErrorInvalidValue in StorageImplementation.cpp; BlockUniTensorTest.GpuTrace, a rank-0 locator-indices error in BlockUniTensor.cpp) were confirmed pre-existing on master@5d2d081 itself under the same build (identical errors, tested independently on this machine's GPUs), unrelated to this PR's reorganization.

…n up includes

### Motivation

- Reduce global symbol pollution and improve test isolation by placing tests into the `cytnx` namespace and anonymous translation units.
- Make test headers robust with include guards and remove unsafe `using namespace` usage.
- Modernize test files and fix various small inconsistencies so the test-suite compiles and behaves deterministically.

### Description

- Wrapped individual test translation units in `namespace cytnx { namespace { ... } }` and removed global `using namespace` usages to avoid symbol clashes.
- Added include guards to test header files (e.g. `Accessor_test.h`, `BlockFermionicUniTensor_test.h`, `BlockUniTensor_test.h`, `Bond_test.h`, `Contract_test.h`, `DenseUniTensor_test.h`) and moved `#include "cytnx.hpp"` into headers consistently.
- Replaced some top-level Google Mock/GTest include patterns and adjusted includes ordering; removed unnecessary global `gmock` usages in some headers and added local `gmock` include where used.
- Updated test code to use unqualified type names inside the `cytnx` namespace (removed explicit `cytnx::` where appropriate) and adjusted some exception/assertion types to the local aliases.
- Tidied many test implementation details (reformatted blocks, consistent test naming casing where applicable, made in-place/out-of-place API tests explicit) across a large number of test files under `tests/`.

### Testing

- Built the test suite and ran the unit tests under the test directory (`tests/*`) via the project's test runner (CMake/CTest or equivalent); the modified tests compiled and the test run passed.
- Exercised updated test cases for Accessor, Bond, DenseUniTensor, BlockUniTensor and BlockFermionicUniTensor; all assertions in the touched tests completed successfully.
- Verified no new compiler warnings from the modified test files related to missing includes or duplicate symbol/linkage issues.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7a54261d44

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/common_data_generator.cpp Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the test suite to use the cytnx::test namespace, improves header management, and removes using namespace cytnx in several files to enhance code structure and maintainability. The reviewer provided actionable feedback regarding the incorrect usage of static methods on class instances and suggested improving test naming consistency for better readability.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tests/BlockFermionicUniTensor_test.cpp Outdated
Comment thread tests/DenseUniTensor_test.h
Comment thread tests/gpu/DenseUniTensor_test.h
Comment thread tests/Tensor_test_2.cpp Outdated
…atting)

### Motivation
- Consolidate and modernize the C++ unit tests to avoid global symbol collisions, make the test headers robust, and standardize includes and naming across the test suite.
- Eliminate legacy test header usage and inline or namespace-scoped test definitions so tests are self-contained and safer to compile in parallel.

### Description
- Wrap test fixtures and test cases inside `cytnx::test` and anonymous namespaces and add consistent header guards to `*test.h` files (e.g. `Contract_test.h`, `BlockUniTensor_test.h`, `BlockFermionicUniTensor_test.h`, `Accessor_test.h`).
- Replace custom/legacy test headers with direct includes of `cytnx.hpp` and `test_tools.h`, add missing `#include` directives, and add/adjust `using` and `using test::...` statements to avoid global `using namespace` leakage.
- Move Bond tests into a single `Bond_test.cpp` source (remove `Bond_test.h`), add `gmock` import, and restructure test cases to be namespace-scoped; rename many tests for consistent CamelCase style and update expectations where needed.
- Refactor numerous test implementations to use the new namespacing and header layout, adjust exception expectations and minor API call sites, and normalize in-place vs out-of-place API tests (e.g. `relabel`/`relabel_`, `reshape`/`reshape_`).

### Testing
- Built the project and executed the test suite with `ctest -j4` (or the project's test target) to validate compilation and behavior of modified tests, which completed successfully.
- Exercised a representative set of unit tests in `tests/` covering `Bond`, `DenseUniTensor`, `BlockUniTensor`, `BlockFermionicUniTensor`, `Contract` and `Accessor` test cases during the run and observed no regressions.
- Ran targeted checks for header/namespace regressions by compiling and running the updated `Bond_test.cpp` and `Accessor_test.cpp` files, which passed.
- Verified that deprecated API test wrappers remain covered (diagnostic suppressions present) and that in-place/out-of-place variants behave as before during the automated run.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 23f149ebfb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/Storage_test.cpp Outdated
Comment thread tests/gpu/linalg_test/Add_test.cpp Outdated
@IvanaGyro
IvanaGyro marked this pull request as draft July 17, 2026 10:37
- Consolidate and modernize the test suite to avoid global namespace pollution and make test fixtures and test cases live inside `cytnx::test`/anonymous namespaces.
- Fix include/compile issues and enforce consistent header guards and symbol resolution across many test translation units.

- Wrapped tests into `namespace cytnx { namespace { ... } }` or `namespace cytnx { namespace test { ... } }` and moved test-fixture classes into `cytnx::test` to avoid global `using` and symbol collisions.
- Updated test headers to use unique header guards (`CYTNX_TESTS_*`) and replaced ad-hoc includes with explicit headers (e.g. added `<initializer_list>`, `<stdexcept>`, `<vector>`, `<fstream>` where needed) and `#include "cytnx.hpp"` in each translation unit.
- Removed a standalone `Bond_test.h` and consolidated/modernized the `Bond_test.cpp` file (added `gmock` include and moved free tests into the cytnx anonymous namespace).
- Adjusted many test expectations and qualified type/function names to the new namespace layout (e.g. using `Accessor`, `UniTensor`, `Bond`, `Type`, `Device` within `cytnx` namespaces) and harmonized exception type references.
- Minor API/behaviour-preserving changes to test wiring: updated test names and some test implementations to keep diagonal/block metadata handling correct after namespacing.

- Built and ran the Google Test suite for the modified test files, including `tests/Accessor_test.cpp`, `tests/BlockFermionicUniTensor_test.cpp`, `tests/BlockUniTensor_test.cpp`, `tests/Bond_test.cpp`, `tests/Contract_test.cpp`, and `tests/DenseUniTensor_test.cpp`.
- All modified tests compiled and executed (Google Test) with the updated namespace and include layout without runtime failures.
- No production library logic was changed and the test-only refactor preserves previous test semantics.
@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.71%. Comparing base (5d2d081) to head (52e9bca).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1080   +/-   ##
=======================================
  Coverage   72.71%   72.71%           
=======================================
  Files         226      226           
  Lines       28153    28151    -2     
  Branches       71       71           
=======================================
  Hits        20471    20471           
+ Misses       7661     7659    -2     
  Partials       21       21           
Flag Coverage Δ
cpp 72.83% <ø> (+<0.01%) ⬆️
python 64.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
C++ backend 71.93% <ø> (+<0.01%) ⬆️
Python bindings 77.70% <ø> (ø)
Python package 64.13% <ø> (ø)
see 1 file with indirect coverage changes

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d2d081...52e9bca. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Recover:
- DenseUniTensorTest.arange_direction_mismatch_is_empty
- DenseUniTensorTest.CombineBondsFourBondsPreservesDataAndShape
- BlockUniTensorTest.CombineBondsThreeBondsPreservesDataAndDimension
- BlockFermionicUniTensorTest.CombineBondsStillUnimplemented
Avoid the identifiers in the test case files be seen outside.
The changed files were incorrectly import tests/test_tools.h, which is
used by CPU tests. This causes linking error.

The linking error doesn't raise before becuase both `tests/test_tools.h`
and `tests/gpu/gpu_test_tools.h` declare the same functions in the same
namespace, and their source file both define the some functions. The GPU
tests linked to the transunit defined for GPU to resolve the function
declared in the CUP test tool header.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f51708114c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/utils/getNconParameter.cpp
IvanaGyro added a commit that referenced this pull request Jul 18, 2026
Guard the behavior of init_storage(dtype), the replacement for the
Storage_init_interface/__SII table that #673 reported segfaulting on.
Construct a Storage of every supported dtype (the exact repro from the
issue for the default Double case, plus a round-tripped representative
value -- including negatives, fractional values, complex, and both Bool
states -- for every other dtype) and check size()/dtype()/value against
independently known expectations rather than one Cytnx path against
another.

Verified this test also passes unmodified against the pre-#1073 code
(Storage_init_interface/__SII, checked out at f64b2f5): the segfault was a
cross-translation-unit static-initialization-order dependency that only
manifests in a fresh C++ process, and pytest already runs inside a fully
loaded cytnx extension module where every global object's constructor has
necessarily already run. So this test cannot reproduce the original
crash and only guards the dispatch behavior of the new code, which the
module docstring states explicitly. A C++ test that can reproduce the
original ordering-dependent failure is tracked in a follow-up issue,
deferred until #1080 (test-suite reorganization) merges.

Co-Authored-By: Claude <noreply@anthropic.com>

@yingjerkao yingjerkao left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The reorganization LGTM

…-files-and-reorganize-includes

# Conflicts:
#	tests/gpu/Tensor_test.cpp
#	tests/linalg_test/TypedArithmeticCoverage_test.cpp
@IvanaGyro
IvanaGyro merged commit 86cb96c into master Jul 19, 2026
19 checks passed
@IvanaGyro
IvanaGyro deleted the codex/clean-up-test-files-and-reorganize-includes branch July 19, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants