Skip to content

Commit 33f12e6

Browse files
committed
refact(TCs): unittestize assertions
1 parent e33235a commit 33f12e6

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

smmap/test/test_buf.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,39 @@ def test_basics(self):
3838
# can call end access any time
3939
buf.end_access()
4040
buf.end_access()
41-
assert len(buf) == 0
41+
self.assertEqual(len(buf), 0)
4242

4343
# begin access can revive it, if the offset is suitable
4444
offset = 100
45-
assert buf.begin_access(c, fc.size) == False
46-
assert buf.begin_access(c, offset) == True
47-
assert len(buf) == fc.size - offset
45+
self.assertEqual(buf.begin_access(c, fc.size), False)
46+
self.assertEqual(buf.begin_access(c, offset), True)
47+
self.assertEqual(len(buf), fc.size - offset)
4848
assert buf.cursor().is_valid()
4949

5050
# empty begin access keeps it valid on the same path, but alters the offset
51-
assert buf.begin_access() == True
52-
assert len(buf) == fc.size
51+
self.assertEqual(buf.begin_access(), True)
52+
self.assertEqual(len(buf), fc.size)
5353
assert buf.cursor().is_valid()
5454

5555
# simple access
5656
with open(fc.path, 'rb') as fp:
5757
data = fp.read()
58-
assert data[offset] == buf[0]
59-
assert data[offset:offset * 2] == buf[0:offset]
58+
self.assertEqual(data[offset], buf[0])
59+
self.assertEqual(data[offset:offset * 2], buf[0:offset])
6060

6161
# negative indices, partial slices
62-
assert buf[-1] == buf[len(buf) - 1]
63-
assert buf[-10:] == buf[len(buf) - 10:len(buf)]
62+
self.assertEqual(buf[-1], buf[len(buf) - 1])
63+
self.assertEqual(buf[-10:], buf[len(buf) - 10:len(buf)])
6464

6565
# end access makes its cursor invalid
6666
buf.end_access()
6767
assert not buf.cursor().is_valid()
6868
assert buf.cursor().is_associated() # but it remains associated
6969

7070
# an empty begin access fixes it up again
71-
assert buf.begin_access() == True and buf.cursor().is_valid()
71+
self.assertEqual(buf.begin_access(), True and buf.cursor().is_valid())
7272

73-
assert man_optimal.num_file_handles() == 1
73+
self.assertEqual(man_optimal.num_file_handles(), 1)
7474

7575
def test_performance(self):
7676
# PERFORMANCE
@@ -91,7 +91,7 @@ def test_performance(self):
9191
(static_man, 'static optimal')):
9292
with manager:
9393
with SlidingWindowMapBuffer(manager.make_cursor(item)) as buf:
94-
assert manager.num_file_handles() == 1
94+
self.assertEqual(manager.num_file_handles(), 1)
9595
for access_mode in range(2): # single, multi
9696
num_accesses_left = max_num_accesses
9797
num_bytes = 0
@@ -105,21 +105,21 @@ def test_performance(self):
105105
ofs_start = randint(0, fsize)
106106
ofs_end = randint(ofs_start, fsize)
107107
d = buf[ofs_start:ofs_end]
108-
assert len(d) == ofs_end - ofs_start
109-
assert d == data[ofs_start:ofs_end]
108+
self.assertEqual(len(d), ofs_end - ofs_start)
109+
self.assertEqual(d, data[ofs_start:ofs_end])
110110
num_bytes += len(d)
111111
del d
112112
else:
113113
pos = randint(0, fsize)
114-
assert buf[pos] == data[pos]
114+
self.assertEqual(buf[pos], data[pos])
115115
num_bytes += 1
116116
# END handle mode
117117
# END handle num accesses
118118

119119
buf.end_access()
120120
assert manager.num_file_handles()
121121
assert manager.collect()
122-
assert manager.num_file_handles() == 0
122+
self.assertEqual(manager.num_file_handles(), 0)
123123
elapsed = max(time() - st, 0.001) # prevent zero division errors on windows
124124
mb = float(1000 * 1000)
125125
mode_str = (access_mode and "slice") or "single byte"

smmap/test/test_mman.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def test_cursor(self):
2424
ci = WindowCursor(man) # invalid cursor
2525
assert not ci.is_valid()
2626
assert not ci.is_associated()
27-
assert ci.size() == 0 # this is cached, so we can query it in invalid state
27+
self.assertEqual(ci.size(), 0) # this is cached, so we can query it in invalid state
2828

2929
cv = man.make_cursor(fc.path)
3030
assert not cv.is_valid() # no region mapped yet
3131
assert cv.is_associated() # but it know where to map it from
32-
assert cv.file_size() == fc.size
33-
assert cv.path() == fc.path
32+
self.assertEqual(cv.file_size(), fc.size)
33+
self.assertEqual(cv.path(), fc.path)
3434

3535
# copy module
3636
cio = copy(cv)
@@ -55,14 +55,14 @@ def test_memory_manager(self):
5555

5656
for man in (static_man, slide_man):
5757
with man:
58-
assert man.num_file_handles() == 0
59-
assert man.num_open_files() == 0
58+
self.assertEqual(man.num_file_handles(), 0)
59+
self.assertEqual(man.num_open_files(), 0)
6060
winsize_cmp_val = 0
6161
if isinstance(man, StaticWindowMapManager):
6262
winsize_cmp_val = -1
6363
# END handle window size
6464
assert man.window_size() > winsize_cmp_val
65-
assert man.mapped_memory_size() == 0
65+
self.assertEqual(man.mapped_memory_size(), 0)
6666
assert man.max_mapped_memory_size() > 0
6767

6868
# collection doesn't raise in 'any' mode
@@ -71,7 +71,7 @@ def test_memory_manager(self):
7171
man._collect_lru_region(10)
7272

7373
# doesn't fail if we over-allocate
74-
assert man._collect_lru_region(sys.maxsize) == 0
74+
self.assertEqual(man._collect_lru_region(sys.maxsize), 0)
7575

7676
# use a region, verify most basic functionality
7777
with FileCreator(self.k_window_test_size, "manager_test") as fc:
@@ -81,10 +81,10 @@ def test_memory_manager(self):
8181
c = man.make_cursor(item)
8282
assert c.path_or_fd() is item
8383
assert c.use_region(10, 10).is_valid()
84-
assert c.ofs_begin() == 10
85-
assert c.size() == 10
84+
self.assertEqual(c.ofs_begin(), 10)
85+
self.assertEqual(c.size(), 10)
8686
with open(fc.path, 'rb') as fp:
87-
assert c.buffer()[:] == fp.read(20)[10:]
87+
self.assertEqual(c.buffer()[:], fp.read(20)[10:])
8888

8989
if isinstance(item, int):
9090
self.assertRaises(ValueError, c.path)
@@ -108,73 +108,73 @@ def test_memman_operation(self):
108108
for mtype, args in ((StaticWindowMapManager, (0, fc.size // 3, max_num_handles)),
109109
(SlidingWindowMapManager, (fc.size // 100, fc.size // 3, max_num_handles)),):
110110
for item in (fc.path, fd):
111-
assert len(data) == fc.size
111+
self.assertEqual(len(data), fc.size)
112112

113113
# small windows, a reasonable max memory. Not too many regions at once
114114
with mtype(window_size=args[0], max_memory_size=args[1], max_open_handles=args[2]) as man:
115115
c = man.make_cursor(item)
116116

117117
# still empty (more about that is tested in test_memory_manager()
118-
assert man.num_open_files() == 0
119-
assert man.mapped_memory_size() == 0
118+
self.assertEqual(man.num_open_files(), 0)
119+
self.assertEqual(man.mapped_memory_size(), 0)
120120

121121
base_offset = 5000
122122
# window size is 0 for static managers, hence size will be 0. We take that into consideration
123123
size = man.window_size() // 2
124124
assert c.use_region(base_offset, size).is_valid()
125125
rr = c.region()
126-
assert rr.client_count() == 2 # the manager and the cursor and us
126+
self.assertEqual(rr.client_count(), 2) # the manager and the cursor and us
127127

128-
assert man.num_open_files() == 1
129-
assert man.num_file_handles() == 1
130-
assert man.mapped_memory_size() == rr.size()
128+
self.assertEqual(man.num_open_files(), 1)
129+
self.assertEqual(man.num_file_handles(), 1)
130+
self.assertEqual(man.mapped_memory_size(), rr.size())
131131

132-
# assert c.size() == size # the cursor may overallocate in its static version
133-
assert c.ofs_begin() == base_offset
134-
assert rr.ofs_begin() == 0 # it was aligned and expanded
132+
# self.assertEqual(c.size(), size # the cursor may overallocate in its static version)
133+
self.assertEqual(c.ofs_begin(), base_offset)
134+
self.assertEqual(rr.ofs_begin(), 0) # it was aligned and expanded
135135
if man.window_size():
136136
# but isn't larger than the max window (aligned)
137-
assert rr.size() == align_to_mmap(man.window_size(), True)
137+
self.assertEqual(rr.size(), align_to_mmap(man.window_size(), True))
138138
else:
139-
assert rr.size() == fc.size
139+
self.assertEqual(rr.size(), fc.size)
140140
# END ignore static managers which dont use windows and are aligned to file boundaries
141141

142-
assert c.buffer()[:] == data[base_offset:base_offset + (size or c.size())]
142+
self.assertEqual(c.buffer()[:], data[base_offset:base_offset + (size or c.size())])
143143

144144
# obtain second window, which spans the first part of the file - it is a still the same window
145145
nsize = (size or fc.size) - 10
146146
assert c.use_region(0, nsize).is_valid()
147-
assert c.region() == rr
148-
assert man.num_file_handles() == 1
149-
assert c.size() == nsize
150-
assert c.ofs_begin() == 0
151-
assert c.buffer()[:] == data[:nsize]
147+
self.assertEqual(c.region(), rr)
148+
self.assertEqual(man.num_file_handles(), 1)
149+
self.assertEqual(c.size(), nsize)
150+
self.assertEqual(c.ofs_begin(), 0)
151+
self.assertEqual(c.buffer()[:], data[:nsize])
152152

153153
# map some part at the end, our requested size cannot be kept
154154
overshoot = 4000
155155
base_offset = fc.size - (size or c.size()) + overshoot
156156
assert c.use_region(base_offset, size).is_valid()
157157
if man.window_size():
158-
assert man.num_file_handles() == 2
158+
self.assertEqual(man.num_file_handles(), 2)
159159
assert c.size() < size
160160
assert c.region() is not rr # old region is still available, but has not curser ref anymore
161-
assert rr.client_count() == 1 # only held by manager
161+
self.assertEqual(rr.client_count(), 1) # only held by manager
162162
else:
163163
assert c.size() < fc.size
164164
# END ignore static managers which only have one handle per file
165165
rr = c.region()
166-
assert rr.client_count() == 2 # manager + cursor
166+
self.assertEqual(rr.client_count(), 2) # manager + cursor
167167
assert rr.ofs_begin() < c.ofs_begin() # it should have extended itself to the left
168168
assert rr.ofs_end() <= fc.size # it cannot be larger than the file
169-
assert c.buffer()[:] == data[base_offset:base_offset + (size or c.size())]
169+
self.assertEqual(c.buffer()[:], data[base_offset:base_offset + (size or c.size())])
170170

171171
# unising a region makes the cursor invalid
172172
c.unuse_region()
173173
assert not c.is_valid()
174174
if man.window_size():
175175
# but doesn't change anything regarding the handle count - we cache it and only
176176
# remove mapped regions if we have to
177-
assert man.num_file_handles() == 2
177+
self.assertEqual(man.num_file_handles(), 2)
178178
# END ignore this for static managers
179179

180180
# iterate through the windows, verify data contents
@@ -201,7 +201,7 @@ def test_memman_operation(self):
201201
assert max_file_handles >= num_file_handles()
202202
assert c.use_region(base_offset, (size or c.size())).is_valid()
203203
csize = c.size()
204-
assert c.buffer()[:] == data[base_offset:base_offset + csize]
204+
self.assertEqual(c.buffer()[:], data[base_offset:base_offset + csize])
205205
memory_read += csize
206206

207207
assert includes_ofs(base_offset)
@@ -222,7 +222,7 @@ def test_memman_operation(self):
222222
# collection - it should be able to collect all
223223
assert man.num_file_handles()
224224
assert man.collect()
225-
assert man.num_file_handles() == 0
225+
self.assertEqual(man.num_file_handles(), 0)
226226
# END for each item
227227
# END for each manager type
228228
finally:

0 commit comments

Comments
 (0)