Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
simplex_allocation_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
42# include <gtest/gtest.h>
43
44# include "allocation_failure.H"
45
46# include <cstddef>
47# include <new>
48# include <string>
49
50# include <Simplex.H>
51
52using namespace Aleph;
53
54// The tableau was kept as soon as it was allocated: after a preparation
55// that failed later, as with std::bad_alloc, every restriction was refused
56// as if the program had been prepared
57TEST(SimplexAllocation, AFailedPreparationStillTakesRestrictions)
58{
60 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
61
62 size_t failures = 0;
63 for (size_t n = 1; n < 10000; ++n)
64 {
65 SCOPED_TRACE("the allocation " + std::to_string(n) + " fails");
66 Simplex<double> simplex(2); // maximize x + 2y
67 simplex.put_objetive_function_coef(0, 1.0);
68 simplex.put_objetive_function_coef(1, 2.0);
69 double sum[] = {1.0, 1.0, 4.0}; // x + y <= 4
70 simplex.put_restriction(sum);
71
72 bool threw = false;
73 {
74 const Fail_At failing(n);
75 try
76 {
77 simplex.prepare_linear_program();
78 }
79 catch (const std::bad_alloc &)
80 {
81 threw = true;
82 }
83 }
84 if (not threw)
85 {
86 // completed: it must not have caught a failed allocation
87 EXPECT_EQ(refused_allocations, 0u) << "no std::bad_alloc reached the caller";
88 break;
89 }
90 ++failures;
91
92 double y[] = {0.0, 1.0, 3.0}; // y <= 3
93 ASSERT_NO_THROW(simplex.put_restriction(y));
94 ASSERT_EQ(simplex.get_num_restrictions(), 2u);
95 simplex.prepare_linear_program();
96 ASSERT_EQ(simplex.solve(), Simplex<double>::Solved);
97 simplex.load_solution(); // x = 1, y = 3
98 EXPECT_NEAR(simplex.get_solution(0), 1.0, 1e-9);
99 EXPECT_NEAR(simplex.get_solution(1), 3.0, 1e-9);
100 EXPECT_NEAR(simplex.objetive_value(), 7.0, 1e-9);
101 }
102 EXPECT_GT(failures, 0u);
103}
Simplex algorithm for linear programming.
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Linear program solver using the Simplex method.
Definition Simplex.H:279
T objetive_value() const noexcept
Computes and returns the objective function value.
Definition Simplex.H:1374
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
Definition Simplex.H:1132
void prepare_linear_program()
Prepares the linear program for solving.
Definition Simplex.H:1287
size_t get_num_restrictions() const noexcept
Gets the number of constraints.
Definition Simplex.H:1216
const T & get_solution(size_t i) const noexcept
Gets the solution value for a specific variable.
Definition Simplex.H:1360
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
Definition Simplex.H:1072
void load_solution() noexcept
Loads the solution values into the solution array.
Definition Simplex.H:1337
State solve()
Solves the linear program.
Definition Simplex.H:1310
#define TEST(name)
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.