Branch data Line data Source code
1 : : /**
2 : : * \author: Abed Yassine
3 : : * \date: July 29th, 2020
4 : : * \file: util_tests.cxx
5 : : *
6 : : * \brief contains unit tests for functions in Util namespace
7 : : */
8 : :
9 : : #include "gtest/gtest.h"
10 : :
11 : : #include "util.h"
12 : :
13 : : #include <vector>
14 : : #include <random>
15 : : #include <cmath>
16 : : #include <iostream>
17 : :
18 : :
19 : : /**
20 : : * This test test the regression functionality under Util that returns the slope of a linear fit given (x,y) data points
21 : : */
22 [ + - ][ + - ]: 32 : GTEST_TEST(UtilTests, SlopeRegression)
[ + - ][ - + ]
23 : : {
24 : 4 : unsigned num_tests = 100;
25 [ + - ]: 4 : std::mt19937 mt_random_generator(100);
26 : :
27 [ + - ]: 4 : std::uniform_int_distribution<unsigned> rng_un(2, 100);
28 [ + - ]: 4 : std::uniform_real_distribution<double> rng_d(0.0, 1.0);
29 [ + - ]: 4 : std::uniform_real_distribution<double> rand_noise(-2.0, 2.0);
30 : :
31 [ + + ]: 404 : for (unsigned i = 0; i < num_tests; i++)
32 : : {
33 [ + - ]: 400 : unsigned rand_size = rng_un(mt_random_generator);
34 [ + - ]: 400 : double rand_slope = 100.0*rng_d(mt_random_generator) - 50.0;
35 [ + - ]: 400 : double rand_intercept = 200.0*rng_d(mt_random_generator) - 200.0;
36 [ + - ][ + - ]: 800 : vector<double> x(rand_size), y(rand_size);
37 [ + + ]: 19284 : for (unsigned j = 0; j < rand_size; j++) {
38 [ + - ]: 18884 : x[j] = j + rand_noise(mt_random_generator);
39 : 18884 : y[j] = rand_slope*x[j] + rand_intercept;
40 : : }
41 : :
42 [ + - ]: 400 : double actual = Util::compute_slope_regression(x, y);
43 : :
44 [ + - ][ - + ]: 400 : EXPECT_NEAR(actual, rand_slope, 1e-2) << "Testing regression slope. Iteration: " << i;
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
45 : : }
46 : 4 : }
47 : :
48 : : /**
49 : : * This test tests the functionality of the tri-modal normal random distribution class
50 : : */
51 [ + - ][ + - ]: 32 : GTEST_TEST(UtilTests, TriModalDistribution) {
[ + - ][ - + ]
52 : 4 : unsigned num_tests = 10000;
53 [ + - ]: 4 : std::mt19937 mt_random_generator(100);
54 [ + - ]: 8 : Util::multimodal_distribution<double> rng_multi(5.0, 1.0); // Center model has a mean value of 5 and sigma of 0.5
55 : :
56 [ + - ]: 8 : std::vector<int> stars(10,0);
57 [ + + ]: 40004 : for (unsigned i = 0; i < num_tests; i++) {
58 [ + - ]: 40000 : size_t temp = static_cast<size_t>(rng_multi(mt_random_generator));
59 : 40000 : stars[temp]++;
60 : : }
61 : :
62 [ + - ]: 8 : vector<double> expected_percentages = {0.0, 1.0, 22.0, 20.0, 7.0, 7.0, 20.0, 22.0, 1.0, 0.0};
63 [ + + ]: 44 : for (unsigned i = 0; i < 10; i++) {
64 : 40 : int s = stars[i];
65 : 40 : double percentage = (s*1.0/num_tests)*100.0;
66 : 40 : double expected = expected_percentages[i];
67 : 40 : double tol = 3;
68 : :
69 [ + - ][ + - ]: 40 : EXPECT_TRUE(percentage >= expected - tol && percentage <= expected + tol);
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ]
70 : : }
71 [ + - ][ + - ]: 20 : }
|