1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <gtest/gtest.h>
6
7 extern "C" {
8 #include "rate_estimator.h"
9 }
10
11 static struct timespec window = {.tv_sec = 0, .tv_nsec = 10000000};
12
TEST(RateEstimatorTest,EstimateOutputLinear)13 TEST(RateEstimatorTest, EstimateOutputLinear) {
14 struct rate_estimator* re;
15 struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
16 int i, rc, level, tmp;
17
18 re = rate_estimator_create(10000, &window, 0.0f);
19 level = 240;
20 for (i = 0; i < 20; i++) {
21 rc = rate_estimator_check(re, level, &t);
22 EXPECT_EQ(0, rc);
23
24 /* Test that output device consumes 5 frames. */
25 tmp = rand() % 10;
26 rate_estimator_add_frames(re, 5 + tmp);
27 level += tmp;
28 t.tv_nsec += 500000;
29 }
30 t.tv_nsec += 1;
31 rc = rate_estimator_check(re, level, &t);
32 EXPECT_EQ(1, rc);
33 EXPECT_GT(10000, rate_estimator_get_rate(re));
34 EXPECT_LT(9999, rate_estimator_get_rate(re));
35
36 rate_estimator_destroy(re);
37 }
38
TEST(RateEstimatorTest,EstimateOutputLinear2)39 TEST(RateEstimatorTest, EstimateOutputLinear2) {
40 struct rate_estimator* re;
41 struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
42 int level = 240;
43 int i, rc, tmp;
44
45 int interval_nsec[5] = {1000000, 1500000, 2000000, 2500000, 3000000};
46 int frames_written[5] = {30, 25, 20, 15, 10};
47
48 re = rate_estimator_create(7470, &window, 0.0f);
49 for (i = 0; i < 5; i++) {
50 rc = rate_estimator_check(re, level, &t);
51 EXPECT_EQ(0, rc);
52
53 tmp = rand() % 10;
54 rate_estimator_add_frames(re, frames_written[i] + tmp);
55 level += tmp;
56 t.tv_nsec += interval_nsec[i];
57 }
58 t.tv_nsec += 1;
59 rc = rate_estimator_check(re, level, &t);
60 EXPECT_EQ(1, rc);
61 /* Calculated rate is 7475.72 */
62 EXPECT_GT(7476, rate_estimator_get_rate(re));
63 EXPECT_LT(7475, rate_estimator_get_rate(re));
64
65 rate_estimator_destroy(re);
66 }
67
TEST(RateEstimatorTest,EstimateRateSkewTooLarge)68 TEST(RateEstimatorTest, EstimateRateSkewTooLarge) {
69 struct rate_estimator* re;
70 struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
71 int level = 240;
72 int i, rc, tmp;
73
74 int interval_nsec[5] = {1000000, 1500000, 2000000, 2500000, 3000000};
75 int frames_written[5] = {30, 25, 20, 15, 10};
76
77 re = rate_estimator_create(10000, &window, 0.0f);
78 for (i = 0; i < 5; i++) {
79 rc = rate_estimator_check(re, level, &t);
80 EXPECT_EQ(0, rc);
81
82 tmp = rand() % 10;
83 rate_estimator_add_frames(re, frames_written[i] + tmp);
84 level += tmp;
85 t.tv_nsec += interval_nsec[i];
86 }
87 t.tv_nsec += 1;
88 rc = rate_estimator_check(re, level, &t);
89 EXPECT_EQ(1, rc);
90 /* Estimated rate too far from allowed max rate skew */
91 EXPECT_EQ(10000, rate_estimator_get_rate(re));
92
93 rate_estimator_destroy(re);
94 }
95
TEST(RateEstimatorTest,EstimateOutputSmooth)96 TEST(RateEstimatorTest, EstimateOutputSmooth) {
97 struct rate_estimator* re;
98 struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
99 int rc;
100
101 re = rate_estimator_create(10010, &window, 0.9f);
102 rc = rate_estimator_check(re, 240, &t);
103 EXPECT_EQ(0, rc);
104
105 /* Test that output device consumes 100 frames in
106 * 10ms. */
107 rate_estimator_add_frames(re, 55);
108 t.tv_nsec += 5000000;
109 rc = rate_estimator_check(re, 245, &t);
110 EXPECT_EQ(0, rc);
111
112 rate_estimator_add_frames(re, 55);
113 t.tv_nsec += 5000001;
114 rc = rate_estimator_check(re, 250, &t);
115 EXPECT_EQ(1, rc);
116
117 /* Assert the rate is smoothed 10010 * 0.9 + 10000 * 0.1 */
118 EXPECT_LT(10008, rate_estimator_get_rate(re));
119 EXPECT_GT(10009, rate_estimator_get_rate(re));
120
121 rate_estimator_destroy(re);
122 }
123
TEST(RateEstimatorTest,EstimateInputLinear)124 TEST(RateEstimatorTest, EstimateInputLinear) {
125 struct rate_estimator* re;
126 struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
127 int i, rc, level, tmp;
128
129 re = rate_estimator_create(10000, &window, 0.0f);
130 level = 1200;
131 for (i = 0; i < 20; i++) {
132 rc = rate_estimator_check(re, level, &t);
133 EXPECT_EQ(0, rc);
134
135 /* Test that stream consumes 5 frames. */
136 tmp = rand() % 10;
137 rate_estimator_add_frames(re, -(5 + tmp));
138 level -= tmp;
139 t.tv_nsec += 500000;
140 }
141 t.tv_nsec += 1;
142 rc = rate_estimator_check(re, level, &t);
143 EXPECT_EQ(1, rc);
144 EXPECT_GT(10000, rate_estimator_get_rate(re));
145 EXPECT_LT(9999, rate_estimator_get_rate(re));
146
147 rate_estimator_destroy(re);
148 }
149
TEST(RateEstimatorTest,EstimateInputLinear2)150 TEST(RateEstimatorTest, EstimateInputLinear2) {
151 struct rate_estimator* re;
152 struct timespec t;
153 int rc;
154 static struct timespec this_window = {.tv_sec = 0, .tv_nsec = 100000000};
155
156 re = rate_estimator_create(10000, &this_window, 0.0f);
157 t.tv_sec = 1;
158 t.tv_nsec = 0;
159 rc = rate_estimator_check(re, 200, &t);
160 EXPECT_EQ(0, rc);
161
162 t.tv_nsec += 50000000;
163 rc = rate_estimator_check(re, 700, &t);
164 EXPECT_EQ(0, rc);
165
166 rate_estimator_add_frames(re, -100);
167
168 t.tv_nsec += 50000000;
169 rc = rate_estimator_check(re, 1100, &t);
170 t.tv_nsec += 1;
171 rc = rate_estimator_check(re, 1100, &t);
172 EXPECT_EQ(1, rc);
173 EXPECT_GT(10000, rate_estimator_get_rate(re));
174 EXPECT_LT(9999, rate_estimator_get_rate(re));
175
176 rate_estimator_destroy(re);
177 }
178
main(int argc,char ** argv)179 int main(int argc, char** argv) {
180 ::testing::InitGoogleTest(&argc, argv);
181 return RUN_ALL_TESTS();
182 }
183