xref: /aosp_15_r20/external/eigen/test/sizeoverflow.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2011 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li 
12*bf2c3715SXin Li #define VERIFY_THROWS_BADALLOC(a) {                           \
13*bf2c3715SXin Li     bool threw = false;                                       \
14*bf2c3715SXin Li     try {                                                     \
15*bf2c3715SXin Li       a;                                                      \
16*bf2c3715SXin Li     }                                                         \
17*bf2c3715SXin Li     catch (std::bad_alloc&) { threw = true; }                 \
18*bf2c3715SXin Li     VERIFY(threw && "should have thrown bad_alloc: " #a);     \
19*bf2c3715SXin Li   }
20*bf2c3715SXin Li 
21*bf2c3715SXin Li template<typename MatrixType>
triggerMatrixBadAlloc(Index rows,Index cols)22*bf2c3715SXin Li void triggerMatrixBadAlloc(Index rows, Index cols)
23*bf2c3715SXin Li {
24*bf2c3715SXin Li   VERIFY_THROWS_BADALLOC( MatrixType m(rows, cols) );
25*bf2c3715SXin Li   VERIFY_THROWS_BADALLOC( MatrixType m; m.resize(rows, cols) );
26*bf2c3715SXin Li   VERIFY_THROWS_BADALLOC( MatrixType m; m.conservativeResize(rows, cols) );
27*bf2c3715SXin Li }
28*bf2c3715SXin Li 
29*bf2c3715SXin Li template<typename VectorType>
triggerVectorBadAlloc(Index size)30*bf2c3715SXin Li void triggerVectorBadAlloc(Index size)
31*bf2c3715SXin Li {
32*bf2c3715SXin Li   VERIFY_THROWS_BADALLOC( VectorType v(size) );
33*bf2c3715SXin Li   VERIFY_THROWS_BADALLOC( VectorType v; v.resize(size) );
34*bf2c3715SXin Li   VERIFY_THROWS_BADALLOC( VectorType v; v.conservativeResize(size) );
35*bf2c3715SXin Li }
36*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(sizeoverflow)37*bf2c3715SXin Li EIGEN_DECLARE_TEST(sizeoverflow)
38*bf2c3715SXin Li {
39*bf2c3715SXin Li   // there are 2 levels of overflow checking. first in PlainObjectBase.h we check for overflow in rows*cols computations.
40*bf2c3715SXin Li   // this is tested in tests of the form times_itself_gives_0 * times_itself_gives_0
41*bf2c3715SXin Li   // Then in Memory.h we check for overflow in size * sizeof(T) computations.
42*bf2c3715SXin Li   // this is tested in tests of the form times_4_gives_0 * sizeof(float)
43*bf2c3715SXin Li 
44*bf2c3715SXin Li   size_t times_itself_gives_0 = size_t(1) << (8 * sizeof(Index) / 2);
45*bf2c3715SXin Li   VERIFY(times_itself_gives_0 * times_itself_gives_0 == 0);
46*bf2c3715SXin Li 
47*bf2c3715SXin Li   size_t times_4_gives_0 = size_t(1) << (8 * sizeof(Index) - 2);
48*bf2c3715SXin Li   VERIFY(times_4_gives_0 * 4 == 0);
49*bf2c3715SXin Li 
50*bf2c3715SXin Li   size_t times_8_gives_0 = size_t(1) << (8 * sizeof(Index) - 3);
51*bf2c3715SXin Li   VERIFY(times_8_gives_0 * 8 == 0);
52*bf2c3715SXin Li 
53*bf2c3715SXin Li   triggerMatrixBadAlloc<MatrixXf>(times_itself_gives_0, times_itself_gives_0);
54*bf2c3715SXin Li   triggerMatrixBadAlloc<MatrixXf>(times_itself_gives_0 / 4, times_itself_gives_0);
55*bf2c3715SXin Li   triggerMatrixBadAlloc<MatrixXf>(times_4_gives_0, 1);
56*bf2c3715SXin Li 
57*bf2c3715SXin Li   triggerMatrixBadAlloc<MatrixXd>(times_itself_gives_0, times_itself_gives_0);
58*bf2c3715SXin Li   triggerMatrixBadAlloc<MatrixXd>(times_itself_gives_0 / 8, times_itself_gives_0);
59*bf2c3715SXin Li   triggerMatrixBadAlloc<MatrixXd>(times_8_gives_0, 1);
60*bf2c3715SXin Li 
61*bf2c3715SXin Li   triggerVectorBadAlloc<VectorXf>(times_4_gives_0);
62*bf2c3715SXin Li 
63*bf2c3715SXin Li   triggerVectorBadAlloc<VectorXd>(times_8_gives_0);
64*bf2c3715SXin Li }
65