Skip to content

Commit c6c23dc

Browse files
Added more test, formatted with clang-format
1 parent 1fd6081 commit c6c23dc

File tree

2 files changed

+62
-59
lines changed

2 files changed

+62
-59
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
Checks: '-*,google-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,cppcoreguidelines-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-*,openmp-*,performance-*,portability-*,modernize-*,-modernize-use-trailing-*'
33
WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-performance-move-const-arg,-performance-noexcept-move-constructor,-performance-unnecessary-value-param,-cppcoreguidelines-init-variables,-cppcoreguidelines-pro-*,-cppcoreguidelines-owning-memory,-clang-analyzer-cplusplus.Move'
44
HeaderFilterRegex: ''
5-
AnalyzeTemporaryDtors: false
5+
# AnalyzeTemporaryDtors: false
66
FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: true, ColumnLimit: 80, AccessModifierOffset: -3, AlignConsecutiveMacros: true }'

probability/exponential_dist.cpp

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,103 @@
11
/**
22
* @file
3-
* @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
4-
*
3+
* @brief [Exponential
4+
* Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
5+
*
56
* The exponential distribution is used to model
67
* events occuring between a Poisson process like radioactive decay.
7-
*
8+
*
89
* \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
9-
*
10+
*
1011
* Summary of variables used:
1112
* \f$\lambda\f$ : rate parameter
1213
*/
1314

14-
#include<cmath> // For std::pow
15-
#include<cassert> // For assert
16-
#include<iostream> // For I/O operation
15+
#include <cassert> // For asserting the test cases
16+
#include <cmath> // For power function
17+
#include <iostream> // For I/O operation
1718

18-
/**
19+
/**
1920
* @brief the expected value of the exponential distribution
2021
* @returns \f[\mu = \frac{1}{\lambda}\f]
2122
*/
22-
double exponential_expected(double lambda){
23-
if (lambda<=0){
24-
std::cout << "Error: Lambda must be greater than 0." << std::endl;
25-
assert(lambda>0);
23+
double exponential_expected(double lambda) {
24+
if (lambda <= 0) {
25+
std::cout << "Error: Lambda must be greater than 0." << '\n';
26+
assert(lambda > 0);
2627
}
27-
return 1/lambda;
28+
return 1 / lambda;
2829
}
2930

30-
/**
31+
/**
3132
* @brief the variance of the exponential distribution
3233
* @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f]
3334
*/
34-
double exponential_var(double lambda){
35-
if (lambda<=0){
36-
std::cout << "Error: Lambda must be greater than 0." << std::endl;
37-
assert(lambda>0);
35+
double exponential_var(double lambda) {
36+
if (lambda <= 0) {
37+
std::cout << "Error: Lambda must be greater than 0." << '\n';
38+
assert(lambda > 0);
3839
}
39-
return 1/pow(lambda,2);
40+
return 1 / pow(lambda, 2);
4041
}
4142

42-
/**
43+
/**
4344
* @brief the standard deviation of the exponential distribution
4445
* @returns \f[\sigma = \frac{1}{\lambda}\f]
4546
*/
46-
double exponential_std(double lambda){
47-
if (lambda<=0){
48-
std::cout << "Error: Lambda must be greater than 0." << std::endl;
49-
assert(lambda>0);
47+
double exponential_std(double lambda) {
48+
if (lambda <= 0) {
49+
std::cout << "Error: Lambda must be greater than 0." << '\n';
50+
assert(lambda > 0);
5051
}
51-
return 1/lambda;
52+
return 1 / lambda;
5253
}
5354

5455
/**
55-
* @brief Self-test implementation
56+
* @brief Self-test implementations
5657
* @returns void
5758
*/
58-
static void test(){
59-
double lambda = 2;
60-
double expected = 0.5;
61-
double var = 0.25;
62-
double std = 0.5;
59+
static void test() {
60+
double lambda_1 = 1;
61+
double expected_1 = 1;
62+
double var_1 = 1;
63+
double std_1 = 1;
64+
65+
double lambda_2 = 2;
66+
double expected_2 = 0.5;
67+
double var_2 = 0.25;
68+
double std_2 = 0.5;
6369

64-
//Test 1
65-
std::cout << "Expected Value" << std::endl;
66-
std::cout << "Lambda : " << lambda << std::endl;
67-
std::cout << "Expected Output : " << expected <<std::endl;
68-
std::cout << "Output : " << exponential_expected(lambda) << std::endl;
69-
assert(exponential_expected(lambda)==expected);
70-
std::cout << "TEST PASSED" << std::endl;
71-
std::cout<<std::endl;
70+
double lambda_3 = 3;
71+
double expected_3 = 0.333333;
72+
double var_3 = 0.111111;
73+
double std_3 = 0.333333;
7274

73-
//Test 2
74-
std::cout << "Variance" << std::endl;
75-
std::cout << "Lambda : " << lambda << std::endl;
76-
std::cout << "Expected Output : " << var <<std::endl;
77-
std::cout << "Output : " << exponential_var(lambda) << std::endl;
78-
assert(exponential_var(lambda)==var);
79-
std::cout << "TEST PASSED" << std::endl;
80-
std::cout<<std::endl;
75+
const float threshold = 1e-3f;
8176

82-
//Test 3
83-
std::cout << "Standard Deviation" << std::endl;
84-
std::cout << "Lambda : " << lambda << std::endl;
85-
std::cout << "Expected Output : " << std <<std::endl;
86-
std::cout << "Output : " << exponential_std(lambda) << std::endl;
87-
assert(exponential_std(lambda)==std);
88-
std::cout << "TEST PASSED" << std::endl;
89-
std::cout<<std::endl;
77+
std::cout << "Test for lambda = 1 \n";
78+
assert(std::abs(expected_1 - exponential_expected(lambda_1)) < threshold);
79+
assert(std::abs(var_1 - exponential_var(lambda_1)) < threshold);
80+
assert(std::abs(std_1 - exponential_std(lambda_1)) < threshold);
81+
std::cout << "ALL TEST PASSED\n\n";
82+
83+
std::cout << "Test for lambda = 2 \n";
84+
assert(std::abs(expected_2 - exponential_expected(lambda_2)) < threshold);
85+
assert(std::abs(var_2 - exponential_var(lambda_2)) < threshold);
86+
assert(std::abs(std_2 - exponential_std(lambda_2)) < threshold);
87+
std::cout << "ALL TEST PASSED\n\n";
88+
89+
std::cout << "Test for lambda = 3 \n";
90+
assert(std::abs(expected_3 - exponential_expected(lambda_3)) < threshold);
91+
assert(std::abs(var_3 - exponential_var(lambda_3)) < threshold);
92+
assert(std::abs(std_3 - exponential_std(lambda_3)) < threshold);
93+
std::cout << "ALL TEST PASSED\n";
9094
}
9195

9296
/**
9397
* @brief Main function
9498
* @return 0 on exit
9599
*/
96-
int main(){
97-
test(); // Self test implementation
100+
int main() {
101+
test(); // Self test implementation
98102
return 0;
99103
}
100-

0 commit comments

Comments
 (0)