about summary refs log tree commit diff
path: root/src/libcore/tests
diff options
context:
space:
mode:
authorJohn Downey <jdowney@gmail.com>2019-03-06 12:17:26 -0600
committerJohn Downey <jdowney@gmail.com>2019-03-06 12:17:26 -0600
commita35cdd4e41a2bec5d8896653f898aaff61cb0106 (patch)
treed5e86d8d735b99b95bac9dcff987a50f6567d701 /src/libcore/tests
parentf22dca0a1bef4141e75326caacc3cd59f3d5be8e (diff)
downloadrust-a35cdd4e41a2bec5d8896653f898aaff61cb0106.tar.gz
rust-a35cdd4e41a2bec5d8896653f898aaff61cb0106.zip
Implement `iter::Sum` and `iter::Product` for `Option`
This is similar to the existing implementation for `Result`. It will
take each item into the accumulator unless a `None` is returned.
Diffstat (limited to 'src/libcore/tests')
-rw-r--r--src/libcore/tests/iter.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs
index d880abb181c..814f4ec4ca5 100644
--- a/src/libcore/tests/iter.rs
+++ b/src/libcore/tests/iter.rs
@@ -1067,6 +1067,14 @@ fn test_iterator_sum_result() {
 }
 
 #[test]
+fn test_iterator_sum_option() {
+    let v: &[Option<i32>] = &[Some(1), Some(2), Some(3), Some(4)];
+    assert_eq!(v.iter().cloned().sum::<Option<i32>>(), Some(10));
+    let v: &[Option<i32>] = &[Some(1), None, Some(3), Some(4)];
+    assert_eq!(v.iter().cloned().sum::<Option<i32>>(), None);
+}
+
+#[test]
 fn test_iterator_product() {
     let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
     assert_eq!(v[..4].iter().cloned().product::<i32>(), 0);
@@ -1083,6 +1091,14 @@ fn test_iterator_product_result() {
 }
 
 #[test]
+fn test_iterator_product_option() {
+    let v: &[Option<i32>] = &[Some(1), Some(2), Some(3), Some(4)];
+    assert_eq!(v.iter().cloned().product::<Option<i32>>(), Some(24));
+    let v: &[Option<i32>] = &[Some(1), None, Some(3), Some(4)];
+    assert_eq!(v.iter().cloned().product::<Option<i32>>(), None);
+}
+
+#[test]
 fn test_iterator_max() {
     let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
     assert_eq!(v[..4].iter().cloned().max(), Some(3));