Skip to content

Commit 7e3fe82

Browse files
committed
code cleanup, additional tests
1 parent f5a6627 commit 7e3fe82

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/tri_map.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ TriMap_map_dst_no_fill(TriMapObject *self, PyObject *arg) {
11351135
return AK_TM_map_no_fill(self, from_src, array_from);
11361136
}
11371137

1138-
11391138
static inline PyObject *
11401139
TriMap_map_merge(TriMapObject *tm, PyObject *args)
11411140
{
@@ -1178,30 +1177,15 @@ TriMap_map_merge(TriMapObject *tm, PyObject *args)
11781177
Py_DECREF(dtype); // not needed
11791178
// will initialize to NULL, not None
11801179
array_to = (PyArrayObject*)PyArray_SimpleNew(1, dims, NPY_OBJECT);
1181-
// Py_INCREF(array_from); // normalize refs when casting
11821180
}
11831181
else if (dtype_is_unicode || dtype_is_string) {
11841182
array_to = (PyArrayObject*)PyArray_Zeros(1, dims, dtype, 0); // steals dtype ref
1185-
// Py_INCREF(array_from); // normalize refs when casting
11861183
}
11871184
else {
11881185
array_to = (PyArrayObject*)PyArray_Empty(1, dims, dtype, 0); // steals dtype ref
1189-
// if (PyArray_TYPE(array_from) == NPY_DATETIME &&
1190-
// PyArray_TYPE(array_to) == NPY_DATETIME &&
1191-
// AK_dt_unit_from_array(array_from) != AK_dt_unit_from_array(array_to)
1192-
// ) {
1193-
// // if trying to cast into a dt64 array, need to pre-convert; array_from is originally borrowed; calling cast sets it to a new ref
1194-
// dtype = PyArray_DESCR(array_to); // borrowed ref
1195-
// Py_INCREF(dtype);
1196-
// array_from = (PyArrayObject*)PyArray_CastToType(array_from, dtype, 0);
1197-
// }
1198-
// else {
1199-
// Py_INCREF(array_from); // normalize refs when casting
1200-
// }
12011186
}
12021187
if (array_to == NULL) {
12031188
PyErr_SetNone(PyExc_MemoryError);
1204-
// Py_DECREF((PyObject*)array_from);
12051189
return NULL;
12061190
}
12071191

test/test_tri_map.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,3 +1274,42 @@ def test_tri_map_merge_g(self) -> None:
12741274

12751275
post = tm.map_merge(src, dst)
12761276
self.assertEqual(post.tolist(), [None, False, -42, 'ee', 'ff'])
1277+
1278+
def test_tri_map_merge_h(self) -> None:
1279+
src = np.array([0, 200, 300, 40], dtype=np.int64)
1280+
dst = np.array([0, 200, 300, 40, 50], dtype=np.int64)
1281+
1282+
tm = TriMap(len(src), len(dst))
1283+
tm.register_one(0, 0)
1284+
tm.register_one(1, 1)
1285+
tm.register_one(2, 2)
1286+
tm.register_one(3, 3)
1287+
tm.register_unmatched_dst()
1288+
tm.finalize()
1289+
1290+
post = tm.map_merge(src, dst)
1291+
self.assertEqual(post.tolist(), [0, 200, 300, 40, 50])
1292+
1293+
def test_tri_map_merge_i(self) -> None:
1294+
src = np.array(['2022-01', '1954-03', '1743-09', '1988-12'], dtype=np.datetime64)
1295+
dst = np.array(['1743-09', '2022-01', '2022-01', '2022-01', '1743-09', '2005-11'], dtype=np.datetime64)
1296+
1297+
tm = TriMap(len(src), len(dst))
1298+
tm.register_many(0, np.array([1, 2, 3], dtype=np.dtype(np.int64)))
1299+
tm.register_one(1, -1)
1300+
tm.register_many(2, np.array([0, 4], dtype=np.dtype(np.int64)))
1301+
tm.register_one(3, -1)
1302+
tm.register_unmatched_dst()
1303+
tm.finalize()
1304+
1305+
post = tm.map_merge(src, dst)
1306+
self.assertEqual(list(post),
1307+
[np.datetime64('2022-01'),
1308+
np.datetime64('2022-01'),
1309+
np.datetime64('2022-01'),
1310+
np.datetime64('1954-03'),
1311+
np.datetime64('1743-09'),
1312+
np.datetime64('1743-09'),
1313+
np.datetime64('1988-12'),
1314+
np.datetime64('2005-11')]
1315+
)

0 commit comments

Comments
 (0)