This repository has been archived by the owner on May 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_atomiclong.py
95 lines (78 loc) · 2.36 KB
/
test_atomiclong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from atomiclong import AtomicLong, ffi, lib
def test_long_add_and_fetch():
l = ffi.new('long *', 0)
assert lib.long_add_and_fetch(l, 1) == 1
assert lib.long_add_and_fetch(l, 10) == 11
def test_long_sub_and_fetch():
l = ffi.new('long *', 0)
assert lib.long_sub_and_fetch(l, 1) == -1
assert lib.long_sub_and_fetch(l, 10) == -11
def test_long_bool_compare_and_swap():
l = ffi.new('long *', 0)
assert lib.long_bool_compare_and_swap(l, 0, 10) == True
assert lib.long_bool_compare_and_swap(l, 1, 20) == False
def test_atomiclong_repr():
l = AtomicLong(123456789)
assert '<AtomicLong at ' in repr(l)
assert '123456789>' in repr(l)
def test_atomiclong_value():
l = AtomicLong(0)
assert l.value == 0
l.value = 10
assert l.value == 10
def test_atomiclong_iadd():
l = AtomicLong(0)
l += 10
assert l.value == 10
def test_atomiclong_isub():
l = AtomicLong(0)
l -= 10
assert l.value == -10
def test_atomiclong_eq():
l1 = AtomicLong(0)
l2 = AtomicLong(1)
l3 = AtomicLong(0)
assert l1 == 0
assert l1 != 1
assert not (l2 == 0)
assert not (l2 != 1)
assert l1 == l3
assert not (l1 != l3)
assert l1 != l2
assert not (l1 == l2)
assert l1 == l1
def test_atomiclong_ordering():
l1 = AtomicLong(0)
l2 = AtomicLong(1)
l3 = AtomicLong(0)
assert l1 < l2
assert l1 <= l2
assert l1 <= l3
assert l2 > l1
assert l2 >= l3
assert l2 >= l2
assert l1 < 1
assert l1 <= 0
assert l1 <= 1
assert l1 > -1
assert l1 >= -1
assert l1 >= 0
def test_atomiclong_self_comparison_race():
"""
When comparing an AtomicLong to itself, it is possible that
self.value and other.value will not be equal because the underlying
long may have been incremented by another thread during the comparison.
Here we simulate this situation by ensuring that AtomicLong.value returns
two different numbers during the comparisons, by swapping out the
underlying storage for an object that pops items off a list.
"""
class StubStorage(object):
def __init__(self, values):
self._values = values
def __getitem__(self, _idx):
return self._values.pop()
l1 = AtomicLong(0)
l1._storage = StubStorage([0, 1])
assert l1 == l1
l1._storage = StubStorage([0, 1])
assert not (l1 < l1)