File: timed_test.cpp

package info (click to toggle)
libfplus 0.2.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: cpp: 27,543; javascript: 634; sh: 105; python: 103; makefile: 6
file content (180 lines) | stat: -rw-r--r-- 5,114 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"

#include <vector>
#include <cmath>
#include <chrono>
#include <fplus/fplus.hpp>

// Utility functions
namespace
{
    void require_are_execution_times_close(double t1, double t2)
    {
        // up to 30 ms difference, since the cpu scheduler might switch to another process during a sleep
        double max_acceptable_delta__task_scheduler = 0.03;
        #if defined(__APPLE__)
            max_acceptable_delta__task_scheduler = 0.1; // travis osx builders are slow...
        #endif
        REQUIRE(fabs(t1 - t2) < max_acceptable_delta__task_scheduler);
    }

    template<typename T>
    void require_are_timed_equal(const fplus::timed<T> & a, const fplus::timed<T> & b)
    {
        REQUIRE(a.get() == b.get());
        require_are_execution_times_close(a.time_in_s(), b.time_in_s());
    }

    void sleep_seconds(double sleep_seconds)
    {
        long long sleep_ns = static_cast<long long>(sleep_seconds * 1E9);
        std::this_thread::sleep_for(std::chrono::nanoseconds(sleep_ns));
    }

    int add(int a, int b)  // a simple function that will be decorated
    {
        sleep_seconds(0.002);
        return a + b;
    }

    void void_function()
    {
        sleep_seconds(0.002);
    }
}


// Test timed class
TEST_CASE("timed, ctor")
{
    using namespace fplus;
    {
        // default constructor
        fplus::timed<double> v;
        REQUIRE(v.get() == doctest::Approx(0.));
        REQUIRE(v.time_in_s() == doctest::Approx(0.));
    }
    {
        // T +  time constructor
        fplus::timed<double> v(1., 3.);
        REQUIRE(v.get() == doctest::Approx(1.));
        REQUIRE(v.time_in_s() == doctest::Approx(3.));
    }
    {
        // timed<T> constructor
        fplus::timed<double> v1(2.);
        auto v2(v1);
        REQUIRE(v1.time_in_s() == doctest::Approx(v2.time_in_s()));
        REQUIRE(v1.get() == doctest::Approx(v2.get()));
    }
}

TEST_CASE("timed, operator=")
{
    {
        auto v1 = fplus::timed<double>(1.);
        auto v2 = fplus::timed<double>(2.);
        REQUIRE(v1.get() != doctest::Approx(v2.get()));
        v2 = v1;
        REQUIRE(v1.get() == doctest::Approx(v2.get()));
    }
}


TEST_CASE("timed, show_timed")
{
    {
        fplus::timed<int> v(42, 1);
        auto s = show_timed(v);
        REQUIRE_EQ(s, "42 (1000ms)");
    }
}

TEST_CASE("timed, duration_in_s")
{
    {
        fplus::timed<int> v(42, 1.2345);
        auto d = v.duration_in_s();
        double seconds = d.count();
        REQUIRE( seconds == doctest::Approx(1.2345) );
    }
}


// Test make_timed_function
TEST_CASE("make_timed_function")
{
    using namespace fplus;

    {
        // Test decorated function
        auto add_timed = make_timed_function(add);
        auto result = add_timed(39, 3);
        auto expected = timed<int>(42, 0.02);
        require_are_timed_equal(result, expected);
    }

    {
        // Test void function
        auto void_function_timed = fplus::make_timed_void_function(void_function);
        auto execution_time = void_function_timed();
        require_are_execution_times_close(execution_time, 0.02);
    }

    {
        // Test decorated lambda
        auto sub = [](int a, int b) -> int {
            sleep_seconds(0.03);
            return a - b;
        };
        auto sub_timed = make_timed_function(sub);
        auto result = sub_timed(45, 3);
        auto expected = timed<int>(42, 0.03);
        require_are_timed_equal(result, expected);
    }

    {
        // Test decorated void lambda
        auto fn = []() {
            sleep_seconds(0.03);
        };
        auto fn_timed = make_timed_void_function(fn);
        auto execution_time = fn_timed();
        require_are_execution_times_close(execution_time, 0.03);
    }

    {
        // Test std::function
        auto sub_lambda = [](int a, int b) -> int {
            sleep_seconds(0.03);
            return a - b;
        };
        std::function<int(int,int)> sub = sub_lambda;
        auto sub_timed = make_timed_function(sub);
        auto result = sub_timed(45, 3);
        auto expected = timed<int>(42, 0.03);
        require_are_timed_equal(result, expected);
    }

    {
        using Ints = std::vector<int>;

        Ints ascending_numbers = fplus::numbers(0, 1000);
        Ints shuffled_numbers = fplus::shuffle(std::mt19937::default_seed, ascending_numbers);

        auto sort_func = [](const Ints& values) { return fplus::sort(values); };
        auto sort_bench = fplus::make_timed_function(sort_func);

        auto sorted_numbers = sort_bench(shuffled_numbers);
        REQUIRE_EQ(sorted_numbers.get(), ascending_numbers);
        // sorting 1000 numbers should require less than 0.1 seconds (in practice it requires about 0.2ms)
        REQUIRE_LT(sorted_numbers.time_in_s(), 0.1);
    }
}