Skip to content

Commit 96b816a

Browse files
committed
Add tests for insincere implementations of ExactSizeIterator
#262 (comment)
1 parent e7b851b commit 96b816a

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/array.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,9 @@ impl<T: Element + AsPrimitive<f64>> PyArray<T, Ix1> {
12381238

12391239
#[cfg(test)]
12401240
mod tests {
1241-
use super::PyArray;
1241+
use super::*;
1242+
1243+
use std::ops::Range;
12421244

12431245
#[test]
12441246
fn test_get_unchecked() {
@@ -1269,4 +1271,36 @@ mod tests {
12691271
py_run!(py, arr, "assert arr.dtype.hasobject");
12701272
});
12711273
}
1274+
1275+
struct InsincereIterator(Range<usize>, usize);
1276+
1277+
impl Iterator for InsincereIterator {
1278+
type Item = usize;
1279+
1280+
fn next(&mut self) -> Option<Self::Item> {
1281+
self.0.next()
1282+
}
1283+
}
1284+
1285+
impl ExactSizeIterator for InsincereIterator {
1286+
fn len(&self) -> usize {
1287+
self.1
1288+
}
1289+
}
1290+
1291+
#[test]
1292+
#[should_panic]
1293+
fn from_exact_iter_too_short() {
1294+
Python::with_gil(|py| {
1295+
PyArray::from_exact_iter(py, InsincereIterator(0..3, 5));
1296+
});
1297+
}
1298+
1299+
#[test]
1300+
#[should_panic]
1301+
fn from_exact_iter_too_long() {
1302+
Python::with_gil(|py| {
1303+
PyArray::from_exact_iter(py, InsincereIterator(0..5, 3));
1304+
});
1305+
}
12721306
}

0 commit comments

Comments
 (0)