1
0
mirror of https://github.com/django/django.git synced 2025-06-11 06:29:13 +00:00

Used subTest in GEOS tests.

This commit is contained in:
David Smith 2024-11-07 21:33:33 +00:00
parent 91d7fd3295
commit a902233d8b
4 changed files with 763 additions and 678 deletions

View File

@ -37,6 +37,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_wkt(self):
"Testing WKT output."
for g in self.geometries.wkt_out:
with self.subTest(g=g):
geom = fromstr(g.wkt)
if geom.hasz:
self.assertEqual(g.ewkt, geom.wkt)
@ -51,6 +52,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_hex(self):
"Testing HEX output."
for g in self.geometries.hex_wkt:
with self.subTest(g=g):
geom = fromstr(g.wkt)
self.assertEqual(g.hex, geom.hex.decode())
@ -89,9 +91,10 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_kml(self):
"Testing KML output."
for tg in self.geometries.wkt_out:
geom = fromstr(tg.wkt)
kml = getattr(tg, "kml", False)
for g in self.geometries.wkt_out:
with self.subTest(g=g):
geom = fromstr(g.wkt)
kml = getattr(g, "kml", False)
if kml:
self.assertEqual(kml, geom.kml)
@ -133,6 +136,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_create_hex(self):
"Testing creation from HEX."
for g in self.geometries.hex_wkt:
with self.subTest(g=g):
geom_h = GEOSGeometry(g.hex)
# we need to do this so decimal places get normalized
geom_t = fromstr(g.wkt)
@ -141,6 +145,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_create_wkb(self):
"Testing creation from WKB."
for g in self.geometries.hex_wkt:
with self.subTest(g=g):
wkb = memoryview(bytes.fromhex(g.hex))
geom_h = GEOSGeometry(wkb)
# we need to do this so decimal places get normalized
@ -152,6 +157,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
srids = (-1, 32140)
for srid in srids:
for p in self.geometries.polygons:
with self.subTest(p=p):
ewkt = "SRID=%d;%s" % (srid, p.wkt)
poly = fromstr(ewkt)
self.assertEqual(srid, poly.srid)
@ -161,6 +167,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_json(self):
"Testing GeoJSON input/output (via GDAL)."
for g in self.geometries.json_geoms:
with self.subTest(g=g):
geom = GEOSGeometry(g.wkt)
if not hasattr(g, "not_equal"):
# Loading jsons to prevent decimal differences
@ -193,6 +200,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# Other tests use `fromfile()` on string filenames so those
# aren't tested here.
for fh in (wkt_f, wkb_f):
with self.subTest(fh=fh):
fh.seek(0)
pnt = fromfile(fh)
self.assertEqual(ref_pnt, pnt)
@ -209,6 +217,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# Error shouldn't be raise on equivalence testing with
# an invalid type.
for g in (p, ls):
with self.subTest(g=g):
self.assertIsNotNone(g)
self.assertNotEqual(g, {"foo": "bar"})
self.assertIsNot(g, False)
@ -323,6 +332,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
"Testing Point objects."
prev = fromstr("POINT(0 0)")
for p in self.geometries.points:
with self.subTest(p=p):
# Creating the point from the WKT
pnt = fromstr(p.wkt)
self.assertEqual(pnt.geom_type, "Point")
@ -384,6 +394,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_multipoints(self):
"Testing MultiPoint objects."
for mp in self.geometries.multipoints:
with self.subTest(mp=mp):
mpnt = fromstr(mp.wkt)
self.assertEqual(mpnt.geom_type, "MultiPoint")
self.assertEqual(mpnt.geom_typeid, 4)
@ -408,6 +419,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
"Testing LineString objects."
prev = fromstr("POINT(0 0)")
for line in self.geometries.linestrings:
with self.subTest(line=line):
ls = fromstr(line.wkt)
self.assertEqual(ls.geom_type, "LineString")
self.assertEqual(ls.geom_typeid, 1)
@ -430,7 +442,9 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# Creating a LineString from a tuple, list, and numpy array
self.assertEqual(ls, LineString(ls.tuple)) # tuple
self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments
self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list
self.assertEqual(
ls, LineString([list(tup) for tup in ls.tuple])
) # as list
# Point individual arguments
self.assertEqual(
ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt
@ -496,6 +510,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
"Testing MultiLineString objects."
prev = fromstr("POINT(0 0)")
for line in self.geometries.multilinestrings:
with self.subTest(line=line):
ml = fromstr(line.wkt)
self.assertEqual(ml.geom_type, "MultiLineString")
self.assertEqual(ml.geom_typeid, 5)
@ -517,7 +532,9 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
msg = f"invalid index: {ml_len}"
with self.assertRaisesMessage(IndexError, msg):
ml.__getitem__(ml_len)
self.assertEqual(ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt)
self.assertEqual(
ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt
)
self.assertEqual(
ml, MultiLineString(*tuple(LineString(s.tuple) for s in ml))
)
@ -525,6 +542,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_linearring(self):
"Testing LinearRing objects."
for rr in self.geometries.linearrings:
with self.subTest(rr=rr):
lr = fromstr(rr.wkt)
self.assertEqual(lr.geom_type, "LinearRing")
self.assertEqual(lr.geom_typeid, 2)
@ -580,6 +598,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
prev = fromstr("POINT(0 0)")
for p in self.geometries.polygons:
with self.subTest(p=p):
# Creating the Polygon, testing its properties.
poly = fromstr(p.wkt)
self.assertEqual(poly.geom_type, "Polygon")
@ -608,7 +627,9 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
self.assertEqual(ring.geom_typeid, 2)
if p.ext_ring_cs:
self.assertEqual(p.ext_ring_cs, ring.tuple)
self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing __getitem__
self.assertEqual(
p.ext_ring_cs, poly[0].tuple
) # Testing __getitem__
# Testing __getitem__ and __setitem__ on invalid indices
poly_len = len(poly)
@ -674,6 +695,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
"Testing MultiPolygon objects."
fromstr("POINT (0 0)")
for mp in self.geometries.multipolygons:
with self.subTest(mp=mp):
mpoly = fromstr(mp.wkt)
self.assertEqual(mpoly.geom_type, "MultiPolygon")
self.assertEqual(mpoly.geom_typeid, 6)
@ -693,7 +715,8 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
self.assertEqual(p.geom_typeid, 3)
self.assertIs(p.valid, True)
self.assertEqual(
mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt
mpoly.wkt,
MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt,
)
def test_memory_hijinks(self):
@ -723,6 +746,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_coord_seq(self):
"Testing Coordinate Sequence objects."
for p in self.geometries.polygons:
with self.subTest(p=p):
if p.ext_ring_cs:
# Constructing the polygon and getting the coordinate sequence
poly = fromstr(p.wkt)
@ -741,7 +765,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
c2 = cs[i] # Value from coordseq
self.assertEqual(c1, c2)
# Constructing the test value to set the coordinate sequence with
# Construct the test value to set the coordinate sequence with
if len(c1) == 2:
tset = (5, 23)
else:
@ -760,6 +784,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
with self.assertRaisesMessage(GEOSException, msg):
g.relate_pattern(0, "invalid pattern, yo")
for rg in self.geometries.relate_geoms:
with self.subTest(rg=rg):
a = fromstr(rg.wkt_a)
b = fromstr(rg.wkt_b)
self.assertEqual(rg.result, a.relate_pattern(b, rg.pattern))
@ -768,6 +793,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_intersection(self):
"Testing intersects() and intersection()."
for i in range(len(self.geometries.topology_geoms)):
with self.subTest(i=i):
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
i1 = fromstr(self.geometries.intersect_geoms[i].wkt)
@ -781,6 +807,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_union(self):
"Testing union()."
for i in range(len(self.geometries.topology_geoms)):
with self.subTest(i=i):
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
u1 = fromstr(self.geometries.union_geoms[i].wkt)
@ -793,6 +820,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_unary_union(self):
"Testing unary_union."
for i in range(len(self.geometries.topology_geoms)):
with self.subTest(i=i):
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
u1 = fromstr(self.geometries.union_geoms[i].wkt)
@ -802,6 +830,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_difference(self):
"Testing difference()."
for i in range(len(self.geometries.topology_geoms)):
with self.subTest(i=i):
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
d1 = fromstr(self.geometries.diff_geoms[i].wkt)
@ -814,6 +843,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_symdifference(self):
"Testing sym_difference()."
for i in range(len(self.geometries.topology_geoms)):
with self.subTest(i=i):
a = fromstr(self.geometries.topology_geoms[i].wkt_a)
b = fromstr(self.geometries.topology_geoms[i].wkt_b)
d1 = fromstr(self.geometries.sdiff_geoms[i].wkt)
@ -902,6 +932,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def _test_buffer(self, geometries, buffer_method_name):
for bg in geometries:
with self.subTest(bg=bg):
g = fromstr(bg.wkt)
# The buffer we expect
@ -966,6 +997,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
poly = fromstr(self.geometries.polygons[1].wkt, srid=4269)
self.assertEqual(4269, poly.srid)
for ring in poly:
with self.subTest(ring=ring):
self.assertEqual(4269, ring.srid)
poly.srid = 4326
self.assertEqual(4326, poly.shell.srid)
@ -976,6 +1008,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
)
self.assertEqual(32021, gc.srid)
for i in range(len(gc)):
with self.subTest(i=i):
self.assertEqual(32021, gc[i].srid)
# GEOS may get the SRID from HEXEWKB
@ -1009,6 +1042,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
def test_custom_srid(self):
"""Test with a null srid and a srid unknown to GDAL."""
for srid in [None, 999999]:
with self.subTest(srid=srid):
pnt = Point(111200, 220900, srid=srid)
self.assertTrue(
pnt.ewkt.startswith(
@ -1035,6 +1069,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
"Testing the mutability of Polygons and Geometry Collections."
# ### Testing the mutability of Polygons ###
for p in self.geometries.polygons:
with self.subTest(p=p):
poly = fromstr(p.wkt)
# Should only be able to use __setitem__ with LinearRing geometries.
@ -1046,7 +1081,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
with self.assertRaisesMessage(TypeError, msg):
poly.__setitem__(0, LineString((1, 1), (2, 2)))
# Constructing the new shell by adding 500 to every point in the old shell.
# Construct the new shell by adding 500 to every point in the old shell.
shell_tup = poly.shell.tuple
new_coords = []
for point in shell_tup:
@ -1061,6 +1096,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# ### Testing the mutability of Geometry Collections
for tg in self.geometries.multipoints:
with self.subTest(tg=tg):
mp = fromstr(tg.wkt)
for i in range(len(mp)):
# Creating a random point.
@ -1076,6 +1112,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# MultiPolygons involve much more memory management because each
# Polygon w/in the collection has its own rings.
for tg in self.geometries.multipolygons:
with self.subTest(tg=tg):
mpoly = fromstr(tg.wkt)
for i in range(len(mpoly)):
poly = mpoly[i]
@ -1250,6 +1287,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
geoms.append(LineString(numpy.array([])))
for g in geoms:
with self.subTest(g=g):
self.assertIs(g.empty, True)
# Testing len() and num_geom.
@ -1373,6 +1411,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# correct as having a 1 meter accuracy.
prec = -1
for p in (t1, t2, t3, k2):
with self.subTest(p=p):
self.assertAlmostEqual(trans.x, p.x, prec)
self.assertAlmostEqual(trans.y, p.y, prec)
@ -1448,6 +1487,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
tgeoms.append(Point(srid=4326))
tgeoms.append(Point())
for geom in tgeoms:
with self.subTest(geom=geom):
s1 = pickle.dumps(geom)
g1 = pickle.loads(s1)
self.assertEqual(geom, g1)
@ -1464,6 +1504,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
# A set of test points.
pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)]
for pnt in pnts:
with self.subTest(pnt=pnt):
# Results should be the same (but faster)
self.assertEqual(mpoly.contains(pnt), prep.contains(pnt))
self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt))

View File

@ -86,6 +86,7 @@ class GEOSMutationTest(SimpleTestCase):
"Testing Geometry IndexError"
p = Point(1, 2)
for i in range(-2, 2):
with self.subTest(i=i):
p._checkindex(i)
msg = "invalid index: 2"
with self.assertRaisesMessage(IndexError, msg):
@ -97,6 +98,7 @@ class GEOSMutationTest(SimpleTestCase):
def test01_PointMutations(self):
"Testing Point mutations"
for p in (Point(1, 2, 3), fromstr("POINT (1 2 3)")):
with self.subTest(p=p):
self.assertEqual(
p._get_single_external(1), 2.0, "Point _get_single_external"
)
@ -121,6 +123,7 @@ class GEOSMutationTest(SimpleTestCase):
"Testing Point API"
q = Point(4, 5, 3)
for p in (Point(1, 2, 3), fromstr("POINT (1 2 3)")):
with self.subTest(p=p):
p[0:2] = [4, 5]
for f in geos_function_tests:
self.assertEqual(f(q), f(p), "Point " + f.__name__)
@ -131,6 +134,7 @@ class GEOSMutationTest(SimpleTestCase):
LineString((1, 0), (4, 1), (6, -1)),
fromstr("LINESTRING (1 0,4 1,6 -1)"),
):
with self.subTest(ls=ls):
self.assertEqual(
ls._get_single_external(1),
(4.0, 1.0),
@ -153,6 +157,7 @@ class GEOSMutationTest(SimpleTestCase):
lsa = LineString(ls.coords)
for f in geos_function_tests:
with self.subTest(f=f):
self.assertEqual(f(lsa), f(ls), "LineString " + f.__name__)
def test05_Polygon(self):
@ -164,6 +169,7 @@ class GEOSMutationTest(SimpleTestCase):
),
fromstr("POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))"),
):
with self.subTest(pg=pg):
self.assertEqual(
pg._get_single_external(0),
LinearRing((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
@ -186,7 +192,13 @@ class GEOSMutationTest(SimpleTestCase):
self.assertEqual(
pg.coords,
(
((1.0, 2.0), (10.0, 0.0), (12.0, 9.0), (-1.0, 15.0), (1.0, 2.0)),
(
(1.0, 2.0),
(10.0, 0.0),
(12.0, 9.0),
(-1.0, 15.0),
(1.0, 2.0),
),
((4.0, 2.0), (5.0, 2.0), (5.0, 3.0), (4.0, 2.0)),
),
"Polygon _set_list",
@ -194,6 +206,7 @@ class GEOSMutationTest(SimpleTestCase):
lsa = Polygon(*pg.coords)
for f in geos_function_tests:
with self.subTest(f=f):
self.assertEqual(f(lsa), f(pg), "Polygon " + f.__name__)
def test06_Collection(self):
@ -203,6 +216,7 @@ class GEOSMutationTest(SimpleTestCase):
fromstr("MULTIPOINT (3 4,-1 2,5 -4,2 8)"),
)
for mp in points:
with self.subTest(mp=mp):
self.assertEqual(
mp._get_single_external(2),
Point(5, -4),
@ -211,9 +225,12 @@ class GEOSMutationTest(SimpleTestCase):
mp._set_list(3, map(Point, ((5, 5), (3, -2), (8, 1))))
self.assertEqual(
mp.coords, ((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)), "Collection _set_list"
mp.coords,
((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)),
"Collection _set_list",
)
lsa = MultiPoint(*map(Point, ((5, 5), (3, -2), (8, 1))))
for f in geos_function_tests:
with self.subTest(f=f):
self.assertEqual(f(lsa), f(mp), "MultiPoint " + f.__name__)

View File

@ -23,9 +23,8 @@ class GEOSIOTest(SimpleTestCase):
ref = GEOSGeometry(wkt)
g1 = wkt_r.read(wkt.encode())
g2 = wkt_r.read(wkt)
for geom in (g1, g2):
self.assertEqual(ref, geom)
self.assertEqual(g1, ref)
self.assertEqual(g2, ref)
# Should only accept string objects.
msg = "'wkt' must be bytes or str."
@ -71,12 +70,14 @@ class GEOSIOTest(SimpleTestCase):
g1 = wkb_r.read(wkb)
g2 = wkb_r.read(hex_bin)
g3 = wkb_r.read(hex_str)
for geom in (g1, g2, g3):
self.assertEqual(ref, geom)
self.assertEqual(ref, g1)
self.assertEqual(ref, g2)
self.assertEqual(ref, g3)
bad_input = (1, 5.23, None, False)
msg = "'wkb' must be bytes, str or memoryview."
for bad_wkb in bad_input:
with self.subTest(bad_wkb=bad_wkb):
with self.assertRaisesMessage(TypeError, msg):
wkb_r.read(bad_wkb)
@ -96,8 +97,11 @@ class GEOSIOTest(SimpleTestCase):
# Ensuring bad byteorders are not accepted.
for bad_byteorder in (-1, 2, 523, "foo", None):
with self.subTest(bad_byteorder=bad_byteorder):
# Equivalent of `wkb_w.byteorder = bad_byteorder`
msg = "Byte order parameter must be 0 (Big Endian) or 1 (Little Endian)."
msg = (
"Byte order parameter must be 0 (Big Endian) or 1 (Little Endian)."
)
with self.assertRaisesMessage(ValueError, msg):
wkb_w._set_byteorder(bad_byteorder)
@ -122,6 +126,7 @@ class GEOSIOTest(SimpleTestCase):
# Ensuring bad output dimensions are not accepted
for bad_outdim in (-1, 0, 1, 4, 423, "foo", None):
with self.subTest(bad_outdim=bad_outdim):
with self.assertRaisesMessage(
ValueError, "WKB output dimension must be 2 or 3"
):
@ -225,6 +230,7 @@ class GEOSIOTest(SimpleTestCase):
(b"010300000000000000", b"0103000020E610000000000000"),
]
):
with self.subTest(byteorder=byteorder, hexes=hexes):
wkb_w.byteorder = byteorder
for srid, hex in enumerate(hexes):
wkb_w.srid = srid
@ -233,4 +239,6 @@ class GEOSIOTest(SimpleTestCase):
GEOSGeometry(wkb_w.write_hex(p)), p if srid else p_no_srid
)
self.assertEqual(wkb_w.write(p), memoryview(binascii.a2b_hex(hex)))
self.assertEqual(GEOSGeometry(wkb_w.write(p)), p if srid else p_no_srid)
self.assertEqual(
GEOSGeometry(wkb_w.write(p)), p if srid else p_no_srid
)

View File

@ -78,6 +78,7 @@ class ListMixinTest(SimpleTestCase):
"Slice retrieval"
pl, ul = self.lists_of_len()
for i in self.limits_plus(1):
with self.subTest(i=i):
self.assertEqual(pl[i:], ul[i:], "slice [%d:]" % (i))
self.assertEqual(pl[:i], ul[:i], "slice [:%d]" % (i))
@ -93,6 +94,7 @@ class ListMixinTest(SimpleTestCase):
self.assertEqual(pl[:i:k], ul[:i:k], "slice [:%d:%d]" % (i, k))
for k in self.step_range():
with self.subTest(k=k):
self.assertEqual(pl[::k], ul[::k], "slice [::%d]" % (k))
def test02_setslice(self):
@ -103,6 +105,7 @@ class ListMixinTest(SimpleTestCase):
pl, ul = self.lists_of_len()
for slen in range(self.limit + 1):
with self.subTest(slen=slen):
ssl = nextRange(slen)
ul[:] = ssl
pl[:] = ssl
@ -129,7 +132,9 @@ class ListMixinTest(SimpleTestCase):
ssl = nextRange(len(ul[i:j:k]))
ul[i:j:k] = ssl
pl[i:j:k] = ssl
self.assertEqual(pl, ul[:], "set slice [%d:%d:%d]" % (i, j, k))
self.assertEqual(
pl, ul[:], "set slice [%d:%d:%d]" % (i, j, k)
)
sliceLen = len(ul[i:j:k])
msg = (
@ -166,6 +171,7 @@ class ListMixinTest(SimpleTestCase):
def test03_delslice(self):
"Delete slice"
for Len in range(self.limit):
with self.subTest(Len=Len):
pl, ul = self.lists_of_len(Len)
del pl[:]
del ul[:]
@ -213,15 +219,18 @@ class ListMixinTest(SimpleTestCase):
"Get/set/delete single item"
pl, ul = self.lists_of_len()
for i in self.limits_plus(0):
with self.subTest(i=i):
self.assertEqual(pl[i], ul[i], "get single item [%d]" % i)
for i in self.limits_plus(0):
with self.subTest(i=i):
pl, ul = self.lists_of_len()
pl[i] = 100
ul[i] = 100
self.assertEqual(pl[:], ul[:], "set single item [%d]" % i)
for i in self.limits_plus(0):
with self.subTest(i=i):
pl, ul = self.lists_of_len()
del pl[i]
del ul[i]
@ -242,6 +251,7 @@ class ListMixinTest(SimpleTestCase):
pl, ul = self.lists_of_len()
for i in (-1 - self.limit, self.limit):
msg = f"invalid index: {i}"
with self.subTest(i=i):
with self.assertRaisesMessage(IndexError, msg): # 'set index %d' % i)
setfcn(ul, i)
with self.assertRaisesMessage(IndexError, msg): # 'get index %d' % i)
@ -265,12 +275,14 @@ class ListMixinTest(SimpleTestCase):
self.assertEqual(pl[:], ul[:], "reverse")
for i in self.limits_plus(1):
with self.subTest(i=i):
pl, ul = self.lists_of_len()
pl.insert(i, 50)
ul.insert(i, 50)
self.assertEqual(pl[:], ul[:], "insert at %d" % i)
for i in self.limits_plus(0):
with self.subTest(i=i):
pl, ul = self.lists_of_len()
self.assertEqual(pl.pop(i), ul.pop(i), "popped value at %d" % i)
self.assertEqual(pl[:], ul[:], "after pop at %d" % i)
@ -293,12 +305,15 @@ class ListMixinTest(SimpleTestCase):
pl, ul = self.lists_of_len()
for val in range(self.limit):
with self.subTest(val=val):
self.assertEqual(pl.index(val), ul.index(val), "index of %d" % val)
for val in self.limits_plus(2):
with self.subTest(val=val):
self.assertEqual(pl.count(val), ul.count(val), "count %d" % val)
for val in range(self.limit):
with self.subTest(val=val):
pl, ul = self.lists_of_len()
pl.remove(val)
ul.remove(val)
@ -345,6 +360,7 @@ class ListMixinTest(SimpleTestCase):
msg = "Must have at least 3 items"
for i in range(len(ul) - ul._minlength + 1, len(ul)):
with self.subTest(i=i):
with self.assertRaisesMessage(ValueError, msg):
delfcn(ul, i)
with self.assertRaisesMessage(ValueError, msg):
@ -353,6 +369,7 @@ class ListMixinTest(SimpleTestCase):
ul._maxlength = 4
for i in range(0, ul._maxlength - len(ul)):
with self.subTest(i=i):
ul.append(i)
msg = "Cannot have more than 4 items"
with self.assertRaisesMessage(ValueError, msg):
@ -373,6 +390,7 @@ class ListMixinTest(SimpleTestCase):
"Index check"
pl, ul = self.lists_of_len()
for i in self.limits_plus(0):
with self.subTest(i=i):
if i < 0:
self.assertEqual(
ul._checkindex(i), i + self.limit, "_checkindex(neg index)"
@ -382,6 +400,7 @@ class ListMixinTest(SimpleTestCase):
for i in (-self.limit - 1, self.limit):
msg = f"invalid index: {i}"
with self.subTest(i=i):
with self.assertRaisesMessage(IndexError, msg):
ul._checkindex(i)