2016-10-26 09:46:20 -04:00
|
|
|
//
|
|
|
|
// Copyright (c) 2016 KAMADA Ken'ichi.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
|
|
|
// 1. Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
|
|
// documentation and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
// SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
|
2016-11-17 07:14:07 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-04-01 08:11:54 -04:00
|
|
|
use crate::endian::Endian;
|
2016-10-26 09:46:20 -04:00
|
|
|
|
|
|
|
/// Types and values of TIFF fields (for Exif attributes).
|
2019-12-15 07:11:20 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2019-12-14 07:57:35 -05:00
|
|
|
pub enum Value {
|
2016-11-03 06:49:10 -04:00
|
|
|
/// Vector of 8-bit unsigned integers.
|
|
|
|
Byte(Vec<u8>),
|
2016-12-30 09:19:02 -05:00
|
|
|
/// Vector of slices of 8-bit bytes containing 7-bit ASCII characters.
|
2018-01-13 09:33:45 -05:00
|
|
|
/// The trailing null characters are not included. Note that
|
2016-12-30 09:19:02 -05:00
|
|
|
/// the 8th bits may present if a non-conforming data is given.
|
2019-12-14 07:57:35 -05:00
|
|
|
Ascii(Vec<Vec<u8>>),
|
2016-10-26 09:46:20 -04:00
|
|
|
/// Vector of 16-bit unsigned integers.
|
|
|
|
Short(Vec<u16>),
|
2016-11-17 07:03:34 -05:00
|
|
|
/// Vector of 32-bit unsigned integers.
|
|
|
|
Long(Vec<u32>),
|
2016-11-17 07:14:07 -05:00
|
|
|
/// Vector of unsigned rationals.
|
|
|
|
/// An unsigned rational number is a pair of 32-bit unsigned integers.
|
|
|
|
Rational(Vec<Rational>),
|
2016-11-17 07:32:17 -05:00
|
|
|
/// Vector of 8-bit signed integers. Unused in the Exif specification.
|
|
|
|
SByte(Vec<i8>),
|
2016-11-19 06:10:12 -05:00
|
|
|
/// Slice of 8-bit bytes.
|
2017-08-04 08:15:19 -04:00
|
|
|
///
|
|
|
|
/// The second member keeps the offset of the value in the Exif data.
|
|
|
|
/// The interpretation of the value does not generally depend on
|
|
|
|
/// the location, but if it does, the offset information helps.
|
|
|
|
/// When encoding Exif, it is ignored.
|
2019-12-14 07:57:35 -05:00
|
|
|
Undefined(Vec<u8>, u32),
|
2016-11-19 06:16:49 -05:00
|
|
|
/// Vector of 16-bit signed integers. Unused in the Exif specification.
|
|
|
|
SShort(Vec<i16>),
|
2016-11-19 06:21:08 -05:00
|
|
|
/// Vector of 32-bit signed integers.
|
|
|
|
SLong(Vec<i32>),
|
2016-11-19 06:28:17 -05:00
|
|
|
/// Vector of signed rationals.
|
|
|
|
/// A signed rational number is a pair of 32-bit signed integers.
|
|
|
|
SRational(Vec<SRational>),
|
2016-11-19 06:43:41 -05:00
|
|
|
/// Vector of 32-bit (single precision) floating-point numbers.
|
|
|
|
/// Unused in the Exif specification.
|
|
|
|
Float(Vec<f32>),
|
2016-11-19 06:46:49 -05:00
|
|
|
/// Vector of 64-bit (double precision) floating-point numbers.
|
|
|
|
/// Unused in the Exif specification.
|
|
|
|
Double(Vec<f64>),
|
2016-10-26 09:46:20 -04:00
|
|
|
/// The type is unknown to this implementation.
|
2016-12-30 09:19:02 -05:00
|
|
|
/// The associated values are the type, the count, and the
|
2016-10-26 09:46:20 -04:00
|
|
|
/// offset of the "Value Offset" element.
|
|
|
|
Unknown(u16, u32, u32),
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
impl Value {
|
2017-05-11 10:19:16 -04:00
|
|
|
/// Returns an object that implements `std::fmt::Display` for
|
|
|
|
/// printing a value in a tag-specific format.
|
|
|
|
/// The tag of the value is specified as the argument.
|
2017-06-17 08:57:38 -04:00
|
|
|
///
|
2019-04-07 08:11:14 -04:00
|
|
|
/// If you want to display with the unit, use `Field::display_value`.
|
|
|
|
///
|
2017-06-17 08:57:38 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2017-10-12 09:55:56 -04:00
|
|
|
/// use exif::{Value, Tag};
|
2019-12-14 07:57:35 -05:00
|
|
|
/// let val = Value::Undefined(b"0231".to_vec(), 0);
|
2019-04-09 10:11:05 -04:00
|
|
|
/// assert_eq!(val.display_as(Tag::ExifVersion).to_string(), "2.31");
|
2017-06-17 08:57:38 -04:00
|
|
|
/// let val = Value::Short(vec![2]);
|
2019-04-09 10:11:05 -04:00
|
|
|
/// assert_eq!(val.display_as(Tag::ResolutionUnit).to_string(), "inch");
|
2017-06-17 08:57:38 -04:00
|
|
|
/// ```
|
2017-05-11 10:19:16 -04:00
|
|
|
#[inline]
|
2019-04-01 08:11:54 -04:00
|
|
|
pub fn display_as(&self, tag: crate::tag::Tag) -> Display {
|
|
|
|
crate::tag::display_value_as(self, tag)
|
2017-05-11 10:19:16 -04:00
|
|
|
}
|
2017-06-17 08:47:27 -04:00
|
|
|
|
|
|
|
/// Returns the unsigned integer at the given position.
|
|
|
|
/// None is returned if the value type is not unsigned integer
|
|
|
|
/// (BYTE, SHORT, or LONG) or the position is out of bounds.
|
|
|
|
pub fn get_uint(&self, index: usize) -> Option<u32> {
|
|
|
|
match *self {
|
|
|
|
Value::Byte(ref v) if v.len() > index => Some(v[index] as u32),
|
|
|
|
Value::Short(ref v) if v.len() > index => Some(v[index] as u32),
|
|
|
|
Value::Long(ref v) if v.len() > index => Some(v[index]),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 10:33:14 -04:00
|
|
|
|
|
|
|
/// Returns an iterator over the unsigned integers (BYTE, SHORT, or LONG).
|
|
|
|
/// The iterator yields `u32` regardless of the underlying integer size.
|
|
|
|
/// The returned iterator implements `Iterator` and `ExactSizeIterator`
|
|
|
|
/// traits.
|
|
|
|
/// `None` is returned if the value is not an unsigned integer type.
|
|
|
|
#[inline]
|
|
|
|
pub fn iter_uint(&self) -> Option<UIntIter> {
|
|
|
|
match *self {
|
|
|
|
Value::Byte(ref v) =>
|
|
|
|
Some(UIntIter { iter: Box::new(v.iter().map(|&x| x as u32)) }),
|
|
|
|
Value::Short(ref v) =>
|
|
|
|
Some(UIntIter { iter: Box::new(v.iter().map(|&x| x as u32)) }),
|
|
|
|
Value::Long(ref v) =>
|
|
|
|
Some(UIntIter { iter: Box::new(v.iter().map(|&x| x)) }),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A struct that wraps std::slice::Iter<'a, u8/u16/u32>.
|
|
|
|
pub struct UIntIter<'a> {
|
2019-10-21 10:53:43 -04:00
|
|
|
iter: Box<dyn ExactSizeIterator<Item=u32> + 'a>
|
2017-05-11 10:19:16 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 10:33:14 -04:00
|
|
|
impl<'a> Iterator for UIntIter<'a> {
|
|
|
|
type Item = u32;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<u32> {
|
|
|
|
self.iter.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ExactSizeIterator for UIntIter<'a> {}
|
|
|
|
|
2017-05-11 10:19:16 -04:00
|
|
|
/// Helper struct for printing a value in a tag-specific format.
|
2019-04-07 08:11:14 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2017-05-11 10:19:16 -04:00
|
|
|
pub struct Display<'a> {
|
2019-10-21 10:53:43 -04:00
|
|
|
pub fmt: fn(&mut dyn fmt::Write, &Value) -> fmt::Result,
|
2019-12-14 07:57:35 -05:00
|
|
|
pub value: &'a Value,
|
2017-05-11 10:19:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> fmt::Display for Display<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
(self.fmt)(f, self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 09:46:26 -04:00
|
|
|
// Static default values.
|
|
|
|
pub enum DefaultValue {
|
|
|
|
None,
|
|
|
|
Byte(&'static [u8]),
|
|
|
|
Ascii(&'static [&'static [u8]]),
|
|
|
|
Short(&'static [u16]),
|
|
|
|
Rational(&'static [(u32, u32)]),
|
|
|
|
Undefined(&'static [u8]),
|
|
|
|
// Depends on other tags, JPEG markers, etc.
|
|
|
|
ContextDependent,
|
|
|
|
// Unspecified in the Exif standard.
|
|
|
|
Unspecified,
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
impl From<&DefaultValue> for Option<Value> {
|
2017-03-24 09:46:26 -04:00
|
|
|
fn from(defval: &DefaultValue) -> Option<Value> {
|
|
|
|
match *defval {
|
|
|
|
DefaultValue::None => None,
|
|
|
|
DefaultValue::Byte(s) => Some(Value::Byte(s.to_vec())),
|
2019-12-14 07:57:35 -05:00
|
|
|
DefaultValue::Ascii(s) => Some(Value::Ascii(
|
|
|
|
s.iter().map(|&x| x.to_vec()).collect())),
|
2017-03-24 09:46:26 -04:00
|
|
|
DefaultValue::Short(s) => Some(Value::Short(s.to_vec())),
|
|
|
|
DefaultValue::Rational(s) => Some(Value::Rational(
|
2019-04-12 10:35:28 -04:00
|
|
|
s.iter().map(|&x| x.into()).collect())),
|
2019-12-14 07:57:35 -05:00
|
|
|
DefaultValue::Undefined(s) => Some(Value::Undefined(
|
|
|
|
s.to_vec(), 0)),
|
2017-03-24 09:46:26 -04:00
|
|
|
DefaultValue::ContextDependent => None,
|
|
|
|
DefaultValue::Unspecified => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 07:14:07 -05:00
|
|
|
/// An unsigned rational number, which is a pair of 32-bit unsigned integers.
|
2017-06-14 10:23:53 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2016-11-17 07:14:07 -05:00
|
|
|
pub struct Rational { pub num: u32, pub denom: u32 }
|
|
|
|
|
2017-06-14 10:28:20 -04:00
|
|
|
impl Rational {
|
|
|
|
/// Converts the value to an f64.
|
|
|
|
#[inline]
|
|
|
|
pub fn to_f64(&self) -> f64 {
|
|
|
|
self.num as f64 / self.denom as f64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
impl From<(u32, u32)> for Rational {
|
|
|
|
fn from(t: (u32, u32)) -> Rational {
|
|
|
|
Rational { num: t.0, denom: t.1 }
|
|
|
|
}
|
2017-03-24 09:46:26 -04:00
|
|
|
}
|
|
|
|
|
2016-11-17 07:14:07 -05:00
|
|
|
impl fmt::Debug for Rational {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Rational({}/{})", self.num, self.denom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 07:21:54 -04:00
|
|
|
impl fmt::Display for Rational {
|
|
|
|
/// Formatting parameters other than width are not supported.
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let buf = fmt_rational_sub(f, self.num, self.denom);
|
|
|
|
f.pad_integral(true, "", &buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 10:28:20 -04:00
|
|
|
impl From<Rational> for f64 {
|
|
|
|
#[inline]
|
|
|
|
fn from(r: Rational) -> f64 { r.to_f64() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Rational> for f32 {
|
|
|
|
#[inline]
|
|
|
|
fn from(r: Rational) -> f32 { r.to_f64() as f32 }
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:28:17 -05:00
|
|
|
/// A signed rational number, which is a pair of 32-bit signed integers.
|
2017-06-14 10:23:53 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2016-11-19 06:28:17 -05:00
|
|
|
pub struct SRational { pub num: i32, pub denom: i32 }
|
|
|
|
|
2017-06-14 10:28:20 -04:00
|
|
|
impl SRational {
|
|
|
|
/// Converts the value to an f64.
|
|
|
|
#[inline]
|
|
|
|
pub fn to_f64(&self) -> f64 {
|
|
|
|
self.num as f64 / self.denom as f64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
impl From<(i32, i32)> for SRational {
|
|
|
|
fn from(t: (i32, i32)) -> SRational {
|
|
|
|
SRational { num: t.0, denom: t.1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:28:17 -05:00
|
|
|
impl fmt::Debug for SRational {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "SRational({}/{})", self.num, self.denom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 07:21:54 -04:00
|
|
|
impl fmt::Display for SRational {
|
|
|
|
/// Formatting parameters other than width are not supported.
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let buf = fmt_rational_sub(
|
|
|
|
f, self.num.wrapping_abs() as u32, self.denom);
|
|
|
|
f.pad_integral(self.num >= 0, "", &buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 10:28:20 -04:00
|
|
|
impl From<SRational> for f64 {
|
|
|
|
#[inline]
|
|
|
|
fn from(r: SRational) -> f64 { r.to_f64() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SRational> for f32 {
|
|
|
|
#[inline]
|
|
|
|
fn from(r: SRational) -> f32 { r.to_f64() as f32 }
|
|
|
|
}
|
|
|
|
|
2017-04-08 07:21:54 -04:00
|
|
|
// Only u32 or i32 are expected for T.
|
|
|
|
fn fmt_rational_sub<T>(f: &mut fmt::Formatter, num: u32, denom: T)
|
|
|
|
-> String where T: fmt::Display {
|
|
|
|
// The API to get the alignment is not yet stable as of Rust 1.16,
|
|
|
|
// so it is not fully supported.
|
|
|
|
match (f.sign_plus(), f.precision(), f.sign_aware_zero_pad()) {
|
|
|
|
(true, Some(prec), true) =>
|
|
|
|
format!("{}/{:+0w$}", num, denom, w = prec),
|
|
|
|
(true, Some(prec), false) =>
|
|
|
|
format!("{}/{:+w$}", num, denom, w = prec),
|
|
|
|
(true, None, _) =>
|
|
|
|
format!("{}/{:+}", num, denom),
|
|
|
|
(false, Some(prec), true) =>
|
|
|
|
format!("{}/{:0w$}", num, denom, w = prec),
|
|
|
|
(false, Some(prec), false) =>
|
|
|
|
format!("{}/{:w$}", num, denom, w = prec),
|
|
|
|
(false, None, _) =>
|
|
|
|
format!("{}/{}", num, denom),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
type Parser = fn(&[u8], usize, usize) -> Value;
|
2016-10-26 09:46:20 -04:00
|
|
|
|
|
|
|
// Return the length of a single value and the parser of the type.
|
2019-12-14 07:57:35 -05:00
|
|
|
pub fn get_type_info<E>(typecode: u16) -> (usize, Parser) where E: Endian {
|
2016-10-26 09:46:20 -04:00
|
|
|
match typecode {
|
2016-11-03 06:49:10 -04:00
|
|
|
1 => (1, parse_byte),
|
2016-10-26 09:46:20 -04:00
|
|
|
2 => (1, parse_ascii),
|
|
|
|
3 => (2, parse_short::<E>),
|
2016-11-17 07:03:34 -05:00
|
|
|
4 => (4, parse_long::<E>),
|
2016-11-17 07:14:07 -05:00
|
|
|
5 => (8, parse_rational::<E>),
|
2016-11-17 07:32:17 -05:00
|
|
|
6 => (1, parse_sbyte),
|
2016-11-19 06:10:12 -05:00
|
|
|
7 => (1, parse_undefined),
|
2016-11-19 06:16:49 -05:00
|
|
|
8 => (2, parse_sshort::<E>),
|
2016-11-19 06:21:08 -05:00
|
|
|
9 => (4, parse_slong::<E>),
|
2016-11-19 06:28:17 -05:00
|
|
|
10 => (8, parse_srational::<E>),
|
2016-11-19 06:43:41 -05:00
|
|
|
11 => (4, parse_float::<E>),
|
2016-11-19 06:46:49 -05:00
|
|
|
12 => (8, parse_double::<E>),
|
2016-10-26 09:46:20 -04:00
|
|
|
_ => (0, parse_unknown),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_byte(data: &[u8], offset: usize, count: usize) -> Value {
|
2016-11-03 06:49:10 -04:00
|
|
|
Value::Byte(data[offset .. offset + count].to_vec())
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_ascii(data: &[u8], offset: usize, count: usize) -> Value {
|
2016-10-27 07:48:38 -04:00
|
|
|
// Any ASCII field can contain multiple strings [TIFF6 Image File
|
|
|
|
// Directory].
|
|
|
|
let iter = (&data[offset .. offset + count]).split(|&b| b == b'\0');
|
2019-12-14 07:57:35 -05:00
|
|
|
let mut v: Vec<Vec<u8>> = iter.map(|x| x.to_vec()).collect();
|
|
|
|
if v.last().map_or(false, |x| x.len() == 0) {
|
2016-10-27 07:48:38 -04:00
|
|
|
v.pop();
|
|
|
|
}
|
|
|
|
Value::Ascii(v)
|
2016-10-26 09:46:20 -04:00
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_short<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-16 09:13:29 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
2016-10-26 09:46:20 -04:00
|
|
|
for i in 0..count {
|
|
|
|
val.push(E::loadu16(data, offset + i * 2));
|
|
|
|
}
|
|
|
|
Value::Short(val)
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_long<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-17 07:03:34 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
|
|
|
val.push(E::loadu32(data, offset + i * 4));
|
|
|
|
}
|
|
|
|
Value::Long(val)
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_rational<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-17 07:14:07 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
|
|
|
val.push(Rational {
|
|
|
|
num: E::loadu32(data, offset + i * 8),
|
|
|
|
denom: E::loadu32(data, offset + i * 8 + 4),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Value::Rational(val)
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_sbyte(data: &[u8], offset: usize, count: usize) -> Value {
|
2019-03-16 01:48:16 -04:00
|
|
|
let bytes = data[offset .. offset + count].into_iter()
|
|
|
|
.map(|x| *x as i8)
|
|
|
|
.collect();
|
|
|
|
Value::SByte(bytes)
|
2016-11-17 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_undefined(data: &[u8], offset: usize, count: usize) -> Value {
|
|
|
|
Value::Undefined(data[offset .. offset + count].to_vec(), offset as u32)
|
2016-11-19 06:10:12 -05:00
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_sshort<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-19 06:16:49 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
|
|
|
val.push(E::loadu16(data, offset + i * 2) as i16);
|
|
|
|
}
|
|
|
|
Value::SShort(val)
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_slong<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-19 06:21:08 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
|
|
|
val.push(E::loadu32(data, offset + i * 4) as i32);
|
|
|
|
}
|
|
|
|
Value::SLong(val)
|
|
|
|
}
|
|
|
|
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_srational<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-19 06:28:17 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
|
|
|
val.push(SRational {
|
|
|
|
num: E::loadu32(data, offset + i * 8) as i32,
|
|
|
|
denom: E::loadu32(data, offset + i * 8 + 4) as i32,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Value::SRational(val)
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:43:41 -05:00
|
|
|
// TIFF and Rust use IEEE 754 format, so no conversion is required.
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_float<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-19 06:43:41 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
2018-04-20 09:16:21 -04:00
|
|
|
val.push(f32::from_bits(E::loadu32(data, offset + i * 4)));
|
2016-11-19 06:43:41 -05:00
|
|
|
}
|
|
|
|
Value::Float(val)
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:46:49 -05:00
|
|
|
// TIFF and Rust use IEEE 754 format, so no conversion is required.
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_double<E>(data: &[u8], offset: usize, count: usize)
|
|
|
|
-> Value where E: Endian {
|
2016-11-19 06:46:49 -05:00
|
|
|
let mut val = Vec::with_capacity(count);
|
|
|
|
for i in 0..count {
|
2018-04-20 09:16:21 -04:00
|
|
|
val.push(f64::from_bits(E::loadu64(data, offset + i * 8)));
|
2016-11-19 06:46:49 -05:00
|
|
|
}
|
|
|
|
Value::Double(val)
|
|
|
|
}
|
|
|
|
|
2016-10-26 09:46:20 -04:00
|
|
|
// This is a dummy function and will never be called.
|
|
|
|
#[allow(unused_variables)]
|
2019-12-14 07:57:35 -05:00
|
|
|
fn parse_unknown(data: &[u8], offset: usize, count: usize) -> Value {
|
2016-10-26 09:46:20 -04:00
|
|
|
unreachable!()
|
|
|
|
}
|
2016-10-27 07:53:07 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-04-01 08:11:54 -04:00
|
|
|
use crate::endian::BigEndian;
|
2016-10-27 07:53:07 -04:00
|
|
|
use super::*;
|
|
|
|
|
2016-11-03 06:49:10 -04:00
|
|
|
#[test]
|
|
|
|
fn byte() {
|
|
|
|
let sets: &[(&[u8], &[u8])] = &[
|
|
|
|
(b"x", b""),
|
|
|
|
(b"x\xbe\xad", b"\xbe\xad"),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(1);
|
|
|
|
for &(data, ans) in sets {
|
2016-11-16 09:16:53 -05:00
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
2016-11-03 06:49:10 -04:00
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::Byte(v) => assert_eq!(v, ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 07:53:07 -04:00
|
|
|
#[test]
|
|
|
|
fn ascii() {
|
|
|
|
let sets: &[(&[u8], Vec<&[u8]>)] = &[
|
2017-03-12 06:13:21 -04:00
|
|
|
(b"x", vec![]), // malformed
|
2016-10-27 07:53:07 -04:00
|
|
|
(b"x\0", vec![b""]),
|
|
|
|
(b"x\0\0", vec![b"", b""]),
|
|
|
|
(b"xA", vec![b"A"]), // malformed
|
|
|
|
(b"xA\0", vec![b"A"]),
|
|
|
|
(b"xA\0B", vec![b"A", b"B"]), // malformed
|
|
|
|
(b"xA\0B\0", vec![b"A", b"B"]),
|
|
|
|
(b"xA\0\xbe\0", vec![b"A", b"\xbe"]), // not ASCII
|
|
|
|
];
|
2016-11-04 09:11:57 -04:00
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(2);
|
2016-10-27 07:53:07 -04:00
|
|
|
for &(data, ref ans) in sets {
|
2016-11-04 09:11:57 -04:00
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
2016-10-27 07:53:07 -04:00
|
|
|
Value::Ascii(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 06:52:18 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn short() {
|
|
|
|
let sets: &[(&[u8], Vec<u16>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\x01\x02\x03\x04", vec![0x0102, 0x0304]),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(3);
|
|
|
|
for &(data, ref ans) in sets {
|
2016-11-16 09:16:53 -05:00
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
2016-11-03 06:52:18 -04:00
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::Short(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 07:03:34 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long() {
|
|
|
|
let sets: &[(&[u8], Vec<u32>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\x01\x02\x03\x04\x05\x06\x07\x08",
|
|
|
|
vec![0x01020304, 0x05060708]),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(4);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::Long(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 07:14:07 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rational() {
|
|
|
|
let sets: &[(&[u8], Vec<Rational>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\xa1\x02\x03\x04\x05\x06\x07\x08\
|
|
|
|
\x09\x0a\x0b\x0c\xbd\x0e\x0f\x10",
|
2019-04-12 10:35:28 -04:00
|
|
|
vec![(0xa1020304, 0x05060708).into(),
|
|
|
|
(0x090a0b0c, 0xbd0e0f10).into()]),
|
2016-11-17 07:14:07 -05:00
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(5);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::Rational(v) => {
|
|
|
|
assert_eq!(v.len(), ans.len());
|
|
|
|
for (x, y) in v.iter().zip(ans.iter()) {
|
|
|
|
assert!(x.num == y.num && x.denom == y.denom);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 07:32:17 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sbyte() {
|
|
|
|
let sets: &[(&[u8], &[i8])] = &[
|
|
|
|
(b"x", &[]),
|
|
|
|
(b"x\xbe\x7d", &[-0x42, 0x7d]),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(6);
|
|
|
|
for &(data, ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::SByte(v) => assert_eq!(v, ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 06:10:12 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn undefined() {
|
|
|
|
let sets: &[(&[u8], &[u8])] = &[
|
|
|
|
(b"x", b""),
|
|
|
|
(b"x\xbe\xad", b"\xbe\xad"),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(7);
|
|
|
|
for &(data, ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
2017-08-04 08:15:19 -04:00
|
|
|
Value::Undefined(v, o) => {
|
|
|
|
assert_eq!(v, ans);
|
|
|
|
assert_eq!(o, 1);
|
|
|
|
},
|
2016-11-19 06:10:12 -05:00
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 06:16:49 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sshort() {
|
|
|
|
let sets: &[(&[u8], Vec<i16>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\x01\x02\xf3\x04", vec![0x0102, -0x0cfc]),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(8);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::SShort(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 06:21:08 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn slong() {
|
|
|
|
let sets: &[(&[u8], Vec<i32>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\x01\x02\x03\x04\x85\x06\x07\x08",
|
|
|
|
vec![0x01020304, -0x7af9f8f8]),
|
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(9);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::SLong(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 06:28:17 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn srational() {
|
|
|
|
let sets: &[(&[u8], Vec<SRational>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\xa1\x02\x03\x04\x05\x06\x07\x08\
|
|
|
|
\x09\x0a\x0b\x0c\xbd\x0e\x0f\x10",
|
2019-04-12 10:35:28 -04:00
|
|
|
vec![(-0x5efdfcfc, 0x05060708).into(),
|
|
|
|
(0x090a0b0c, -0x42f1f0f0).into()]),
|
2016-11-19 06:28:17 -05:00
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(10);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::SRational(v) => {
|
|
|
|
assert_eq!(v.len(), ans.len());
|
|
|
|
for (x, y) in v.iter().zip(ans.iter()) {
|
|
|
|
assert!(x.num == y.num && x.denom == y.denom);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 06:43:41 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn float() {
|
|
|
|
let sets: &[(&[u8], Vec<f32>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\x7f\x7f\xff\xff\x80\x80\x00\x00\x40\x00\x00\x00",
|
2019-04-01 08:11:54 -04:00
|
|
|
vec![std::f32::MAX, -std::f32::MIN_POSITIVE, 2.0]),
|
2016-11-19 06:43:41 -05:00
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(11);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::Float(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 06:46:49 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double() {
|
|
|
|
let sets: &[(&[u8], Vec<f64>)] = &[
|
|
|
|
(b"x", vec![]),
|
|
|
|
(b"x\x7f\xef\xff\xff\xff\xff\xff\xff\
|
|
|
|
\x80\x10\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x40\x00\x00\x00\x00\x00\x00\x00",
|
2019-04-01 08:11:54 -04:00
|
|
|
vec![std::f64::MAX, -std::f64::MIN_POSITIVE, 2.0]),
|
2016-11-19 06:46:49 -05:00
|
|
|
];
|
|
|
|
let (unitlen, parser) = get_type_info::<BigEndian>(12);
|
|
|
|
for &(data, ref ans) in sets {
|
|
|
|
assert!((data.len() - 1) % unitlen == 0);
|
|
|
|
match parser(data, 1, (data.len() - 1) / unitlen) {
|
|
|
|
Value::Double(v) => assert_eq!(v, *ans),
|
|
|
|
v => panic!("wrong variant {:?}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 06:16:00 -04:00
|
|
|
|
|
|
|
// These functions are never called in a way that an out-of-range access
|
|
|
|
// could happen, so this test is hypothetical but just for safety.
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "index 5 out of range for slice of length 4")]
|
|
|
|
fn short_oor() {
|
|
|
|
parse_short::<BigEndian>(b"\x01\x02\x03\x04", 1, 2);
|
|
|
|
}
|
2017-03-14 08:53:48 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown() {
|
|
|
|
let (unitlen, _parser) = get_type_info::<BigEndian>(0xffff);
|
|
|
|
assert_eq!(unitlen, 0);
|
|
|
|
}
|
2017-04-08 07:21:54 -04:00
|
|
|
|
2017-06-17 08:47:27 -04:00
|
|
|
#[test]
|
|
|
|
fn get_uint() {
|
|
|
|
let v = Value::Byte(vec![1, 2]);
|
|
|
|
assert_eq!(v.get_uint(0), Some(1));
|
|
|
|
assert_eq!(v.get_uint(1), Some(2));
|
|
|
|
assert_eq!(v.get_uint(2), None);
|
|
|
|
let v = Value::Short(vec![1, 2]);
|
|
|
|
assert_eq!(v.get_uint(0), Some(1));
|
|
|
|
assert_eq!(v.get_uint(1), Some(2));
|
|
|
|
assert_eq!(v.get_uint(2), None);
|
|
|
|
let v = Value::Long(vec![1, 2]);
|
|
|
|
assert_eq!(v.get_uint(0), Some(1));
|
|
|
|
assert_eq!(v.get_uint(1), Some(2));
|
|
|
|
assert_eq!(v.get_uint(2), None);
|
|
|
|
let v = Value::SLong(vec![1, 2]);
|
|
|
|
assert_eq!(v.get_uint(0), None);
|
|
|
|
assert_eq!(v.get_uint(1), None);
|
|
|
|
assert_eq!(v.get_uint(2), None);
|
|
|
|
}
|
|
|
|
|
2017-07-11 10:33:14 -04:00
|
|
|
#[test]
|
|
|
|
fn iter_uint() {
|
|
|
|
let vlist = &[
|
|
|
|
Value::Byte(vec![1, 2]),
|
|
|
|
Value::Short(vec![1, 2]),
|
|
|
|
Value::Long(vec![1, 2]),
|
|
|
|
];
|
|
|
|
for v in vlist {
|
|
|
|
let mut it = v.iter_uint().unwrap();
|
|
|
|
assert_eq!(it.next(), Some(1));
|
|
|
|
assert_eq!(it.next(), Some(2));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = Value::SLong(vec![1, 2]);
|
|
|
|
assert!(v.iter_uint().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iter_uint_is_exact_size_iter() {
|
|
|
|
let v = Value::Byte(vec![1, 2, 3]);
|
|
|
|
let mut it = v.iter_uint().unwrap();
|
|
|
|
assert_eq!(it.len(), 3);
|
|
|
|
assert_eq!(it.next(), Some(1));
|
|
|
|
assert_eq!(it.len(), 2);
|
|
|
|
}
|
|
|
|
|
2017-04-08 07:21:54 -04:00
|
|
|
#[test]
|
|
|
|
fn rational_fmt_display() {
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = Rational::from((u32::max_value(), u32::max_value()));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "4294967295/4294967295");
|
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = Rational::from((10, 20));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "10/20");
|
|
|
|
assert_eq!(format!("{:11}", r), " 10/20");
|
|
|
|
assert_eq!(format!("{:3}", r), "10/20");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn srational_fmt_display() {
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = SRational::from((i32::min_value(), i32::min_value()));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "-2147483648/-2147483648");
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = SRational::from((i32::max_value(), i32::max_value()));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "2147483647/2147483647");
|
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = SRational::from((-10, 20));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "-10/20");
|
|
|
|
assert_eq!(format!("{:11}", r), " -10/20");
|
|
|
|
assert_eq!(format!("{:3}", r), "-10/20");
|
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = SRational::from((10, -20));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "10/-20");
|
|
|
|
assert_eq!(format!("{:11}", r), " 10/-20");
|
|
|
|
assert_eq!(format!("{:3}", r), "10/-20");
|
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
let r = SRational::from((-10, -20));
|
2017-04-08 07:21:54 -04:00
|
|
|
assert_eq!(format!("{}", r), "-10/-20");
|
|
|
|
assert_eq!(format!("{:11}", r), " -10/-20");
|
|
|
|
assert_eq!(format!("{:3}", r), "-10/-20");
|
|
|
|
}
|
2017-06-14 10:28:20 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ratioanl_f64() {
|
|
|
|
use std::{f64, u32};
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f64::from(Rational::from((1, 2))), 0.5);
|
|
|
|
assert_eq!(f64::from(Rational::from((1, u32::MAX))),
|
2017-06-14 10:28:20 -04:00
|
|
|
2.3283064370807974e-10);
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f64::from(Rational::from((u32::MAX, 1))),
|
2017-06-14 10:28:20 -04:00
|
|
|
u32::MAX as f64);
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f64::from(Rational::from((u32::MAX - 1, u32::MAX))),
|
2017-06-14 10:28:20 -04:00
|
|
|
0.9999999997671694);
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f64::from(Rational::from((u32::MAX, u32::MAX - 1))),
|
2017-06-14 10:28:20 -04:00
|
|
|
1.0000000002328306);
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f64::from(Rational::from((1, 0))), f64::INFINITY);
|
|
|
|
assert!(f64::from(Rational::from((0, 0))).is_nan());
|
2017-06-14 10:28:20 -04:00
|
|
|
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f64::from(SRational::from((1, 2))), 0.5);
|
|
|
|
assert_eq!(f64::from(SRational::from((-1, 2))), -0.5);
|
|
|
|
assert_eq!(f64::from(SRational::from((1, -2))), -0.5);
|
|
|
|
assert_eq!(f64::from(SRational::from((-1, -2))), 0.5);
|
|
|
|
assert_eq!(f64::from(SRational::from((1, 0))), f64::INFINITY);
|
|
|
|
assert_eq!(f64::from(SRational::from((-1, 0))), f64::NEG_INFINITY);
|
2017-06-14 10:28:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rational_f32() {
|
|
|
|
// If num and demon are converted to f32 before the division,
|
|
|
|
// the precision is lost in this example.
|
2019-04-12 10:35:28 -04:00
|
|
|
assert_eq!(f32::from(Rational::from((1, 16777217))), 5.960464e-8);
|
2017-06-14 10:28:20 -04:00
|
|
|
}
|
2016-10-27 07:53:07 -04:00
|
|
|
}
|