./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 34
100.0%
Branches:
49 / 49
100.0%
Functions:
5 / 5
100.0%

src/distribution/composition.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "distribution/composition.h"
4
5 #include <stdexcept>
6
7 namespace terraingen::detail {
8
9 1641 SfdKind CommonKind(Span<const SizeFrequencyDistribution* const> distributions,
10 const std::string& what) {
11
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1634 times.
1641 if (distributions.empty()) {
12
2/2
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 7 times.
14 throw std::invalid_argument(what + " needs at least one distribution");
13 }
14
2/2
✓ Branch 0 taken 2899 times.
✓ Branch 1 taken 1624 times.
4523 for (const SizeFrequencyDistribution* distribution : distributions) {
15
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2893 times.
2899 if (distribution == nullptr) {
16
2/2
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
12 throw std::invalid_argument(what + " cannot hold a null distribution");
17 }
18
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2889 times.
2893 if (distribution->Kind() != distributions.front()->Kind()) {
19
2/2
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
8 throw std::invalid_argument(what + " needs distributions of one kind");
20 }
21 }
22 1624 return distributions.front()->Kind();
23 }
24
25 2005 DiameterRange UsableRange(const SizeFrequencyDistribution& distribution) {
26
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1976 times.
2005 return distribution.AllowsExtrapolation() ? DiameterRange{} : distribution.ValidRange();
27 }
28
29 884 DiameterRange IntersectRanges(Span<const DiameterRange> ranges, const std::string& message) {
30 884 DiameterRange range;
31
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 884 times.
2289 for (const DiameterRange& each : ranges) {
32 1405 range.min = std::fmax(range.min, each.min);
33 1405 range.max = std::fmin(range.max, each.max);
34 }
35
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 881 times.
884 if (!(range.min < range.max)) {
36
1/1
✓ Branch 2 taken 3 times.
3 throw std::invalid_argument(message);
37 }
38 881 return range;
39 }
40
41 780 void CheckStarts(Span<const double> starts, const DiameterRange& range, const std::string& what) {
42
4/4
✓ Branch 0 taken 779 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 778 times.
1559 if (starts.empty() || starts.front() != range.min) {
43
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
4 throw std::invalid_argument(what + " starts must begin at the range minimum");
44 }
45
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 771 times.
1555 for (std::size_t i = 1; i < starts.size(); ++i) {
46
6/6
✓ Branch 1 taken 783 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 780 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 777 times.
✓ Branch 7 taken 3 times.
784 if (!(std::isfinite(starts[i]) && starts[i] > starts[i - 1] && starts[i] < range.max)) {
47
2/2
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 7 times.
14 throw std::invalid_argument(what + " starts must increase within the range");
48 }
49 }
50 771 }
51
52 779 void CheckJoin(double below, double above, double start, double tolerance,
53 bool allowDiscontinuities) {
54
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 774 times.
779 if (above > below * (1.0 + tolerance)) {
55 5 throw std::invalid_argument("A cumulative distribution cannot rise at the join at " +
56
3/3
✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
✓ Branch 8 taken 5 times.
20 std::to_string(start) + " m");
57 }
58
4/4
✓ Branch 0 taken 767 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 764 times.
774 if (!allowDiscontinuities && above < below * (1.0 - tolerance)) {
59
1/1
✓ Branch 1 taken 3 times.
6 throw std::invalid_argument("The pieces are discontinuous at " + std::to_string(start) +
60
2/2
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
9 " m");
61 }
62 771 }
63
64 } // namespace terraingen::detail
65