about summary refs log tree commit diff
path: root/tests/ui/hashmap
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/hashmap
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/hashmap')
-rw-r--r--tests/ui/hashmap/hashmap-capacity-overflow.rs12
-rw-r--r--tests/ui/hashmap/hashmap-index-mut.rs6
-rw-r--r--tests/ui/hashmap/hashmap-index-mut.stderr19
-rw-r--r--tests/ui/hashmap/hashmap-iter-value-lifetime.rs10
-rw-r--r--tests/ui/hashmap/hashmap-iter-value-lifetime.stderr15
-rw-r--r--tests/ui/hashmap/hashmap-lifetimes.rs8
-rw-r--r--tests/ui/hashmap/hashmap-lifetimes.stderr13
-rw-r--r--tests/ui/hashmap/hashmap-memory.rs94
8 files changed, 177 insertions, 0 deletions
diff --git a/tests/ui/hashmap/hashmap-capacity-overflow.rs b/tests/ui/hashmap/hashmap-capacity-overflow.rs
new file mode 100644
index 00000000000..2988af06556
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-capacity-overflow.rs
@@ -0,0 +1,12 @@
+// run-fail
+// error-pattern:capacity overflow
+// ignore-emscripten no processes
+
+use std::collections::hash_map::HashMap;
+use std::mem::size_of;
+
+fn main() {
+    let threshold = usize::MAX / size_of::<(u64, u64, u64)>();
+    let mut h = HashMap::<u64, u64>::with_capacity(threshold + 100);
+    h.insert(0, 0);
+}
diff --git a/tests/ui/hashmap/hashmap-index-mut.rs b/tests/ui/hashmap/hashmap-index-mut.rs
new file mode 100644
index 00000000000..98448e9d5f0
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-index-mut.rs
@@ -0,0 +1,6 @@
+use std::collections::HashMap;
+
+fn main() {
+    let mut map = HashMap::<u32, u32>::new();
+    map[&0] = 1; //~ ERROR cannot assign
+}
diff --git a/tests/ui/hashmap/hashmap-index-mut.stderr b/tests/ui/hashmap/hashmap-index-mut.stderr
new file mode 100644
index 00000000000..c1948ab6271
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-index-mut.stderr
@@ -0,0 +1,19 @@
+error[E0594]: cannot assign to data in an index of `HashMap<u32, u32>`
+  --> $DIR/hashmap-index-mut.rs:5:5
+   |
+LL |     map[&0] = 1;
+   |     ^^^^^^^^^^^ cannot assign
+   |
+   = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<u32, u32>`
+help: to modify a `HashMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API
+   |
+LL |     map.insert(&0, 1);
+   |        ~~~~~~~~  ~  +
+LL |     map.get_mut(&0).map(|val| { *val = 1; });
+   |        ~~~~~~~~~  ~~~~~~~~~~~~~~~~~~    ++++
+LL |     let val = map.entry(&0).or_insert(1);
+   |     +++++++++    ~~~~~~~  ~~~~~~~~~~~~ +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0594`.
diff --git a/tests/ui/hashmap/hashmap-iter-value-lifetime.rs b/tests/ui/hashmap/hashmap-iter-value-lifetime.rs
new file mode 100644
index 00000000000..260ea8c7ae1
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-iter-value-lifetime.rs
@@ -0,0 +1,10 @@
+fn main() {
+    let mut my_stuff = std::collections::HashMap::new();
+    my_stuff.insert(0, 42);
+
+    let (_, thing) = my_stuff.iter().next().unwrap();
+
+    my_stuff.clear(); //~ ERROR cannot borrow
+
+    println!("{}", *thing);
+}
diff --git a/tests/ui/hashmap/hashmap-iter-value-lifetime.stderr b/tests/ui/hashmap/hashmap-iter-value-lifetime.stderr
new file mode 100644
index 00000000000..d6e7a1d45f0
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-iter-value-lifetime.stderr
@@ -0,0 +1,15 @@
+error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as immutable
+  --> $DIR/hashmap-iter-value-lifetime.rs:7:5
+   |
+LL |     let (_, thing) = my_stuff.iter().next().unwrap();
+   |                      --------------- immutable borrow occurs here
+LL |
+LL |     my_stuff.clear();
+   |     ^^^^^^^^^^^^^^^^ mutable borrow occurs here
+LL |
+LL |     println!("{}", *thing);
+   |                    ------ immutable borrow later used here
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0502`.
diff --git a/tests/ui/hashmap/hashmap-lifetimes.rs b/tests/ui/hashmap/hashmap-lifetimes.rs
new file mode 100644
index 00000000000..295bf3b0e66
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-lifetimes.rs
@@ -0,0 +1,8 @@
+fn main() {
+    let mut my_stuff = std::collections::HashMap::new();
+    my_stuff.insert(0, 42);
+
+    let mut it = my_stuff.iter();
+    my_stuff.insert(1, 43); //~ ERROR cannot borrow
+    it;
+}
diff --git a/tests/ui/hashmap/hashmap-lifetimes.stderr b/tests/ui/hashmap/hashmap-lifetimes.stderr
new file mode 100644
index 00000000000..d1bcd53ae3b
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-lifetimes.stderr
@@ -0,0 +1,13 @@
+error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as immutable
+  --> $DIR/hashmap-lifetimes.rs:6:5
+   |
+LL |     let mut it = my_stuff.iter();
+   |                  --------------- immutable borrow occurs here
+LL |     my_stuff.insert(1, 43);
+   |     ^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
+LL |     it;
+   |     -- immutable borrow later used here
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0502`.
diff --git a/tests/ui/hashmap/hashmap-memory.rs b/tests/ui/hashmap/hashmap-memory.rs
new file mode 100644
index 00000000000..2031196ab21
--- /dev/null
+++ b/tests/ui/hashmap/hashmap-memory.rs
@@ -0,0 +1,94 @@
+// run-pass
+
+#![allow(non_camel_case_types)]
+#![allow(dead_code)]
+#![allow(unused_mut)]
+// ignore-emscripten No support for threads
+
+/**
+   A somewhat reduced test case to expose some Valgrind issues.
+
+   This originally came from the word-count benchmark.
+*/
+
+pub fn map(filename: String, mut emit: map_reduce::putter) {
+    emit(filename, "1".to_string());
+}
+
+mod map_reduce {
+    use std::collections::HashMap;
+    use std::sync::mpsc::{channel, Sender};
+    use std::str;
+    use std::thread;
+
+    pub type putter<'a> = Box<dyn FnMut(String, String) + 'a>;
+
+    pub type mapper = extern "C" fn(String, putter);
+
+    enum ctrl_proto { find_reducer(Vec<u8>, Sender<isize>), mapper_done, }
+
+    fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<String>) {
+        for i in &inputs {
+            let ctrl = ctrl.clone();
+            let i = i.clone();
+            thread::spawn(move|| map_task(ctrl.clone(), i.clone()) );
+        }
+    }
+
+    fn map_task(ctrl: Sender<ctrl_proto>, input: String) {
+        let mut intermediates = HashMap::new();
+
+        fn emit(im: &mut HashMap<String, isize>,
+                ctrl: Sender<ctrl_proto>, key: String,
+                _val: String) {
+            if im.contains_key(&key) {
+                return;
+            }
+            let (tx, rx) = channel();
+            println!("sending find_reducer");
+            ctrl.send(ctrl_proto::find_reducer(key.as_bytes().to_vec(), tx)).unwrap();
+            println!("receiving");
+            let c = rx.recv().unwrap();
+            println!("{}", c);
+            im.insert(key, c);
+        }
+
+        let ctrl_clone = ctrl.clone();
+        ::map(input, Box::new(|a,b| emit(&mut intermediates, ctrl.clone(), a, b)));
+        ctrl_clone.send(ctrl_proto::mapper_done).unwrap();
+    }
+
+    pub fn map_reduce(inputs: Vec<String>) {
+        let (tx, rx) = channel();
+
+        // This thread becomes the master control thread. It spawns others
+        // to do the rest.
+
+        let mut reducers: HashMap<String, isize>;
+
+        reducers = HashMap::new();
+
+        start_mappers(tx, inputs.clone());
+
+        let mut num_mappers = inputs.len() as isize;
+
+        while num_mappers > 0 {
+            match rx.recv().unwrap() {
+              ctrl_proto::mapper_done => { num_mappers -= 1; }
+              ctrl_proto::find_reducer(k, cc) => {
+                let mut c;
+                match reducers.get(&str::from_utf8(&k).unwrap().to_string()) {
+                  Some(&_c) => { c = _c; }
+                  None => { c = 0; }
+                }
+                cc.send(c).unwrap();
+              }
+            }
+        }
+    }
+}
+
+pub fn main() {
+    map_reduce::map_reduce(
+        vec!["../src/test/run-pass/hashmap-memory.rs".to_string()]);
+}