Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
5d9025a7
Commit
5d9025a7
authored
8 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
tests/extmod: Add test for utimeq module.
parent
d02f6a99
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/extmod/utimeq1.py
+103
-0
103 additions, 0 deletions
tests/extmod/utimeq1.py
tests/extmod/utimeq1.py.exp
+1
-0
1 addition, 0 deletions
tests/extmod/utimeq1.py.exp
with
104 additions
and
0 deletions
tests/extmod/utimeq1.py
0 → 100644
+
103
−
0
View file @
5d9025a7
# Test for utimeq module which implements task queue with support for
# wraparound time (utime.ticks_ms() style).
try
:
from
utime
import
ticks_add
,
ticks_diff
from
utimeq
import
utimeq
except
ImportError
:
print
(
"
SKIP
"
)
import
sys
sys
.
exit
()
DEBUG
=
0
MAX
=
ticks_add
(
0
,
-
1
)
MODULO_HALF
=
MAX
//
2
+
1
if
DEBUG
:
def
dprint
(
*
v
):
print
(
*
v
)
else
:
def
dprint
(
*
v
):
pass
# Try not to crash on invalid data
h
=
utimeq
(
10
)
try
:
h
.
push
(
1
)
assert
False
except
TypeError
:
pass
try
:
h
.
pop
(
1
)
assert
False
except
IndexError
:
pass
def
pop_all
(
h
):
l
=
[]
while
h
:
item
=
[
0
,
0
,
0
]
h
.
pop
(
item
)
#print("!", item)
l
.
append
(
tuple
(
item
))
dprint
(
l
)
return
l
def
add
(
h
,
v
):
h
.
push
(
v
,
0
,
0
)
dprint
(
"
-----
"
)
#h.dump()
dprint
(
"
-----
"
)
h
=
utimeq
(
10
)
add
(
h
,
0
)
add
(
h
,
MAX
)
add
(
h
,
MAX
-
1
)
add
(
h
,
101
)
add
(
h
,
100
)
add
(
h
,
MAX
-
2
)
dprint
(
h
)
l
=
pop_all
(
h
)
for
i
in
range
(
len
(
l
)
-
1
):
diff
=
ticks_diff
(
l
[
i
+
1
][
0
],
l
[
i
][
0
])
assert
diff
>
0
def
edge_case
(
edge
,
offset
):
h
=
utimeq
(
10
)
add
(
h
,
ticks_add
(
0
,
offset
))
add
(
h
,
ticks_add
(
edge
,
offset
))
dprint
(
h
)
l
=
pop_all
(
h
)
diff
=
ticks_diff
(
l
[
1
][
0
],
l
[
0
][
0
])
dprint
(
diff
,
diff
>
0
)
return
diff
dprint
(
"
===
"
)
diff
=
edge_case
(
MODULO_HALF
-
1
,
0
)
assert
diff
==
MODULO_HALF
-
1
assert
edge_case
(
MODULO_HALF
-
1
,
100
)
==
diff
assert
edge_case
(
MODULO_HALF
-
1
,
-
100
)
==
diff
# We expect diff to be always positive, per the definition of heappop() which should return
# the smallest value.
# This is the edge case where this invariant breaks, due to assymetry of two's-complement
# range - there's one more negative integer than positive, so heappushing values like below
# will then make ticks_diff() return the minimum negative value. We could make heappop
# return them in a different order, but ticks_diff() result would be the same. Conclusion:
# never add to a heap values where (a - b) == MODULO_HALF (and which are >= MODULO_HALF
# ticks apart in real time of course).
dprint
(
"
===
"
)
diff
=
edge_case
(
MODULO_HALF
,
0
)
assert
diff
==
-
MODULO_HALF
assert
edge_case
(
MODULO_HALF
,
100
)
==
diff
assert
edge_case
(
MODULO_HALF
,
-
100
)
==
diff
dprint
(
"
===
"
)
diff
=
edge_case
(
MODULO_HALF
+
1
,
0
)
assert
diff
==
MODULO_HALF
-
1
assert
edge_case
(
MODULO_HALF
+
1
,
100
)
==
diff
assert
edge_case
(
MODULO_HALF
+
1
,
-
100
)
==
diff
print
(
"
OK
"
)
This diff is collapsed.
Click to expand it.
tests/extmod/utimeq1.py.exp
0 → 100644
+
1
−
0
View file @
5d9025a7
OK
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment