|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Test that .zip() specialization preserves side effects |
| 12 | +// in sideeffectful iterator adaptors. |
| 13 | + |
| 14 | +use std::cell::Cell; |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +struct CountClone(Cell<i32>); |
| 18 | + |
| 19 | +fn count_clone() -> CountClone { CountClone(Cell::new(0)) } |
| 20 | + |
| 21 | +impl PartialEq<i32> for CountClone { |
| 22 | + fn eq(&self, rhs: &i32) -> bool { |
| 23 | + self.0.get() == *rhs |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl Clone for CountClone { |
| 28 | + fn clone(&self) -> Self { |
| 29 | + let ret = CountClone(self.0.clone()); |
| 30 | + let n = self.0.get(); |
| 31 | + self.0.set(n + 1); |
| 32 | + ret |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn test_zip_cloned_sideffectful() { |
| 37 | + let xs = [count_clone(), count_clone(), count_clone(), count_clone()]; |
| 38 | + let ys = [count_clone(), count_clone()]; |
| 39 | + |
| 40 | + for _ in xs.iter().cloned().zip(ys.iter().cloned()) { } |
| 41 | + |
| 42 | + assert_eq!(&xs, &[1, 1, 1, 0][..]); |
| 43 | + assert_eq!(&ys, &[1, 1][..]); |
| 44 | + |
| 45 | + let xs = [count_clone(), count_clone()]; |
| 46 | + let ys = [count_clone(), count_clone(), count_clone(), count_clone()]; |
| 47 | + |
| 48 | + for _ in xs.iter().cloned().zip(ys.iter().cloned()) { } |
| 49 | + |
| 50 | + assert_eq!(&xs, &[1, 1][..]); |
| 51 | + assert_eq!(&ys, &[1, 1, 0, 0][..]); |
| 52 | +} |
| 53 | + |
| 54 | +fn test_zip_map_sideffectful() { |
| 55 | + let mut xs = [0; 6]; |
| 56 | + let mut ys = [0; 4]; |
| 57 | + |
| 58 | + for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { } |
| 59 | + |
| 60 | + assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]); |
| 61 | + assert_eq!(&ys, &[1, 1, 1, 1]); |
| 62 | + |
| 63 | + let mut xs = [0; 4]; |
| 64 | + let mut ys = [0; 6]; |
| 65 | + |
| 66 | + for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { } |
| 67 | + |
| 68 | + assert_eq!(&xs, &[1, 1, 1, 1]); |
| 69 | + assert_eq!(&ys, &[1, 1, 1, 1, 0, 0]); |
| 70 | +} |
| 71 | + |
| 72 | +fn test_zip_map_rev_sideffectful() { |
| 73 | + let mut xs = [0; 6]; |
| 74 | + let mut ys = [0; 4]; |
| 75 | + |
| 76 | + { |
| 77 | + let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)); |
| 78 | + it.next_back(); |
| 79 | + } |
| 80 | + assert_eq!(&xs, &[0, 0, 0, 1, 1, 1]); |
| 81 | + assert_eq!(&ys, &[0, 0, 0, 1]); |
| 82 | + |
| 83 | + let mut xs = [0; 6]; |
| 84 | + let mut ys = [0; 4]; |
| 85 | + |
| 86 | + { |
| 87 | + let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)); |
| 88 | + (&mut it).take(5).count(); |
| 89 | + it.next_back(); |
| 90 | + } |
| 91 | + assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]); |
| 92 | + assert_eq!(&ys, &[1, 1, 1, 1]); |
| 93 | +} |
| 94 | + |
| 95 | +fn test_zip_nested_sideffectful() { |
| 96 | + let mut xs = [0; 6]; |
| 97 | + let ys = [0; 4]; |
| 98 | + |
| 99 | + { |
| 100 | + // test that it has the side effect nested inside enumerate |
| 101 | + let it = xs.iter_mut().map(|x| *x = 1).enumerate().zip(&ys); |
| 102 | + it.count(); |
| 103 | + } |
| 104 | + assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]); |
| 105 | +} |
| 106 | + |
| 107 | +fn main() { |
| 108 | + test_zip_cloned_sideffectful(); |
| 109 | + test_zip_map_sideffectful(); |
| 110 | + test_zip_map_rev_sideffectful(); |
| 111 | + test_zip_nested_sideffectful(); |
| 112 | +} |
0 commit comments