mirror of
https://github.com/MarioSpore/Grinch-AP.git
synced 2025-10-21 04:01:32 -06:00
Speedups: remove dependency on c++ (#2796)
* Speedups: remove dependency on c++ * Speedups: intset: handle malloc failing * Speedups: intset: fix corner case for int64 on 32bit systems original idea was to only use bucket->val if int<pointer, but we always have a union now anyway * Speedups: add size comment to player_set bucket configuration * test: more tests for LocationStore.find_item * test: require _speedups in CI This kind of tests that the build succeeds. * test: even more tests for LocationStore.find_item * Speedups: intset uniform comment style * Speedups: intset: avoid memory leak when realloc fails * Speedups: intset: make `gcc -pedantic -std=c99 -fanalyzer` without warnings Unnamed unions are not in C99, this got fixed. The overhead of setting count=0 is minimal or optimized-out and silences -fanalizer (see comment). * Speedups: don't leak memory in case of exception * Speedups: intset: validate alloc and free This won't happen in our cython, but it's still a good addition. * CI: add test framework for C/C++ code * CI: ctest: fix cwd * Speedups: intset: ignore msvc warning * Tests: intset: revert attempt at no-asan We solve this with env vars in ctest now, and this fails for msvc. * Test: cpp: docs: fix typo * Test: cpp: docs: fix another typo * Test: intset: proper bucket count for Negative test INTxx_MIN % 1 would not produce a negative number, so the test was flawed.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# Tests for _speedups.LocationStore and NetUtils._LocationStore
|
||||
import os
|
||||
import typing
|
||||
import unittest
|
||||
import warnings
|
||||
@@ -7,6 +8,8 @@ from NetUtils import LocationStore, _LocationStore
|
||||
State = typing.Dict[typing.Tuple[int, int], typing.Set[int]]
|
||||
RawLocations = typing.Dict[int, typing.Dict[int, typing.Tuple[int, int, int]]]
|
||||
|
||||
ci = bool(os.environ.get("CI")) # always set in GitHub actions
|
||||
|
||||
sample_data: RawLocations = {
|
||||
1: {
|
||||
11: (21, 2, 7),
|
||||
@@ -24,6 +27,9 @@ sample_data: RawLocations = {
|
||||
3: {
|
||||
9: (99, 4, 0),
|
||||
},
|
||||
5: {
|
||||
9: (99, 5, 0),
|
||||
}
|
||||
}
|
||||
|
||||
empty_state: State = {
|
||||
@@ -45,14 +51,14 @@ class Base:
|
||||
store: typing.Union[LocationStore, _LocationStore]
|
||||
|
||||
def test_len(self) -> None:
|
||||
self.assertEqual(len(self.store), 4)
|
||||
self.assertEqual(len(self.store), 5)
|
||||
self.assertEqual(len(self.store[1]), 3)
|
||||
|
||||
def test_key_error(self) -> None:
|
||||
with self.assertRaises(KeyError):
|
||||
_ = self.store[0]
|
||||
with self.assertRaises(KeyError):
|
||||
_ = self.store[5]
|
||||
_ = self.store[6]
|
||||
locations = self.store[1] # no Exception
|
||||
with self.assertRaises(KeyError):
|
||||
_ = locations[7]
|
||||
@@ -71,7 +77,7 @@ class Base:
|
||||
self.assertEqual(self.store[1].get(10, (None, None, None)), (None, None, None))
|
||||
|
||||
def test_iter(self) -> None:
|
||||
self.assertEqual(sorted(self.store), [1, 2, 3, 4])
|
||||
self.assertEqual(sorted(self.store), [1, 2, 3, 4, 5])
|
||||
self.assertEqual(len(self.store), len(sample_data))
|
||||
self.assertEqual(list(self.store[1]), [11, 12, 13])
|
||||
self.assertEqual(len(self.store[1]), len(sample_data[1]))
|
||||
@@ -85,13 +91,26 @@ class Base:
|
||||
self.assertEqual(sorted(self.store[1].items())[0][1], self.store[1][11])
|
||||
|
||||
def test_find_item(self) -> None:
|
||||
# empty player set
|
||||
self.assertEqual(sorted(self.store.find_item(set(), 99)), [])
|
||||
# no such player, single
|
||||
self.assertEqual(sorted(self.store.find_item({6}, 99)), [])
|
||||
# no such player, set
|
||||
self.assertEqual(sorted(self.store.find_item({7, 8, 9}, 99)), [])
|
||||
# no such item
|
||||
self.assertEqual(sorted(self.store.find_item({3}, 1)), [])
|
||||
self.assertEqual(sorted(self.store.find_item({5}, 99)), [])
|
||||
# valid matches
|
||||
self.assertEqual(sorted(self.store.find_item({3}, 99)),
|
||||
[(4, 9, 99, 3, 0)])
|
||||
self.assertEqual(sorted(self.store.find_item({3, 4}, 99)),
|
||||
[(3, 9, 99, 4, 0), (4, 9, 99, 3, 0)])
|
||||
self.assertEqual(sorted(self.store.find_item({2, 3, 4}, 99)),
|
||||
[(3, 9, 99, 4, 0), (4, 9, 99, 3, 0)])
|
||||
# test hash collision in set
|
||||
self.assertEqual(sorted(self.store.find_item({3, 5}, 99)),
|
||||
[(4, 9, 99, 3, 0), (5, 9, 99, 5, 0)])
|
||||
self.assertEqual(sorted(self.store.find_item(set(range(2048)), 13)),
|
||||
[(1, 13, 13, 1, 0)])
|
||||
|
||||
def test_get_for_player(self) -> None:
|
||||
self.assertEqual(self.store.get_for_player(3), {4: {9}})
|
||||
@@ -196,18 +215,20 @@ class TestPurePythonLocationStoreConstructor(Base.TestLocationStoreConstructor):
|
||||
super().setUp()
|
||||
|
||||
|
||||
@unittest.skipIf(LocationStore is _LocationStore, "_speedups not available")
|
||||
@unittest.skipIf(LocationStore is _LocationStore and not ci, "_speedups not available")
|
||||
class TestSpeedupsLocationStore(Base.TestLocationStore):
|
||||
"""Run base method tests for cython implementation."""
|
||||
def setUp(self) -> None:
|
||||
self.assertFalse(LocationStore is _LocationStore, "Failed to load _speedups")
|
||||
self.store = LocationStore(sample_data)
|
||||
super().setUp()
|
||||
|
||||
|
||||
@unittest.skipIf(LocationStore is _LocationStore, "_speedups not available")
|
||||
@unittest.skipIf(LocationStore is _LocationStore and not ci, "_speedups not available")
|
||||
class TestSpeedupsLocationStoreConstructor(Base.TestLocationStoreConstructor):
|
||||
"""Run base constructor tests and tests the additional constraints for cython implementation."""
|
||||
def setUp(self) -> None:
|
||||
self.assertFalse(LocationStore is _LocationStore, "Failed to load _speedups")
|
||||
self.type = LocationStore
|
||||
super().setUp()
|
||||
|
||||
|
Reference in New Issue
Block a user