From ba9d00d0b477ed8aa77c67a4e6d884703c92179d Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Wed, 28 Jun 2017 01:54:30 -0400 Subject: [PATCH] Move tests under appropriate modules --- src/ebml.rs | 84 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 103 +--------------------------------------------------- src/webm.rs | 24 ++++++++++++ 3 files changed, 110 insertions(+), 101 deletions(-) diff --git a/src/ebml.rs b/src/ebml.rs index 30079e5..fe6d56f 100644 --- a/src/ebml.rs +++ b/src/ebml.rs @@ -133,3 +133,87 @@ pub trait Schema<'a> { Ebml(self, source) } } + +#[cfg(test)] +mod tests { + use ebml::*; + use ebml::Error::{CorruptVarint, UnknownElementId}; + use ebml::Varint::{Unknown, Value}; + use tests::TEST_FILE; + + #[test] + fn fail_corrupted_varints() { + assert_eq!(decode_varint(&[0]), Err(CorruptVarint)); + assert_eq!(decode_varint(&[0, 0, 0]), Err(CorruptVarint)); + } + + #[test] + fn incomplete_varints() { + assert_eq!(decode_varint(&[]), Ok(None)); + assert_eq!(decode_varint(&[0x40]), Ok(None)); + assert_eq!(decode_varint(&[0x01, 0, 0]), Ok(None)); + } + + #[test] + fn parse_varints() { + assert_eq!(decode_varint(&[0xFF]), Ok(Some((Unknown, 1)))); + assert_eq!(decode_varint(&[0x7F, 0xFF]), Ok(Some((Unknown, 2)))); + assert_eq!(decode_varint(&[0x80]), Ok(Some((Value(0), 1)))); + assert_eq!(decode_varint(&[0x81]), Ok(Some((Value(1), 1)))); + assert_eq!(decode_varint(&[0x40, 52]), Ok(Some((Value(52), 2)))); + + // test extra data in buffer + assert_eq!(decode_varint(&[0x83, 0x11]), Ok(Some((Value(3), 1)))); + } + + #[test] + fn fail_corrupted_tags() { + assert_eq!(decode_tag(&[0]), Err(CorruptVarint)); + assert_eq!(decode_tag(&[0x80, 0]), Err(CorruptVarint)); + assert_eq!(decode_tag(&[0xFF, 0x80]), Err(UnknownElementId)); + assert_eq!(decode_tag(&[0x7F, 0xFF, 0x40, 0]), Err(UnknownElementId)); + } + + #[test] + fn incomplete_tags() { + assert_eq!(decode_tag(&[]), Ok(None)); + assert_eq!(decode_tag(&[0x80]), Ok(None)); + assert_eq!(decode_tag(&[0x40, 0, 0x40]), Ok(None)); + } + + #[test] + fn parse_tags() { + assert_eq!(decode_tag(&[0x80, 0x80]), Ok(Some((0, Value(0), 2)))); + assert_eq!(decode_tag(&[0x81, 0x85]), Ok(Some((1, Value(5), 2)))); + assert_eq!(decode_tag(&[0x80, 0xFF]), Ok(Some((0, Unknown, 2)))); + assert_eq!(decode_tag(&[0x80, 0x7F, 0xFF]), Ok(Some((0, Unknown, 3)))); + assert_eq!(decode_tag(&[0x85, 0x40, 52]), Ok(Some((5, Value(52), 3)))); + } + + struct Dummy; + + #[derive(Debug, PartialEq)] + struct GenericElement(u64, usize); + + impl<'a> Schema<'a> for Dummy { + type Element = GenericElement; + + fn should_unwrap(&self, element_id: u64) -> bool { + match element_id { + _ => false + } + } + + fn decode<'b: 'a>(&self, element_id: u64, bytes: &'b[u8]) -> Result { + match element_id { + _ => Ok(GenericElement(element_id, bytes.len())) + } + } + } + + #[test] + fn decode_sanity_test() { + let decoded = Dummy.decode_element(TEST_FILE); + assert_eq!(decoded, Ok(Some((GenericElement(0x0A45DFA3, 31), 43)))); + } +} diff --git a/src/lib.rs b/src/lib.rs index a6c7517..f30276a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,11 +9,9 @@ pub use ebml::{Error, Schema}; #[cfg(test)] mod tests { - use futures::future::{ok, Future}; - use super::*; - use super::Error::{CorruptVarint, UnknownElementId}; - use super::Varint::{Unknown, Value}; + + pub const TEST_FILE: &'static [u8] = include_bytes!("data/test1.webm"); #[test] fn hello_futures() { @@ -24,101 +22,4 @@ mod tests { assert_eq!(string_result, "Hello, Futures!"); } - - #[test] - fn fail_corrupted_varints() { - assert_eq!(decode_varint(&[0]), Err(CorruptVarint)); - assert_eq!(decode_varint(&[0, 0, 0]), Err(CorruptVarint)); - } - - #[test] - fn incomplete_varints() { - assert_eq!(decode_varint(&[]), Ok(None)); - assert_eq!(decode_varint(&[0x40]), Ok(None)); - assert_eq!(decode_varint(&[0x01, 0, 0]), Ok(None)); - } - - #[test] - fn parse_varints() { - assert_eq!(decode_varint(&[0xFF]), Ok(Some((Unknown, 1)))); - assert_eq!(decode_varint(&[0x7F, 0xFF]), Ok(Some((Unknown, 2)))); - assert_eq!(decode_varint(&[0x80]), Ok(Some((Value(0), 1)))); - assert_eq!(decode_varint(&[0x81]), Ok(Some((Value(1), 1)))); - assert_eq!(decode_varint(&[0x40, 52]), Ok(Some((Value(52), 2)))); - - // test extra data in buffer - assert_eq!(decode_varint(&[0x83, 0x11]), Ok(Some((Value(3), 1)))); - } - - #[test] - fn fail_corrupted_tags() { - assert_eq!(decode_tag(&[0]), Err(CorruptVarint)); - assert_eq!(decode_tag(&[0x80, 0]), Err(CorruptVarint)); - assert_eq!(decode_tag(&[0xFF, 0x80]), Err(UnknownElementId)); - assert_eq!(decode_tag(&[0x7F, 0xFF, 0x40, 0]), Err(UnknownElementId)); - } - - #[test] - fn incomplete_tags() { - assert_eq!(decode_tag(&[]), Ok(None)); - assert_eq!(decode_tag(&[0x80]), Ok(None)); - assert_eq!(decode_tag(&[0x40, 0, 0x40]), Ok(None)); - } - - #[test] - fn parse_tags() { - assert_eq!(decode_tag(&[0x80, 0x80]), Ok(Some((0, Value(0), 2)))); - assert_eq!(decode_tag(&[0x81, 0x85]), Ok(Some((1, Value(5), 2)))); - assert_eq!(decode_tag(&[0x80, 0xFF]), Ok(Some((0, Unknown, 2)))); - assert_eq!(decode_tag(&[0x80, 0x7F, 0xFF]), Ok(Some((0, Unknown, 3)))); - assert_eq!(decode_tag(&[0x85, 0x40, 52]), Ok(Some((5, Value(52), 3)))); - } - - const TEST_FILE: &'static [u8] = include_bytes!("data/test1.webm"); - - struct Dummy; - - #[derive(Debug, PartialEq)] - struct GenericElement(u64, usize); - - impl<'a> Schema<'a> for Dummy { - type Element = GenericElement; - - fn should_unwrap(&self, element_id: u64) -> bool { - match element_id { - _ => false - } - } - - fn decode<'b: 'a>(&self, element_id: u64, bytes: &'b[u8]) -> Result { - match element_id { - _ => Ok(GenericElement(element_id, bytes.len())) - } - } - } - - #[test] - fn decode_sanity_test() { - let decoded = Dummy.decode_element(TEST_FILE); - assert_eq!(decoded, Ok(Some((GenericElement(0x0A45DFA3, 31), 43)))); - } - - #[test] - fn decode_webm_test1() { - let mut iter = Webm.parse(TEST_FILE).into_iter(); - - // test that we match the structure of the test file - assert_eq!(iter.next(), Some(WebmElement::EbmlHead)); - assert_eq!(iter.next(), Some(WebmElement::Segment)); - assert_eq!(iter.next(), Some(WebmElement::SeekHead)); - assert_eq!(iter.next(), Some(WebmElement::Void)); - assert_eq!(iter.next(), Some(WebmElement::Info)); - assert_eq!(iter.next(), Some(WebmElement::Tracks(&TEST_FILE[358..421]))); - assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[433..13739]))); - assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[13751..34814]))); - assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[34826..56114]))); - assert_eq!(iter.next(), Some(WebmElement::Cues)); - assert_eq!(iter.next(), None); - } - } diff --git a/src/webm.rs b/src/webm.rs index 8e3fbef..4775e95 100644 --- a/src/webm.rs +++ b/src/webm.rs @@ -46,3 +46,27 @@ impl<'a> Schema<'a> for Webm { } } } + +#[cfg(test)] +mod tests { + use tests::TEST_FILE; + use webm::*; + + #[test] + fn decode_webm_test1() { + let mut iter = Webm.parse(TEST_FILE).into_iter(); + + // test that we match the structure of the test file + assert_eq!(iter.next(), Some(WebmElement::EbmlHead)); + assert_eq!(iter.next(), Some(WebmElement::Segment)); + assert_eq!(iter.next(), Some(WebmElement::SeekHead)); + assert_eq!(iter.next(), Some(WebmElement::Void)); + assert_eq!(iter.next(), Some(WebmElement::Info)); + assert_eq!(iter.next(), Some(WebmElement::Tracks(&TEST_FILE[358..421]))); + assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[433..13739]))); + assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[13751..34814]))); + assert_eq!(iter.next(), Some(WebmElement::Cluster(&TEST_FILE[34826..56114]))); + assert_eq!(iter.next(), Some(WebmElement::Cues)); + assert_eq!(iter.next(), None); + } +}