Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/unittest/src/utils/common/ValueTimeLineTest.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file ValueTimeLineTest.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Matthias Heppner
17
/// @author Michael Behrisch
18
/// @date Sept 2009
19
///
20
// Tests ValueTimeLine class from <SUMO>/src/utils/common
21
/****************************************************************************/
22
23
24
// ===========================================================================
25
// included modules
26
// ===========================================================================
27
#include <config.h>
28
29
#include <gtest/gtest.h>
30
#include <utils/common/ValueTimeLine.h>
31
32
33
// ===========================================================================
34
// test definitions
35
// ===========================================================================
36
/* Tests what happens if one tries to get a value from an empty ValueTimeLine. */
37
/*
38
TEST(ValueTimeLine, test_get_from_empty) {
39
ValueTimeLine<int> vtl;
40
EXPECT_EQ(1, vtl.getValue(0)) << "Something should happen if nothing was stored.";
41
}
42
*/
43
44
45
// --------------------------------
46
// plain retrieval / overwriting tests
47
// --------------------------------
48
49
/* Tests what happens if one tries to get a stored value (one value stored, fillGaps not called). */
50
TEST(ValueTimeLine, test_get_single_nocollect) {
51
ValueTimeLine<int> vtl;
52
vtl.add(0, 100, 2);
53
EXPECT_EQ(2, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
54
EXPECT_EQ(2, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
55
EXPECT_EQ(2, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
56
}
57
58
/* Tests what happens if one tries to get a stored value (three values stored, fillGaps not called). */
59
TEST(ValueTimeLine, test_get_multi_nocollect) {
60
ValueTimeLine<int> vtl;
61
vtl.add(0, 100, 1);
62
vtl.add(100, 200, 2);
63
vtl.add(200, 300, 3);
64
EXPECT_EQ(1, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
65
EXPECT_EQ(1, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
66
EXPECT_EQ(1, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
67
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
68
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
69
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
70
EXPECT_EQ(3, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
71
EXPECT_EQ(3, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
72
EXPECT_EQ(3, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
73
}
74
75
/* Tests what happens if one tries to get a stored value (one value stored, fillGaps called). */
76
TEST(ValueTimeLine, test_get_single_collect) {
77
ValueTimeLine<int> vtl;
78
vtl.add(0, 100, 2);
79
vtl.fillGaps(0);
80
EXPECT_EQ(2, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
81
EXPECT_EQ(2, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
82
EXPECT_EQ(2, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
83
}
84
85
/* Tests what happens if one tries to get a stored value (three values stored, fillGaps called). */
86
TEST(ValueTimeLine, test_get_multi_collect) {
87
ValueTimeLine<int> vtl;
88
vtl.add(0, 100, 1);
89
vtl.add(100, 200, 2);
90
vtl.add(200, 300, 3);
91
vtl.fillGaps(0);
92
EXPECT_EQ(1, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
93
EXPECT_EQ(1, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
94
EXPECT_EQ(1, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
95
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
96
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
97
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
98
EXPECT_EQ(3, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
99
EXPECT_EQ(3, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
100
EXPECT_EQ(3, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
101
}
102
103
// --------------------------------
104
// overwriting filling tests
105
// --------------------------------
106
107
/* Tests what happens if one overwrites a value (three values stored, fillGaps not called). */
108
TEST(ValueTimeLine, test_overwrite_nocollect) {
109
ValueTimeLine<int> vtl;
110
vtl.add(0, 100, 1);
111
vtl.add(200, 300, 3);
112
vtl.add(50, 250, 2);
113
EXPECT_EQ(1, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
114
EXPECT_EQ(2, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
115
EXPECT_EQ(2, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
116
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
117
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
118
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
119
EXPECT_EQ(2, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
120
EXPECT_EQ(3, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
121
EXPECT_EQ(3, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
122
}
123
124
/* Tests what happens if one overwrites a value (three values stored, fillGaps called). */
125
TEST(ValueTimeLine, test_overwrite_collect) {
126
ValueTimeLine<int> vtl;
127
vtl.add(0, 100, 1);
128
vtl.add(200, 300, 3);
129
vtl.add(50, 250, 2);
130
vtl.fillGaps(0);
131
EXPECT_EQ(1, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
132
EXPECT_EQ(2, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
133
EXPECT_EQ(2, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
134
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
135
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
136
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
137
EXPECT_EQ(2, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
138
EXPECT_EQ(3, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
139
EXPECT_EQ(3, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
140
}
141
142
/* Tests what happens if one overwrites a value (three values stored, fillGaps not called, order changed). */
143
TEST(ValueTimeLine, test_overwrite_nocollect2) {
144
ValueTimeLine<int> vtl;
145
vtl.add(50, 250, 2);
146
vtl.add(0, 100, 1);
147
vtl.add(200, 300, 3);
148
EXPECT_EQ(1, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
149
EXPECT_EQ(1, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
150
EXPECT_EQ(1, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
151
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
152
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
153
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
154
EXPECT_EQ(3, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
155
EXPECT_EQ(3, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
156
EXPECT_EQ(3, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
157
}
158
159
/* Tests what happens if one overwrites a value (three values stored, fillGaps called, order changed). */
160
TEST(ValueTimeLine, test_overwrite_collect2) {
161
ValueTimeLine<int> vtl;
162
vtl.add(50, 250, 2);
163
vtl.add(0, 100, 1);
164
vtl.add(200, 300, 3);
165
vtl.fillGaps(0);
166
EXPECT_EQ(1, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
167
EXPECT_EQ(1, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
168
EXPECT_EQ(1, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
169
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
170
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
171
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
172
EXPECT_EQ(3, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
173
EXPECT_EQ(3, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
174
EXPECT_EQ(3, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
175
}
176
177
178
// --------------------------------
179
// gap filling tests
180
// --------------------------------
181
182
/* Tests what happens if one overwrites a value (three values stored, fillGaps called). */
183
TEST(ValueTimeLine, test_fill_gaps_withbounds) {
184
ValueTimeLine<int> vtl;
185
vtl.add(50, 250, 2);
186
vtl.fillGaps(4, true);
187
EXPECT_EQ(2, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
188
EXPECT_EQ(2, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
189
EXPECT_EQ(2, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
190
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
191
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
192
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
193
EXPECT_EQ(2, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
194
EXPECT_EQ(2, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
195
EXPECT_EQ(2, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
196
}
197
198
199
/* Tests what happens if one overwrites a value (three values stored, fillGaps called). */
200
TEST(ValueTimeLine, test_fill_gaps_nobounds) {
201
ValueTimeLine<int> vtl;
202
vtl.add(50, 250, 2);
203
vtl.fillGaps(4, false);
204
EXPECT_EQ(4, vtl.getValue(0)) << "The stored number should be returned when asking within the interval.";
205
EXPECT_EQ(2, vtl.getValue(99)) << "The stored number should be returned when asking within the interval.";
206
EXPECT_EQ(2, vtl.getValue(50)) << "The stored number should be returned when asking within the interval.";
207
EXPECT_EQ(2, vtl.getValue(100)) << "The stored number should be returned when asking within the interval.";
208
EXPECT_EQ(2, vtl.getValue(199)) << "The stored number should be returned when asking within the interval.";
209
EXPECT_EQ(2, vtl.getValue(150)) << "The stored number should be returned when asking within the interval.";
210
EXPECT_EQ(2, vtl.getValue(200)) << "The stored number should be returned when asking within the interval.";
211
EXPECT_EQ(4, vtl.getValue(299)) << "The stored number should be returned when asking within the interval.";
212
EXPECT_EQ(4, vtl.getValue(250)) << "The stored number should be returned when asking within the interval.";
213
}
214
215