Lolly 1.4.27
Loading...
Searching...
No Matches
tm_timer_test.cpp
Go to the documentation of this file.
1/** \file tm_timer_test.cpp
2 * \copyright GPLv3
3 * \details A unitest for tm_timer.
4 * \author Paradisuman
5 * \date 2023
6 */
7#include "a_lolly_test.hpp"
8#include "analyze.hpp"
10#include "string.hpp"
11#include "tm_timer.hpp"
12#include <cstring>
13
14#include "tbox/tbox.h"
15
20
21int
22get_timing_cumul (string out) {
23 // out: Task 'task1' took 0 ms
24 // out: Task 'task2' took 12 ms
25 int pos= 0;
26 int result;
27 auto tokens= tokenize (out, " took ");
28
30 return result;
31}
32
33int
34get_timing_nr (string out) {
35 // out: Task 'task1' took 0 ms (0 invocations)
36 // out: Task 'task2' took 12 ms (1 invocations)
37 int pos= 0;
38 int result;
39 auto tokens= tokenize (out, " (");
40
41 if (N (tokens) == 1) return -1;
42 if (read_int (tokens[1], pos, result) == false) return -1;
43 return result;
44}
45
46bool
47test_same (tm_ostream& a, string b) {
48 string sa= a.unbuffer ();
49
50 return sa == b;
51}
52
53string
55 string s1= out.unbuffer ();
56 c_string ans (s1);
57 char* current= ans;
58 while ((current= strstr (current, "took"))) {
59 current+= strlen ("took");
60 while (*current && (*current == '(')) {
61 current+= 2;
62 }
63 while (*current && (*current < '0' || *current > '9')) {
64 current++;
65 }
66 while (*current && *current >= '0' && *current <= '9') {
67 *current= '0';
68 current++;
69 }
70 }
71 return (char*) ans;
72}
73
75
76TEST_CASE ("if time_t and long 64 bit") { CHECK (sizeof (time_t) == 8); }
77
78TEST_CASE ("function get_sec_time") {
79 tb_timeval_t tp= {0};
81 time_t t1= tp.tv_sec;
83
84 CHECK (t2 >= t1);
85}
86
87TEST_CASE ("function get_usec_time") {
88 tb_timeval_t tp= {0};
90 time_t t1= tp.tv_usec;
92
93 CHECK (t2 >= t1);
94}
95
96TEST_CASE ("function raw_time") {
98
99 // CHECK (startTime >= 0);
100
102
104}
105
106TEST_CASE ("function texmacs_time") {
107 long t1= texmacs_time ();
108
109 CHECK (t1 >= 0);
110
111 long t2= texmacs_time ();
112
113 CHECK (t2 >= t1);
114}
115
116TEST_CASE ("function timer_start and timer_cumul") {
118 ostream.buffer ();
119
120 SUBCASE ("start once") {
121 timer_start ("task1");
122 bench_print (ostream, "task1");
123 string out= ostream.unbuffer ();
124 int t1 = get_timing_cumul (out);
125
126 CHECK (t1 == 0);
127
128 timer_cumul ("task1");
129 ostream.buffer ();
130 bench_print (ostream, "task1");
131 out = ostream.unbuffer ();
132 int t2= get_timing_cumul (out);
133
134 CHECK (t2 >= 0);
135 }
136
137 SUBCASE ("start multiple times") {
138 timer_start ("task2");
139
140 timer_cumul ("task2");
141 bench_print (ostream, "task1");
142 string out= ostream.unbuffer ();
143 int t1 = get_timing_cumul (out);
144
145 CHECK (t1 >= 0);
146 CHECK (get_timing_nr (out) == -1);
147
148 timer_start ("task2");
149
150 timer_cumul ("task2");
151 ostream.buffer ();
152 bench_print (ostream, "task2");
153 out = ostream.unbuffer ();
154 int t2= get_timing_cumul (out);
155 int nr= get_timing_nr (out);
156
157 CHECK (t2 >= 0);
158 CHECK (nr == 2);
159 }
160}
161
162TEST_CASE ("function timer_reset") {
164 ostream.buffer ();
165
166 SUBCASE ("if task empty") {
167 timer_reset ("task");
168 bench_print (ostream, "task");
169 string out= ostream.unbuffer ();
170
171 CHECK (get_timing_nr (out) == -1);
172 }
173
174 SUBCASE ("if task not empty") {
175 timer_start ("task");
176 timer_cumul ("task");
177 timer_start ("task");
178 timer_cumul ("task");
179
180 timer_reset ("task");
181 bench_print (ostream, "task");
182 string out= ostream.unbuffer ();
183
184 CHECK (get_timing_nr (out) == -1);
185 }
186}
187
188TEST_CASE ("function bench_print") {
190 ostream.buffer ();
191
192 SUBCASE ("print one task") {
193 bench_print (ostream, "task");
194 string b= "Task 'task' took 0 ms\n";
195
197
198 ostream.buffer ();
199 timer_reset ("task1");
200 timer_start ("task1");
201 timer_cumul ("task1");
202 timer_start ("task1");
203 timer_cumul ("task1");
204 bench_print (ostream, "task1");
205
206 string ans= "Task 'task1' took 0 ms (2 invocations)\n";
207 string out= to_zero (ostream);
208
209 CHECK (out == ans);
210 }
211
212 SUBCASE ("print multiple task") {
213 timer_reset ("task1");
214 timer_reset ("task2");
215
216 timer_start ("task1");
217 timer_cumul ("task1");
218 timer_start ("task2");
219 timer_cumul ("task2");
220 bench_print (ostream);
221
222 string out= to_zero (ostream);
223 string b = "Task 'task1' took 0 ms\nTask 'task2' took 0 ms\n";
224
225 CHECK (out == b);
226 }
227}
228
229TEST_CASE ("testing memory leakage of tm_timer") {
230 timer_reset ("task");
231 timer_reset ("task1");
232 timer_reset ("task2");
234}
array< string > tokenize(string s, string sep)
Definition analyze.cpp:948
bool read_int(string s, int &i, int &result)
Definition analyze.cpp:634
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
blackbox t1
blackbox t2
blackbox b[13]
The list class represents a linked list.
Definition list.hpp:48
string unbuffer()
int mem_used()
#define TEST_MEMORY_LEAK_INIT
void timer_reset(string task)
Definition timer.cpp:51
void bench_print(tm_ostream &ostream, string task, uint32_t threshold)
Definition timer.cpp:60
void timer_start(string task)
Definition timer.cpp:32
void timer_cumul(string task)
Definition timer.cpp:39
TEST_CASE("test for operator+= and advance()")
time_t get_usec_time()
Definition tm_timer.cpp:31
time_t texmacs_time()
Definition tm_timer.cpp:47
time_t raw_time()
Definition tm_timer.cpp:38
time_t get_sec_time()
Definition tm_timer.cpp:24
int get_timing_cumul(string out)
int get_timing_nr(string out)
string to_zero(tm_ostream &out)
bool test_same(tm_ostream &a, string b)