diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2017-12-07 17:38:15 -0500 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2017-12-20 14:38:13 -0500 |
| commit | cba82561cf0773b8a2bc3358313e555ffd0352c3 (patch) | |
| tree | fccafd5ce316ec17a2a5f189a5e7d41307a862bd /src | |
| parent | 80c510e353b34f006c284f9557e2b59d2f356029 (diff) | |
| download | rust-cba82561cf0773b8a2bc3358313e555ffd0352c3.tar.gz rust-cba82561cf0773b8a2bc3358313e555ffd0352c3.zip | |
add some run-pass tests for NLL showing that things work as expected
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/run-pass/nll/get_default.rs | 31 | ||||
| -rw-r--r-- | src/test/run-pass/nll/process_or_insert_default.rs | 37 | ||||
| -rw-r--r-- | src/test/run-pass/nll/rc-loop.rs | 40 |
3 files changed, 108 insertions, 0 deletions
diff --git a/src/test/run-pass/nll/get_default.rs b/src/test/run-pass/nll/get_default.rs new file mode 100644 index 00000000000..13ef907d8d0 --- /dev/null +++ b/src/test/run-pass/nll/get_default.rs @@ -0,0 +1,31 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(nll)] + +use std::collections::HashMap; + +fn get_default(map: &mut HashMap<usize, String>, key: usize) -> &mut String { + match map.get_mut(&key) { + Some(value) => value, + None => { + map.insert(key, "".to_string()); + map.get_mut(&key).unwrap() + } + } +} + +fn main() { + let map = &mut HashMap::new(); + map.insert(22, format!("Hello, world")); + map.insert(44, format!("Goodbye, world")); + assert_eq!(&*get_default(map, 22), "Hello, world"); + assert_eq!(&*get_default(map, 66), ""); +} diff --git a/src/test/run-pass/nll/process_or_insert_default.rs b/src/test/run-pass/nll/process_or_insert_default.rs new file mode 100644 index 00000000000..a3a484402cc --- /dev/null +++ b/src/test/run-pass/nll/process_or_insert_default.rs @@ -0,0 +1,37 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(nll)] + +use std::collections::HashMap; + +fn process_or_insert_default(map: &mut HashMap<usize, String>, key: usize) { + match map.get_mut(&key) { + Some(value) => { + process(value); + } + None => { + map.insert(key, "".to_string()); + } + } +} + +fn process(x: &str) { + assert_eq!(x, "Hello, world"); +} + +fn main() { + let map = &mut HashMap::new(); + map.insert(22, format!("Hello, world")); + map.insert(44, format!("Goodbye, world")); + process_or_insert_default(map, 22); + process_or_insert_default(map, 66); + assert_eq!(map[&66], ""); +} diff --git a/src/test/run-pass/nll/rc-loop.rs b/src/test/run-pass/nll/rc-loop.rs new file mode 100644 index 00000000000..2b746fac4d4 --- /dev/null +++ b/src/test/run-pass/nll/rc-loop.rs @@ -0,0 +1,40 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// A test for something that NLL enables. It sometimes happens that +// the `while let` pattern makes some borrows from a variable (in this +// case, `x`) that you need in order to compute the next value for +// `x`. The lexical checker makes this very painful. The NLL checker +// does not. + +#![feature(match_default_bindings)] +#![feature(nll)] + +use std::rc::Rc; + +#[derive(Debug, PartialEq, Eq)] +enum Foo { + Base(usize), + Next(Rc<Foo>), +} + +fn find_base(mut x: Rc<Foo>) -> Rc<Foo> { + while let Foo::Next(n) = &*x { + x = n.clone(); + } + x +} + +fn main() { + let chain = Rc::new(Foo::Next(Rc::new(Foo::Base(44)))); + let base = find_base(chain); + assert_eq!(&*base, &Foo::Base(44)); +} + |
