1 /*
2 
3 Copyright (c) 2014, Arvid Norberg
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #include "test.hpp"
34 #include "libtorrent/sliding_average.hpp"
35 
36 namespace {
37 
38 // normal distributed samples. mean=60 stddev=10
39 int samples[] =  {
40 49, 51, 60, 46, 65, 53, 76, 59, 57, 54, 56, 51, 45, 80, 53, 62,
41 69, 67, 66, 56, 56, 61, 52, 61, 61, 62, 59, 53, 48, 68, 47, 47,
42 63, 51, 53, 54, 46, 65, 64, 64, 45, 68, 64, 66, 53, 42, 57, 58,
43 57, 47, 55, 59, 64, 61, 37, 67, 55, 52, 60, 60, 44, 57, 50, 77,
44 56, 54, 49, 68, 66, 64, 47, 60, 46, 47, 81, 74, 65, 62, 44, 75,
45 65, 43, 58, 59, 53, 67, 49, 51, 33, 47, 49, 50, 54, 48, 55, 80,
46 67, 51, 66, 52, 48, 57, 30, 51, 72, 65, 78, 56, 74, 68, 49, 66,
47 63, 57, 61, 62, 64, 62, 61, 52, 67, 64, 59, 61, 69, 60, 54, 69 };
48 
49 } // anonymous namespace
50 
51 using namespace lt;
52 
53 // make sure we react quickly for the first few samples
TORRENT_TEST(reaction_time)54 TORRENT_TEST(reaction_time)
55 {
56 	sliding_average<int, 10> avg;
57 
58 	avg.add_sample(-10);
59 	avg.add_sample(10);
60 
61 	TEST_EQUAL(avg.mean(), 0);
62 }
63 
TORRENT_TEST(reaction_time2)64 TORRENT_TEST(reaction_time2)
65 {
66 	sliding_average<int, 10> avg;
67 
68 	avg.add_sample(10);
69 	avg.add_sample(20);
70 
71 	TEST_EQUAL(avg.mean(), 15);
72 }
73 
74 // make sure we converge
TORRENT_TEST(converge)75 TORRENT_TEST(converge)
76 {
77 	sliding_average<int, 10> avg;
78 	avg.add_sample(100);
79 	for (int i = 0; i < 20; ++i)
80 		avg.add_sample(10);
81 	TEST_CHECK(abs(avg.mean() - 10) <= 3);
82 }
83 
TORRENT_TEST(converge2)84 TORRENT_TEST(converge2)
85 {
86 	sliding_average<int, 10> avg;
87 	avg.add_sample(-100);
88 	for (int i = 0; i < 20; ++i)
89 		avg.add_sample(-10);
90 	TEST_CHECK(abs(avg.mean() + 10) <= 3);
91 }
92 
93 // test with a more realistic input
TORRENT_TEST(random_converge)94 TORRENT_TEST(random_converge)
95 {
96 	sliding_average<int, 10> avg;
97 	for (int i = 0; i < int(sizeof(samples)/sizeof(samples[0])); ++i)
98 		avg.add_sample(samples[i]);
99 	TEST_CHECK(abs(avg.mean() - 60) <= 3);
100 }
101 
TORRENT_TEST(sliding_average)102 TORRENT_TEST(sliding_average)
103 {
104 	sliding_average<int, 4> avg;
105 	TEST_EQUAL(avg.mean(), 0);
106 	TEST_EQUAL(avg.avg_deviation(), 0);
107 	avg.add_sample(500);
108 	TEST_EQUAL(avg.mean(), 500);
109 	TEST_EQUAL(avg.avg_deviation(), 0);
110 	avg.add_sample(501);
111 	TEST_EQUAL(avg.avg_deviation(), 1);
112 	avg.add_sample(0);
113 	avg.add_sample(0);
114 	std::printf("avg: %d dev: %d\n", avg.mean(), avg.avg_deviation());
115 	TEST_CHECK(abs(avg.mean() - 250) < 50);
116 	TEST_CHECK(abs(avg.avg_deviation() - 250) < 80);
117 }
118