Updated documentation for mock a1 tool
[ric-app/admin.git] / test / unit_test_sliding_window.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 /* Author : Ashwin Sridharan
21    Date   : Sept 2019
22 */
23
24 #define CATCH_CONFIG_MAIN
25 #include <catch2/catch.hpp>
26
27 #include <sliding_window.hpp>
28
29 TEST_CASE("Unit tests for sliding window", "sliding window"){
30
31   SECTION("Invalid window"){
32     REQUIRE_THROWS(sliding_window(-1));
33     REQUIRE_THROWS(sliding_window(MAX_WINDOW_SIZE + 1));
34   }
35
36   SECTION("Windowing"){
37     int window_size = 4;
38     sliding_window my_window(window_size);
39     REQUIRE(my_window.net_events == 0);
40     int num_events = 20;
41     
42     for(int i = 0; i < num_events; i++){
43       my_window.update_window(1);
44     }
45     
46     REQUIRE(my_window.net_events == num_events);
47
48     // test window shift after delay more than window size
49     std::this_thread::sleep_for(std::chrono::seconds(window_size + 3));
50
51     my_window.update_window(1);
52     REQUIRE(my_window.net_events == 1);
53
54     std::this_thread::sleep_for(std::chrono::seconds(window_size + 3));
55
56
57     // update every shift
58     for(int i = 0; i < window_size; i++){
59       my_window.update_window(1);
60       std::this_thread::sleep_for(std::chrono::seconds(1));
61     }
62
63     REQUIRE(my_window.net_events == window_size);
64     std::stringstream ss;
65     ss << "[Events] = " << my_window.net_events << std::endl;
66     REQUIRE_THAT(my_window.display_window(), Catch::Matchers::EndsWith(ss.str()));
67     
68   }
69
70   SECTION("Resizing"){
71
72     bool res;
73     int window_size = 4;
74     sliding_window my_window(window_size);
75     REQUIRE(my_window.net_events == 0);
76     int num_events = 20;
77     
78     for(int i = 0; i < num_events; i++){
79       my_window.update_window(1);
80     }
81     
82     REQUIRE(my_window.net_events == num_events);
83
84     int new_window_size = 2;
85     res = my_window.resize_window(2);
86     REQUIRE(res == true);
87     REQUIRE(my_window.net_events == 0);
88
89     my_window.update_window(100);
90     REQUIRE(my_window.net_events == 100);
91
92     std::this_thread::sleep_for(std::chrono::seconds(new_window_size + 1));
93     my_window.update_window(1);
94     REQUIRE(my_window.net_events == 1);
95
96     res = my_window.resize_window(-1);
97     REQUIRE(res == false);
98
99     res = my_window.resize_window(MAX_WINDOW_SIZE + 1);
100     REQUIRE(res == false);
101
102
103     my_window.clear();
104     REQUIRE(my_window.net_events == 0);
105   }
106
107 }