| 1 |
#include "dxxsconf.h" |
| 2 |
#include "compiler-poison.h" |
| 3 |
|
| 4 |
/* If DXX_HAVE_POISON_UNDEFINED is not zero, |
| 5 |
* array_managed_type::array_managed_type() is declared and must be |
| 6 |
* defined out of line. These tests do not need to run any code at |
| 7 |
* array construction time, so force DXX_HAVE_POISON_UNDEFINED to be zero. |
| 8 |
*/ |
| 9 |
#undef DXX_HAVE_POISON_UNDEFINED |
| 10 |
#define DXX_HAVE_POISON_UNDEFINED 0 |
| 11 |
#include "valptridx.h" |
| 12 |
#include "valptridx.tcc" |
| 13 |
|
| 14 |
#define BOOST_TEST_DYN_LINK |
| 15 |
#define BOOST_TEST_MODULE Rebirth valptridx |
| 16 |
#include <boost/test/unit_test.hpp> |
| 17 |
|
| 18 |
/* Convenience macro to prevent the compiler optimizer from performing |
| 19 |
* constant propagation of the input. This ensures that build-time |
| 20 |
* tests are suppressed, allowing the runtime test to be emitted and |
| 21 |
* executed. |
| 22 |
*/ |
| 23 |
#define OPTIMIZER_HIDE_VARIABLE(V) asm("" : "=rm" (V) : "0" (V) : "memory") |
| 24 |
|
| 25 |
/* Convenience function to pass an anonymous value (such as an integer |
| 26 |
* constant) through OPTIMIZER_HIDE_VARIABLE, then return it. This is |
| 27 |
* needed for contexts where a bare asm() statement would not be legal. |
| 28 |
* |
| 29 |
* It also has the useful secondary effect of casting the input to a |
| 30 |
* type, if the caller explicitly sets the typename T. |
| 31 |
*/ |
| 32 |
template <typename T> |
| 33 |
static inline T optimizer_hidden_variable(T v) |
| 34 |
{ |
| 35 |
OPTIMIZER_HIDE_VARIABLE(v); |
| 36 |
return v; |
| 37 |
} |
| 38 |
|
| 39 |
/* Convenience macro to ignore the result of an expression in a way that |
| 40 |
* __attribute__((warn_unused_result)) does not warn that the result was |
| 41 |
* ignored. |
| 42 |
*/ |
| 43 |
#define DXX_TEST_VALPTRIDX_IGNORE_RETURN(EXPR) ({ auto &&r = EXPR; static_cast<void>(r); }) |
| 44 |
|
| 45 |
struct object |
| 46 |
{ |
| 47 |
/* No state needed - this module is testing valptridx, not the type |
| 48 |
* managed by valptridx. |
| 49 |
*/ |
| 50 |
}; |
| 51 |
|
| 52 |
using objnum_t = uint16_t; |
| 53 |
constexpr std::integral_constant<std::size_t, 350> MAX_OBJECTS{}; |
| 54 |
DXX_VALPTRIDX_DECLARE_SUBTYPE(, object, objnum_t, MAX_OBJECTS); |
| 55 |
DXX_VALPTRIDX_DEFINE_SUBTYPE_TYPEDEFS(object, obj); |
| 56 |
DXX_VALPTRIDX_DEFINE_GLOBAL_FACTORIES(object, obj, Objects); |
| 57 |
|
| 58 |
valptridx<object>::array_managed_type Objects; |
| 59 |
|
| 60 |
/* Some valptridx names are normally protected. The tests need access |
| 61 |
* to them. Define a subclass that relaxes the permissions. |
| 62 |
*/ |
| 63 |
struct valptridx_access_override : valptridx<object> |
| 64 |
{ |
| 65 |
using base_type = valptridx<object>; |
| 66 |
using typename base_type::allow_end_construction; |
| 67 |
using typename base_type::index_range_exception; |
| 68 |
using typename base_type::null_pointer_exception; |
| 69 |
using typename base_type::report_error_uses_exception; |
| 70 |
}; |
| 71 |
|
| 72 |
/* Test that using the factory does not throw an exception on the |
| 73 |
* highest valid index. |
| 74 |
*/ |
| 75 |
BOOST_AUTO_TEST_CASE(idx_no_exception_near_end) |
| 76 |
{ |
| 77 |
BOOST_CHECK_NO_THROW( |
| 78 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN( |
| 79 |
Objects.vmptr(optimizer_hidden_variable<objnum_t>(MAX_OBJECTS - 1))) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/* Test that using the factory does throw an exception on the lowest |
| 84 |
* invalid index and on the highest representable integer. |
| 85 |
*/ |
| 86 |
BOOST_AUTO_TEST_CASE(idx_exception_at_end) |
| 87 |
{ |
| 88 |
if (!valptridx_access_override::report_error_uses_exception::value) |
| 89 |
/* If valptridx is configured not to use exceptions to report |
| 90 |
* errors, these tests will fail even when the logic is correct. |
| 91 |
* Skip them in that case. |
| 92 |
*/ |
| 93 |
return; |
| 94 |
BOOST_CHECK_THROW( |
| 95 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN( |
| 96 |
Objects.vmptr( |
| 97 |
optimizer_hidden_variable<objnum_t>(MAX_OBJECTS))), |
| 98 |
valptridx_access_override::index_range_exception); |
| 99 |
|
| 100 |
BOOST_CHECK_THROW( |
| 101 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN( |
| 102 |
Objects.vmptr( |
| 103 |
optimizer_hidden_variable<objnum_t>(UINT16_MAX))), |
| 104 |
valptridx_access_override::index_range_exception); |
| 105 |
} |
| 106 |
|
| 107 |
BOOST_AUTO_TEST_CASE(iter_no_exception_at_allowed_end) |
| 108 |
{ |
| 109 |
/* Test the allowed side of end() handling for off-by-one bugs. |
| 110 |
*/ |
| 111 |
BOOST_CHECK_NO_THROW(DXX_TEST_VALPTRIDX_IGNORE_RETURN( |
| 112 |
(Objects.set_count(optimizer_hidden_variable<objnum_t>(MAX_OBJECTS - 1)), Objects.vmptr.end()) |
| 113 |
)); |
| 114 |
|
| 115 |
BOOST_CHECK_NO_THROW(DXX_TEST_VALPTRIDX_IGNORE_RETURN( |
| 116 |
(Objects.set_count(optimizer_hidden_variable<objnum_t>(MAX_OBJECTS)), Objects.vmptr.end()) |
| 117 |
)); |
| 118 |
} |
| 119 |
|
| 120 |
BOOST_AUTO_TEST_CASE(iter_exception_if_beyond_end) |
| 121 |
{ |
| 122 |
/* Test that end() throws an exception if the range's count exceeds |
| 123 |
* the valid storage size. |
| 124 |
*/ |
| 125 |
if (!valptridx_access_override::report_error_uses_exception::value) |
| 126 |
return; |
| 127 |
BOOST_CHECK_THROW((DXX_TEST_VALPTRIDX_IGNORE_RETURN( |
| 128 |
(Objects.set_count(optimizer_hidden_variable<objnum_t>(MAX_OBJECTS + 1)), Objects.vmptr.end()) |
| 129 |
)), valptridx_access_override::index_range_exception); |
| 130 |
} |
| 131 |
|
| 132 |
BOOST_AUTO_TEST_CASE(guarded_in_range) |
| 133 |
{ |
| 134 |
auto &&g = Objects.vmptr.check_untrusted(optimizer_hidden_variable<objnum_t>(0)); |
| 135 |
optimizer_hidden_variable(g); |
| 136 |
/* Test that a `guarded` returned by `check_untrusted` on a valid |
| 137 |
* index throws if accessed without testing its validity. A runtime |
| 138 |
* check on validity is mandatory. |
| 139 |
* |
| 140 |
* These exceptions are not configurable, so no test on |
| 141 |
* `report_error_uses_exception` is required. |
| 142 |
*/ |
| 143 |
BOOST_CHECK_THROW(DXX_TEST_VALPTRIDX_IGNORE_RETURN(*g), std::logic_error); |
| 144 |
/* Test its validity */ |
| 145 |
BOOST_TEST(static_cast<bool>(g)); |
| 146 |
/* Test that it can now be read successfully. */ |
| 147 |
BOOST_CHECK_NO_THROW(DXX_TEST_VALPTRIDX_IGNORE_RETURN(*g)); |
| 148 |
} |
| 149 |
|
| 150 |
BOOST_AUTO_TEST_CASE(guarded_out_of_range) |
| 151 |
{ |
| 152 |
auto &&g = Objects.vmptr.check_untrusted(optimizer_hidden_variable<objnum_t>(MAX_OBJECTS)); |
| 153 |
optimizer_hidden_variable(g); |
| 154 |
/* Test that a `guarded` returned by `check_untrusted` on an invalid |
| 155 |
* index throws if accessed without testing its validity. |
| 156 |
* |
| 157 |
* Test its validity. |
| 158 |
* |
| 159 |
* Test that it still throws afterward. |
| 160 |
*/ |
| 161 |
BOOST_CHECK_THROW(DXX_TEST_VALPTRIDX_IGNORE_RETURN(*g), std::logic_error); |
| 162 |
BOOST_TEST(!static_cast<bool>(g)); |
| 163 |
BOOST_CHECK_THROW(DXX_TEST_VALPTRIDX_IGNORE_RETURN(*g), std::logic_error); |
| 164 |
} |
| 165 |
|
| 166 |
BOOST_AUTO_TEST_CASE(idx_convert_check) |
| 167 |
{ |
| 168 |
using vo = valptridx<object>; |
| 169 |
BOOST_CHECK_NO_THROW( |
| 170 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 171 |
typename vo::imidx i(objnum_t{0}); |
| 172 |
typename vo::vmidx{i}; |
| 173 |
})) |
| 174 |
); |
| 175 |
typename vo::imidx i_none0(static_cast<objnum_t>(~0)); |
| 176 |
auto i_none = optimizer_hidden_variable(i_none0); |
| 177 |
if (!valptridx_access_override::report_error_uses_exception::value) |
| 178 |
return; |
| 179 |
BOOST_CHECK_THROW( |
| 180 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 181 |
typename vo::vmidx{i_none}; |
| 182 |
})), valptridx_access_override::index_range_exception |
| 183 |
); |
| 184 |
BOOST_CHECK_THROW( |
| 185 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 186 |
typename vo::vmidx{optimizer_hidden_variable<objnum_t>(~0)}; |
| 187 |
})), valptridx_access_override::index_range_exception |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
BOOST_AUTO_TEST_CASE(ptr_convert_check) |
| 192 |
{ |
| 193 |
using vo = valptridx<object>; |
| 194 |
typename vo::imptr i_none0(nullptr); |
| 195 |
auto i_none = optimizer_hidden_variable(i_none0); |
| 196 |
auto &&i_zero = Objects.imptr(optimizer_hidden_variable<objnum_t>(0)); |
| 197 |
BOOST_CHECK_NO_THROW( |
| 198 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 199 |
typename vo::vmptr v_zero{i_zero}; |
| 200 |
v_zero.get_nonnull_pointer(); |
| 201 |
})) |
| 202 |
); |
| 203 |
if (!valptridx_access_override::report_error_uses_exception::value) |
| 204 |
return; |
| 205 |
BOOST_CHECK_THROW( |
| 206 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 207 |
typename vo::vmptr{i_none}; |
| 208 |
})), valptridx_access_override::null_pointer_exception |
| 209 |
); |
| 210 |
BOOST_CHECK_THROW( |
| 211 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 212 |
Objects.vmptr(optimizer_hidden_variable<object *>(nullptr)); |
| 213 |
})), valptridx_access_override::null_pointer_exception |
| 214 |
); |
| 215 |
BOOST_CHECK_THROW( |
| 216 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 217 |
i_none.get_nonnull_pointer(); |
| 218 |
})), valptridx_access_override::null_pointer_exception |
| 219 |
); |
| 220 |
auto &&pi_zero = Objects.vmptridx(optimizer_hidden_variable<objnum_t>(0)); |
| 221 |
BOOST_CHECK_THROW( |
| 222 |
DXX_TEST_VALPTRIDX_IGNORE_RETURN(({ |
| 223 |
pi_zero.absolute_sibling(optimizer_hidden_variable<objnum_t>(Objects.size())); |
| 224 |
})), valptridx_access_override::index_range_exception |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
/* For each style selector, set a type. Test that the type is used. |
| 229 |
* Reset to a different type. Test that the second type is used. Clear |
| 230 |
* the selector, so that any further use is an error. |
| 231 |
* |
| 232 |
* This combination verifies that the requested style is set, and that |
| 233 |
* the match is not an accident. If the first test succeeded because |
| 234 |
* the style happened to match for the wrong reason, then redefining the |
| 235 |
* style would have no effect, and the second test would fail. |
| 236 |
*/ |
| 237 |
|
| 238 |
/* Test DXX_VALPTRIDX_REPORT_ERROR_STYLE_default - the final fallback, |
| 239 |
* used only when everything else is unset. |
| 240 |
*/ |
| 241 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_default |
| 242 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_default exception |
| 243 |
|
| 244 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::exception); |
| 245 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::exception); |
| 246 |
|
| 247 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_default |
| 248 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_default undefined |
| 249 |
|
| 250 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::undefined); |
| 251 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::undefined); |
| 252 |
|
| 253 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_default |
| 254 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_default /* invalid mapping - force an error on use */ |
| 255 |
|
| 256 |
/* Test the const/mutable defaults. These are the second lowest |
| 257 |
* priority, above only DXX_VALPTRIDX_REPORT_ERROR_STYLE_default. |
| 258 |
*/ |
| 259 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_default |
| 260 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_default trap_terse |
| 261 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_default |
| 262 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_default trap_verbose |
| 263 |
|
| 264 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::trap_terse); |
| 265 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::trap_verbose); |
| 266 |
|
| 267 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_default |
| 268 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_default exception |
| 269 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_default |
| 270 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_default trap_terse |
| 271 |
|
| 272 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::exception); |
| 273 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::trap_terse); |
| 274 |
|
| 275 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_default |
| 276 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_default |
| 277 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_default |
| 278 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_default |
| 279 |
|
| 280 |
/* Test the type-specific default. This is the third lowest priority. |
| 281 |
*/ |
| 282 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_default_error_report_test undefined |
| 283 |
|
| 284 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::undefined); |
| 285 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::undefined); |
| 286 |
|
| 287 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_default_error_report_test |
| 288 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_default_error_report_test exception |
| 289 |
|
| 290 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::exception); |
| 291 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::exception); |
| 292 |
|
| 293 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_default_error_report_test |
| 294 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_default_error_report_test |
| 295 |
|
| 296 |
/* Test the const/mutable type-specific. These are the highest |
| 297 |
* priority. |
| 298 |
*/ |
| 299 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_error_report_test trap_terse |
| 300 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_error_report_test trap_verbose |
| 301 |
|
| 302 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::trap_terse); |
| 303 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::trap_verbose); |
| 304 |
|
| 305 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_error_report_test |
| 306 |
#undef DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_error_report_test |
| 307 |
|
| 308 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_const_error_report_test exception |
| 309 |
#define DXX_VALPTRIDX_REPORT_ERROR_STYLE_mutable_error_report_test undefined |
| 310 |
|
| 311 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(const, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::exception); |
| 312 |
static_assert(valptridx_detail::untyped_utilities::error_style_dispatch<DXX_VALPTRIDX_ERROR_STYLE_DISPATCH_PARAMETERS(mutable, error_report_test)>::value == valptridx_detail::untyped_utilities::report_error_style::undefined); |