From 46cd3e5cc6f523aa9d4f4858543650cc968dec25 Mon Sep 17 00:00:00 2001 From: Azam Besharat Date: Thu, 24 Sep 2026 09:39:27 -0400 Subject: [PATCH] tile: assert non-null in Tile::tensor(), add Tile unit tests - Tile::tensor() (both overloads) now TA_ASSERTs that the Tile is not null instead of silently dereferencing a null pimpl_ (undefined behavior on a default-constructed or reset() Tile). - Clarify Tile's null-state semantics and document tensor()'s return value and precondition. - Add tests/tile.cpp (tile_suite) with a null_tile test that checks both tensor() overloads via BOOST_CHECK_TA_ASSERT. --- src/TiledArray/tile.h | 27 +++++++++++++++++------- tests/CMakeLists.txt | 1 + tests/tile.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 tests/tile.cpp diff --git a/src/TiledArray/tile.h b/src/TiledArray/tile.h index 49f42d8dfa..a010aadbc5 100644 --- a/src/TiledArray/tile.h +++ b/src/TiledArray/tile.h @@ -33,11 +33,14 @@ namespace TiledArray { * @{ */ -/// An N-dimensional shallow-copy wrapper for Tensor-like types that, unlike -/// Tensor, have deep-copy semantics. Like Tensor, Tile is -/// default-constructible. The default constructor produced a Tile in -/// null state (not referring to any tensor object). The name refers to its -/// intended use as a tile of DistArray. +/// A shallow-copy wrapper for deep-copy Tensor-like types. The name +/// refers to its intended use as a tile of DistArray. +/// Like Tensor, Tile is default-constructible. The default constructor +/// produces a Tile in null state (not referring to any tensor object); +/// calling reset() on a Tile also produces a null Tile. +/// Most operations on a null Tile will produce a failing TA_ASSERT +/// (see Tile::tensor()), even in cases where valid semantics could be defined +/// (such as add_to). /// /// \tparam T a tensor type. It may provide a subset of the full operation /// set of Tensor, since only those operations that are actually used @@ -196,9 +199,19 @@ class Tile { // Tile accessor ----------------------------------------------------------- - tensor_type& tensor() { return *pimpl_; } + /// \return reference to the referred-to value + /// \pre TA_ASSERT that this is not in a null state + tensor_type& tensor() { + TA_ASSERT(pimpl_); + return *pimpl_; + } - const tensor_type& tensor() const { return *pimpl_; } + /// \return const reference to the referred-to value + /// \pre TA_ASSERT that this is not in a null state + const tensor_type& tensor() const { + TA_ASSERT(pimpl_); + return *pimpl_; + } // Iterator accessor ------------------------------------------------------- diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e77f7cf1b0..d00ebc5443 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -41,6 +41,7 @@ set(ta_test_src_files ta_test.cpp btas_zb_inner_tile.cpp tensor_tensor_view.cpp tensor_shift_wrapper.cpp + tile.cpp tiled_range1.cpp tiled_range.cpp blocked_pmap.cpp diff --git a/tests/tile.cpp b/tests/tile.cpp new file mode 100644 index 0000000000..8a9472616c --- /dev/null +++ b/tests/tile.cpp @@ -0,0 +1,49 @@ +/* + * This file is a part of TiledArray. + * Copyright (C) 2026 Virginia Tech + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * tile.cpp + * September 24, 2026 + * + */ + +#include + +#ifdef TILEDARRAY_HAS_BTAS + +#include +#include +#include "tiledarray.h" +#include "unit_test_config.h" + +BOOST_AUTO_TEST_SUITE(tile_suite, TA_UT_LABEL_SERIAL) + +// TA::Tile is meant to wrap deep-copy tensor types, such as btas::Tensor +using tile_type = TiledArray::Tile>; + +BOOST_AUTO_TEST_CASE(null_tile) { + tile_type null_tile; + const tile_type& const_null_tile = null_tile; + BOOST_CHECK(null_tile.empty()); + BOOST_CHECK_EQUAL(null_tile.use_count(), 0); + // tensor() on a null tile must trigger TA_ASSERT + BOOST_CHECK_TA_ASSERT(null_tile.tensor(), TiledArray::Exception); + BOOST_CHECK_TA_ASSERT(const_null_tile.tensor(), TiledArray::Exception); +} + +BOOST_AUTO_TEST_SUITE_END() + +#endif // TILEDARRAY_HAS_BTAS