<commit_before>// Copyright 2016 Pierre Talbot (IRCAM)

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use ast::*;
use partial::*;

pub fn generate_chococubes(ast: Program) -> Partial<String> {
  let mut gen = CodeGenerator::new();
  gen.push_block(ast.header);
  gen.push_line(&format!("public class {}", ast.class_name));
  gen.open_block();
  generate_execute_process(&mut gen);
  generate_items(&mut gen, ast.items);
  gen.close_block();
  Partial::Value(gen.code)
}

fn generate_items(gen: &mut CodeGenerator, items: Vec<Item>) {
  let items = generate_init_proc(gen, items);
  for item in items {
    match item {
      Item::Statement(_) =>
        unreachable!("Should have been moved into the init process."),
      Item::Proc(process) => generate_process(gen, process),
      Item::JavaStaticMethod(_, method) => gen.push_java_method(method)
    }
  }
}

fn generate_execute_process(gen: &mut CodeGenerator) {
  gen.push_line("public void execute()");
  gen.open_block();
  gen.push_block(String::from("\
    Program program = init();\n\
    SpaceMachine machine = SpaceMachine.create(program);\n\
    machine.execute();"));
  gen.close_block();
}

fn generate_init_proc(gen: &mut CodeGenerator, items: Vec<Item>) -> Vec<Item> {
  let mut body = vec![];
  let mut remaining = vec![];
  for item in items {
    match item {
      Item::Statement(stmt) => body.push(stmt),
      item => remaining.push(item)
    }
  }
  let init_proc = Process::new(String::from("init"),
    String::from("()"), body);
  generate_process(gen, init_proc);
  remaining
}

fn generate_process(gen: &mut CodeGenerator, process: Process) {
  gen.push_line(&format!(
    "public Program {}{}", process.name, process.params));
  gen.open_block();
  gen.push_line("return");
  gen.indent();
  generate_program(gen, process.body);
  gen.unindent();
  gen.push_line(";");
  gen.close_block();
}

fn generate_program(gen: &mut CodeGenerator, body: Block) {
}

struct CodeGenerator {
  indent: usize,
  pub code: String
}

impl CodeGenerator {
  pub fn new() -> CodeGenerator {
    CodeGenerator {
      indent: 0,
      code: String::new()
    }
  }

  pub fn indent(&mut self) {
    self.indent += 2;
  }

  pub fn unindent(&mut self) {
    self.indent -= 2;
  }

  pub fn open_block(&mut self) {
    self.push_line("{");
    self.indent();
  }

  pub fn close_block(&mut self) {
    self.unindent();
    self.push_line("}");
  }

  pub fn push_line(&mut self, code_line: &str) {
    self.code += &self.indent_spaces();
    self.raw_push_line(code_line);
  }

  pub fn raw_push_line(&mut self, code_line: &str) {
    self.code += code_line;
    self.newline();
  }

  pub fn push_java_method(&mut self, code_block: String) {
    let mut lines_iter = code_block.lines();
    self.push_line(lines_iter.next().unwrap());
    for line in lines_iter {
      self.raw_push_line(line);
    }
    self.newline();
  }

  pub fn push_block(&mut self, code_block: String) {
    for line in code_block.lines() {
      self.push_line(line);
    }
  }

  fn newline(&mut self) {
    self.code += "\n";
  }

  fn indent_spaces(&self) -> String {
    let mut res = String::new();
    for _ in 0..self.indent {
      res.push(' ');
    }
    res
  }
}
<commit_msg>[Bonsai][Code generation] Generate the sequence statement.<commit_after>// Copyright 2016 Pierre Talbot (IRCAM)

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use ast::*;
use partial::*;

pub fn generate_chococubes(ast: Program) -> Partial<String> {
  let mut gen = CodeGenerator::new();
  gen.push_block(ast.header);
  gen.push_line(&format!("public class {}", ast.class_name));
  gen.open_block();
  generate_execute_process(&mut gen);
  generate_items(&mut gen, ast.items);
  gen.close_block();
  Partial::Value(gen.code)
}

fn generate_items(gen: &mut CodeGenerator, items: Vec<Item>) {
  let items = generate_init_proc(gen, items);
  for item in items {
    match item {
      Item::Statement(_) =>
        unreachable!("Should have been moved into the init process."),
      Item::Proc(process) => generate_process(gen, process),
      Item::JavaStaticMethod(_, method) => gen.push_java_method(method)
    }
  }
}

fn generate_execute_process(gen: &mut CodeGenerator) {
  gen.push_line("public void execute()");
  gen.open_block();
  gen.push_block(String::from("\
    Program program = init();\n\
    SpaceMachine machine = SpaceMachine.create(program);\n\
    machine.execute();"));
  gen.close_block();
}

fn generate_init_proc(gen: &mut CodeGenerator, items: Vec<Item>) -> Vec<Item> {
  let mut body = vec![];
  let mut remaining = vec![];
  for item in items {
    match item {
      Item::Statement(stmt) => body.push(stmt),
      item => remaining.push(item)
    }
  }
  let init_proc = Process::new(String::from("init"),
    String::from("()"), body);
  generate_process(gen, init_proc);
  remaining
}

fn generate_process(gen: &mut CodeGenerator, process: Process) {
  gen.push_line(&format!(
    "public Program {}{}", process.name, process.params));
  gen.open_block();
  gen.push_line("return");
  gen.indent();
  generate_program(gen, process.body);
  gen.unindent();
  gen.continue_line(";");
  gen.close_block();
}

fn generate_program(gen: &mut CodeGenerator, body: Block) {
  generate_sequence(gen, body);
}

fn generate_sequence(gen: &mut CodeGenerator, mut body: Block) {
  if body.len() == 1 {
    generate_statement(gen, body.pop().unwrap());
  }
  else {
    let mid = body.len() / 2;
    let right = body.split_off(mid);
    gen.push_line("SC.seq(");
    gen.indent();
    generate_sequence(gen, body);
    gen.continue_line(",");
    generate_sequence(gen, right);
    gen.push(")");
    gen.unindent();
  }
}

fn generate_statement(gen: &mut CodeGenerator, stmt: Stmt) {
  gen.push("_");
}

struct CodeGenerator {
  indent: usize,
  pub code: String
}

impl CodeGenerator {
  pub fn new() -> CodeGenerator {
    CodeGenerator {
      indent: 0,
      code: String::new()
    }
  }

  pub fn indent(&mut self) {
    self.indent += 2;
  }

  pub fn unindent(&mut self) {
    self.indent -= 2;
  }

  pub fn open_block(&mut self) {
    self.push_line("{");
    self.indent();
  }

  pub fn close_block(&mut self) {
    self.unindent();
    self.push_line("}");
  }

  pub fn push(&mut self, code: &str) {
    if self.code.ends_with("\n") {
      self.push_indent();
    }
    self.code += code;
  }

  pub fn push_line(&mut self, code_line: &str) {
    self.push_indent();
    self.continue_line(code_line);
  }

  pub fn continue_line(&mut self, code_line: &str) {
    self.code += code_line;
    self.newline();
  }

  pub fn push_java_method(&mut self, code_block: String) {
    let mut lines_iter = code_block.lines();
    self.push_line(lines_iter.next().unwrap());
    for line in lines_iter {
      self.continue_line(line);
    }
    self.newline();
  }

  pub fn push_block(&mut self, code_block: String) {
    for line in code_block.lines() {
      self.push_line(line);
    }
  }

  fn newline(&mut self) {
    self.code += "\n";
  }

  fn push_indent(&mut self) {
    for _ in 0..self.indent {
      self.code.push(' ');
    }
  }
}
<|endoftext|>


<commit_before>use std::env;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process;

fn spawn_run_bindgen<P, Q, R>(run_bindgen: P, bindgen: Q, header: R) -> process::Child
    where P: AsRef<Path>,
          Q: AsRef<Path>,
          R: AsRef<Path>
{
    let run_bindgen = run_bindgen.as_ref();
    let bindgen = bindgen.as_ref();
    let header = header.as_ref();

    // Convert from "tests/headers/foo.hpp" to "tests/expectations/foo.rs" by
    // saving the filename, popping off "headers/foo.hpp", pushing
    // "expectations", pushing the saved filename, and finally modifying the
    // extension.

    let mut expected = PathBuf::from(header);
    let file_name = expected.file_name()
        .expect("Should have filename")
        .to_os_string();
    expected.pop();
    expected.pop();
    expected.push("expectations");
    expected.push(file_name);
    expected.set_extension("rs");

    let mut cmd = process::Command::new(run_bindgen);
    cmd.stdout(process::Stdio::piped())
        .stderr(process::Stdio::piped())
        .arg(bindgen)
        .arg(header)
        .arg(expected);

    if cfg!(feature = "llvm_stable") {
        cmd.arg("--feature")
            .arg("llvm_stable");
    }

    cmd.spawn()
        .expect("Should be able to spawn run-bindgen.py child process")
}

#[test]
fn run_bindgen_tests() {
    let crate_root = env::var("CARGO_MANIFEST_DIR")
        .expect("should have CARGO_MANIFEST_DIR environment variable");

    let mut run_bindgen = PathBuf::from(&crate_root);
    run_bindgen.push("tests");
    run_bindgen.push("tools");
    run_bindgen.push("run-bindgen.py");

    let mut bindgen = PathBuf::from(&crate_root);
    bindgen.push("target");
    bindgen.push("debug");
    bindgen.push("bindgen");
    if !bindgen.is_file() {
        panic!("{} is not a file! Build bindgen before running tests.",
               bindgen.display());
    }

    let mut headers_dir = PathBuf::from(&crate_root);
    headers_dir.push("tests");
    headers_dir.push("headers");

    let entries = fs::read_dir(&headers_dir)
        .expect("Should read directory")
        .map(|result| result.expect("Should read directory entry"));

    let tests = entries.filter(|entry| {
        match entry.path().extension().map(|s| s.to_str()) {
            Some(Some("h")) |
            Some(Some("hpp")) => true,
            _ => false,
        }
    });

    // First spawn all child processes and collect them, then wait on each
    // one. This runs the tests in parallel rather than serially.

    let children: Vec<_> = tests.map(|entry| {
            let child = spawn_run_bindgen(run_bindgen.clone(), bindgen.clone(), entry.path());
            (entry.path(), child)
        })
        .collect();

    let failures: Vec<_> = children.into_iter()
        .filter_map(|(path, mut child)| {
            let passed = child.wait()
                .expect("Should wait on child process")
                .success();

            if passed { None } else { Some((path, child)) }
        })
        .collect();

    let num_failures = failures.len();

    for (path, child) in failures {
        println!("FAIL: {}", path.display());

        let mut buf = String::new();

        child.stdout
            .expect("should have stdout piped")
            .read_to_string(&mut buf)
            .expect("should read child's stdout");
        for line in buf.lines() {
            println!("child stdout> {}", line);
        }

        child.stderr
            .expect("should have stderr piped")
            .read_to_string(&mut buf)
            .expect("should read child's stderr");
        for line in buf.lines() {
            println!("child stderr> {}", line);
        }
    }

    if num_failures > 0 {
        panic!("{} test failures!", num_failures);
    }
}
<commit_msg>Ensure that the bindgen executable is up-to-date when we run tests<commit_after>// We add this `extern crate` here to ensure that bindgen is up-to-date and
// rebuilt, even though we aren't using any of its types or functions here, only
// indirectly calling the executable.
#[allow(dead_code)]
extern crate bindgen;

use std::env;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process;

fn spawn_run_bindgen<P, Q, R>(run_bindgen: P, bindgen: Q, header: R) -> process::Child
    where P: AsRef<Path>,
          Q: AsRef<Path>,
          R: AsRef<Path>
{
    let run_bindgen = run_bindgen.as_ref();
    let bindgen = bindgen.as_ref();
    let header = header.as_ref();

    // Convert from "tests/headers/foo.hpp" to "tests/expectations/foo.rs" by
    // saving the filename, popping off "headers/foo.hpp", pushing
    // "expectations", pushing the saved filename, and finally modifying the
    // extension.

    let mut expected = PathBuf::from(header);
    let file_name = expected.file_name()
        .expect("Should have filename")
        .to_os_string();
    expected.pop();
    expected.pop();
    expected.push("expectations");
    expected.push(file_name);
    expected.set_extension("rs");

    let mut cmd = process::Command::new(run_bindgen);
    cmd.stdout(process::Stdio::piped())
        .stderr(process::Stdio::piped())
        .arg(bindgen)
        .arg(header)
        .arg(expected);

    if cfg!(feature = "llvm_stable") {
        cmd.arg("--feature")
            .arg("llvm_stable");
    }

    cmd.spawn()
        .expect("Should be able to spawn run-bindgen.py child process")
}

#[test]
fn run_bindgen_tests() {
    let crate_root = env::var("CARGO_MANIFEST_DIR")
        .expect("should have CARGO_MANIFEST_DIR environment variable");

    let mut run_bindgen = PathBuf::from(&crate_root);
    run_bindgen.push("tests");
    run_bindgen.push("tools");
    run_bindgen.push("run-bindgen.py");

    let mut bindgen = PathBuf::from(&crate_root);
    bindgen.push("target");
    bindgen.push("debug");
    bindgen.push("bindgen");
    if !bindgen.is_file() {
        panic!("{} is not a file! Build bindgen before running tests.",
               bindgen.display());
    }

    let mut headers_dir = PathBuf::from(&crate_root);
    headers_dir.push("tests");
    headers_dir.push("headers");

    let entries = fs::read_dir(&headers_dir)
        .expect("Should read directory")
        .map(|result| result.expect("Should read directory entry"));

    let tests = entries.filter(|entry| {
        match entry.path().extension().map(|s| s.to_str()) {
            Some(Some("h")) |
            Some(Some("hpp")) => true,
            _ => false,
        }
    });

    // First spawn all child processes and collect them, then wait on each
    // one. This runs the tests in parallel rather than serially.

    let children: Vec<_> = tests.map(|entry| {
            let child = spawn_run_bindgen(run_bindgen.clone(), bindgen.clone(), entry.path());
            (entry.path(), child)
        })
        .collect();

    let failures: Vec<_> = children.into_iter()
        .filter_map(|(path, mut child)| {
            let passed = child.wait()
                .expect("Should wait on child process")
                .success();

            if passed { None } else { Some((path, child)) }
        })
        .collect();

    let num_failures = failures.len();

    for (path, child) in failures {
        println!("FAIL: {}", path.display());

        let mut buf = String::new();

        child.stdout
            .expect("should have stdout piped")
            .read_to_string(&mut buf)
            .expect("should read child's stdout");
        for line in buf.lines() {
            println!("child stdout> {}", line);
        }

        child.stderr
            .expect("should have stderr piped")
            .read_to_string(&mut buf)
            .expect("should read child's stderr");
        for line in buf.lines() {
            println!("child stderr> {}", line);
        }
    }

    if num_failures > 0 {
        panic!("{} test failures!", num_failures);
    }
}
<|endoftext|>


<commit_before>//! Licensed under the Apache License, Version 2.0
//! http://www.apache.org/licenses/LICENSE-2.0 or the MIT license
//! http://opensource.org/licenses/MIT, at your
//! option. This file may not be copied, modified, or distributed
//! except according to those terms.

#![feature(phase)]

#[phase(plugin, link)] extern crate itertools;

extern crate test;

use std::iter::order;
use itertools::Itertools;
use itertools::Interleave;
use itertools::BoxIter;

use itertools::ItertoolsClonable;

use itertools as it;

#[test]
fn product2() {
    let s = "αβ";

    let mut prod = iproduct!(s.chars(), range(0, 2i));
    assert!(prod.next() == Some(('α', 0)));
    assert!(prod.next() == Some(('α', 1)));
    assert!(prod.next() == Some(('β', 0)));
    assert!(prod.next() == Some(('β', 1)));
    assert!(prod.next() == None);
}

#[test]
fn product3() {
    let mut prod = iproduct!(range(0, 3i), range(0, 2i), range(0, 2i));
    assert_eq!(prod.size_hint(), (12, Some(12)));
    let v = prod.collect::<Vec<_>>();
    for i in range(0,3i) {
        for j in range(0, 2i) {
            for k in range(0, 2i) {
                assert!((i, j, k) == v[(i * 2 * 2 + j * 2 + k) as uint]);
            }
        }
    }
    for (a, b, c, d) in iproduct!(range(0, 3i), range(0, 2i), range(0, 2i), range(0, 3i)) {
        /* test compiles */
    }
}

#[test]
fn izip3() {
    let mut zip = izip!(range(0, 3u), range(0, 2i), range(0, 2i8));
    for i in range(0, 2i) {
        assert!((i as uint, i, i as i8) == zip.next().unwrap());
    }
    assert!(zip.next().is_none());

    let xs: [int, ..0] = [];
    let mut zip = izip!(range(0, 3u), range(0, 2i), range(0, 2i8), xs.iter());
    assert!(zip.next().is_none());

    for (a, b, c, d) in izip!(range(0, 3i), range(0, 2i), range(0, 2i), range(0, 3i)) {
        /* test compiles */
    }
}

#[test]
fn fn_map() {
    let xs = [0, 1, 2i];
    fn mapper(x: &int) -> String { x.to_string() }
    let it = xs.iter().fn_map(mapper);
    let jt = it.clone();
    assert!(it.zip(jt).all(|(x,y)| x == y));
}

#[test]
fn write_to() {
    let xs = [7i, 9, 8];
    let mut ys = [0i, ..5];
    let cnt = it::write(ys.iter_mut(), xs.iter().map(|x| *x));
    assert!(cnt == xs.len());
    assert!(ys == &[7i, 9, 8, 0, 0]);

    let cnt = it::write(ys.iter_mut(), range(0,10i));
    assert!(cnt == ys.len());
    assert!(ys == &[0, 1, 2, 3, 4]);
}

#[test]
fn interleave() {
    let xs: [u8, ..0]  = [];
    let ys = [7u8, 9, 8, 10];
    let zs = [2u8, 77];
    let it = Interleave::new(xs.iter(), ys.iter());
    assert!(order::eq(it, ys.iter()));

    let rs = [7u8, 2, 9, 77, 8, 10];
    let it = Interleave::new(ys.iter(), zs.iter());
    assert!(order::eq(it, rs.iter()));
}

#[test]
fn times() {
    assert!(it::times(0).count() == 0);
    assert!(it::times(5).count() == 5);
}

#[test]
fn drain() {
    let xs = [1i,2,3];
    let mut sum = 0;
    xs.iter().map(|elt| sum += *elt).drain();
    assert!(sum == 6);
}

#[test]
fn apply() {
    let xs = [1i, 2, 3];
    let mut sum = 0;
    xs.iter().apply(|elt| sum += *elt);
    assert!(sum == 6);
}

#[test]
fn dropn() {
    let xs = [1i, 2, 3];
    let mut it = xs.iter();
    assert!(it.dropn(2) == 2);
    assert!(it.next().is_some());
    assert!(it.next().is_none());
    let mut it = xs.iter();
    assert!(it.dropn(5) == 3);
    assert!(it.next().is_none());
}

#[test]
fn intersperse() {
    let xs = ["a", "", "b", "c"];
    let v: Vec<&str> = xs.iter().map(|x| x.clone()).intersperse(", ").collect();
    let text = v.concat();
    assert_eq!(text, "a, , b, c".to_string());

    let ys = [0, 1, 2, 3i];
    let mut it = ys.slice_to(0).iter().map(|x| *x).intersperse(1i);
    assert!(it.next() == None);
}

#[test]
fn clones() {
    let xs = ["a", "", "b", "c"];
    let v: Vec<&str> = xs.iter().clones().collect();
    let text = v.concat();
    assert_eq!(text, "abc".to_string());
}
<commit_msg>Remove reference to BoxIter<commit_after>//! Licensed under the Apache License, Version 2.0
//! http://www.apache.org/licenses/LICENSE-2.0 or the MIT license
//! http://opensource.org/licenses/MIT, at your
//! option. This file may not be copied, modified, or distributed
//! except according to those terms.

#![feature(phase)]

#[phase(plugin, link)] extern crate itertools;

extern crate test;

use std::iter::order;
use itertools::Itertools;
use itertools::Interleave;

use itertools::ItertoolsClonable;

use itertools as it;

#[test]
fn product2() {
    let s = "αβ";

    let mut prod = iproduct!(s.chars(), range(0, 2i));
    assert!(prod.next() == Some(('α', 0)));
    assert!(prod.next() == Some(('α', 1)));
    assert!(prod.next() == Some(('β', 0)));
    assert!(prod.next() == Some(('β', 1)));
    assert!(prod.next() == None);
}

#[test]
fn product3() {
    let mut prod = iproduct!(range(0, 3i), range(0, 2i), range(0, 2i));
    assert_eq!(prod.size_hint(), (12, Some(12)));
    let v = prod.collect::<Vec<_>>();
    for i in range(0,3i) {
        for j in range(0, 2i) {
            for k in range(0, 2i) {
                assert!((i, j, k) == v[(i * 2 * 2 + j * 2 + k) as uint]);
            }
        }
    }
    for (a, b, c, d) in iproduct!(range(0, 3i), range(0, 2i), range(0, 2i), range(0, 3i)) {
        /* test compiles */
    }
}

#[test]
fn izip3() {
    let mut zip = izip!(range(0, 3u), range(0, 2i), range(0, 2i8));
    for i in range(0, 2i) {
        assert!((i as uint, i, i as i8) == zip.next().unwrap());
    }
    assert!(zip.next().is_none());

    let xs: [int, ..0] = [];
    let mut zip = izip!(range(0, 3u), range(0, 2i), range(0, 2i8), xs.iter());
    assert!(zip.next().is_none());

    for (a, b, c, d) in izip!(range(0, 3i), range(0, 2i), range(0, 2i), range(0, 3i)) {
        /* test compiles */
    }
}

#[test]
fn fn_map() {
    let xs = [0, 1, 2i];
    fn mapper(x: &int) -> String { x.to_string() }
    let it = xs.iter().fn_map(mapper);
    let jt = it.clone();
    assert!(it.zip(jt).all(|(x,y)| x == y));
}

#[test]
fn write_to() {
    let xs = [7i, 9, 8];
    let mut ys = [0i, ..5];
    let cnt = it::write(ys.iter_mut(), xs.iter().map(|x| *x));
    assert!(cnt == xs.len());
    assert!(ys == &[7i, 9, 8, 0, 0]);

    let cnt = it::write(ys.iter_mut(), range(0,10i));
    assert!(cnt == ys.len());
    assert!(ys == &[0, 1, 2, 3, 4]);
}

#[test]
fn interleave() {
    let xs: [u8, ..0]  = [];
    let ys = [7u8, 9, 8, 10];
    let zs = [2u8, 77];
    let it = Interleave::new(xs.iter(), ys.iter());
    assert!(order::eq(it, ys.iter()));

    let rs = [7u8, 2, 9, 77, 8, 10];
    let it = Interleave::new(ys.iter(), zs.iter());
    assert!(order::eq(it, rs.iter()));
}

#[test]
fn times() {
    assert!(it::times(0).count() == 0);
    assert!(it::times(5).count() == 5);
}

#[test]
fn drain() {
    let xs = [1i,2,3];
    let mut sum = 0;
    xs.iter().map(|elt| sum += *elt).drain();
    assert!(sum == 6);
}

#[test]
fn apply() {
    let xs = [1i, 2, 3];
    let mut sum = 0;
    xs.iter().apply(|elt| sum += *elt);
    assert!(sum == 6);
}

#[test]
fn dropn() {
    let xs = [1i, 2, 3];
    let mut it = xs.iter();
    assert!(it.dropn(2) == 2);
    assert!(it.next().is_some());
    assert!(it.next().is_none());
    let mut it = xs.iter();
    assert!(it.dropn(5) == 3);
    assert!(it.next().is_none());
}

#[test]
fn intersperse() {
    let xs = ["a", "", "b", "c"];
    let v: Vec<&str> = xs.iter().map(|x| x.clone()).intersperse(", ").collect();
    let text = v.concat();
    assert_eq!(text, "a, , b, c".to_string());

    let ys = [0, 1, 2, 3i];
    let mut it = ys.slice_to(0).iter().map(|x| *x).intersperse(1i);
    assert!(it.next() == None);
}

#[test]
fn clones() {
    let xs = ["a", "", "b", "c"];
    let v: Vec<&str> = xs.iter().clones().collect();
    let text = v.concat();
    assert_eq!(text, "abc".to_string());
}
<|endoftext|>


<commit_before>extern crate specs;

use specs::{Entity};
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};

#[derive(Clone, Debug)]
struct CompInt(i8);
impl specs::Component for CompInt {
    type Storage = specs::VecStorage<CompInt>;
}
#[derive(Clone, Debug)]
struct CompBool(bool);
impl specs::Component for CompBool {
    type Storage = specs::HashMapStorage<CompBool>;
}

fn create_world() -> specs::Planner<()> {
    let mut w = specs::World::new();
    w.register::<CompInt>();
    w.register::<CompBool>();
    specs::Planner::new(w, 4)
}

#[test]
fn wait() {
    let mut planner = create_world();

    for _ in 0..100 {
        let found_ent_0 = Arc::new(AtomicBool::new(false));
        let found_ent_1 = Arc::new(AtomicBool::new(false));

        planner.mut_world().create_now()
            .with(CompInt(7))
            .with(CompBool(false))
            .build();

        let marker = found_ent_0.clone();
        planner.run1w1r(move |b: &mut CompBool, r: &CompInt| {
            b.0 = r.0 == 7;
            marker.store(true, Ordering::SeqCst);
        });
        let marker = found_ent_1.clone();
        planner.run0w2r(move |r: &CompInt, b: &CompBool| {
            assert_eq!(r.0, 7);
            assert_eq!(b.0, true);
            marker.store(true, Ordering::SeqCst);
        });
        planner.wait();

        assert_eq!(found_ent_0.load(Ordering::SeqCst), true);
        assert_eq!(found_ent_1.load(Ordering::SeqCst), true);
    }
}

//#[should_panic]
//#[test] //TODO
fn _task_panics() {
    let mut planner = create_world();
    planner.mut_world().create_now()
        .with(CompInt(7))
        .with(CompBool(false))
        .build();

    planner.run_custom(|args| {
        args.fetch(|_| ());
        panic!();
    });
    planner.wait();
}

#[should_panic]
#[test]
fn task_panics_args_captured() {
    let mut planner = create_world();
    planner.mut_world().create_now()
        .with(CompInt(7))
        .with(CompBool(false))
        .build();

    planner.run_custom(|_| {
        panic!();
    });
    planner.wait();
}

#[test]
fn dynamic_create() {
    let mut planner = create_world();

    for _ in 0..1_000 {
        planner.run_custom(|arg| {
            arg.fetch(|_| ());
            arg.create();
        });
        planner.wait();
    }
}

#[test]
fn dynamic_deletion() {
    let mut planner = create_world();

    for _ in 0..1_000 {
        planner.run_custom(|arg| {
            arg.fetch(|_| ());
            let e = arg.create();
            arg.delete(e);
            arg.delete(e); // double free
        });
        planner.wait();
    }
}

#[test]
fn dynamic_create_and_delete() {
    use std::mem::swap;
    let mut planner = create_world();

    let (mut ent0, mut ent1) = (
        Arc::new(Mutex::new(None)),
        Arc::new(Mutex::new(None))
    );

    for i in 0..1_000 {
        let e = ent0.clone();
        planner.run_custom(move |arg| {
            arg.fetch(|_| ());
            let mut e = e.lock().unwrap();
            *e = Some(arg.create());
        });
        if i >= 1 {
            let e = ent1.clone();
            planner.run_custom(move |arg| {
                arg.fetch(|_| ());
                let mut e = e.lock().unwrap();
                arg.delete(e.take().unwrap());
            })
        }
        planner.wait();
        swap(&mut ent1, &mut ent0)
    }
}

#[test]
fn mixed_create_merge() {
    use std::collections::HashSet;
    let mut planner = create_world();
    let mut set = HashSet::new();

    let add = |set: &mut HashSet<Entity>, e: Entity| {
        assert!(!set.contains(&e));
        set.insert(e);
    };

    let insert = |planner: &mut specs::Planner<()>, set: &mut HashSet<Entity>, cnt: usize| {
        // Check to make sure there is no conflict between create_now
        // and create_later
        for _ in 0..10 {
            for _ in 0..cnt {
                let mut w = planner.mut_world();
                add(set, w.create_now().build());
                let e = w.create_now().build();
                w.delete_now(e);
                add(set, w.create_later());
                //  swap order
                add(set, w.create_later());
                add(set, w.create_now().build());
            }
            planner.wait();
        }
    };

    insert(&mut planner, &mut set, 10);
    for e in set.drain() {
        planner.mut_world().delete_later(e);
    }
    insert(&mut planner, &mut set, 20);
    for e in set.drain() {
        planner.mut_world().delete_now(e);
    }
    insert(&mut planner, &mut set, 40);
}

#[test]
fn is_alive() {
    let mut w = specs::World::new();

    let e = w.create_now().build();
    assert!(w.is_alive(e));
    w.delete_now(e);
    assert!(!w.is_alive(e));

    let e2 = w.create_now().build();
    assert!(w.is_alive(e2));
    w.delete_later(e2);
    assert!(w.is_alive(e2));
    w.maintain();
    assert!(!w.is_alive(e2));
}
<commit_msg>Test for stillborn entities<commit_after>extern crate specs;

use specs::{Entity};
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};

#[derive(Clone, Debug)]
struct CompInt(i8);
impl specs::Component for CompInt {
    type Storage = specs::VecStorage<CompInt>;
}
#[derive(Clone, Debug)]
struct CompBool(bool);
impl specs::Component for CompBool {
    type Storage = specs::HashMapStorage<CompBool>;
}

fn create_world() -> specs::Planner<()> {
    let mut w = specs::World::new();
    w.register::<CompInt>();
    w.register::<CompBool>();
    specs::Planner::new(w, 4)
}

#[test]
fn wait() {
    let mut planner = create_world();

    for _ in 0..100 {
        let found_ent_0 = Arc::new(AtomicBool::new(false));
        let found_ent_1 = Arc::new(AtomicBool::new(false));

        planner.mut_world().create_now()
            .with(CompInt(7))
            .with(CompBool(false))
            .build();

        let marker = found_ent_0.clone();
        planner.run1w1r(move |b: &mut CompBool, r: &CompInt| {
            b.0 = r.0 == 7;
            marker.store(true, Ordering::SeqCst);
        });
        let marker = found_ent_1.clone();
        planner.run0w2r(move |r: &CompInt, b: &CompBool| {
            assert_eq!(r.0, 7);
            assert_eq!(b.0, true);
            marker.store(true, Ordering::SeqCst);
        });
        planner.wait();

        assert_eq!(found_ent_0.load(Ordering::SeqCst), true);
        assert_eq!(found_ent_1.load(Ordering::SeqCst), true);
    }
}

//#[should_panic]
//#[test] //TODO
fn _task_panics() {
    let mut planner = create_world();
    planner.mut_world().create_now()
        .with(CompInt(7))
        .with(CompBool(false))
        .build();

    planner.run_custom(|args| {
        args.fetch(|_| ());
        panic!();
    });
    planner.wait();
}

#[should_panic]
#[test]
fn task_panics_args_captured() {
    let mut planner = create_world();
    planner.mut_world().create_now()
        .with(CompInt(7))
        .with(CompBool(false))
        .build();

    planner.run_custom(|_| {
        panic!();
    });
    planner.wait();
}

#[test]
fn dynamic_create() {
    let mut planner = create_world();

    for _ in 0..1_000 {
        planner.run_custom(|arg| {
            arg.fetch(|_| ());
            arg.create();
        });
        planner.wait();
    }
}

#[test]
fn dynamic_deletion() {
    let mut planner = create_world();

    for _ in 0..1_000 {
        planner.run_custom(|arg| {
            arg.fetch(|_| ());
            let e = arg.create();
            arg.delete(e);
            arg.delete(e); // double free
        });
        planner.wait();
    }
}

#[test]
fn dynamic_create_and_delete() {
    use std::mem::swap;
    let mut planner = create_world();

    let (mut ent0, mut ent1) = (
        Arc::new(Mutex::new(None)),
        Arc::new(Mutex::new(None))
    );

    for i in 0..1_000 {
        let e = ent0.clone();
        planner.run_custom(move |arg| {
            arg.fetch(|_| ());
            let mut e = e.lock().unwrap();
            *e = Some(arg.create());
        });
        if i >= 1 {
            let e = ent1.clone();
            planner.run_custom(move |arg| {
                arg.fetch(|_| ());
                let mut e = e.lock().unwrap();
                arg.delete(e.take().unwrap());
            })
        }
        planner.wait();
        swap(&mut ent1, &mut ent0)
    }
}

#[test]
fn mixed_create_merge() {
    use std::collections::HashSet;
    let mut planner = create_world();
    let mut set = HashSet::new();

    let add = |set: &mut HashSet<Entity>, e: Entity| {
        assert!(!set.contains(&e));
        set.insert(e);
    };

    let insert = |planner: &mut specs::Planner<()>, set: &mut HashSet<Entity>, cnt: usize| {
        // Check to make sure there is no conflict between create_now
        // and create_later
        for _ in 0..10 {
            for _ in 0..cnt {
                let mut w = planner.mut_world();
                add(set, w.create_now().build());
                let e = w.create_now().build();
                w.delete_now(e);
                add(set, w.create_later());
                //  swap order
                add(set, w.create_later());
                add(set, w.create_now().build());
            }
            planner.wait();
        }
    };

    insert(&mut planner, &mut set, 10);
    for e in set.drain() {
        planner.mut_world().delete_later(e);
    }
    insert(&mut planner, &mut set, 20);
    for e in set.drain() {
        planner.mut_world().delete_now(e);
    }
    insert(&mut planner, &mut set, 40);
}

#[test]
fn is_alive() {
    let mut w = specs::World::new();

    let e = w.create_now().build();
    assert!(w.is_alive(e));
    w.delete_now(e);
    assert!(!w.is_alive(e));

    let e2 = w.create_now().build();
    assert!(w.is_alive(e2));
    w.delete_later(e2);
    assert!(w.is_alive(e2));
    w.maintain();
    assert!(!w.is_alive(e2));
}

// Checks whether entities are considered dead immediately after creation
#[test]
fn stillborn_entities() {
    struct LCG(u32);
    const RANDMAX: u32 = 32767;
    impl LCG {
        fn new() -> Self { LCG(0xdeadbeef) }
        fn geni(&mut self) -> i8 { ((self.gen() as i32) - 0x7f) as i8 }
        fn gen(&mut self) -> u32 {
            self.0 = self.0.wrapping_mul(214013).wrapping_add(2531011);
            self.0 % RANDMAX
        }
    }

    let mut rng = LCG::new();

    // Construct a bunch of entities
    let mut planner = specs::Planner::<()>::new({
        let mut world = specs::World::new();
        world.register::<CompInt>();

        for _ in 0 .. 100 {
            world.create_now().with(CompInt(rng.geni())).build();
        }

        world
    }, 4);

    for _ in 0 .. 100 {
        let count = (rng.gen() % 25) as usize;
        let mut values = vec![];
        for _ in 0 .. count { values.push(rng.geni()); }

        // Cull the same number of entities we expect to insert
        planner.run_custom(move |arg| {
            use specs::Join;

            let (compint, eids) = arg.fetch(|w| {
                (w.read::<CompInt>(), w.entities())
            });

            let mut lowest = vec![];
            for (&CompInt(k), eid) in (&compint, &eids).iter() {
                if lowest.iter().all(|&(n, _)| n >= k) {
                    lowest.push((k, eid));
                }
            }

            lowest.reverse();
            lowest.truncate(count);
            for (_, eid) in lowest.into_iter() { arg.delete(eid); }
        });

        planner.run_custom(move |arg| {
            let mut compint = arg.fetch(|w| w.write::<CompInt>());

            for &i in values.iter() {
                use specs::InsertResult::EntityIsDead;

                let result = compint.insert(arg.create(), CompInt(i));
                if let EntityIsDead(_) = result {
                    panic!("Couldn't insert {} into a stillborn entity", i);
                }
            }
        });

        planner.wait();
    }
}
<|endoftext|>


<commit_before>#[macro_use]
extern crate abomonation;

use abomonation::*;

#[test] fn test_array() { _test_pass(vec![[0, 1, 2]; 1024]); }
#[test] fn test_opt_vec() { _test_pass(vec![Some(vec![0,1,2]), None]); }
// #[test] fn test_result_vec_str() { _test_pass(vec![Ok(vec![0, 1, 2]), Err("grawwwwrr!"), Ok(vec![])]); }
// #[test] fn test_str_ref() { _test_pass(vec!["hi there"; 245]); }
#[test] fn test_alignment() { _test_pass(vec![(format!("x"), vec![1,2,3]); 1024]); }
#[test] fn test_option_box_u64() { _test_pass(vec![Some(Box::new(0u64))]); }
#[test] fn test_option_vec() { _test_pass(vec![Some(vec![0, 1, 2])]); }
#[test] fn test_u32x4_pass() { _test_pass(vec![((1,2,3),vec![(0u32, 0u32, 0u32, 0u32); 1024])]); }
#[test] fn test_u64_pass() { _test_pass(vec![0u64; 1024]); }
#[test] fn test_string_pass() { _test_pass(vec![format!("grawwwwrr!"); 1024]); }
#[test] fn test_vec_u_s_pass() { _test_pass(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }

#[test] fn test_u64_fail() { _test_fail(vec![0u64; 1024]); }
#[test] fn test_string_fail() { _test_fail(vec![format!("grawwwwrr!"); 1024]); }
#[test] fn test_vec_u_s_fail() { _test_fail(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }

#[test] fn test_array_size() { _test_size(vec![[0, 1, 2]; 1024]); }
#[test] fn test_opt_vec_size() { _test_size(vec![Some(vec![0,1,2]), None]); }
#[test] fn test_alignment_size() { _test_size(vec![(format!("x"), vec![1,2,3]); 1024]); }
#[test] fn test_option_box_u64_size() { _test_size(vec![Some(Box::new(0u64))]); }
#[test] fn test_option_vec_size() { _test_size(vec![Some(vec![0, 1, 2])]); }
#[test] fn test_u32x4_size() { _test_size(vec![((1,2,3),vec![(0u32, 0u32, 0u32, 0u32); 1024])]); }
#[test] fn test_u64_size() { _test_size(vec![0u64; 1024]); }
#[test] fn test_string_size() { _test_size(vec![format!("grawwwwrr!"); 1024]); }
#[test] fn test_vec_u_s_size() { _test_size(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }

#[test]
fn test_phantom_data_for_non_abomonatable_type() {
    use std::marker::PhantomData;
    struct NotAbomonatable;
    _test_pass(PhantomData::<NotAbomonatable>::default());
}

fn _test_pass<T: Abomonation+Eq>(record: T) {
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    {
        let (result, rest) = unsafe { decode::<T>(&mut bytes[..]) }.unwrap();
        assert!(&record == result);
        assert!(rest.len() == 0);
    }
}

fn _test_fail<T: Abomonation>(record: T) {
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    bytes.pop();
    assert!(unsafe { decode::<T>(&mut bytes[..]) }.is_none());
}

fn _test_size<T: Abomonation>(record: T) {
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    assert_eq!(bytes.len(), measure(&record));
}

#[derive(Eq, PartialEq)]
struct MyStruct {
    a: String,
    b: u64,
    c: Vec<u8>,
}

unsafe_abomonate!(MyStruct : a, b, c);

#[test]
fn test_macro() {
    // create some test data out of abomonation-approved types
    let record = MyStruct{ a: "test".to_owned(), b: 0, c: vec![0, 1, 2] };

    // encode vector into a Vec<u8>
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }

    // decode a &Vec<(u64, String)> from binary data
    if let Some((result, rest)) = unsafe { decode::<MyStruct>(&mut bytes) } {
        assert!(result == &record);
        assert!(rest.len() == 0);
    }
}

#[test]
fn test_macro_size() {
    // create some test data out of abomonation-approved types
    let record = MyStruct{ a: "test".to_owned(), b: 0, c: vec![0, 1, 2] };

    // encode vector into a Vec<u8>
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    assert_eq!(bytes.len(), measure(&record));
}

// #[derive(Eq, PartialEq)]
// struct MyGenericStruct<T: Ord> {
//     item: T
// }

// unsafe_abomonate!(T:Ord+Abomonation; MyStruct<T> : item);

// #[test]
// fn test_generic_macro() {
//     // create some test data out of abomonation-approved types
//     let record = MyGenericStruct{ item: "hello".to_owned() };

//     // encode vector into a Vec<u8>
//     let mut bytes = Vec::new();
//     unsafe { encode(&record, &mut bytes); }

//     // decode a &Vec<(u64, String)> from binary data
//     if let Some((result, rest)) = unsafe { decode::<MyGenericStruct>(&mut bytes) } {
//         assert!(result == &record);
//         assert!(rest.len() == 0);
//     }
// }

#[test]
fn test_multiple_encode_decode() {
    let mut bytes = Vec::new();
    unsafe { encode(&0u32, &mut bytes).unwrap(); }
    unsafe { encode(&7u64, &mut bytes).unwrap(); }
    unsafe { encode(&vec![1,2,3], &mut bytes).unwrap(); }
    unsafe { encode(&"grawwwwrr".to_owned(), &mut bytes).unwrap(); }

    let (t, r) = unsafe { decode::<u32>(&mut bytes) }.unwrap(); assert!(*t == 0);
    let (t, r) = unsafe { decode::<u64>(r) }.unwrap(); assert!(*t == 7);
    let (t, r) = unsafe { decode::<Vec<i32>>(r) }.unwrap(); assert!(*t == vec![1,2,3]);
    let (t, _r) = unsafe { decode::<String>(r) }.unwrap(); assert!(*t == "grawwwwrr".to_owned());
}

#[test]
fn test_net_types() {

    use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr};

    let socket_addr4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(128, 0, 0, 1)), 1234);
    let socket_addr6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 1234);

    let mut bytes = Vec::new();

    unsafe { encode(&socket_addr4, &mut bytes).unwrap(); }
    unsafe { encode(&socket_addr6, &mut bytes).unwrap(); }

    let (t, r) = unsafe { decode::<SocketAddr>(&mut bytes) }.unwrap(); assert!(*t == socket_addr4);
    let (t, _r) = unsafe { decode::<SocketAddr>(r) }.unwrap(); assert!(*t == socket_addr6);
}
<commit_msg>update tests<commit_after>#[macro_use]
extern crate abomonation;

use abomonation::*;

#[test] fn test_array() { _test_pass(vec![[0, 1, 2]; 1024]); }
#[test] fn test_opt_vec() { _test_pass(vec![Some(vec![0,1,2]), None]); }
#[test] fn test_alignment() { _test_pass(vec![(format!("x"), vec![1,2,3]); 1024]); }
#[test] fn test_alignment_128() { _test_pass(vec![(format!("x"), vec![1u128,2,3]); 1024]); }
#[test] fn test_option_box_u64() { _test_pass(vec![Some(Box::new(0u64))]); }
#[test] fn test_option_vec() { _test_pass(vec![Some(vec![0, 1, 2])]); }
#[test] fn test_u32x4_pass() { _test_pass(vec![((1,2,3),vec![(0u32, 0u32, 0u32, 0u32); 1024])]); }
#[test] fn test_u64_pass() { _test_pass(vec![0u64; 1024]); }
#[test] fn test_string_pass() { _test_pass(vec![format!("grawwwwrr!"); 1024]); }
#[test] fn test_vec_u_s_pass() { _test_pass(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }

#[test] fn test_u64_fail() { _test_fail(vec![0u64; 1024]); }
#[test] fn test_string_fail() { _test_fail(vec![format!("grawwwwrr!"); 1024]); }
#[test] fn test_vec_u_s_fail() { _test_fail(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }

#[test] fn test_array_size() { _test_size(vec![[0, 1, 2]; 1024]); }
#[test] fn test_opt_vec_size() { _test_size(vec![Some(vec![0,1,2]), None]); }
#[test] fn test_alignment_size() { _test_size(vec![(format!("x"), vec![1,2,3]); 1024]); }
#[test] fn test_option_box_u64_size() { _test_size(vec![Some(Box::new(0u64))]); }
#[test] fn test_option_vec_size() { _test_size(vec![Some(vec![0, 1, 2])]); }
#[test] fn test_u32x4_size() { _test_size(vec![((1,2,3),vec![(0u32, 0u32, 0u32, 0u32); 1024])]); }
#[test] fn test_u64_size() { _test_size(vec![0u64; 1024]); }
#[test] fn test_string_size() { _test_size(vec![format!("grawwwwrr!"); 1024]); }
#[test] fn test_vec_u_s_size() { _test_size(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }

#[test]
fn test_phantom_data_for_non_abomonatable_type() {
    use std::marker::PhantomData;
    struct NotAbomonatable;
    _test_pass(PhantomData::<NotAbomonatable>::default());
}

fn _test_pass<T: Abomonation+Eq>(record: T) {
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    {
        let (result, rest) = unsafe { decode::<T>(&mut bytes[..]) }.unwrap();
        assert!(&record == result);
        assert!(rest.len() == 0);
    }
}

fn _test_fail<T: Abomonation>(record: T) {
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    bytes.pop();
    assert!(unsafe { decode::<T>(&mut bytes[..]) }.is_none());
}

fn _test_size<T: Abomonation>(record: T) {
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    assert_eq!(bytes.len(), measure(&record));
}

#[derive(Eq, PartialEq)]
struct MyStruct {
    a: String,
    b: u64,
    c: Vec<u8>,
}

unsafe_abomonate!(MyStruct : a, b, c);

#[test]
fn test_macro() {
    // create some test data out of abomonation-approved types
    let record = MyStruct{ a: "test".to_owned(), b: 0, c: vec![0, 1, 2] };

    // encode vector into a Vec<u8>
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }

    // decode a &Vec<(u64, String)> from binary data
    if let Some((result, rest)) = unsafe { decode::<MyStruct>(&mut bytes) } {
        assert!(result == &record);
        assert!(rest.len() == 0);
    }
}

#[test]
fn test_macro_size() {
    // create some test data out of abomonation-approved types
    let record = MyStruct{ a: "test".to_owned(), b: 0, c: vec![0, 1, 2] };

    // encode vector into a Vec<u8>
    let mut bytes = Vec::new();
    unsafe { encode(&record, &mut bytes).unwrap(); }
    assert_eq!(bytes.len(), measure(&record));
}

#[test]
fn test_multiple_encode_decode() {
    let mut bytes = Vec::new();
    unsafe { encode(&0u32, &mut bytes).unwrap(); }
    unsafe { encode(&7u64, &mut bytes).unwrap(); }
    unsafe { encode(&vec![1,2,3], &mut bytes).unwrap(); }
    unsafe { encode(&"grawwwwrr".to_owned(), &mut bytes).unwrap(); }

    let (t, r) = unsafe { decode::<u32>(&mut bytes) }.unwrap(); assert!(*t == 0);
    let (t, r) = unsafe { decode::<u64>(r) }.unwrap(); assert!(*t == 7);
    let (t, r) = unsafe { decode::<Vec<i32>>(r) }.unwrap(); assert!(*t == vec![1,2,3]);
    let (t, _r) = unsafe { decode::<String>(r) }.unwrap(); assert!(*t == "grawwwwrr".to_owned());
}

#[test]
fn test_net_types() {

    use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr};

    let socket_addr4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(128, 0, 0, 1)), 1234);
    let socket_addr6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 1234);

    let mut bytes = Vec::new();

    unsafe { encode(&socket_addr4, &mut bytes).unwrap(); }
    unsafe { encode(&socket_addr6, &mut bytes).unwrap(); }

    let (t, r) = unsafe { decode::<SocketAddr>(&mut bytes) }.unwrap(); assert!(*t == socket_addr4);
    let (t, _r) = unsafe { decode::<SocketAddr>(r) }.unwrap(); assert!(*t == socket_addr6);
}
<|endoftext|>


<commit_before>use std::result:: { Result };
use lcov_parser:: { LCOVParser, LCOVRecord, RecordParseError };
use report:: { Report, FileResult };
use processor:: { FileProcessor, ToFileResult };

pub struct ReportParser {
    parser: LCOVParser,
    files: Vec<FileResult>,
    processor: Option<FileProcessor>
}

impl ReportParser {
    pub fn new(report: &str) -> Self {
        let parser = LCOVParser::new(report);
        ReportParser { parser: parser, files: vec!(), processor: None }
    }
    pub fn parse(&mut self) -> Result<Report, RecordParseError> {
        let records = try!(self.parser.parse());

        for record in records.iter() {
            self.on_parsed(record);
        }

        let report = Report::new(self.files.clone());
        Ok(report)
    }

    pub fn on_parsed(&mut self, record: &LCOVRecord) {
        match record {
            &LCOVRecord::SourceFile(ref name) => self.on_source_file(name.clone()),
            &LCOVRecord::Data(_, excution_count, _) => self.on_data(excution_count),
            &LCOVRecord::EndOfRecord => self.on_end_of_record(),
            _ => {}
        }
    }

    pub fn on_source_file(&mut self, name: String) {
        println!("{}", name);
        self.processor = Some(FileProcessor::new(name));
    }

    pub fn on_data(&mut self, excution_count: u32) {
        println!("{}", excution_count);
        match self.processor.as_mut() {
            Some(processor) => processor.proceed(excution_count),
            None => {}
        }
    }

    pub fn on_end_of_record(&mut self) {
        println!("{}", "excution_count");
        match self.processor.as_mut() {
            Some(processor) => self.files.push(processor.to_file_result()),
            None => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs:: { File };
    use parser:: { ReportParser };
    use coverage:: { Coverage };
    use std::io:: { Read };

    #[test]
    fn test_parse_report() {
        let mut buffer = String::new();
        let mut f = File::open("tests/fixtures/report.lcov").unwrap();
        let _ = f.read_to_string(&mut buffer);

        let mut parser = ReportParser::new(buffer.as_str());
        let report = parser.parse().unwrap();
        let files = report.files();

        assert_eq!(files.len(), 2);

        let first = files.get(0).unwrap();
        assert_eq!(first.coverage(), &Coverage::new(0.75));
    }
}
<commit_msg>Remove debug print<commit_after>use std::result:: { Result };
use lcov_parser:: { LCOVParser, LCOVRecord, RecordParseError };
use report:: { Report, FileResult };
use processor:: { FileProcessor, ToFileResult };

pub struct ReportParser {
    parser: LCOVParser,
    files: Vec<FileResult>,
    processor: Option<FileProcessor>
}

impl ReportParser {
    pub fn new(report: &str) -> Self {
        let parser = LCOVParser::new(report);
        ReportParser { parser: parser, files: vec!(), processor: None }
    }
    pub fn parse(&mut self) -> Result<Report, RecordParseError> {
        let records = try!(self.parser.parse());

        for record in records.iter() {
            self.on_parsed(record);
        }

        let report = Report::new(self.files.clone());
        Ok(report)
    }

    pub fn on_parsed(&mut self, record: &LCOVRecord) {
        match record {
            &LCOVRecord::SourceFile(ref name) => self.on_source_file(name.clone()),
            &LCOVRecord::Data(_, excution_count, _) => self.on_data(excution_count),
            &LCOVRecord::EndOfRecord => self.on_end_of_record(),
            _ => {}
        }
    }

    pub fn on_source_file(&mut self, name: String) {
        self.processor = Some(FileProcessor::new(name));
    }

    pub fn on_data(&mut self, excution_count: u32) {
        match self.processor.as_mut() {
            Some(processor) => processor.proceed(excution_count),
            None => {}
        }
    }

    pub fn on_end_of_record(&mut self) {
        match self.processor.as_mut() {
            Some(processor) => self.files.push(processor.to_file_result()),
            None => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs:: { File };
    use parser:: { ReportParser };
    use coverage:: { Coverage };
    use std::io:: { Read };

    #[test]
    fn test_parse_report() {
        let mut buffer = String::new();
        let mut f = File::open("tests/fixtures/report.lcov").unwrap();
        let _ = f.read_to_string(&mut buffer);

        let mut parser = ReportParser::new(buffer.as_str());
        let report = parser.parse().unwrap();
        let files = report.files();

        assert_eq!(files.len(), 2);

        let first = files.get(0).unwrap();
        assert_eq!(first.coverage(), &Coverage::new(0.75));
    }
}
<|endoftext|>


<commit_before>use lexicon::Token;
use lexicon::Token::*;
use tokenizer::Tokenizer;
use std::iter::Peekable;
use grammar::*;
use grammar::Statement::*;
use grammar::Expression::*;
use grammar::ClassMember::*;
use grammar::OperatorType::*;

/// If the next token matches `$p`, consume that token and return
/// true, else do nothing and return false
macro_rules! allow {
    ($parser:ident, $p:pat) => {
        match $parser.lookahead() {
            Some(&$p) => {
                $parser.consume();
                true
            },
            _ => false
        }
    };
    {$parser:ident $( $p:pat => $then:expr ),* } => ({
        match $parser.lookahead() {
            $(
                Some(&$p) => {
                    $parser.consume();
                    $then;
                }
            )*
            _ => {}
        }
    });
}

/// Expects next token to match `$p`, otherwise panics.
macro_rules! expect {
    ($parser:ident, $p:pat => $value:ident) => (
        match $parser.consume() {
            $p    => $value,
            token => unexpected_token!($parser, token),
        }
    );
    ($parser:ident, $p:pat) => (
        match $parser.consume() {
            $p    => {},
            token => unexpected_token!($parser, token),
        }
    )
}

macro_rules! unexpected_token {
    ($parser:ident) => ({
        unexpected_token!($parser, $parser.consume());
    });
    ($parser:ident, $token:expr) => {
        panic!("Unexpected token {:?}", $token);
    }
}

/// Evaluates the `$eval` expression, then expects a semicolon or
/// end of program. If neither is found, but a LineTermination
/// occured on previous token, parsing will continue as if a
/// semicolon was present. In other cases cause a panic.
macro_rules! statement {
    ($parser:ident, $eval:expr) => ({
        let value = $eval;

        let is_end = match $parser.lookahead() {
            Some(&Semicolon) => {
                $parser.consume();
                true
            },
            Some(&ParenOff)  => true,
            Some(&BlockOff)  => true,
            None             => true,
            _                => false
        };

        if !is_end && !$parser.allow_asi {
            unexpected_token!($parser);
        };

        value
    })
}

/// Read a list of items with predefined `$start`, `$end` and
/// `$separator` tokens and an `$item` expression that is then
/// pushed onto a vector.
macro_rules! list {
    ($parser:ident, $item:expr, $end:pat) => ({
        let mut list = Vec::new();
        loop {
            if allow!($parser, $end) {
                break;
            }
            list.push($item);

            match $parser.consume() {
                Comma => allow!{ $parser $end => break },
                $end  => break,
                _     => {},
            }
        }

        list
    });
    ($parser:ident, $item:expr, $start:pat, $end:pat) => ({
        expect!($parser, $start);
        list!($parser, $item, $end)
    });
    ($parser:ident ( $item:expr )) => {
        list!($parser, $item, ParenOn, ParenOff)
    };
    ($parser:ident [ $item:expr ]) => {
        list!($parser, $item, BracketOn, BracketOff)
    };
    ($parser:ident { $item:expr }) => {
        list!($parser, $item, BlockOn, BlockOff)
    };
}

macro_rules! surround {
    ($parser:ident ( $eval:expr )) => ({
        expect!($parser, ParenOn);
        let value = $eval;
        expect!($parser, ParenOff);
        value
    });
    ($parser:ident [ $eval:expr ]) => ({
        expect!($parser, BracketOn);
        let value = $eval;
        expect!($parser, BracketOff);
        value
    });
}

pub struct Parser<'a> {
    tokenizer: Peekable<Tokenizer<'a>>,
    allow_asi: bool,
}

impl<'a> Parser<'a> {
    pub fn new(source: &'a String) -> Self {
        Parser {
            tokenizer: Tokenizer::new(source).peekable(),
            allow_asi: false,
        }
    }

    #[inline(always)]
    fn handle_line_termination(&mut self) {
        while let Some(&LineTermination) = self.tokenizer.peek() {
            self.tokenizer.next();
            self.allow_asi = true;
        }
    }

    #[inline(always)]
    fn consume(&mut self) -> Token {
        self.handle_line_termination();
        let token = self.tokenizer.next().expect("Unexpected end of program");

        // println!("Consume {:?}", token);

        self.allow_asi = false;
        token
    }

    #[inline(always)]
    fn lookahead(&mut self) -> Option<&Token> {
        self.handle_line_termination();
        self.tokenizer.peek()
    }

    fn array_expression(&mut self) -> Expression {
        ArrayExpression(list!(self, self.expression(0), BracketOff))
    }

    #[inline(always)]
    fn object_member(&mut self) -> ObjectMember {
        match self.consume() {
            Identifier(key) | Literal(LiteralString(key)) => {
                if allow!(self, Colon) {
                    ObjectMember::Literal {
                        key: key,
                        value: self.expression(0),
                    }
                } else {
                    ObjectMember::Shorthand {
                        key: key,
                    }
                }
            },
            BracketOn => {
                let key = self.expression(0);
                expect!(self, BracketOff);
                expect!(self, Colon);
                ObjectMember::Computed {
                    key: key,
                    value: self.expression(0),
                }
            },
            token => {
                panic!("Expected object key, got {:?}", token)
            }
        }
    }

    fn object_expression(&mut self) -> Expression {
        ObjectExpression(list!(self, self.object_member(), BlockOff))
    }

    fn block_or_statement(&mut self) -> Statement {
        if let Some(&BlockOn) = self.lookahead() {
            BlockStatement {
                body: self.block_body()
            }
        } else {
            self.expression_statement()
        }
    }

    fn block_body(&mut self) -> Vec<Statement> {
        expect!(self, BlockOn);

        let mut body = Vec::new();
        loop {
            allow!{ self BlockOff => break };

            body.push(
                self.statement().expect("Unexpected end of statements block")
            )
        }

        body
    }

    fn arrow_function_expression(&mut self, p: Option<Expression>) -> Expression {
        let params: Vec<Parameter> = match p {
            None => Vec::new(),
            Some(IdentifierExpression(name)) => {
                vec![Parameter { name: name }]
            },
            Some(SequenceExpression(mut list)) => {
                list.drain(..).map(|expression| {
                    match expression {
                        IdentifierExpression(name) => Parameter { name: name },
                        _ => panic!("Cannot cast {:?} to a parameter", expression),
                    }
                }).collect()
            },
            _ =>
                panic!("Cannot cast {:?} to parameters", p),
        };

        let body = if let Some(&BlockOn) = self.lookahead() {
            BlockStatement {
                body: self.block_body()
            }
        } else {
            ExpressionStatement(self.expression(0))
        };

        ArrowFunctionExpression {
            params: params,
            body: Box::new(body)
        }
    }

    #[inline(always)]
    fn prefix_expression(&mut self, operator: OperatorType) -> Expression {
        let bp = operator.binding_power(true);

        if !operator.prefix() {
            panic!("Unexpected operator {:?}", operator);
        }

        PrefixExpression {
            operator: operator,
            operand: Box::new(self.expression(bp)),
        }
    }

    #[inline(always)]
    fn infix_expression(&mut self, left: Expression, bp: u8) -> Expression {
        let operator = expect!(self, Operator(op) => op);

        match operator {
            Increment | Decrement => PostfixExpression {
                operator: operator,
                operand: Box::new(left),
            },

            Accessor => MemberExpression {
                object: Box::new(left),
                property: Box::new(MemberKey::Literal(
                    expect!(self, Identifier(key) => key)
                )),
            },

            Conditional => ConditionalExpression {
                test: Box::new(left),
                consequent: Box::new(self.expression(bp)),
                alternate: {
                    expect!(self, Colon);
                    Box::new(self.expression(bp))
                }
            },

            FatArrow => self.arrow_function_expression(Some(left)),

            _ => {
                if !operator.infix() {
                    panic!("Unexpected operator {:?}", operator);
                }

                BinaryExpression {
                    left: Box::new(left),
                    operator: operator,
                    right: Box::new(
                        self.expression(bp)
                    )
                }
            }
        }
    }

    fn function_expression(&mut self) -> Expression {
        let name = if let Some(&Identifier(_)) = self.lookahead() {
            Some(expect!(self, Identifier(name) => name))
        } else {
            None
        };

        FunctionExpression {
            name: name,
            params: list!(self ( self.parameter() )),
            body: self.block_body(),
        }
    }

    fn sequence_expression(&mut self) -> Expression {
        let mut list = list!(self, self.expression(0), ParenOff);
        match list.len() {
            0 => {
                expect!(self, Operator(FatArrow));
                self.arrow_function_expression(None)
            },
            1 => {
                list.pop().unwrap()
            },
            _ => SequenceExpression(list)
        }
    }

    fn expression(&mut self, lbp: u8) -> Expression {
        let mut left = match self.consume() {
            This              => ThisExpression,
            Identifier(value) => IdentifierExpression(value),
            Literal(value)    => LiteralExpression(value),
            Operator(optype)  => self.prefix_expression(optype),
            ParenOn           => self.sequence_expression(),
            BracketOn         => self.array_expression(),
            BlockOn           => self.object_expression(),
            Function          => self.function_expression(),
            token             => unexpected_token!(self, token)
        };

        'right: loop {
            let rbp = match self.lookahead() {
                Some(&Operator(ref op)) => op.binding_power(false),
                _                       => 0,
            };

            if lbp > rbp {
                break 'right;
            }

            left = match self.lookahead() {
                Some(&Operator(_)) => self.infix_expression(left, rbp),

                Some(&ParenOn)     => CallExpression {
                    callee: Box::new(left),
                    arguments: list!(self ( self.expression(0) ))
                },

                Some(&BracketOn)   => MemberExpression {
                    object: Box::new(left),
                    property: Box::new(MemberKey::Computed(
                        surround!(self [ self.expression(0) ])
                    ))
                },

                _                  => break 'right,
            }
        }

        left
    }

    fn variable_declaration_statement(
        &mut self, kind: VariableDeclarationKind
    ) -> Statement {
        let mut declarators = Vec::new();

        loop {
            let name = expect!(self, Identifier(name) => name);
            expect!(self, Operator(Assign));
            declarators.push(VariableDeclarator {
                name: name,
                value: self.expression(0),
            });

            allow!{ self Comma => continue };
            break;
        }

        statement!(self, VariableDeclarationStatement {
            kind: kind,
            declarators: declarators,
        })
    }

    fn expression_statement(&mut self) -> Statement {
        statement!(self, ExpressionStatement(
            self.expression(0)
        ))
    }

    fn return_statement(&mut self) -> Statement {
        statement!(self, ReturnStatement(
            self.expression(0)
        ))
    }

    fn if_statement(&mut self) -> Statement {
        let test = surround!(self ( self.expression(0) ));
        let consequent = Box::new(self.block_or_statement());
        let alternate = if allow!(self, Else) {
            if allow!(self, If) {
                Some(Box::new(self.if_statement()))
            } else {
                Some(Box::new(self.block_or_statement()))
            }
        } else {
            None
        };

        statement!(self, IfStatement {
            test: test,
            consequent: consequent,
            alternate: alternate,
        })
    }

    fn while_statement(&mut self) -> Statement {
        statement!(self, WhileStatement {
            test: surround!(self ( self.expression(0) )),
            body: Box::new(self.block_or_statement()),
        })
    }

    fn parameter(&mut self) -> Parameter {
        Parameter {
            name: expect!(self, Identifier(name) => name)
        }
    }

    fn function_statement(&mut self) -> Statement {
        FunctionStatement {
            name: expect!(self, Identifier(name) => name),
            params: list!(self ( self.parameter() )),
            body: self.block_body(),
        }
    }

    fn class_member(&mut self, name: String, is_static: bool) -> ClassMember {
        match self.lookahead() {
            Some(&ParenOn) => {
                if !is_static && name == "constructor" {
                    ClassConstructor {
                        params: list!(self ( self.parameter() )),
                        body: self.block_body(),
                    }
                } else {
                    ClassMethod {
                        is_static: is_static,
                        name: name,
                        params: list!(self ( self.parameter())),
                        body: self.block_body(),
                    }
                }
            },
            Some(&Operator(Assign)) => {
                self.consume();
                ClassProperty {
                    is_static: is_static,
                    name: name,
                    value: self.expression(0),
                }
            },
            _ => unexpected_token!(self),
        }
    }

    fn class_statement(&mut self) -> Statement {
        let name = expect!(self, Identifier(id) => id);
        let super_class = if allow!(self, Extends) {
            Some(expect!(self, Identifier(name) => name))
        } else {
            None
        };
        expect!(self, BlockOn);
        let mut members = Vec::new();
        'members: loop {
            members.push(match self.consume() {
                Identifier(name) => self.class_member(name, false),
                Static           => {
                    let name = expect!(self, Identifier(name) => name);
                    self.class_member(name, true)
                },
                Semicolon        => continue 'members,
                BlockOff         => break 'members,
                token            => unexpected_token!(self, token)
            });
        }

        ClassStatement {
            name: name,
            extends: super_class,
            body: members,
        }
    }

    fn statement(&mut self) -> Option<Statement> {
        allow!{self
            Var       => return Some(self.variable_declaration_statement(
                VariableDeclarationKind::Var
            )),
            Let       => return Some(self.variable_declaration_statement(
                VariableDeclarationKind::Let
            )),
            Const     => return Some(self.variable_declaration_statement(
                VariableDeclarationKind::Const
            )),
            Return    => return Some(self.return_statement()),
            Function  => return Some(self.function_statement()),
            Class     => return Some(self.class_statement()),
            If        => return Some(self.if_statement()),
            While     => return Some(self.while_statement()),
            Semicolon => return self.statement()
        };

        if self.lookahead().is_some() {
            Some(self.expression_statement())
        } else {
            None
        }
    }
}

pub fn parse(source: String) -> Program {
    let mut parser = Parser::new(&source);
    let mut program = Program { body: Vec::new() };

    while let Some(statement) = parser.statement() {
        program.body.push(statement);
    }

    return program;
}
<commit_msg>Don’t create unnecessary vectors<commit_after>use lexicon::Token;
use lexicon::Token::*;
use tokenizer::Tokenizer;
use std::iter::Peekable;
use grammar::*;
use grammar::Statement::*;
use grammar::Expression::*;
use grammar::ClassMember::*;
use grammar::OperatorType::*;

/// If the next token matches `$p`, consume that token and return
/// true, else do nothing and return false
macro_rules! allow {
    ($parser:ident, $p:pat) => {
        match $parser.lookahead() {
            Some(&$p) => {
                $parser.consume();
                true
            },
            _ => false
        }
    };
    {$parser:ident $( $p:pat => $then:expr ),* } => ({
        match $parser.lookahead() {
            $(
                Some(&$p) => {
                    $parser.consume();
                    $then;
                }
            )*
            _ => {}
        }
    });
}

/// Expects next token to match `$p`, otherwise panics.
macro_rules! expect {
    ($parser:ident, $p:pat => $value:ident) => (
        match $parser.consume() {
            $p    => $value,
            token => unexpected_token!($parser, token),
        }
    );
    ($parser:ident, $p:pat) => (
        match $parser.consume() {
            $p    => {},
            token => unexpected_token!($parser, token),
        }
    )
}

macro_rules! unexpected_token {
    ($parser:ident) => ({
        unexpected_token!($parser, $parser.consume());
    });
    ($parser:ident, $token:expr) => {
        panic!("Unexpected token {:?}", $token);
    }
}

/// Evaluates the `$eval` expression, then expects a semicolon or
/// end of program. If neither is found, but a LineTermination
/// occured on previous token, parsing will continue as if a
/// semicolon was present. In other cases cause a panic.
macro_rules! statement {
    ($parser:ident, $eval:expr) => ({
        let value = $eval;

        let is_end = match $parser.lookahead() {
            Some(&Semicolon) => {
                $parser.consume();
                true
            },
            Some(&ParenOff)  => true,
            Some(&BlockOff)  => true,
            None             => true,
            _                => false
        };

        if !is_end && !$parser.allow_asi {
            unexpected_token!($parser);
        };

        value
    })
}

/// Read a list of items with predefined `$start`, `$end` and
/// `$separator` tokens and an `$item` expression that is then
/// pushed onto a vector.
macro_rules! list {
    ($parser:ident, $item:expr, $end:pat) => ({
        let mut list = Vec::new();
        loop {
            if allow!($parser, $end) {
                break;
            }
            list.push($item);

            match $parser.consume() {
                Comma => allow!{ $parser $end => break },
                $end  => break,
                _     => {},
            }
        }

        list
    });
    ($parser:ident, $item:expr, $start:pat, $end:pat) => ({
        expect!($parser, $start);
        list!($parser, $item, $end)
    });
    ($parser:ident ( $item:expr )) => {
        list!($parser, $item, ParenOn, ParenOff)
    };
    ($parser:ident [ $item:expr ]) => {
        list!($parser, $item, BracketOn, BracketOff)
    };
    ($parser:ident { $item:expr }) => {
        list!($parser, $item, BlockOn, BlockOff)
    };
}

macro_rules! surround {
    ($parser:ident ( $eval:expr )) => ({
        expect!($parser, ParenOn);
        let value = $eval;
        expect!($parser, ParenOff);
        value
    });
    ($parser:ident [ $eval:expr ]) => ({
        expect!($parser, BracketOn);
        let value = $eval;
        expect!($parser, BracketOff);
        value
    });
}

pub struct Parser<'a> {
    tokenizer: Peekable<Tokenizer<'a>>,
    allow_asi: bool,
}

impl<'a> Parser<'a> {
    pub fn new(source: &'a String) -> Self {
        Parser {
            tokenizer: Tokenizer::new(source).peekable(),
            allow_asi: false,
        }
    }

    #[inline(always)]
    fn handle_line_termination(&mut self) {
        while let Some(&LineTermination) = self.tokenizer.peek() {
            self.tokenizer.next();
            self.allow_asi = true;
        }
    }

    #[inline(always)]
    fn consume(&mut self) -> Token {
        self.handle_line_termination();
        let token = self.tokenizer.next().expect("Unexpected end of program");

        // println!("Consume {:?}", token);

        self.allow_asi = false;
        token
    }

    #[inline(always)]
    fn lookahead(&mut self) -> Option<&Token> {
        self.handle_line_termination();
        self.tokenizer.peek()
    }

    fn array_expression(&mut self) -> Expression {
        ArrayExpression(list!(self, self.expression(0), BracketOff))
    }

    #[inline(always)]
    fn object_member(&mut self) -> ObjectMember {
        match self.consume() {
            Identifier(key) | Literal(LiteralString(key)) => {
                if allow!(self, Colon) {
                    ObjectMember::Literal {
                        key: key,
                        value: self.expression(0),
                    }
                } else {
                    ObjectMember::Shorthand {
                        key: key,
                    }
                }
            },
            BracketOn => {
                let key = self.expression(0);
                expect!(self, BracketOff);
                expect!(self, Colon);
                ObjectMember::Computed {
                    key: key,
                    value: self.expression(0),
                }
            },
            token => {
                panic!("Expected object key, got {:?}", token)
            }
        }
    }

    fn object_expression(&mut self) -> Expression {
        ObjectExpression(list!(self, self.object_member(), BlockOff))
    }

    fn block_or_statement(&mut self) -> Statement {
        if let Some(&BlockOn) = self.lookahead() {
            BlockStatement {
                body: self.block_body()
            }
        } else {
            self.expression_statement()
        }
    }

    fn block_body(&mut self) -> Vec<Statement> {
        expect!(self, BlockOn);

        let mut body = Vec::new();
        loop {
            allow!{ self BlockOff => break };

            body.push(
                self.statement().expect("Unexpected end of statements block")
            )
        }

        body
    }

    fn arrow_function_expression(&mut self, p: Option<Expression>) -> Expression {
        let params: Vec<Parameter> = match p {
            None => Vec::new(),
            Some(IdentifierExpression(name)) => {
                vec![Parameter { name: name }]
            },
            Some(SequenceExpression(mut list)) => {
                list.drain(..).map(|expression| {
                    match expression {
                        IdentifierExpression(name) => Parameter { name: name },
                        _ => panic!("Cannot cast {:?} to a parameter", expression),
                    }
                }).collect()
            },
            _ =>
                panic!("Cannot cast {:?} to parameters", p),
        };

        let body = if let Some(&BlockOn) = self.lookahead() {
            BlockStatement {
                body: self.block_body()
            }
        } else {
            ExpressionStatement(self.expression(0))
        };

        ArrowFunctionExpression {
            params: params,
            body: Box::new(body)
        }
    }

    #[inline(always)]
    fn prefix_expression(&mut self, operator: OperatorType) -> Expression {
        let bp = operator.binding_power(true);

        if !operator.prefix() {
            panic!("Unexpected operator {:?}", operator);
        }

        PrefixExpression {
            operator: operator,
            operand: Box::new(self.expression(bp)),
        }
    }

    #[inline(always)]
    fn infix_expression(&mut self, left: Expression, bp: u8) -> Expression {
        let operator = expect!(self, Operator(op) => op);

        match operator {
            Increment | Decrement => PostfixExpression {
                operator: operator,
                operand: Box::new(left),
            },

            Accessor => MemberExpression {
                object: Box::new(left),
                property: Box::new(MemberKey::Literal(
                    expect!(self, Identifier(key) => key)
                )),
            },

            Conditional => ConditionalExpression {
                test: Box::new(left),
                consequent: Box::new(self.expression(bp)),
                alternate: {
                    expect!(self, Colon);
                    Box::new(self.expression(bp))
                }
            },

            FatArrow => self.arrow_function_expression(Some(left)),

            _ => {
                if !operator.infix() {
                    panic!("Unexpected operator {:?}", operator);
                }

                BinaryExpression {
                    left: Box::new(left),
                    operator: operator,
                    right: Box::new(
                        self.expression(bp)
                    )
                }
            }
        }
    }

    fn function_expression(&mut self) -> Expression {
        let name = if let Some(&Identifier(_)) = self.lookahead() {
            Some(expect!(self, Identifier(name) => name))
        } else {
            None
        };

        FunctionExpression {
            name: name,
            params: list!(self ( self.parameter() )),
            body: self.block_body(),
        }
    }

    fn sequence_expression(&mut self) -> Expression {
        if allow!(self, ParenOff) {
            expect!(self, Operator(FatArrow));
            return self.arrow_function_expression(None);
        }

        let first = self.expression(0);
        allow!{ self ParenOff => return first };

        let mut list = vec![first];
        loop {
            allow!{ self ParenOff => break };
            expect!(self, Comma);
            list.push(self.expression(0));
        }

        SequenceExpression(list)
    }

    fn expression(&mut self, lbp: u8) -> Expression {
        let mut left = match self.consume() {
            This              => ThisExpression,
            Identifier(value) => IdentifierExpression(value),
            Literal(value)    => LiteralExpression(value),
            Operator(optype)  => self.prefix_expression(optype),
            ParenOn           => self.sequence_expression(),
            BracketOn         => self.array_expression(),
            BlockOn           => self.object_expression(),
            Function          => self.function_expression(),
            token             => unexpected_token!(self, token)
        };

        'right: loop {
            let rbp = match self.lookahead() {
                Some(&Operator(ref op)) => op.binding_power(false),
                _                       => 0,
            };

            if lbp > rbp {
                break 'right;
            }

            left = match self.lookahead() {
                Some(&Operator(_)) => self.infix_expression(left, rbp),

                Some(&ParenOn)     => CallExpression {
                    callee: Box::new(left),
                    arguments: list!(self ( self.expression(0) ))
                },

                Some(&BracketOn)   => MemberExpression {
                    object: Box::new(left),
                    property: Box::new(MemberKey::Computed(
                        surround!(self [ self.expression(0) ])
                    ))
                },

                _                  => break 'right,
            }
        }

        left
    }

    fn variable_declaration_statement(
        &mut self, kind: VariableDeclarationKind
    ) -> Statement {
        let mut declarators = Vec::new();

        loop {
            let name = expect!(self, Identifier(name) => name);
            expect!(self, Operator(Assign));
            declarators.push(VariableDeclarator {
                name: name,
                value: self.expression(0),
            });

            allow!{ self Comma => continue };
            break;
        }

        statement!(self, VariableDeclarationStatement {
            kind: kind,
            declarators: declarators,
        })
    }

    fn expression_statement(&mut self) -> Statement {
        statement!(self, ExpressionStatement(
            self.expression(0)
        ))
    }

    fn return_statement(&mut self) -> Statement {
        statement!(self, ReturnStatement(
            self.expression(0)
        ))
    }

    fn if_statement(&mut self) -> Statement {
        let test = surround!(self ( self.expression(0) ));
        let consequent = Box::new(self.block_or_statement());
        let alternate = if allow!(self, Else) {
            if allow!(self, If) {
                Some(Box::new(self.if_statement()))
            } else {
                Some(Box::new(self.block_or_statement()))
            }
        } else {
            None
        };

        statement!(self, IfStatement {
            test: test,
            consequent: consequent,
            alternate: alternate,
        })
    }

    fn while_statement(&mut self) -> Statement {
        statement!(self, WhileStatement {
            test: surround!(self ( self.expression(0) )),
            body: Box::new(self.block_or_statement()),
        })
    }

    fn parameter(&mut self) -> Parameter {
        Parameter {
            name: expect!(self, Identifier(name) => name)
        }
    }

    fn function_statement(&mut self) -> Statement {
        FunctionStatement {
            name: expect!(self, Identifier(name) => name),
            params: list!(self ( self.parameter() )),
            body: self.block_body(),
        }
    }

    fn class_member(&mut self, name: String, is_static: bool) -> ClassMember {
        match self.lookahead() {
            Some(&ParenOn) => {
                if !is_static && name == "constructor" {
                    ClassConstructor {
                        params: list!(self ( self.parameter() )),
                        body: self.block_body(),
                    }
                } else {
                    ClassMethod {
                        is_static: is_static,
                        name: name,
                        params: list!(self ( self.parameter())),
                        body: self.block_body(),
                    }
                }
            },
            Some(&Operator(Assign)) => {
                self.consume();
                ClassProperty {
                    is_static: is_static,
                    name: name,
                    value: self.expression(0),
                }
            },
            _ => unexpected_token!(self),
        }
    }

    fn class_statement(&mut self) -> Statement {
        let name = expect!(self, Identifier(id) => id);
        let super_class = if allow!(self, Extends) {
            Some(expect!(self, Identifier(name) => name))
        } else {
            None
        };
        expect!(self, BlockOn);
        let mut members = Vec::new();
        'members: loop {
            members.push(match self.consume() {
                Identifier(name) => self.class_member(name, false),
                Static           => {
                    let name = expect!(self, Identifier(name) => name);
                    self.class_member(name, true)
                },
                Semicolon        => continue 'members,
                BlockOff         => break 'members,
                token            => unexpected_token!(self, token)
            });
        }

        ClassStatement {
            name: name,
            extends: super_class,
            body: members,
        }
    }

    fn statement(&mut self) -> Option<Statement> {
        allow!{self
            Var       => return Some(self.variable_declaration_statement(
                VariableDeclarationKind::Var
            )),
            Let       => return Some(self.variable_declaration_statement(
                VariableDeclarationKind::Let
            )),
            Const     => return Some(self.variable_declaration_statement(
                VariableDeclarationKind::Const
            )),
            Return    => return Some(self.return_statement()),
            Function  => return Some(self.function_statement()),
            Class     => return Some(self.class_statement()),
            If        => return Some(self.if_statement()),
            While     => return Some(self.while_statement()),
            Semicolon => return self.statement()
        };

        if self.lookahead().is_some() {
            Some(self.expression_statement())
        } else {
            None
        }
    }
}

pub fn parse(source: String) -> Program {
    let mut parser = Parser::new(&source);
    let mut program = Program { body: Vec::new() };

    while let Some(statement) = parser.statement() {
        program.body.push(statement);
    }

    return program;
}
<|endoftext|>


<commit_before>use std::env;
use std::fs::{File, read_dir};
use std::io::Read;
use serde::de::Deserialize;
use serde_protobuf::descriptor::Descriptors;
use serde_protobuf::de::Deserializer;
use serde_value::Value;
use protobuf::{CodedInputStream, parse_from_reader};

pub fn process_single(read: &mut Read) {
    for mut fdset_file in discover_fdsets() {
        let proto = parse_from_reader(&mut fdset_file).unwrap();
        let descriptors = Descriptors::from_proto(&proto);
        let byte_is = CodedInputStream::new(read);

        let mut deserializer = Deserializer::for_named_message(&descriptors, ".com.example.dog.Dog", byte_is).unwrap();
        let value = Value::deserialize(&mut deserializer).unwrap();
        println!("{:?}", value);
    }
}

pub fn process_stream(read: &mut Read) {
    let mut stream = CodedInputStream::new(read);

    loop {
        match stream.eof() {
            Err(e) => panic!(e),
            Ok(true) => break,
            Ok(false) => break,
            //todo: actually do deserialization here
        }
    }
}

fn discover_fdsets() -> Vec<File> {
    let mut fdset_files = Vec::new();

    let mut home = env::home_dir().expect("Could not find $HOME");
    home.push(".pq");
    let paths = read_dir(home.as_path()).unwrap();

    for p in paths {
        let path = match p {
            Ok(p) => p.path(),
            Err(_) => continue,
        };
        match path.extension() {
            Some(x) => {
                if x != "fdset" {
                    continue;
                }
            },
            None => continue,
        }
        fdset_files.push(File::open(path.as_path()).unwrap());
    }
    return fdset_files;
}
<commit_msg>Revert + discover MD from fdset<commit_after>use std::env;
use std::fs::{File, read_dir};
use std::io::Read;
use std::path::PathBuf;
use serde::de::Deserialize;
use serde_protobuf::descriptor::{Descriptors, MessageDescriptor};
use serde_protobuf::de::Deserializer;
use serde_value::Value;
use protobuf::{CodedInputStream, parse_from_reader};

pub fn process_single(read: &mut Read) {
    for mut fdset_path in discover_fdsets() {
        let fdset_file = File::open(fdset_path.as_path()).unwrap();
        let proto = parse_from_reader(&mut fdset_file).unwrap();
        let md = MessageDescriptor::from_proto(fdset_path, proto);
        let descriptors = Descriptors::from_proto(proto);
        let byte_is = CodedInputStream::new(read);

        let mut deserializer = Deserializer::new(&descriptors, md, byte_is).unwrap();
        let value = Value::deserialize(&mut deserializer).unwrap();
        println!("{:?}", value);
    }
}

pub fn process_stream(read: &mut Read) {
    let mut stream = CodedInputStream::new(read);

    loop {
        match stream.eof() {
            Err(e) => panic!(e),
            Ok(true) => break,
            Ok(false) => break,
            //todo: actually do deserialization here
        }
    }
}

fn discover_fdsets() -> Vec<PathBuf> {
    let mut fdset_files = Vec::new();

    let mut home = env::home_dir().expect("Could not find $HOME");
    home.push(".pq");
    let paths = read_dir(home.as_path()).unwrap();

    for p in paths {
        let path = match p {
            Ok(p) => p.path(),
            Err(_) => continue,
        };
        match path.extension() {
            Some(x) => {
                if x != "fdset" {
                    continue;
                }
            },
            None => continue,
        }
        fdset_files.push(path);
    }
    return fdset_files;
}
<|endoftext|>


<commit_before>use ::color::FloatColor;
use ::color::Color;
use ::quantizer::HistColor;
use ::colormap::ColorMap;
use ::histogram::Histogram;
use ::colorspace::ColorSpace;
use std::f64;

struct KMeansCluster {
    sum: FloatColor,
    weight: f64,
}

pub fn kmeans_step(colors: Vec<FloatColor>, histogram: &[HistColor]) -> Vec<FloatColor> {
    let map = ColorMap::from_float_colors(colors.iter().cloned().collect());
    let mut clusters: Vec<_> = (0..colors.len())
        .map(|_| {
            KMeansCluster {
                sum: FloatColor::default(),
                weight: 0.0,
            }
        })
        .collect();
    for entry in histogram {
        let index = map.find_nearest(entry.color);
        let mut cluster = &mut clusters[index];
        cluster.sum += entry.color * entry.count as f64;
        cluster.weight += entry.count as f64;
    }
    clusters.iter().map(|cluster| cluster.sum * (1.0 / cluster.weight)).collect()
}

pub fn optimize_palette<T: ColorSpace>(colorspace: &T,
                                       palette: &[Color],
                                       histogram: &Histogram,
                                       num_iterations: usize)
                                       -> Vec<Color> {
    let hist = histogram.to_hist_colors(colorspace);
    let mut colors = palette.iter().map(|c| colorspace.to_float(*c)).collect();
    for _ in 0..num_iterations {
        colors = kmeans_step(colors, &hist);
    }
    colors.iter().map(|&c| colorspace.from_float(c)).collect()
}

pub fn kmeans_step_weighted(mut colors: Vec<FloatColor>,
                            histogram: &[HistColor])
                            -> Vec<FloatColor> {
    let map = ColorMap::from_float_colors(colors.clone());
    let mut clusters: Vec<_> = (0..colors.len())
        .map(|_| {
            KMeansCluster {
                sum: FloatColor::default(),
                weight: 0.0,
            }
        })
        .collect();
    for entry in histogram {
        let index = map.find_nearest(entry.color);
        let neighbors = map.neighbors(index);
        let mut error_sum = 0.;
        let mut color = entry.color;
        for _ in 0..4 {
            let mut best_i = 0;
            let mut best_error = f64::MAX;
            for &i in neighbors {
                let diff = color - colors[i];
                let error = diff.abs();
                if error < best_error {
                    best_i = i;
                    best_error = error;
                }
            }
            error_sum += best_error * best_error;
            color = entry.color + color - colors[best_i];
        }
        let mut cluster = &mut clusters[index];
        let weight = entry.count as f64 * error_sum;
        cluster.sum += entry.color * weight;
        cluster.weight += weight;
    }
    for i in 0..colors.len() {
        let mut cluster = &mut clusters[i];
        if cluster.weight > 0.0 {
            colors[i] = cluster.sum * (1.0 / cluster.weight);
        }
        cluster.sum = FloatColor::default();
        cluster.weight = 0.0;
    }
    colors
}

pub fn optimize_palette_weighted<T: ColorSpace>(colorspace: &T,
                                                palette: &[Color],
                                                histogram: &Histogram,
                                                num_iterations: usize)
                                                -> Vec<Color> {
    let hist = histogram.to_hist_colors(colorspace);
    let mut colors = palette.iter().map(|c| colorspace.to_float(*c)).collect();
    for _ in 0..num_iterations {
        colors = kmeans_step_weighted(colors, &hist);
    }
    colors.iter().map(|&c| colorspace.from_float(c)).collect()
}
<commit_msg>more aggressive weighted kmeans<commit_after>use ::color::FloatColor;
use ::color::Color;
use ::quantizer::HistColor;
use ::colormap::ColorMap;
use ::histogram::Histogram;
use ::colorspace::ColorSpace;
use std::f64;

struct KMeansCluster {
    sum: FloatColor,
    weight: f64,
}

pub fn kmeans_step(colors: Vec<FloatColor>, histogram: &[HistColor]) -> Vec<FloatColor> {
    let map = ColorMap::from_float_colors(colors.iter().cloned().collect());
    let mut clusters: Vec<_> = (0..colors.len())
        .map(|_| {
            KMeansCluster {
                sum: FloatColor::default(),
                weight: 0.0,
            }
        })
        .collect();
    for entry in histogram {
        let index = map.find_nearest(entry.color);
        let mut cluster = &mut clusters[index];
        cluster.sum += entry.color * entry.count as f64;
        cluster.weight += entry.count as f64;
    }
    clusters.iter().map(|cluster| cluster.sum * (1.0 / cluster.weight)).collect()
}

pub fn optimize_palette<T: ColorSpace>(colorspace: &T,
                                       palette: &[Color],
                                       histogram: &Histogram,
                                       num_iterations: usize)
                                       -> Vec<Color> {
    let hist = histogram.to_hist_colors(colorspace);
    let mut colors = palette.iter().map(|c| colorspace.to_float(*c)).collect();
    for _ in 0..num_iterations {
        colors = kmeans_step(colors, &hist);
    }
    colors.iter().map(|&c| colorspace.from_float(c)).collect()
}

pub fn kmeans_step_weighted(mut colors: Vec<FloatColor>,
                            histogram: &[HistColor])
                            -> Vec<FloatColor> {
    let map = ColorMap::from_float_colors(colors.clone());
    let mut clusters: Vec<_> = (0..colors.len())
        .map(|_| {
            KMeansCluster {
                sum: FloatColor::default(),
                weight: 0.0,
            }
        })
        .collect();
    for entry in histogram {
        let index = map.find_nearest(entry.color);
        let neighbors = map.neighbors(index);
        let mut error_sum = 0.;
        let mut color = entry.color;
        for _ in 0..4 {
            let mut best_i = 0;
            let mut best_error = f64::MAX;
            for &i in neighbors {
                let diff = color - colors[i];
                let error = diff.abs();
                if error < best_error {
                    best_i = i;
                    best_error = error;
                }
            }
            error_sum += best_error;
            color = entry.color + color - colors[best_i];
        }
        let mut cluster = &mut clusters[index];
        let weight = entry.count as f64 * error_sum.powi(4);
        cluster.sum += entry.color * weight;
        cluster.weight += weight;
    }
    for i in 0..colors.len() {
        let mut cluster = &mut clusters[i];
        if cluster.weight > 0.0 {
            colors[i] = cluster.sum * (1.0 / cluster.weight);
        }
        cluster.sum = FloatColor::default();
        cluster.weight = 0.0;
    }
    colors
}

pub fn optimize_palette_weighted<T: ColorSpace>(colorspace: &T,
                                                palette: &[Color],
                                                histogram: &Histogram,
                                                num_iterations: usize)
                                                -> Vec<Color> {
    let hist = histogram.to_hist_colors(colorspace);
    let mut colors = palette.iter().map(|c| colorspace.to_float(*c)).collect();
    for _ in 0..num_iterations {
        colors = kmeans_step_weighted(colors, &hist);
    }
    colors.iter().map(|&c| colorspace.from_float(c)).collect()
}
<|endoftext|>