Directory: | ./ |
---|---|
File: | tests/test-distribution.cc |
Date: | 2025-03-06 12:03:19 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 22 | 73 | 30.1% |
Branches: | 13 | 190 | 6.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2014, LAAS-CNRS | ||
2 | // Authors: Joseph Mirabel (joseph.mirabel@laas.fr) | ||
3 | // | ||
4 | // This file is part of hpp-statistics. | ||
5 | // hpp-statistics is free software: you can redistribute it | ||
6 | // and/or modify it under the terms of the GNU Lesser General Public | ||
7 | // License as published by the Free Software Foundation, either version | ||
8 | // 3 of the License, or (at your option) any later version. | ||
9 | // | ||
10 | // hpp-statistics is distributed in the hope that it will be | ||
11 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
12 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | // General Lesser Public License for more details. You should have | ||
14 | // received a copy of the GNU Lesser General Public License along with | ||
15 | // hpp-statistics. If not, see <http://www.gnu.org/licenses/>. | ||
16 | |||
17 | #include <time.h> | ||
18 | |||
19 | #include <cfloat> | ||
20 | #include <iostream> | ||
21 | |||
22 | #include "hpp/statistics/distribution.hh" | ||
23 | |||
24 | 1 | int test1() { | |
25 | using namespace hpp::statistics; | ||
26 | typedef int Value; | ||
27 | typedef DiscreteDistribution<Value> Distrib; | ||
28 | 1 | Distrib dd; | |
29 | 1 | const int nbValues = 3; | |
30 | 1 | const Value values[] = {1, 2, 3}; | |
31 | 1 | const Distrib::Weight_t weight[] = {0, 1, 1}; | |
32 | 1 | Distrib::Weight_t total_weight = 0; | |
33 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (int i = 0; i < nbValues; i++) { |
34 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | dd.insert(values[i], weight[i]); |
35 | 3 | total_weight += weight[i]; | |
36 | } | ||
37 | |||
38 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::vector<Proba_t> p = dd.probabilities(); |
39 | |||
40 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
|
4 | for (size_t i = 0; i < p.size(); i++) |
41 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
6 | if (p[i] - ((double)weight[i] / (double)total_weight) > DBL_EPSILON || |
42 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | -p[i] + ((double)weight[i] / (double)total_weight) > DBL_EPSILON) { |
43 | ✗ | std::cout << "p[" << i << "] = " << p[i] << std::endl | |
44 | ✗ | << "weight[" << i << "] = " << weight[i] << std::endl | |
45 | ✗ | << "Total weight = " << total_weight << std::endl | |
46 | ✗ | << "Difference = " | |
47 | ✗ | << p[i] - ((double)weight[i] / (double)total_weight) | |
48 | ✗ | << std::endl; | |
49 | ✗ | return EXIT_FAILURE; | |
50 | } | ||
51 | |||
52 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (dd((Distrib::Weight_t)0) != values[1]) { |
53 | ✗ | std::cout << "dichotomy (" << 0 << ") returns " << dd((Distrib::Weight_t)0) | |
54 | ✗ | << std::endl; | |
55 | ✗ | return EXIT_FAILURE; | |
56 | } | ||
57 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (dd((Distrib::Weight_t)1) != values[2]) { |
58 | ✗ | std::cout << "dichotomy (" << 1 << ") returns " << dd((Distrib::Weight_t)1) | |
59 | ✗ | << std::endl; | |
60 | ✗ | return EXIT_FAILURE; | |
61 | } | ||
62 | 1 | return EXIT_SUCCESS; | |
63 | 1 | } | |
64 | |||
65 | ✗ | int test2() { | |
66 | using namespace hpp::statistics; | ||
67 | typedef int Value; | ||
68 | typedef DiscreteDistribution<Value> Distrib; | ||
69 | ✗ | Distrib dd; | |
70 | ✗ | const int nbValues = 4; | |
71 | ✗ | const Value values[] = {1, 3, 2, 4}; | |
72 | ✗ | const Distrib::Weight_t weight[] = {1, 23, 99, 6}; | |
73 | ✗ | Distrib::Weight_t total_weight = 0; | |
74 | ✗ | dd.insert(values[0], 9.0); | |
75 | ✗ | for (int i = 0; i < nbValues; i++) { | |
76 | ✗ | dd.insert(values[i], weight[i]); | |
77 | ✗ | total_weight += weight[i]; | |
78 | } | ||
79 | |||
80 | ✗ | std::vector<Proba_t> p = dd.probabilities(); | |
81 | |||
82 | ✗ | for (size_t i = 0; i < p.size(); i++) | |
83 | ✗ | if (p[i] - ((double)weight[i] / (double)total_weight) > DBL_EPSILON || | |
84 | ✗ | -p[i] + ((double)weight[i] / (double)total_weight) > DBL_EPSILON) { | |
85 | ✗ | std::cout << "p[" << i << "] = " << p[i] << std::endl | |
86 | ✗ | << "weight[" << i << "] = " << weight[i] << std::endl | |
87 | ✗ | << "Total weight = " << total_weight << std::endl | |
88 | ✗ | << "Difference = " | |
89 | ✗ | << p[i] - ((double)weight[i] / (double)total_weight) | |
90 | ✗ | << std::endl; | |
91 | ✗ | return EXIT_FAILURE; | |
92 | } | ||
93 | |||
94 | ✗ | if (dd((Distrib::Weight_t)0) != values[0]) { | |
95 | ✗ | std::cout << "dichotomy (" << 0 << ") returns " << dd((Distrib::Weight_t)0) | |
96 | ✗ | << std::endl; | |
97 | ✗ | return EXIT_FAILURE; | |
98 | } | ||
99 | ✗ | if (dd((Distrib::Weight_t)21) != values[1]) { | |
100 | ✗ | std::cout << "dichotomy (" << 21 << ") returns " | |
101 | ✗ | << dd((Distrib::Weight_t)21) << std::endl; | |
102 | ✗ | return EXIT_FAILURE; | |
103 | } | ||
104 | ✗ | if (dd((Distrib::Weight_t)50) != values[2]) { | |
105 | ✗ | std::cout << "dichotomy (" << 50 << ") returns " | |
106 | ✗ | << dd((Distrib::Weight_t)50) << std::endl; | |
107 | ✗ | return EXIT_FAILURE; | |
108 | } | ||
109 | ✗ | if (dd((Distrib::Weight_t)125) != values[3]) { | |
110 | ✗ | std::cout << "dichotomy (" << 125 << ") returns " | |
111 | ✗ | << dd((Distrib::Weight_t)125) << std::endl; | |
112 | ✗ | return EXIT_FAILURE; | |
113 | } | ||
114 | ✗ | return EXIT_SUCCESS; | |
115 | } | ||
116 | |||
117 | 1 | int main() { | |
118 | /* initialize random seed: */ | ||
119 | 1 | srand((unsigned int)time(NULL)); | |
120 | |||
121 | 1 | std::cout.precision(15); | |
122 | |||
123 | 1 | int exit_status = test1(); | |
124 | // exit_status |= test2 (); | ||
125 | 1 | return exit_status; | |
126 | } | ||
127 |