use maraiah::{bin, fixed::*, map};

include!("data/rand.rs");

#[test]
fn read_minf_must_process()
{
	let inp = include_bytes!("data/map/minf.in");
	let out = include!("data/map/minf.out");

	assert_eq!(map::minf::read(inp).unwrap(), out);
}

#[test]
fn read_epnt_must_process()
{
	let inp = include_bytes!("data/map/epnt.in");

	let inp = bin::rd_array(inp, map::epnt::read).unwrap();
	let out = include!("data/map/epnt.out.2").to_vec();

	assert_eq!(inp, out);

	let inp = map::epnt::to_pnts(&inp);
	let out = include!("data/map/epnt.out.1").to_vec();

	assert_eq!(inp, out);
}

#[test]
fn read_term_must_process()
{
	let inp = include_bytes!("data/map/term.in");
	let out = include!("data/map/term.out");

	let inp = bin::rd_array(inp, map::term::read).unwrap();

	// for better debug output, we iterate over each item
	assert_eq!(inp.len(), out.len());

	for (itrm, otrm) in inp.iter().zip(&out) {
		assert_eq!(itrm.groups.len(), otrm.groups.len());
		for (igrp, ogrp) in itrm.groups.iter().zip(&otrm.groups) {
			assert_eq!(igrp, ogrp);
		}

		assert_eq!(itrm.faces.len(), otrm.faces.len());
		for (ifac, ofac) in itrm.faces.iter().zip(&otrm.faces) {
			assert_eq!(ifac, ofac);
		}
	}
}

#[test]
fn map_m2()
{
	let inp = include_bytes!("data/m2/Map");
	let mut rd = std::io::BufReader::new(&inp[..]);

	let mp = map::head::read(&mut rd).unwrap();
	let en = map::entr::read_all(&mp).unwrap();

	assert!(map::data::read_all(mp.head(), &en).is_ok());
}

#[test]
#[ignore]
fn map_full_check()
{
	use maraiah::{bin::OptU16,
	map::{data::*, lins::*, lite::*, ltfn::*, minf::*, objs::*,
	plac::*, pnts::*, poly::*},
	xfer::TransferMode};
	use std::collections::BTreeMap;

	let inp = include_bytes!("data/map/testmap.in");
	let mut rd = std::io::BufReader::new(&inp[..]);

	let mp = map::head::read(&mut rd).unwrap();
	let en = map::entr::read_all(&mp).unwrap();
	let ed = map::data::read_all(mp.head(), &en).unwrap();

	let mut out = BTreeMap::new();

	out.insert(0, include!("data/map/testmap.out"));

	assert_eq!(out, ed);
}

#[test]
fn map_must_not_process()
{
	for inp in &RANDOM {
		bin::rd_array(inp, map::fxpx::read).err().unwrap();
		bin::rd_array(inp, map::lins::read).err().unwrap();
		bin::rd_array(inp, map::lite::read).err().unwrap();
		bin::rd_array(inp, map::lite::read_old).err().unwrap();
		bin::rd_array(inp, map::medi::read).err().unwrap();
		bin::rd_array(inp, map::mnpx::read).err().unwrap();
		bin::rd_array(inp, map::note::read).err().unwrap();
		bin::rd_array(inp, map::objs::read).err().unwrap();
		bin::rd_array(inp, map::plat::read).err().unwrap();
		bin::rd_array(inp, map::poly::read).err().unwrap();
		bin::rd_array(inp, map::poly::read_old).err().unwrap();
		bin::rd_array(inp, map::prpx::read).err().unwrap();
		bin::rd_array(inp, map::sids::read).err().unwrap();
		bin::rd_array(inp, map::sids::read_old).err().unwrap();
		bin::rd_array(inp, map::term::read).err().unwrap();
		bin::rd_array(inp, map::trmg::read).err().unwrap();
		bin::rd_array(inp, map::wppx::read).err().unwrap();
		map::minf::read(inp).err().unwrap();
		map::minf::read_old(inp).err().unwrap();
	}
}

#[test]
fn map_must_not_panic()
{
	for inp in &RANDOM {
		drop(bin::rd_array(inp, map::ambi::read));
		drop(bin::rd_array(inp, map::bonk::read));
		drop(bin::rd_array(inp, map::epnt::read));
		drop(bin::rd_array(inp, map::iidx::read));
		drop(bin::rd_array(inp, map::plac::read));
		drop(bin::rd_array(inp, map::pxpx::read));
		drop(bin::rd_array(inp, map::trmf::read));
	}
}

// EOF
