Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
6d3188fc
Commit
6d3188fc
authored
Oct 03, 2014
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(Catch): Начал переводить тесты на использование "Catch".
Написал тесты для PassiveTimer и HourGlass. Исправил найденные ошибки, доработал интерфейсы.
parent
50592bf8
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
222 additions
and
162 deletions
+222
-162
HourGlass.h
include/HourGlass.h
+49
-20
PassiveTimer.cc
src/Timers/PassiveTimer.cc
+0
-2
Makefile.am
tests/Makefile.am
+53
-48
hourglass.cc
tests/hourglass.cc
+77
-46
passivetimer.cc
tests/passivetimer.cc
+40
-46
tests.cc
tests/tests.cc
+3
-0
No files found.
include/HourGlass.h
View file @
6d3188fc
...
...
@@ -43,8 +43,9 @@
\par Пример использования.
Допустим у вас есть сигнал "перегрев"(in_overheating) и вам необходимо выставить какой-то
флаг о перегреве (isOverheating), если этот сигнал устойчиво держится в течение 10 секунд,
и при этом если сигнал снялся, то вам необходимо как минимум те же 10 секунд,
подождать прежде чем "снять" флаг. Для этого удобно использовать данный класс.
то check() станет "true". При этом если сигнал снимется на 5 секунд ("песок начнёт обратно пересыпаться"),
а потом опять выставиться, то до срабатывания check() == true уже останется 5 сек, а не 10 сек.
Получается, что для срабатывания check()=true сигнал должен не колеблясь держаться больше заданного времени.
\code
HourGlass hg;
hg.run(10000); // настраиваем часы на 10 сек..
...
...
@@ -77,6 +78,7 @@ class HourGlass
run
(
_size
);
}
// "ёмкость" песочных часов..
inline
int
duration
()
{
return
_size
;
...
...
@@ -94,42 +96,38 @@ class HourGlass
if
(
!
_state
)
{
int
cur
=
t
.
getCurrent
();
_sand
-=
cur
;
if
(
cur
>
_size
)
cur
=
_size
;
_sand
-=
cur
;
if
(
_sand
<
0
)
_sand
=
0
;
// std::cout << "перевернули: прошло " << cur
// << " осталось " << sand
// << " засекаем " << cur << endl;
t
.
setTiming
(
cur
);
}
else
{
_sand
+=
t
.
getCurrent
();
int
cur
=
t
.
getCurrent
();
if
(
cur
>
_size
)
cur
=
_size
;
_sand
+=
cur
;
if
(
_sand
>
_size
)
_sand
=
_size
;
// std::cout << "вернули: прошло " << t.getCurrent()
// << " осталось " << sand
// << " засекам " << sand << endl;
t
.
setTiming
(
_sand
);
}
return
st
;
}
// получить прошедшее время
// для положения st
inline
timeout_t
current
(
bool
st
)
inline
timeout_t
current
()
{
return
t
.
getCurrent
();
}
// получить заданное время
// для положения st
inline
timeout_t
interval
(
bool
st
)
inline
timeout_t
interval
()
{
return
t
.
getInterval
();
}
...
...
@@ -147,11 +145,42 @@ class HourGlass
inline
bool
state
(){
return
_state
;
}
// текущее "насыпавшееся" количество "песка"
inline
timeout_t
amount
()
{
return
(
_size
-
remain
()
);
}
// остаток песка (времени)
inline
timeout_t
remain
()
{
timeout_t
c
=
t
.
getCurrent
();
if
(
c
>
_size
)
c
=
_size
;
// _state=false - означает, что песок пересыпается обратно..
if
(
!
_state
)
{
int
ret
=
(
_sand
+
c
);
if
(
ret
>
_size
)
return
_size
;
return
ret
;
}
// _state=true - означает, что песок пересыпается..
int
ret
=
(
_sand
-
c
);
if
(
ret
<
0
)
return
0
;
return
ret
;
}
protected
:
PassiveTimer
t
;
bool
_state
;
int
_sand
;
timeout_t
_size
;
PassiveTimer
t
;
/*!< таймер для отсчёта времени.. */
bool
_state
;
/*!< текущее "положение часов", true - прямое, false - обратное (перевёрнутое) */
int
_sand
;
/*!< сколько песка ещё осталось.. */
timeout_t
_size
;
/*!< размер часов */
};
// --------------------------------------------------------------------------
#endif
...
...
src/Timers/PassiveTimer.cc
View file @
6d3188fc
...
...
@@ -81,9 +81,7 @@ bool PassiveTimer::checkTime()
timeout_t
PassiveTimer
::
setTiming
(
timeout_t
timeMS
)
{
if
(
timeMS
==
WaitUpTime
)
{
timeSS
=
WaitUpTime
;
}
else
{
timeSS
=
timeMS
/
10
;
// задержка в сантисекундах
...
...
tests/Makefile.am
View file @
6d3188fc
...
...
@@ -2,51 +2,56 @@
# This file is part of the UniSet library #
############################################################################
noinst_PROGRAMS
=
passivetimer hourglass delaytimer unixml ui umutex conftest iterator_test sscanf_hex calibration threadtst dlog
passivetimer_SOURCES
=
passivetimer.cc
passivetimer_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
passivetimer_CPPFLAGS
=
-I
$(top_builddir)
/include
hourglass_SOURCES
=
hourglass.cc
hourglass_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
hourglass_CPPFLAGS
=
-I
$(top_builddir)
/include
delaytimer_SOURCES
=
delaytimer.cc
delaytimer_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
delaytimer_CPPFLAGS
=
-I
$(top_builddir)
/include
unixml_SOURCES
=
unixml.cc
unixml_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
${
SIGC_LIBS
}
unixml_CPPFLAGS
=
-I
$(top_builddir)
/include
${
SIGC_CFLAGS
}
iterator_test_SOURCES
=
iterator_test.cc
iterator_test_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
iterator_test_CPPFLAGS
=
-I
$(top_builddir)
/include
ui_SOURCES
=
ui.cc
ui_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
ui_CPPFLAGS
=
-I
$(top_builddir)
/include
umutex_SOURCES
=
umutex.cc
umutex_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
$(COMCPP_LIBS)
umutex_CPPFLAGS
=
-I
$(top_builddir)
/include
$(COMCPP_CFLAGS)
conftest_SOURCES
=
conftest.cc
conftest_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
conftest_CPPFLAGS
=
-I
$(top_builddir)
/include
sscanf_hex_SOURCES
=
sscanf_hex.cc
calibration_SOURCES
=
calibration.cc
calibration_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
${
SIGC_LIBS
}
calibration_CPPFLAGS
=
-I
$(top_builddir)
/include
${
SIGC_CFLAGS
}
threadtst_SOURCES
=
threadtst.cc
threadtst_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
${
SIGC_LIBS
}
$(COMCPP_LIBS)
threadtst_CPPFLAGS
=
-I
$(top_builddir)
/include
${
SIGC_CFLAGS
}
$(COMCPP_CFLAGS)
dlog_SOURCES
=
dlog.cc
dlog_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
${
SIGC_LIBS
}
$(COMCPP_LIBS)
dlog_CPPFLAGS
=
-I
$(top_builddir)
/include
${
SIGC_CFLAGS
}
$(COMCPP_CFLAGS)
noinst_PROGRAMS
=
tests
#hourglass delaytimer unixml ui umutex conftest iterator_test sscanf_hex calibration threadtst dlog
tests_SOURCES
=
tests.cc passivetimer.cc hourglass.cc
tests_LDADD
=
$(top_builddir)
/lib/libUniSet2.la
tests_CPPFLAGS
=
-I
$(top_builddir)
/include
-I
$(includeir)
/Catch
#passivetimer_SOURCES = passivetimer.cc
#passivetimer_LDADD = $(top_builddir)/lib/libUniSet2.la
#passivetimer_CPPFLAGS = -I$(top_builddir)/include -ICatch
# hourglass_SOURCES = hourglass.cc
# hourglass_LDADD = $(top_builddir)/lib/libUniSet2.la
# hourglass_CPPFLAGS = -I$(top_builddir)/include
#
# delaytimer_SOURCES = delaytimer.cc
# delaytimer_LDADD = $(top_builddir)/lib/libUniSet2.la
# delaytimer_CPPFLAGS = -I$(top_builddir)/include
#
# unixml_SOURCES = unixml.cc
# unixml_LDADD = $(top_builddir)/lib/libUniSet2.la ${SIGC_LIBS}
# unixml_CPPFLAGS = -I$(top_builddir)/include ${SIGC_CFLAGS}
#
# iterator_test_SOURCES = iterator_test.cc
# iterator_test_LDADD = $(top_builddir)/lib/libUniSet2.la
# iterator_test_CPPFLAGS = -I$(top_builddir)/include
#
# ui_SOURCES = ui.cc
# ui_LDADD = $(top_builddir)/lib/libUniSet2.la
# ui_CPPFLAGS = -I$(top_builddir)/include
#
# umutex_SOURCES = umutex.cc
# umutex_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
# umutex_CPPFLAGS = -I$(top_builddir)/include $(COMCPP_CFLAGS)
#
# conftest_SOURCES = conftest.cc
# conftest_LDADD = $(top_builddir)/lib/libUniSet2.la
# conftest_CPPFLAGS = -I$(top_builddir)/include
#
# sscanf_hex_SOURCES = sscanf_hex.cc
#
# calibration_SOURCES = calibration.cc
# calibration_LDADD = $(top_builddir)/lib/libUniSet2.la ${SIGC_LIBS}
# calibration_CPPFLAGS = -I$(top_builddir)/include ${SIGC_CFLAGS}
#
# threadtst_SOURCES = threadtst.cc
# threadtst_LDADD = $(top_builddir)/lib/libUniSet2.la ${SIGC_LIBS} $(COMCPP_LIBS)
# threadtst_CPPFLAGS = -I$(top_builddir)/include ${SIGC_CFLAGS} $(COMCPP_CFLAGS)
#
# dlog_SOURCES = dlog.cc
# dlog_LDADD = $(top_builddir)/lib/libUniSet2.la ${SIGC_LIBS} $(COMCPP_LIBS)
# dlog_CPPFLAGS = -I$(top_builddir)/include ${SIGC_CFLAGS} $(COMCPP_CFLAGS)
#
tests/hourglass.cc
View file @
6d3188fc
#include <iostream>
#include <Catch/catch.hpp>
#include <cmath>
using
namespace
std
;
#include "HourGlass.h"
#include "DelayTimer.h"
#include "UniSetTypes.h"
int
main
()
TEST_CASE
(
"HourGlass"
,
"[HourGlass]"
)
{
HourGlass
hg
;
hg
.
run
(
1000
);
hg
.
rotate
(
true
);
msleep
(
200
);
if
(
hg
.
check
()
)
SECTION
(
"Constructor"
)
{
cerr
<<
"HourGlass: TEST1 FAILED! "
<<
endl
;
return
1
;
HourGlass
hg
;
REQUIRE
(
hg
.
duration
()
==
0
);
msleep
(
60
);
// т.к. точность +-10 мсек.. делаем паузу 60.. а проверяем 50
REQUIRE
(
std
::
abs
(
hg
.
current
()
-
60
)
<=
10
);
REQUIRE
(
hg
.
interval
()
==
0
);
REQUIRE
(
hg
.
amount
()
==
0
);
REQUIRE
(
hg
.
remain
()
==
0
);
CHECK_FALSE
(
hg
.
state
()
);
CHECK_FALSE
(
hg
.
check
()
);
}
msleep
(
1000
);
if
(
!
hg
.
check
()
)
SECTION
(
"Working"
)
{
cerr
<<
"HourGlass: TEST1 FAILED! "
<<
endl
;
return
1
;
}
cout
<<
"HourGlass: TEST1 OK!"
<<
endl
;
HourGlass
hg
;
hg
.
run
(
100
);
CHECK
(
hg
.
state
()
);
// часы начали тикать.. (в нормальном положении)
REQUIRE
(
hg
.
duration
()
==
100
);
CHECK_FALSE
(
hg
.
check
()
);
msleep
(
110
);
CHECK
(
hg
.
check
()
);
// Переворачиваем обратно..
// "песок высыпается назад" в течении 50 мсек,
// потом опять ставим "на ноги", ждём 60 мсек.. должно сработать
hg
.
rotate
(
false
);
msleep
(
1000
);
if
(
hg
.
check
()
)
{
cerr
<<
"HourGlass: TEST2 FAILED! "
<<
endl
;
return
1
;
}
cout
<<
"HourGlass: TEST2 OK!"
<<
endl
;
CHECK_FALSE
(
hg
.
state
()
);
CHECK_FALSE
(
hg
.
check
()
);
msleep
(
50
);
CHECK_FALSE
(
hg
.
check
()
);
hg
.
rotate
(
true
);
msleep
(
500
);
if
(
hg
.
check
()
)
{
cerr
<<
"HourGlass: TEST3 FAILED! "
<<
endl
;
return
1
;
msleep
(
60
);
CHECK
(
hg
.
check
()
);
}
cout
<<
"HourGlass: TEST3 OK!"
<<
endl
;
hg
.
rotate
(
false
);
msleep
(
200
);
hg
.
rotate
(
true
);
msleep
(
200
);
if
(
hg
.
check
()
)
SECTION
(
"Reset"
)
{
cerr
<<
"HourGlass: TEST4 FAILED! "
<<
endl
;
return
1
;
HourGlass
hg
;
hg
.
run
(
100
);
msleep
(
110
);
CHECK
(
hg
.
check
()
);
hg
.
reset
();
CHECK_FALSE
(
hg
.
check
()
);
msleep
(
110
);
CHECK
(
hg
.
check
()
);
}
msleep
(
820
);
if
(
!
hg
.
check
()
)
SECTION
(
"Debounce"
)
{
cerr
<<
"HourGlass: TEST5 FAILED! "
<<
endl
;
return
1
;
}
HourGlass
hg
;
hg
.
run
(
100
);
// [ 100 / 0 ]
REQUIRE
(
hg
.
remain
()
==
100
);
REQUIRE
(
hg
.
amount
()
==
0
);
msleep
(
110
);
// [ 0 / 110 ] "110" --> "100"
CHECK
(
hg
.
check
()
);
REQUIRE
(
hg
.
remain
()
==
0
);
REQUIRE
(
hg
.
amount
()
==
100
);
hg
.
rotate
(
false
);
// начинает сыпаться "обратно"..
REQUIRE
(
hg
.
amount
()
==
100
);
msleep
(
55
);
CHECK_FALSE
(
hg
.
check
()
);
REQUIRE
(
hg
.
amount
()
<=
50
);
// +-10 мсек..
hg
.
rotate
(
true
);
// опять начал сыпаться..
CHECK_FALSE
(
hg
.
check
()
);
msleep
(
25
);
CHECK_FALSE
(
hg
.
check
()
);
REQUIRE
(
hg
.
amount
()
>=
60
);
cout
<<
"HourGlass: TEST4 OK!"
<<
endl
;
return
0
;
hg
.
rotate
(
false
);
// опять назад..
msleep
(
80
);
// по сути сигнал сбросился..(т.к. оставалось 70.. а прошло 80)
CHECK_FALSE
(
hg
.
check
()
);
REQUIRE
(
hg
.
amount
()
==
0
);
REQUIRE
(
hg
.
remain
()
==
100
);
hg
.
rotate
(
true
);
// вновь запустили
CHECK_FALSE
(
hg
.
check
()
);
msleep
(
55
);
REQUIRE
(
hg
.
amount
()
>=
50
);
CHECK_FALSE
(
hg
.
check
()
);
msleep
(
60
);
REQUIRE
(
hg
.
remain
()
==
0
);
REQUIRE
(
hg
.
amount
()
==
100
);
CHECK
(
hg
.
check
()
);
}
}
tests/passivetimer.cc
View file @
6d3188fc
#include <iostream>
using
namespace
std
;
#include <Catch/catch.hpp>
#include "PassiveTimer.h"
#include "UniSetTypes.h"
using
namespace
std
;
PassiveTimer
pt
(
1000
);
void
func
(
const
std
::
string
&
s1
)
{
}
int
main
()
TEST_CASE
(
"PassiveTimer"
,
"[PassiveTimer]"
)
{
func
(
"test"
);
SECTION
(
"Default constructor"
)
{
PassiveTimer
pt
;
PassiveTimer
pt1
(
5000
);
cout
<<
" pt1.getInterval()="
<<
pt1
.
getInterval
()
<<
" TEST: "
<<
((
pt1
.
getInterval
()
==
5000
)
?
"OK"
:
"FAILED"
)
<<
endl
;
REQUIRE_FALSE
(
pt
.
checkTime
()
);
msleep
(
15
);
REQUIRE
(
pt
.
getCurrent
()
>=
10
);
REQUIRE
(
pt
.
getInterval
()
==
0
);
// TIMEOUT_INF );
REQUIRE
(
pt
.
getLeft
(
pt
.
getCurrent
()
+
10
)
==
10
);
}
PassiveTimer
pt2
;
cout
<<
" pt2.getInterval()="
<<
pt2
.
getInterval
()
<<
endl
;
if
(
pt2
.
getInterval
()
!=
0
)
SECTION
(
"Init constructor"
)
{
cerr
<<
"BAD DEFAULT INITIALIZATION!!!"
<<
endl
;
return
1
;
PassiveTimer
pt
(
100
);
msleep
(
15
);
// т.к. точность +-10 мсек.. делаем паузу 60.. а проверяем 50
REQUIRE
(
pt
.
getCurrent
()
>=
10
);
REQUIRE
(
pt
.
getInterval
()
==
100
);
REQUIRE
(
pt
.
getLeft
(
50
)
>
0
);
}
PassiveTimer
pt3
(
UniSetTimer
::
WaitUpTime
);
cout
<<
"pt3.getCurrent(): "
<<
pt3
.
getCurrent
()
<<
endl
;
msleep
(
3000
);
int
pt3_ms
=
pt3
.
getCurrent
();
cout
<<
"pt3.getCurrent(): "
<<
pt3_ms
<<
endl
;
if
(
pt3_ms
<
3000
)
SECTION
(
"Init zero"
)
{
cerr
<<
"BAD getCurrent() function for WaitUpTime timer (pt3)"
<<
endl
;
return
1
;
PassiveTimer
pt
(
0
);
REQUIRE
(
pt
.
getInterval
()
==
0
);
// TIMEOUT_INF );
REQUIRE
(
pt
.
getLeft
(
100
)
==
100
);
}
PassiveTimer
pt0
(
0
);
cout
<<
"pt0: check msec=0: "
<<
(
pt0
.
checkTime
()
?
"OK"
:
"FAILED"
)
<<
endl
;
PassiveTimer
pt4
(
350
);
for
(
int
i
=
0
;
i
<
12
;
i
++
)
SECTION
(
"Init < 0 "
)
{
cerr
<<
"pt4: check time = "
<<
pt4
.
checkTime
()
<<
endl
;
if
(
pt4
.
checkTime
()
)
{
cerr
<<
"pt4: reset..."
<<
endl
;
pt4
.
reset
();
}
msleep
(
200
);
PassiveTimer
pt
(
-
10
);
REQUIRE
(
pt
.
getInterval
()
>=
(
timeout_t
)(
-
10
)
);
// '>=' т.к. переданное время может быть округлено в большую сторону.
REQUIRE
(
pt
.
getLeft
(
10
)
==
10
);
}
while
(
1
)
SECTION
(
"Check working"
)
{
cerr
<<
"timer="
<<
pt
.
checkTime
()
<<
endl
;
msleep
(
500
);
PassiveTimer
pt
(
100
);
msleep
(
120
);
// т.к. точность +-10 мсек.. делаем паузу 60.. а проверяем 50
REQUIRE
(
pt
.
getCurrent
()
>=
110
);
CHECK
(
pt
.
checkTime
()
);
INFO
(
"Check reset"
);
pt
.
reset
();
REQUIRE_FALSE
(
pt
.
checkTime
()
);
INFO
(
"Check setTiming"
);
REQUIRE_FALSE
(
pt
.
checkTime
()
);
pt
.
setTiming
(
50
);
msleep
(
55
);
// т.к. точность +-10 мсек.. делаем паузу 55..
CHECK
(
pt
.
checkTime
()
);
}
return
0
;
}
tests/tests.cc
0 → 100644
View file @
6d3188fc
#define CATCH_CONFIG_MAIN
#include <Catch/catch.hpp>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment