Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scalexm committed Feb 6, 2018
1 parent a4ddd09 commit 9d4f4cb
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 4 deletions.
152 changes: 152 additions & 0 deletions src/lower/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,155 @@ fn overlapping_negative_impls() {
}
}
}

#[test]
fn well_formed_trait_decl() {
lowering_success! {
program {
trait Clone { }
trait Copy where Self: Clone { }

struct i32 { }

impl Clone for i32 { }
impl Copy for i32 { }
}
}
}

#[test]
fn ill_formed_trait_decl() {
lowering_error! {
program {
trait Clone { }
trait Copy where Self: Clone { }

struct i32 { }

impl Copy for i32 { }
} error_msg {
"trait impl for \"Copy\" does not meet well-formedness requirements"
}
}
}

#[test]
fn cyclic_traits() {
lowering_success! {
program {
trait A where Self: B { }
trait B where Self: A { }

impl<T> B for T { }
impl<T> A for T { }
}
}
}

#[test]
fn cyclic_traits_error() {
lowering_error! {
program {
trait Copy { }

trait A where Self: B, Self: Copy {}
trait B where Self: A { }

impl<T> B for T {}
impl<T> A for T where T: B {}
} error_msg {
"trait impl for \"B\" does not meet well-formedness requirements"
}
}
}

#[test]
fn cyclic_wf_requirement() {
lowering_success! {
program {
trait Foo where <Self as Foo>::Value: Foo {
type Value;
}

struct Unit { }
impl Foo for Unit {
type Value = Unit;
}
}
}
}

#[test]
fn ill_formed_assoc_ty() {
lowering_error! {
program {
trait Foo { }
struct OnlyFoo<T> where T: Foo { }

struct i32 { }

trait Bar {
type Value;
}

impl Bar for i32 {
type Value = OnlyFoo<i32>;
}
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}

#[test]
fn implied_bounds() {
lowering_success! {
program {
trait Eq { }
trait Hash where Self: Eq { }

struct Set<K> where K: Hash { }

struct OnlyEq<T> where T: Eq { }

trait Foo {
type Value;
}

impl<K> Foo for Set<K> {
type Value = OnlyEq<K>;
}
}
}
}

#[test]
fn ill_formed_ty_decl() {
lowering_error! {
program {
trait Hash { }
struct Set<K> where K: Hash { }

struct MyType<K> {
value: Set<K>
}
} error_msg {
"type declaration \"MyType\" does not meet well-formedness requirements"
}
}
}

#[test]
fn implied_bounds_on_ty_decl() {
lowering_success! {
program {
trait Eq { }
trait Hash where Self: Eq { }
struct OnlyEq<T> where T: Eq { }

struct MyType<K> where K: Hash {
value: OnlyEq<K>
}
}
}
}
6 changes: 5 additions & 1 deletion src/solve/test/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ fn run_bench(
goal_text: &str,
bencher: &mut Bencher,
expected: &str,
skip_coherence: bool
) {
let program = Arc::new(parse_and_lower_program(program_text, solver_choice).unwrap());
let program = Arc::new(parse_and_lower_program(program_text, solver_choice, skip_coherence).unwrap());
let env = Arc::new(program.environment());
ir::tls::set_current_program(&program, || {
let goal = parse_and_lower_goal(&program, goal_text).unwrap();
Expand Down Expand Up @@ -108,6 +109,7 @@ fn cycley_recursive_cached(b: &mut Bencher) {
CYCLEY_GOAL,
b,
"Unique",
true
);
}

Expand All @@ -122,6 +124,7 @@ fn cycley_recursive_uncached(b: &mut Bencher) {
CYCLEY_GOAL,
b,
"Unique",
true
);
}

Expand All @@ -135,5 +138,6 @@ fn cycley_slg(b: &mut Bencher) {
CYCLEY_GOAL,
b,
"Unique",
false
);
}
12 changes: 9 additions & 3 deletions src/solve/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ use std::sync::Arc;

mod bench;

fn parse_and_lower_program(text: &str, solver_choice: SolverChoice) -> Result<ir::Program> {
chalk_parse::parse_program(text)?.lower(solver_choice)
fn parse_and_lower_program(text: &str, solver_choice: SolverChoice, skip_coherence: bool)
-> Result<ir::Program>
{
if skip_coherence {
chalk_parse::parse_program(text)?.lower_without_coherence()
} else {
chalk_parse::parse_program(text)?.lower(solver_choice)
}
}

fn parse_and_lower_goal(program: &ir::Program, text: &str) -> Result<Box<ir::Goal>> {
Expand Down Expand Up @@ -98,7 +104,7 @@ fn solve_goal(program_text: &str, goals: Vec<(&str, SolverChoice, &str)>) {
for (goal_text, solver_choice, expected) in goals {
let (program, env) = program_env_cache.entry(solver_choice).or_insert_with(|| {
let program_text = &program_text[1..program_text.len() - 1]; // exclude `{}`
let program = Arc::new(parse_and_lower_program(program_text, solver_choice).unwrap());
let program = Arc::new(parse_and_lower_program(program_text, solver_choice, false).unwrap());
let env = Arc::new(program.environment());
(program, env)
});
Expand Down

0 comments on commit 9d4f4cb

Please sign in to comment.