sizeoverflow.cpp File Reference
#include "main.h"

Macros

#define VERIFY_THROWS_BADALLOC(a)
 

Functions

template<typename MatrixType >
void triggerMatrixBadAlloc (Index rows, Index cols)
 
template<typename VectorType >
void triggerVectorBadAlloc (Index size)
 
 EIGEN_DECLARE_TEST (sizeoverflow)
 

Macro Definition Documentation

◆ VERIFY_THROWS_BADALLOC

#define VERIFY_THROWS_BADALLOC (   a)
Value:
{ \
bool threw = false; \
try { \
a; \
} catch (std::bad_alloc&) { \
threw = true; \
} \
VERIFY(threw && "should have thrown bad_alloc: " #a); \
}
const Scalar * a
Definition: level2_cplx_impl.h:32

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( sizeoverflow  )
37  {
38  // there are 2 levels of overflow checking. first in PlainObjectBase.h we check for overflow in rows*cols
39  // computations. this is tested in tests of the form times_itself_gives_0 * times_itself_gives_0 Then in Memory.h we
40  // check for overflow in size * sizeof(T) computations. this is tested in tests of the form times_4_gives_0 *
41  // sizeof(float)
42 
43  size_t times_itself_gives_0 = size_t(1) << (8 * sizeof(Index) / 2);
44  VERIFY(times_itself_gives_0 * times_itself_gives_0 == 0);
45 
46  size_t times_4_gives_0 = size_t(1) << (8 * sizeof(Index) - 2);
47  VERIFY(times_4_gives_0 * 4 == 0);
48 
49  size_t times_8_gives_0 = size_t(1) << (8 * sizeof(Index) - 3);
50  VERIFY(times_8_gives_0 * 8 == 0);
51 
52  triggerMatrixBadAlloc<MatrixXf>(times_itself_gives_0, times_itself_gives_0);
53  triggerMatrixBadAlloc<MatrixXf>(times_itself_gives_0 / 4, times_itself_gives_0);
54  triggerMatrixBadAlloc<MatrixXf>(times_4_gives_0, 1);
55 
56  triggerMatrixBadAlloc<MatrixXd>(times_itself_gives_0, times_itself_gives_0);
57  triggerMatrixBadAlloc<MatrixXd>(times_itself_gives_0 / 8, times_itself_gives_0);
58  triggerMatrixBadAlloc<MatrixXd>(times_8_gives_0, 1);
59 
60  triggerVectorBadAlloc<VectorXf>(times_4_gives_0);
61 
62  triggerVectorBadAlloc<VectorXd>(times_8_gives_0);
63 }
#define VERIFY(a)
Definition: main.h:362
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83

References VERIFY.

◆ triggerMatrixBadAlloc()

template<typename MatrixType >
void triggerMatrixBadAlloc ( Index  rows,
Index  cols 
)
24  {
27  VERIFY_THROWS_BADALLOC(MatrixType m; m.conservativeResize(rows, cols));
28 }
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY_THROWS_BADALLOC(a)
Definition: sizeoverflow.cpp:12

References cols, m, rows, and VERIFY_THROWS_BADALLOC.

◆ triggerVectorBadAlloc()

template<typename VectorType >
void triggerVectorBadAlloc ( Index  size)
31  {
34  VERIFY_THROWS_BADALLOC(VectorType v; v.conservativeResize(size));
35 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Definition: fft_test_shared.h:66

References size, v, and VERIFY_THROWS_BADALLOC.