From 07cc7d9960a1bc767dc051b9fae0c7ad2b5cd97f Mon Sep 17 00:00:00 2001 From: Johannes Oertel Date: Fri, 24 Apr 2015 17:30:41 +0200 Subject: Change name of unit test sub-module to "tests". Changes the style guidelines regarding unit tests to recommend using a sub-module named "tests" instead of "test" for unit tests as "test" might clash with imports of libtest. --- src/doc/style/testing/unit.md | 8 +- src/doc/trpl/testing.md | 22 ++-- src/liballoc/heap.rs | 2 +- src/libcollections/linked_list.rs | 2 +- src/libcollections/vec_deque.rs | 2 +- src/libcoretest/num/mod.rs | 2 +- src/librand/chacha.rs | 2 +- src/librand/distributions/exponential.rs | 2 +- src/librand/distributions/gamma.rs | 2 +- src/librand/isaac.rs | 2 +- src/librand/reseeding.rs | 2 +- src/librustc/session/config.rs | 2 +- src/librustc_back/fs.rs | 2 +- src/librustc_back/rpath.rs | 2 +- src/librustc_data_structures/graph/mod.rs | 2 +- src/librustc_data_structures/graph/test.rs | 139 -------------------- src/librustc_data_structures/graph/tests.rs | 139 ++++++++++++++++++++ src/librustc_data_structures/unify/mod.rs | 2 +- src/librustc_data_structures/unify/test.rs | 195 ---------------------------- src/librustc_data_structures/unify/tests.rs | 195 ++++++++++++++++++++++++++++ src/librustdoc/html/toc.rs | 2 +- src/libstd/dynamic_lib.rs | 2 +- src/libstd/io/stdio.rs | 2 +- src/libstd/io/util.rs | 2 +- src/libstd/rand/os.rs | 2 +- src/libstd/rand/reader.rs | 2 +- src/libstd/rt/backtrace.rs | 2 +- src/libstd/sync/future.rs | 2 +- src/libstd/sync/mpsc/mod.rs | 2 +- src/libstd/sync/mpsc/select.rs | 2 +- src/libstd/sync/mpsc/spsc_queue.rs | 2 +- src/libstd/sync/mutex.rs | 2 +- src/libstd/sync/once.rs | 2 +- src/libstd/sys/common/remutex.rs | 2 +- src/libstd/thread/mod.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/codemap.rs | 2 +- src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/fold.rs | 2 +- src/libsyntax/parse/lexer/comments.rs | 2 +- src/libsyntax/parse/lexer/mod.rs | 2 +- src/libsyntax/parse/mod.rs | 2 +- src/libsyntax/parse/token.rs | 2 +- src/libsyntax/print/pprust.rs | 2 +- src/libsyntax/util/parser_testing.rs | 2 +- src/libsyntax/util/small_vector.rs | 2 +- src/libterm/terminfo/parm.rs | 2 +- src/libterm/terminfo/parser/compiled.rs | 2 +- src/test/run-pass/issue-16597.rs | 2 +- 50 files changed, 393 insertions(+), 393 deletions(-) delete mode 100644 src/librustc_data_structures/graph/test.rs create mode 100644 src/librustc_data_structures/graph/tests.rs delete mode 100644 src/librustc_data_structures/unify/test.rs create mode 100644 src/librustc_data_structures/unify/tests.rs diff --git a/src/doc/style/testing/unit.md b/src/doc/style/testing/unit.md index 813660d8fdf..dbbe9fc3ac6 100644 --- a/src/doc/style/testing/unit.md +++ b/src/doc/style/testing/unit.md @@ -1,10 +1,10 @@ % Unit testing -Unit tests should live in a `test` submodule at the bottom of the module they -test. Mark the `test` submodule with `#[cfg(test)]` so it is only compiled when +Unit tests should live in a `tests` submodule at the bottom of the module they +test. Mark the `tests` submodule with `#[cfg(test)]` so it is only compiled when testing. -The `test` module should contain: +The `tests` module should contain: * Imports needed only for testing. * Functions marked with `#[test]` striving for full coverage of the parent module's @@ -17,7 +17,7 @@ For example: // Excerpt from std::str #[cfg(test)] -mod test { +mod tests { #[test] fn test_eq() { assert!((eq(&"".to_owned(), &"".to_owned()))); diff --git a/src/doc/trpl/testing.md b/src/doc/trpl/testing.md index 8cf126cad95..45f87a67405 100644 --- a/src/doc/trpl/testing.md +++ b/src/doc/trpl/testing.md @@ -219,10 +219,10 @@ fn it_works() { This is a very common use of `assert_eq!`: call some function with some known arguments and compare it to the expected output. -# The `test` module +# The `tests` module There is one way in which our existing example is not idiomatic: it's -missing the test module. The idiomatic way of writing our example +missing the `tests` module. The idiomatic way of writing our example looks like this: ```{rust,ignore} @@ -231,7 +231,7 @@ pub fn add_two(a: i32) -> i32 { } #[cfg(test)] -mod test { +mod tests { use super::add_two; #[test] @@ -241,7 +241,7 @@ mod test { } ``` -There's a few changes here. The first is the introduction of a `mod test` with +There's a few changes here. The first is the introduction of a `mod tests` with a `cfg` attribute. The module allows us to group all of our tests together, and to also define helper functions if needed, that don't become a part of the rest of our crate. The `cfg` attribute only compiles our test code if we're @@ -260,7 +260,7 @@ pub fn add_two(a: i32) -> i32 { } #[cfg(test)] -mod test { +mod tests { use super::*; #[test] @@ -279,7 +279,7 @@ $ cargo test Running target/adder-91b3e234d4ed382a running 1 test -test test::it_works ... ok +test tests::it_works ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured @@ -292,7 +292,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured It works! -The current convention is to use the `test` module to hold your "unit-style" +The current convention is to use the `tests` module to hold your "unit-style" tests. Anything that just tests one small bit of functionality makes sense to go here. But what about "integration-style" tests instead? For that, we have the `tests` directory @@ -325,7 +325,7 @@ $ cargo test Running target/adder-91b3e234d4ed382a running 1 test -test test::it_works ... ok +test tests::it_works ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured @@ -346,7 +346,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured Now we have three sections: our previous test is also run, as well as our new one. -That's all there is to the `tests` directory. The `test` module isn't needed +That's all there is to the `tests` directory. The `tests` module isn't needed here, since the whole thing is focused on tests. Let's finally check out that third section: documentation tests. @@ -382,7 +382,7 @@ pub fn add_two(a: i32) -> i32 { } #[cfg(test)] -mod test { +mod tests { use super::*; #[test] @@ -405,7 +405,7 @@ $ cargo test Running target/adder-91b3e234d4ed382a running 1 test -test test::it_works ... ok +test tests::it_works ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs index 4c7441b1d2a..9d1d8a7ac24 100644 --- a/src/liballoc/heap.rs +++ b/src/liballoc/heap.rs @@ -384,7 +384,7 @@ mod imp { } #[cfg(test)] -mod test { +mod tests { extern crate test; use self::test::Bencher; use boxed::Box; diff --git a/src/libcollections/linked_list.rs b/src/libcollections/linked_list.rs index deb1476c23f..c73a6f9b324 100644 --- a/src/libcollections/linked_list.rs +++ b/src/libcollections/linked_list.rs @@ -933,7 +933,7 @@ impl Hash for LinkedList { } #[cfg(test)] -mod test { +mod tests { use std::clone::Clone; use std::iter::{Iterator, IntoIterator}; use std::option::Option::{Some, None, self}; diff --git a/src/libcollections/vec_deque.rs b/src/libcollections/vec_deque.rs index bbe7830b423..61369b30dea 100644 --- a/src/libcollections/vec_deque.rs +++ b/src/libcollections/vec_deque.rs @@ -1772,7 +1772,7 @@ impl fmt::Debug for VecDeque { } #[cfg(test)] -mod test { +mod tests { use core::iter::{Iterator, self}; use core::option::Option::Some; diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index 85ca547da85..0ea9f8afb4e 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -45,7 +45,7 @@ pub fn test_num(ten: T, two: T) where } #[cfg(test)] -mod test { +mod tests { use core::option::Option; use core::option::Option::{Some, None}; use core::num::Float; diff --git a/src/librand/chacha.rs b/src/librand/chacha.rs index 9cd3b74e155..bcb827cb471 100644 --- a/src/librand/chacha.rs +++ b/src/librand/chacha.rs @@ -202,7 +202,7 @@ impl Rand for ChaChaRng { #[cfg(test)] -mod test { +mod tests { use std::prelude::v1::*; use core::iter::order; diff --git a/src/librand/distributions/exponential.rs b/src/librand/distributions/exponential.rs index 5ba6d8912f2..f46d35a33db 100644 --- a/src/librand/distributions/exponential.rs +++ b/src/librand/distributions/exponential.rs @@ -82,7 +82,7 @@ impl IndependentSample for Exp { } #[cfg(test)] -mod test { +mod tests { use std::prelude::v1::*; use distributions::{Sample, IndependentSample}; diff --git a/src/librand/distributions/gamma.rs b/src/librand/distributions/gamma.rs index f37093c6db8..2951563934b 100644 --- a/src/librand/distributions/gamma.rs +++ b/src/librand/distributions/gamma.rs @@ -276,7 +276,7 @@ impl IndependentSample for StudentT { } #[cfg(test)] -mod test { +mod tests { use std::prelude::v1::*; use distributions::{Sample, IndependentSample}; diff --git a/src/librand/isaac.rs b/src/librand/isaac.rs index a7f7889783f..22fe46a8bd8 100644 --- a/src/librand/isaac.rs +++ b/src/librand/isaac.rs @@ -510,7 +510,7 @@ impl Rand for Isaac64Rng { #[cfg(test)] -mod test { +mod tests { use std::prelude::v1::*; use core::iter::order; diff --git a/src/librand/reseeding.rs b/src/librand/reseeding.rs index ea084b2816d..bb0b13c4375 100644 --- a/src/librand/reseeding.rs +++ b/src/librand/reseeding.rs @@ -120,7 +120,7 @@ impl Default for ReseedWithDefault { } #[cfg(test)] -mod test { +mod tests { use std::prelude::v1::*; use core::iter::{order, repeat}; diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index f4ea069447f..050ccd64712 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1111,7 +1111,7 @@ impl fmt::Display for CrateType { } #[cfg(test)] -mod test { +mod tests { use session::config::{build_configuration, optgroups, build_session_options}; use session::build_session; diff --git a/src/librustc_back/fs.rs b/src/librustc_back/fs.rs index 2ab4d7ff78a..37e3efb4d83 100644 --- a/src/librustc_back/fs.rs +++ b/src/librustc_back/fs.rs @@ -41,7 +41,7 @@ pub fn realpath(original: &Path) -> io::Result { } #[cfg(all(not(windows), test))] -mod test { +mod tests { use tempdir::TempDir; use std::fs::{self, File}; use super::realpath; diff --git a/src/librustc_back/rpath.rs b/src/librustc_back/rpath.rs index 58073079d31..1daeb1cb223 100644 --- a/src/librustc_back/rpath.rs +++ b/src/librustc_back/rpath.rs @@ -171,7 +171,7 @@ fn minimize_rpaths(rpaths: &[String]) -> Vec { } #[cfg(all(unix, test))] -mod test { +mod tests { use super::{RPathConfig}; use super::{minimize_rpaths, rpaths_to_flags, get_rpath_relative_to_output}; use std::path::{Path, PathBuf}; diff --git a/src/librustc_data_structures/graph/mod.rs b/src/librustc_data_structures/graph/mod.rs index 5741544fe54..17fd0b81536 100644 --- a/src/librustc_data_structures/graph/mod.rs +++ b/src/librustc_data_structures/graph/mod.rs @@ -36,7 +36,7 @@ use std::usize; use snapshot_vec::{SnapshotVec, SnapshotVecDelegate}; #[cfg(test)] -mod test; +mod tests; pub struct Graph { nodes: SnapshotVec> , diff --git a/src/librustc_data_structures/graph/test.rs b/src/librustc_data_structures/graph/test.rs deleted file mode 100644 index 33b2edd2e10..00000000000 --- a/src/librustc_data_structures/graph/test.rs +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2015 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use graph::*; -use std::fmt::Debug; - -type TestNode = Node<&'static str>; -type TestEdge = Edge<&'static str>; -type TestGraph = Graph<&'static str, &'static str>; - -fn create_graph() -> TestGraph { - let mut graph = Graph::new(); - - // Create a simple graph - // - // A -+> B --> C - // | | ^ - // | v | - // F D --> E - - let a = graph.add_node("A"); - let b = graph.add_node("B"); - let c = graph.add_node("C"); - let d = graph.add_node("D"); - let e = graph.add_node("E"); - let f = graph.add_node("F"); - - graph.add_edge(a, b, "AB"); - graph.add_edge(b, c, "BC"); - graph.add_edge(b, d, "BD"); - graph.add_edge(d, e, "DE"); - graph.add_edge(e, c, "EC"); - graph.add_edge(f, b, "FB"); - - return graph; -} - -#[test] -fn each_node() { - let graph = create_graph(); - let expected = ["A", "B", "C", "D", "E", "F"]; - graph.each_node(|idx, node| { - assert_eq!(&expected[idx.0], graph.node_data(idx)); - assert_eq!(expected[idx.0], node.data); - true - }); -} - -#[test] -fn each_edge() { - let graph = create_graph(); - let expected = ["AB", "BC", "BD", "DE", "EC", "FB"]; - graph.each_edge(|idx, edge| { - assert_eq!(&expected[idx.0], graph.edge_data(idx)); - assert_eq!(expected[idx.0], edge.data); - true - }); -} - -fn test_adjacent_edges(graph: &Graph, - start_index: NodeIndex, - start_data: N, - expected_incoming: &[(E,N)], - expected_outgoing: &[(E,N)]) { - assert!(graph.node_data(start_index) == &start_data); - - let mut counter = 0; - for (edge_index, edge) in graph.incoming_edges(start_index) { - assert!(graph.edge_data(edge_index) == &edge.data); - assert!(counter < expected_incoming.len()); - debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}", - counter, expected_incoming[counter], edge_index, edge); - match expected_incoming[counter] { - (ref e, ref n) => { - assert!(e == &edge.data); - assert!(n == graph.node_data(edge.source())); - assert!(start_index == edge.target); - } - } - counter += 1; - } - assert_eq!(counter, expected_incoming.len()); - - let mut counter = 0; - for (edge_index, edge) in graph.outgoing_edges(start_index) { - assert!(graph.edge_data(edge_index) == &edge.data); - assert!(counter < expected_outgoing.len()); - debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}", - counter, expected_outgoing[counter], edge_index, edge); - match expected_outgoing[counter] { - (ref e, ref n) => { - assert!(e == &edge.data); - assert!(start_index == edge.source); - assert!(n == graph.node_data(edge.target)); - } - } - counter += 1; - } - assert_eq!(counter, expected_outgoing.len()); -} - -#[test] -fn each_adjacent_from_a() { - let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(0), "A", - &[], - &[("AB", "B")]); -} - -#[test] -fn each_adjacent_from_b() { - let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(1), "B", - &[("FB", "F"), ("AB", "A"),], - &[("BD", "D"), ("BC", "C"),]); -} - -#[test] -fn each_adjacent_from_c() { - let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(2), "C", - &[("EC", "E"), ("BC", "B")], - &[]); -} - -#[test] -fn each_adjacent_from_d() { - let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(3), "D", - &[("BD", "B")], - &[("DE", "E")]); -} diff --git a/src/librustc_data_structures/graph/tests.rs b/src/librustc_data_structures/graph/tests.rs new file mode 100644 index 00000000000..33b2edd2e10 --- /dev/null +++ b/src/librustc_data_structures/graph/tests.rs @@ -0,0 +1,139 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use graph::*; +use std::fmt::Debug; + +type TestNode = Node<&'static str>; +type TestEdge = Edge<&'static str>; +type TestGraph = Graph<&'static str, &'static str>; + +fn create_graph() -> TestGraph { + let mut graph = Graph::new(); + + // Create a simple graph + // + // A -+> B --> C + // | | ^ + // | v | + // F D --> E + + let a = graph.add_node("A"); + let b = graph.add_node("B"); + let c = graph.add_node("C"); + let d = graph.add_node("D"); + let e = graph.add_node("E"); + let f = graph.add_node("F"); + + graph.add_edge(a, b, "AB"); + graph.add_edge(b, c, "BC"); + graph.add_edge(b, d, "BD"); + graph.add_edge(d, e, "DE"); + graph.add_edge(e, c, "EC"); + graph.add_edge(f, b, "FB"); + + return graph; +} + +#[test] +fn each_node() { + let graph = create_graph(); + let expected = ["A", "B", "C", "D", "E", "F"]; + graph.each_node(|idx, node| { + assert_eq!(&expected[idx.0], graph.node_data(idx)); + assert_eq!(expected[idx.0], node.data); + true + }); +} + +#[test] +fn each_edge() { + let graph = create_graph(); + let expected = ["AB", "BC", "BD", "DE", "EC", "FB"]; + graph.each_edge(|idx, edge| { + assert_eq!(&expected[idx.0], graph.edge_data(idx)); + assert_eq!(expected[idx.0], edge.data); + true + }); +} + +fn test_adjacent_edges(graph: &Graph, + start_index: NodeIndex, + start_data: N, + expected_incoming: &[(E,N)], + expected_outgoing: &[(E,N)]) { + assert!(graph.node_data(start_index) == &start_data); + + let mut counter = 0; + for (edge_index, edge) in graph.incoming_edges(start_index) { + assert!(graph.edge_data(edge_index) == &edge.data); + assert!(counter < expected_incoming.len()); + debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}", + counter, expected_incoming[counter], edge_index, edge); + match expected_incoming[counter] { + (ref e, ref n) => { + assert!(e == &edge.data); + assert!(n == graph.node_data(edge.source())); + assert!(start_index == edge.target); + } + } + counter += 1; + } + assert_eq!(counter, expected_incoming.len()); + + let mut counter = 0; + for (edge_index, edge) in graph.outgoing_edges(start_index) { + assert!(graph.edge_data(edge_index) == &edge.data); + assert!(counter < expected_outgoing.len()); + debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}", + counter, expected_outgoing[counter], edge_index, edge); + match expected_outgoing[counter] { + (ref e, ref n) => { + assert!(e == &edge.data); + assert!(start_index == edge.source); + assert!(n == graph.node_data(edge.target)); + } + } + counter += 1; + } + assert_eq!(counter, expected_outgoing.len()); +} + +#[test] +fn each_adjacent_from_a() { + let graph = create_graph(); + test_adjacent_edges(&graph, NodeIndex(0), "A", + &[], + &[("AB", "B")]); +} + +#[test] +fn each_adjacent_from_b() { + let graph = create_graph(); + test_adjacent_edges(&graph, NodeIndex(1), "B", + &[("FB", "F"), ("AB", "A"),], + &[("BD", "D"), ("BC", "C"),]); +} + +#[test] +fn each_adjacent_from_c() { + let graph = create_graph(); + test_adjacent_edges(&graph, NodeIndex(2), "C", + &[("EC", "E"), ("BC", "B")], + &[]); +} + +#[test] +fn each_adjacent_from_d() { + let graph = create_graph(); + test_adjacent_edges(&graph, NodeIndex(3), "D", + &[("BD", "B")], + &[("DE", "E")]); +} diff --git a/src/librustc_data_structures/unify/mod.rs b/src/librustc_data_structures/unify/mod.rs index 7036c010c65..a899bbacc03 100644 --- a/src/librustc_data_structures/unify/mod.rs +++ b/src/librustc_data_structures/unify/mod.rs @@ -14,7 +14,7 @@ use std::marker::PhantomData; use snapshot_vec as sv; #[cfg(test)] -mod test; +mod tests; /// This trait is implemented by any type that can serve as a type /// variable. We call such variables *unification keys*. For example, diff --git a/src/librustc_data_structures/unify/test.rs b/src/librustc_data_structures/unify/test.rs deleted file mode 100644 index dbe3cfc7a48..00000000000 --- a/src/librustc_data_structures/unify/test.rs +++ /dev/null @@ -1,195 +0,0 @@ -// Copyright 2015 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![allow(non_snake_case)] - -extern crate test; -use self::test::Bencher; -use std::collections::HashSet; -use unify::{UnifyKey, UnificationTable}; - -#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] -struct UnitKey(u32); - -impl UnifyKey for UnitKey { - type Value = (); - fn index(&self) -> u32 { self.0 } - fn from_index(u: u32) -> UnitKey { UnitKey(u) } - fn tag(_: Option) -> &'static str { "UnitKey" } -} - -#[test] -fn basic() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(()); - let k2 = ut.new_key(()); - assert_eq!(ut.unioned(k1, k2), false); - ut.union(k1, k2); - assert_eq!(ut.unioned(k1, k2), true); -} - -#[test] -fn big_array() { - let mut ut: UnificationTable = UnificationTable::new(); - let mut keys = Vec::new(); - const MAX: usize = 1 << 15; - - for _ in 0..MAX { - keys.push(ut.new_key(())); - } - - for i in 1..MAX { - let l = keys[i-1]; - let r = keys[i]; - ut.union(l, r); - } - - for i in 0..MAX { - assert!(ut.unioned(keys[0], keys[i])); - } -} - -#[bench] -fn big_array_bench(b: &mut Bencher) { - let mut ut: UnificationTable = UnificationTable::new(); - let mut keys = Vec::new(); - const MAX: usize = 1 << 15; - - for _ in 0..MAX { - keys.push(ut.new_key(())); - } - - - b.iter(|| { - for i in 1..MAX { - let l = keys[i-1]; - let r = keys[i]; - ut.union(l, r); - } - - for i in 0..MAX { - assert!(ut.unioned(keys[0], keys[i])); - } - }) -} - -#[test] -fn even_odd() { - let mut ut: UnificationTable = UnificationTable::new(); - let mut keys = Vec::new(); - const MAX: usize = 1 << 10; - - for i in 0..MAX { - let key = ut.new_key(()); - keys.push(key); - - if i >= 2 { - ut.union(key, keys[i-2]); - } - } - - for i in 1..MAX { - assert!(!ut.unioned(keys[i-1], keys[i])); - } - - for i in 2..MAX { - assert!(ut.unioned(keys[i-2], keys[i])); - } -} - -#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] -struct IntKey(u32); - -impl UnifyKey for IntKey { - type Value = Option; - fn index(&self) -> u32 { self.0 } - fn from_index(u: u32) -> IntKey { IntKey(u) } - fn tag(_: Option) -> &'static str { "IntKey" } -} - -/// Test unifying a key whose value is `Some(_)` with a key whose value is `None`. -/// Afterwards both should be `Some(_)`. -#[test] -fn unify_key_Some_key_None() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(Some(22)); - let k2 = ut.new_key(None); - assert!(ut.unify_var_var(k1, k2).is_ok()); - assert_eq!(ut.probe(k2), Some(22)); - assert_eq!(ut.probe(k1), Some(22)); -} - -/// Test unifying a key whose value is `None` with a key whose value is `Some(_)`. -/// Afterwards both should be `Some(_)`. -#[test] -fn unify_key_None_key_Some() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(Some(22)); - let k2 = ut.new_key(None); - assert!(ut.unify_var_var(k2, k1).is_ok()); - assert_eq!(ut.probe(k2), Some(22)); - assert_eq!(ut.probe(k1), Some(22)); -} - -/// Test unifying a key whose value is `Some(x)` with a key whose value is `Some(y)`. -/// This should yield an error. -#[test] -fn unify_key_Some_x_key_Some_y() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(Some(22)); - let k2 = ut.new_key(Some(23)); - assert_eq!(ut.unify_var_var(k1, k2), Err((22, 23))); - assert_eq!(ut.unify_var_var(k2, k1), Err((23, 22))); - assert_eq!(ut.probe(k1), Some(22)); - assert_eq!(ut.probe(k2), Some(23)); -} - -/// Test unifying a key whose value is `Some(x)` with a key whose value is `Some(x)`. -/// This should be ok. -#[test] -fn unify_key_Some_x_key_Some_x() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(Some(22)); - let k2 = ut.new_key(Some(22)); - assert!(ut.unify_var_var(k1, k2).is_ok()); - assert_eq!(ut.probe(k1), Some(22)); - assert_eq!(ut.probe(k2), Some(22)); -} - -/// Test unifying a key whose value is `None` with a value is `x`. -/// Afterwards key should be `x`. -#[test] -fn unify_key_None_val() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(None); - assert!(ut.unify_var_value(k1, 22).is_ok()); - assert_eq!(ut.probe(k1), Some(22)); -} - -/// Test unifying a key whose value is `Some(x)` with the value `y`. -/// This should yield an error. -#[test] -fn unify_key_Some_x_val_y() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(Some(22)); - assert_eq!(ut.unify_var_value(k1, 23), Err((22, 23))); - assert_eq!(ut.probe(k1), Some(22)); -} - -/// Test unifying a key whose value is `Some(x)` with the value `x`. -/// This should be ok. -#[test] -fn unify_key_Some_x_val_x() { - let mut ut: UnificationTable = UnificationTable::new(); - let k1 = ut.new_key(Some(22)); - assert!(ut.unify_var_value(k1, 22).is_ok()); - assert_eq!(ut.probe(k1), Some(22)); -} - diff --git a/src/librustc_data_structures/unify/tests.rs b/src/librustc_data_structures/unify/tests.rs new file mode 100644 index 00000000000..dbe3cfc7a48 --- /dev/null +++ b/src/librustc_data_structures/unify/tests.rs @@ -0,0 +1,195 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(non_snake_case)] + +extern crate test; +use self::test::Bencher; +use std::collections::HashSet; +use unify::{UnifyKey, UnificationTable}; + +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +struct UnitKey(u32); + +impl UnifyKey for UnitKey { + type Value = (); + fn index(&self) -> u32 { self.0 } + fn from_index(u: u32) -> UnitKey { UnitKey(u) } + fn tag(_: Option) -> &'static str { "UnitKey" } +} + +#[test] +fn basic() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(()); + let k2 = ut.new_key(()); + assert_eq!(ut.unioned(k1, k2), false); + ut.union(k1, k2); + assert_eq!(ut.unioned(k1, k2), true); +} + +#[test] +fn big_array() { + let mut ut: UnificationTable = UnificationTable::new(); + let mut keys = Vec::new(); + const MAX: usize = 1 << 15; + + for _ in 0..MAX { + keys.push(ut.new_key(())); + } + + for i in 1..MAX { + let l = keys[i-1]; + let r = keys[i]; + ut.union(l, r); + } + + for i in 0..MAX { + assert!(ut.unioned(keys[0], keys[i])); + } +} + +#[bench] +fn big_array_bench(b: &mut Bencher) { + let mut ut: UnificationTable = UnificationTable::new(); + let mut keys = Vec::new(); + const MAX: usize = 1 << 15; + + for _ in 0..MAX { + keys.push(ut.new_key(())); + } + + + b.iter(|| { + for i in 1..MAX { + let l = keys[i-1]; + let r = keys[i]; + ut.union(l, r); + } + + for i in 0..MAX { + assert!(ut.unioned(keys[0], keys[i])); + } + }) +} + +#[test] +fn even_odd() { + let mut ut: UnificationTable = UnificationTable::new(); + let mut keys = Vec::new(); + const MAX: usize = 1 << 10; + + for i in 0..MAX { + let key = ut.new_key(()); + keys.push(key); + + if i >= 2 { + ut.union(key, keys[i-2]); + } + } + + for i in 1..MAX { + assert!(!ut.unioned(keys[i-1], keys[i])); + } + + for i in 2..MAX { + assert!(ut.unioned(keys[i-2], keys[i])); + } +} + +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +struct IntKey(u32); + +impl UnifyKey for IntKey { + type Value = Option; + fn index(&self) -> u32 { self.0 } + fn from_index(u: u32) -> IntKey { IntKey(u) } + fn tag(_: Option) -> &'static str { "IntKey" } +} + +/// Test unifying a key whose value is `Some(_)` with a key whose value is `None`. +/// Afterwards both should be `Some(_)`. +#[test] +fn unify_key_Some_key_None() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(Some(22)); + let k2 = ut.new_key(None); + assert!(ut.unify_var_var(k1, k2).is_ok()); + assert_eq!(ut.probe(k2), Some(22)); + assert_eq!(ut.probe(k1), Some(22)); +} + +/// Test unifying a key whose value is `None` with a key whose value is `Some(_)`. +/// Afterwards both should be `Some(_)`. +#[test] +fn unify_key_None_key_Some() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(Some(22)); + let k2 = ut.new_key(None); + assert!(ut.unify_var_var(k2, k1).is_ok()); + assert_eq!(ut.probe(k2), Some(22)); + assert_eq!(ut.probe(k1), Some(22)); +} + +/// Test unifying a key whose value is `Some(x)` with a key whose value is `Some(y)`. +/// This should yield an error. +#[test] +fn unify_key_Some_x_key_Some_y() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(Some(22)); + let k2 = ut.new_key(Some(23)); + assert_eq!(ut.unify_var_var(k1, k2), Err((22, 23))); + assert_eq!(ut.unify_var_var(k2, k1), Err((23, 22))); + assert_eq!(ut.probe(k1), Some(22)); + assert_eq!(ut.probe(k2), Some(23)); +} + +/// Test unifying a key whose value is `Some(x)` with a key whose value is `Some(x)`. +/// This should be ok. +#[test] +fn unify_key_Some_x_key_Some_x() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(Some(22)); + let k2 = ut.new_key(Some(22)); + assert!(ut.unify_var_var(k1, k2).is_ok()); + assert_eq!(ut.probe(k1), Some(22)); + assert_eq!(ut.probe(k2), Some(22)); +} + +/// Test unifying a key whose value is `None` with a value is `x`. +/// Afterwards key should be `x`. +#[test] +fn unify_key_None_val() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(None); + assert!(ut.unify_var_value(k1, 22).is_ok()); + assert_eq!(ut.probe(k1), Some(22)); +} + +/// Test unifying a key whose value is `Some(x)` with the value `y`. +/// This should yield an error. +#[test] +fn unify_key_Some_x_val_y() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(Some(22)); + assert_eq!(ut.unify_var_value(k1, 23), Err((22, 23))); + assert_eq!(ut.probe(k1), Some(22)); +} + +/// Test unifying a key whose value is `Some(x)` with the value `x`. +/// This should be ok. +#[test] +fn unify_key_Some_x_val_x() { + let mut ut: UnificationTable = UnificationTable::new(); + let k1 = ut.new_key(Some(22)); + assert!(ut.unify_var_value(k1, 22).is_ok()); + assert_eq!(ut.probe(k1), Some(22)); +} + diff --git a/src/librustdoc/html/toc.rs b/src/librustdoc/html/toc.rs index 78feb6c77c4..93aa74d7005 100644 --- a/src/librustdoc/html/toc.rs +++ b/src/librustdoc/html/toc.rs @@ -198,7 +198,7 @@ impl fmt::Display for Toc { } #[cfg(test)] -mod test { +mod tests { use super::{TocBuilder, Toc, TocEntry}; #[test] diff --git a/src/libstd/dynamic_lib.rs b/src/libstd/dynamic_lib.rs index d0f990eaf78..ef72cbc96e1 100644 --- a/src/libstd/dynamic_lib.rs +++ b/src/libstd/dynamic_lib.rs @@ -117,7 +117,7 @@ impl DynamicLibrary { } #[cfg(all(test, not(target_os = "ios")))] -mod test { +mod tests { use super::*; use prelude::v1::*; use libc; diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 42fad701533..a9dab8191fd 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -418,7 +418,7 @@ pub fn _print(args: fmt::Arguments) { } #[cfg(test)] -mod test { +mod tests { use thread; use super::*; diff --git a/src/libstd/io/util.rs b/src/libstd/io/util.rs index c82f9515e8d..d797e757a48 100644 --- a/src/libstd/io/util.rs +++ b/src/libstd/io/util.rs @@ -102,7 +102,7 @@ impl Write for Sink { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use io::prelude::*; diff --git a/src/libstd/rand/os.rs b/src/libstd/rand/os.rs index 30d5ae5c600..a04bb6705b3 100644 --- a/src/libstd/rand/os.rs +++ b/src/libstd/rand/os.rs @@ -346,7 +346,7 @@ mod imp { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use sync::mpsc::channel; diff --git a/src/libstd/rand/reader.rs b/src/libstd/rand/reader.rs index 2837bac4456..d19bc5b617f 100644 --- a/src/libstd/rand/reader.rs +++ b/src/libstd/rand/reader.rs @@ -63,7 +63,7 @@ impl Rng for ReaderRng { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use super::ReaderRng; diff --git a/src/libstd/rt/backtrace.rs b/src/libstd/rt/backtrace.rs index ced84d7551e..72cbe2b533b 100644 --- a/src/libstd/rt/backtrace.rs +++ b/src/libstd/rt/backtrace.rs @@ -38,7 +38,7 @@ pub fn log_enabled() -> bool { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use sys_common; macro_rules! t { ($a:expr, $b:expr) => ({ diff --git a/src/libstd/sync/future.rs b/src/libstd/sync/future.rs index 2cdde1aca9e..2d281eb4e24 100644 --- a/src/libstd/sync/future.rs +++ b/src/libstd/sync/future.rs @@ -155,7 +155,7 @@ impl Future { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use sync::mpsc::channel; use sync::Future; diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index b3cc133d229..74e85db1a06 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -1065,7 +1065,7 @@ impl error::Error for TryRecvError { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use std::env; diff --git a/src/libstd/sync/mpsc/select.rs b/src/libstd/sync/mpsc/select.rs index b8ad92841f2..58f16a83ba9 100644 --- a/src/libstd/sync/mpsc/select.rs +++ b/src/libstd/sync/mpsc/select.rs @@ -346,7 +346,7 @@ impl Iterator for Packets { #[cfg(test)] #[allow(unused_imports)] -mod test { +mod tests { use prelude::v1::*; use thread; diff --git a/src/libstd/sync/mpsc/spsc_queue.rs b/src/libstd/sync/mpsc/spsc_queue.rs index c75ac130808..4dd84364fa8 100644 --- a/src/libstd/sync/mpsc/spsc_queue.rs +++ b/src/libstd/sync/mpsc/spsc_queue.rs @@ -241,7 +241,7 @@ impl Drop for Queue { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use sync::Arc; diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 5e688717c4a..caf011c54f2 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -361,7 +361,7 @@ pub fn guard_poison<'a, T>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use sync::mpsc::channel; diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 948965f5efa..2ce974c1271 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -121,7 +121,7 @@ impl Once { } #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use thread; diff --git a/src/libstd/sys/common/remutex.rs b/src/libstd/sys/common/remutex.rs index b35063c0e23..00238500c3a 100644 --- a/src/libstd/sys/common/remutex.rs +++ b/src/libstd/sys/common/remutex.rs @@ -151,7 +151,7 @@ impl<'a, T> Drop for ReentrantMutexGuard<'a, T> { #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard}; use cell::RefCell; diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 28e4650478b..9168a716d43 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -722,7 +722,7 @@ fn _assert_sync_and_send() { //////////////////////////////////////////////////////////////////////////////// #[cfg(test)] -mod test { +mod tests { use prelude::v1::*; use any::Any; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 399810cb7f5..94dc36b16ba 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1869,7 +1869,7 @@ pub struct MacroDef { } #[cfg(test)] -mod test { +mod tests { use serialize; use super::*; diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 78f06ce5fd5..720b2095a90 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -632,7 +632,7 @@ pub fn lit_is_str(lit: &Lit) -> bool { } #[cfg(test)] -mod test { +mod tests { use ast::*; use super::*; diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 34ad192845c..5e0cb647c8b 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -949,7 +949,7 @@ pub struct MalformedCodemapPositions { // #[cfg(test)] -mod test { +mod tests { use super::*; use std::rc::Rc; diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index d1db956adb3..4ea2d4e5c68 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1553,7 +1553,7 @@ impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> { #[cfg(test)] -mod test { +mod tests { use super::{pattern_bindings, expand_crate}; use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer, ExpansionConfig}; use ast; diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 8ba36cefc65..c857d4403cb 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -1343,7 +1343,7 @@ pub fn noop_fold_stmt(Spanned {node, span}: Stmt, folder: &mut T) } #[cfg(test)] -mod test { +mod tests { use std::io; use ast; use util::parser_testing::{string_to_crate, matches_codepattern}; diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index fb3a96f4c28..1577b50ad76 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -383,7 +383,7 @@ pub fn gather_comments_and_literals(span_diagnostic: &diagnostic::SpanHandler, } #[cfg(test)] -mod test { +mod tests { use super::*; #[test] fn test_block_doc_comment_1() { diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 8e37b983e21..d13ab65d72b 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -1501,7 +1501,7 @@ fn ident_continue(c: Option) -> bool { } #[cfg(test)] -mod test { +mod tests { use super::*; use codemap::{BytePos, CodeMap, Span, NO_EXPANSION}; diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 1a1713a8ba6..dee3e5fee74 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -761,7 +761,7 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) -> } #[cfg(test)] -mod test { +mod tests { use super::*; use std::rc::Rc; use codemap::{Span, BytePos, Pos, Spanned, NO_EXPANSION}; diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 2bb74944ce9..538aa384327 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -746,7 +746,7 @@ pub fn fresh_mark() -> ast::Mrk { } #[cfg(test)] -mod test { +mod tests { use super::*; use ast; use ext::mtwt; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 5a57e09fcff..980ce720026 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -3008,7 +3008,7 @@ impl<'a> State<'a> { fn repeat(s: &str, n: usize) -> String { iter::repeat(s).take(n).collect() } #[cfg(test)] -mod test { +mod tests { use super::*; use ast; diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs index 6adeb30a94e..d016eb39239 100644 --- a/src/libsyntax/util/parser_testing.rs +++ b/src/libsyntax/util/parser_testing.rs @@ -142,7 +142,7 @@ pub fn is_whitespace(c: char) -> bool { } #[cfg(test)] -mod test { +mod tests { use super::*; #[test] fn eqmodws() { diff --git a/src/libsyntax/util/small_vector.rs b/src/libsyntax/util/small_vector.rs index 153f9d4a26d..5353d12b678 100644 --- a/src/libsyntax/util/small_vector.rs +++ b/src/libsyntax/util/small_vector.rs @@ -204,7 +204,7 @@ impl MoveMap for SmallVector { } #[cfg(test)] -mod test { +mod tests { use super::*; #[test] diff --git a/src/libterm/terminfo/parm.rs b/src/libterm/terminfo/parm.rs index c9ad08bb852..2b8c24741ae 100644 --- a/src/libterm/terminfo/parm.rs +++ b/src/libterm/terminfo/parm.rs @@ -573,7 +573,7 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result ,String> { } #[cfg(test)] -mod test { +mod tests { use super::{expand,Param,Words,Variables,Number}; use std::result::Result::Ok; diff --git a/src/libterm/terminfo/parser/compiled.rs b/src/libterm/terminfo/parser/compiled.rs index 3de99088da4..ef42d8c2506 100644 --- a/src/libterm/terminfo/parser/compiled.rs +++ b/src/libterm/terminfo/parser/compiled.rs @@ -345,7 +345,7 @@ pub fn msys_terminfo() -> Box { } #[cfg(test)] -mod test { +mod tests { use super::{boolnames, boolfnames, numnames, numfnames, stringnames, stringfnames}; diff --git a/src/test/run-pass/issue-16597.rs b/src/test/run-pass/issue-16597.rs index d074095dbde..7f0a341f147 100644 --- a/src/test/run-pass/issue-16597.rs +++ b/src/test/run-pass/issue-16597.rs @@ -11,7 +11,7 @@ // compile-flags:--test // ignore-pretty turns out the pretty-printer doesn't handle gensym'd things... -mod test { +mod tests { use super::*; #[test] -- cgit 1.4.1-3-g733a5