Lolly 1.4.27
Loading...
Searching...
No Matches
Functions
tm_timer_test.cpp File Reference
#include "a_lolly_test.hpp"
#include "analyze.hpp"
#include "lolly/system/timer.hpp"
#include "string.hpp"
#include "tm_timer.hpp"
#include <cstring>
#include "tbox/tbox.h"
Include dependency graph for tm_timer_test.cpp:

Go to the source code of this file.

Functions

int get_timing_cumul (string out)
 
int get_timing_nr (string out)
 
bool test_same (tm_ostream &a, string b)
 
string to_zero (tm_ostream &out)
 
TEST_MEMORY_LEAK_INIT TEST_CASE ("if time_t and long 64 bit")
 
 TEST_CASE ("function get_sec_time")
 
 TEST_CASE ("function get_usec_time")
 
 TEST_CASE ("function raw_time")
 
 TEST_CASE ("function texmacs_time")
 
 TEST_CASE ("function timer_start and timer_cumul")
 
 TEST_CASE ("function timer_reset")
 
 TEST_CASE ("function bench_print")
 
 TEST_CASE ("testing memory leakage of tm_timer")
 

Detailed Description

A unitest for tm_timer.

Author
Paradisuman
Date
2023

Definition in file tm_timer_test.cpp.

Function Documentation

◆ get_timing_cumul()

int get_timing_cumul ( string out)

Definition at line 22 of file tm_timer_test.cpp.

22 {
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}
array< string > tokenize(string s, string sep)
Definition analyze.cpp:948
bool read_int(string s, int &i, int &result)
Definition analyze.cpp:634
The list class represents a linked list.
Definition list.hpp:48

◆ get_timing_nr()

int get_timing_nr ( string out)

Definition at line 34 of file tm_timer_test.cpp.

34 {
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}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170

◆ test_same()

bool test_same ( tm_ostream & a,
string b )

Definition at line 47 of file tm_timer_test.cpp.

47 {
48 string sa= a.unbuffer ();
49
50 return sa == b;
51}
blackbox b[13]
string unbuffer()

◆ to_zero()

string to_zero ( tm_ostream & out)

Definition at line 54 of file tm_timer_test.cpp.

54 {
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}

◆ TEST_CASE() [1/9]

TEST_MEMORY_LEAK_INIT TEST_CASE ( "if time_t and long 64 bit" )

Definition at line 76 of file tm_timer_test.cpp.

76{ CHECK (sizeof (time_t) == 8); }

◆ TEST_CASE() [2/9]

TEST_CASE ( "function get_sec_time" )

Definition at line 78 of file tm_timer_test.cpp.

78 {
79 tb_timeval_t tp= {0};
81 time_t t1= tp.tv_sec;
83
84 CHECK (t2 >= t1);
85}
blackbox t1
blackbox t2
time_t get_sec_time()
Definition tm_timer.cpp:24

◆ TEST_CASE() [3/9]

TEST_CASE ( "function get_usec_time" )

Definition at line 87 of file tm_timer_test.cpp.

87 {
88 tb_timeval_t tp= {0};
90 time_t t1= tp.tv_usec;
92
93 CHECK (t2 >= t1);
94}
time_t get_usec_time()
Definition tm_timer.cpp:31

◆ TEST_CASE() [4/9]

TEST_CASE ( "function raw_time" )

Definition at line 96 of file tm_timer_test.cpp.

96 {
98
99 // CHECK (startTime >= 0);
100
102
104}
time_t raw_time()
Definition tm_timer.cpp:38

◆ TEST_CASE() [5/9]

TEST_CASE ( "function texmacs_time" )

Definition at line 106 of file tm_timer_test.cpp.

106 {
107 long t1= texmacs_time ();
108
109 CHECK (t1 >= 0);
110
111 long t2= texmacs_time ();
112
113 CHECK (t2 >= t1);
114}
time_t texmacs_time()
Definition tm_timer.cpp:47

◆ TEST_CASE() [6/9]

TEST_CASE ( "function timer_start and timer_cumul" )

Definition at line 116 of file tm_timer_test.cpp.

116 {
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}
int get_timing_cumul(string out)
int get_timing_nr(string out)

◆ TEST_CASE() [7/9]

TEST_CASE ( "function timer_reset" )

Definition at line 162 of file tm_timer_test.cpp.

162 {
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}

◆ TEST_CASE() [8/9]

TEST_CASE ( "function bench_print" )

Definition at line 188 of file tm_timer_test.cpp.

188 {
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}
string to_zero(tm_ostream &out)
bool test_same(tm_ostream &a, string b)

◆ TEST_CASE() [9/9]

TEST_CASE ( "testing memory leakage of tm_timer" )

Definition at line 229 of file tm_timer_test.cpp.

229 {
230 timer_reset ("task");
231 timer_reset ("task1");
232 timer_reset ("task2");
234}
int mem_used()