Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExtremumTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
4#include <iostream>
5
10
11namespace {
13
14storm::RationalNumber rational(double value) {
16}
17} // namespace
18
19TEST(ExtremumTest, emptyExtremumIsTheInfinityItStandsFor) {
20 // The extremum over an empty set is +infinity if we minimize and -infinity if we maximize. That is a value like
21 // any other, so reading it needs no special case.
23 EXPECT_TRUE(minimum.empty());
24 EXPECT_TRUE(storm::utility::isInfinity(minimum.getExtendedValue()));
25
27 EXPECT_TRUE(maximum.empty());
28 EXPECT_TRUE(storm::utility::isNegativeInfinity(maximum.getExtendedValue()));
29
30 // A value type that brings its own infinity has always behaved this way.
32 EXPECT_TRUE(doubleMinimum.empty());
33 EXPECT_TRUE(storm::utility::isInfinity(doubleMinimum.getExtendedValue()));
34}
35
36TEST(ExtremumTest, holdsAnInfiniteValue) {
37 // A minimum that has been given +infinity and one that has been given nothing are the same thing: the minimum over
38 // {+infinity} is +infinity, and so is the minimum over the empty set. What matters is that the value is right.
40 EXPECT_FALSE(minimum &= ExtendedRationalNumber::infinity());
41 EXPECT_TRUE(storm::utility::isInfinity(minimum.getExtendedValue()));
42
43 // The infinity that is not the base value is stored like any other value.
45 EXPECT_TRUE(other &= ExtendedRationalNumber::negativeInfinity());
46 EXPECT_FALSE(other.empty());
48
49 // Maximizing, +infinity is an improvement over anything and is kept.
51 EXPECT_TRUE(maximum &= rational(2));
52 EXPECT_TRUE(maximum &= ExtendedRationalNumber::infinity());
53 EXPECT_FALSE(maximum.empty());
54 EXPECT_TRUE(storm::utility::isInfinity(maximum.getExtendedValue()));
55 // Nothing finite improves upon it any more.
56 EXPECT_FALSE(maximum &= rational(1000000));
57}
58
59TEST(ExtremumTest, ordinaryUseIsUnchanged) {
61 EXPECT_TRUE(minimum &= rational(5));
62 EXPECT_FALSE(minimum.empty());
63 EXPECT_EQ(rational(5), *minimum);
64 EXPECT_TRUE(minimum.better(rational(3)));
65 EXPECT_FALSE(minimum.better(rational(7)));
66 EXPECT_TRUE(minimum &= rational(3));
67 EXPECT_FALSE(minimum &= rational(7));
68 EXPECT_EQ(rational(3), *minimum);
69 ASSERT_TRUE(minimum.getOptionalValue().has_value());
70 EXPECT_EQ(rational(3), minimum.getOptionalValue().value());
71
72 minimum.reset();
73 EXPECT_TRUE(minimum.empty());
74 EXPECT_FALSE(minimum.getOptionalValue().has_value());
75
76 // Combining two extrema takes the better of the two.
77 storm::utility::Maximum<storm::RationalNumber> first(rational(2)), second(rational(4));
78 EXPECT_TRUE(first &= second);
79 EXPECT_EQ(rational(4), *first);
80 EXPECT_FALSE(second &= storm::utility::Maximum<storm::RationalNumber>());
81 EXPECT_EQ(rational(4), *second);
82}
83
84TEST(ExtremumTest, derefHasNoPlainValueForAnInfiniteOne) {
85 // The plain value type has no infinity, so there is nothing for operator* to hand out. The extended value is the
86 // way to read an empty extremum.
88 EXPECT_TRUE(storm::utility::isInfinity(minimum.getExtendedValue()));
89}
90
91TEST(ExtremumDeathTest, derefRefusesAnEmptyExtremum) {
92 // Being non-empty is a precondition of operator*, so a violation is caught by an assertion rather than an
93 // exception. It must not return the zero that an infinite value carries as its payload.
95
96#ifndef NDEBUG
97 EXPECT_DEATH_IF_SUPPORTED(*minimum, "");
98#else
99 std::cerr << "WARNING: Not testing the operator* assertion, as it is disabled in release mode.\n";
100 SUCCEED();
101#endif
102}
TEST(ExtremumTest, emptyExtremumIsTheInfinityItStandsFor)
storm::utility::ExtendedValueType< ValueType > const & getExtendedValue() const
Definition Extremum.cpp:121
Extremum< storm::OptimizationDirection::Maximize, ValueType > Maximum
Definition Extremum.h:166
Extremum< storm::OptimizationDirection::Minimize, ValueType > Minimum
Definition Extremum.h:168
bool isNegativeInfinity(ExtendedNumber< ValueType > const &number)
bool isInfinity(ValueType const &a)
TargetType convertNumber(SourceType const &number)
storm::utility::ExtendedValueType< RationalNumber > ExtendedRationalNumber
The rational number type extended with the two infinities.