]> git.proxmox.com Git - rustc.git/blame - src/test/ui/run-pass/iterators/iter-zip.rs
New upstream version 1.30.0+dfsg1
[rustc.git] / src / test / ui / run-pass / iterators / iter-zip.rs
CommitLineData
c30ab7b3
SL
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
b7449926 11// run-pass
c30ab7b3
SL
12// Test that .zip() specialization preserves side effects
13// in sideeffectful iterator adaptors.
14
15use std::cell::Cell;
16
17#[derive(Debug)]
18struct CountClone(Cell<i32>);
19
20fn count_clone() -> CountClone { CountClone(Cell::new(0)) }
21
22impl PartialEq<i32> for CountClone {
23 fn eq(&self, rhs: &i32) -> bool {
24 self.0.get() == *rhs
25 }
26}
27
28impl Clone for CountClone {
29 fn clone(&self) -> Self {
30 let ret = CountClone(self.0.clone());
31 let n = self.0.get();
32 self.0.set(n + 1);
33 ret
34 }
35}
36
37fn test_zip_cloned_sideffectful() {
38 let xs = [count_clone(), count_clone(), count_clone(), count_clone()];
39 let ys = [count_clone(), count_clone()];
40
41 for _ in xs.iter().cloned().zip(ys.iter().cloned()) { }
42
43 assert_eq!(&xs, &[1, 1, 1, 0][..]);
44 assert_eq!(&ys, &[1, 1][..]);
45
46 let xs = [count_clone(), count_clone()];
47 let ys = [count_clone(), count_clone(), count_clone(), count_clone()];
48
49 for _ in xs.iter().cloned().zip(ys.iter().cloned()) { }
50
51 assert_eq!(&xs, &[1, 1][..]);
52 assert_eq!(&ys, &[1, 1, 0, 0][..]);
53}
54
55fn test_zip_map_sideffectful() {
56 let mut xs = [0; 6];
57 let mut ys = [0; 4];
58
59 for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { }
60
61 assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
62 assert_eq!(&ys, &[1, 1, 1, 1]);
63
64 let mut xs = [0; 4];
65 let mut ys = [0; 6];
66
67 for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { }
68
69 assert_eq!(&xs, &[1, 1, 1, 1]);
70 assert_eq!(&ys, &[1, 1, 1, 1, 0, 0]);
71}
72
73fn test_zip_map_rev_sideffectful() {
74 let mut xs = [0; 6];
75 let mut ys = [0; 4];
76
77 {
78 let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
79 it.next_back();
80 }
81 assert_eq!(&xs, &[0, 0, 0, 1, 1, 1]);
82 assert_eq!(&ys, &[0, 0, 0, 1]);
83
84 let mut xs = [0; 6];
85 let mut ys = [0; 4];
86
87 {
88 let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
89 (&mut it).take(5).count();
90 it.next_back();
91 }
92 assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]);
93 assert_eq!(&ys, &[1, 1, 1, 1]);
94}
95
96fn test_zip_nested_sideffectful() {
97 let mut xs = [0; 6];
98 let ys = [0; 4];
99
100 {
101 // test that it has the side effect nested inside enumerate
102 let it = xs.iter_mut().map(|x| *x = 1).enumerate().zip(&ys);
103 it.count();
104 }
105 assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
106}
107
108fn main() {
109 test_zip_cloned_sideffectful();
110 test_zip_map_sideffectful();
111 test_zip_map_rev_sideffectful();
112 test_zip_nested_sideffectful();
113}