From 6e697d796897b32b471b4f0740dcaa03d8ee57cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 4 Jan 2024 00:59:38 +0100 Subject: [PATCH] Rewrite default_init_test() to make harder for the compiler to optimize because we rely on UB to test memory was not initialized. --- test/static_vector_test.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/static_vector_test.cpp b/test/static_vector_test.cpp index 30f62147..a296e976 100644 --- a/test/static_vector_test.cpp +++ b/test/static_vector_test.cpp @@ -627,43 +627,43 @@ bool default_init_test()//Test for default initialization { const std::size_t Capacity = 100; - typedef static_vector di_vector_t; + typedef static_vector di_vector_t; - { - di_vector_t v(Capacity, default_init); - } { typename dtl::aligned_storage::type as; - di_vector_t& v = *::new(as.data) di_vector_t; - int *p = v.data(); + di_vector_t *pv = ::new(as.data)di_vector_t(Capacity); - for(std::size_t i = 0; i != Capacity; ++i, ++p){ - *p = static_cast(i); + //Use volatile pointer to make compiler's job harder, as we are riding on UB + volatile unsigned char * pch_data = pv->data(); + + for (std::size_t i = 0; i != Capacity; ++i) { + pch_data[i] = static_cast(i); } + pv->~di_vector_t(); - di_vector_t &rv = *::new(&v)di_vector_t(Capacity, default_init); - di_vector_t::iterator it = rv.begin(); + pv = ::new(as.data) di_vector_t(Capacity, default_init); + pv->~di_vector_t(); - for(std::size_t i = 0; i != Capacity; ++i, ++it){ - if(*it != static_cast(i)) + for(std::size_t i = 0; i != Capacity; ++i){ + if (pch_data[i] != static_cast(i)){ + std::cout << "failed in iteration" << i << std::endl; return false; + } } - - v.~di_vector_t(); } { di_vector_t v; - int *p = v.data(); + unsigned char *p = v.data(); for(std::size_t i = 0; i != Capacity; ++i, ++p){ - *p = static_cast(i+100); + *p = static_cast(i+100); } v.resize(Capacity, default_init); di_vector_t::iterator it = v.begin(); for(std::size_t i = 0; i != Capacity; ++i, ++it){ - if(*it != static_cast(i+100)) + if(*it != static_cast(i+100)) return false; } }