diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/num-derive/tests | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/num-derive/tests')
-rw-r--r-- | third_party/rust/num-derive/tests/empty_enum.rs | 23 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/issue-6.rs | 17 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/issue-9.rs | 18 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/newtype-2015.rs | 2 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/newtype.rs | 89 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/num_derive_without_num.rs | 20 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/trivial-2015.rs | 2 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/trivial.rs | 64 | ||||
-rw-r--r-- | third_party/rust/num-derive/tests/with_custom_values.rs | 70 |
9 files changed, 305 insertions, 0 deletions
diff --git a/third_party/rust/num-derive/tests/empty_enum.rs b/third_party/rust/num-derive/tests/empty_enum.rs new file mode 100644 index 0000000000..173996c8ed --- /dev/null +++ b/third_party/rust/num-derive/tests/empty_enum.rs @@ -0,0 +1,23 @@ +// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate num as num_renamed; +#[macro_use] +extern crate num_derive; + +#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] +enum Color {} + +#[test] +fn test_empty_enum() { + let v: [Option<Color>; 1] = [num_renamed::FromPrimitive::from_u64(0)]; + + assert_eq!(v, [None]); +} diff --git a/third_party/rust/num-derive/tests/issue-6.rs b/third_party/rust/num-derive/tests/issue-6.rs new file mode 100644 index 0000000000..9eae7fb3f5 --- /dev/null +++ b/third_party/rust/num-derive/tests/issue-6.rs @@ -0,0 +1,17 @@ +#![deny(trivial_numeric_casts)] + +#[macro_use] +extern crate num_derive; + +#[derive(FromPrimitive, ToPrimitive)] +pub enum SomeEnum { + A = 1, +} + +#[test] +fn test_trivial_numeric_casts() { + use num::{FromPrimitive, ToPrimitive}; + assert!(SomeEnum::from_u64(1).is_some()); + assert!(SomeEnum::from_i64(-1).is_none()); + assert_eq!(SomeEnum::A.to_u64(), Some(1)); +} diff --git a/third_party/rust/num-derive/tests/issue-9.rs b/third_party/rust/num-derive/tests/issue-9.rs new file mode 100644 index 0000000000..30c04d2f46 --- /dev/null +++ b/third_party/rust/num-derive/tests/issue-9.rs @@ -0,0 +1,18 @@ +#![deny(unused_qualifications)] + +#[macro_use] +extern crate num_derive; +use num::FromPrimitive; +use num::ToPrimitive; + +#[derive(FromPrimitive, ToPrimitive)] +pub enum SomeEnum { + A = 1, +} + +#[test] +fn test_unused_qualifications() { + assert!(SomeEnum::from_u64(1).is_some()); + assert!(SomeEnum::from_i64(-1).is_none()); + assert!(SomeEnum::A.to_i64().is_some()); +} diff --git a/third_party/rust/num-derive/tests/newtype-2015.rs b/third_party/rust/num-derive/tests/newtype-2015.rs new file mode 100644 index 0000000000..2b581907ec --- /dev/null +++ b/third_party/rust/num-derive/tests/newtype-2015.rs @@ -0,0 +1,2 @@ +// Same source, just compiled for 2015 edition +include!("newtype.rs"); diff --git a/third_party/rust/num-derive/tests/newtype.rs b/third_party/rust/num-derive/tests/newtype.rs new file mode 100644 index 0000000000..6eafd0960b --- /dev/null +++ b/third_party/rust/num-derive/tests/newtype.rs @@ -0,0 +1,89 @@ +extern crate num as num_renamed; +#[macro_use] +extern crate num_derive; + +use crate::num_renamed::{Float, FromPrimitive, Num, NumCast, One, ToPrimitive, Zero}; +use std::ops::Neg; + +#[derive( + Debug, + Clone, + Copy, + PartialEq, + PartialOrd, + ToPrimitive, + FromPrimitive, + NumOps, + NumCast, + One, + Zero, + Num, + Float, +)] +struct MyFloat(f64); + +impl Neg for MyFloat { + type Output = MyFloat; + fn neg(self) -> Self { + MyFloat(self.0.neg()) + } +} + +#[test] +fn test_from_primitive() { + assert_eq!(MyFloat::from_u32(25), Some(MyFloat(25.0))); +} + +#[test] +fn test_from_primitive_128() { + assert_eq!( + MyFloat::from_i128(std::i128::MIN), + Some(MyFloat((-2.0).powi(127))) + ); +} + +#[test] +fn test_to_primitive() { + assert_eq!(MyFloat(25.0).to_u32(), Some(25)); +} + +#[test] +fn test_to_primitive_128() { + let f = MyFloat::from_f32(std::f32::MAX).unwrap(); + assert_eq!(f.to_i128(), None); + assert_eq!(f.to_u128(), Some(0xffff_ff00_0000_0000_0000_0000_0000_0000)); +} + +#[test] +fn test_num_ops() { + assert_eq!(MyFloat(25.0) + MyFloat(10.0), MyFloat(35.0)); + assert_eq!(MyFloat(25.0) - MyFloat(10.0), MyFloat(15.0)); + assert_eq!(MyFloat(25.0) * MyFloat(2.0), MyFloat(50.0)); + assert_eq!(MyFloat(25.0) / MyFloat(10.0), MyFloat(2.5)); + assert_eq!(MyFloat(25.0) % MyFloat(10.0), MyFloat(5.0)); +} + +#[test] +fn test_num_cast() { + assert_eq!(<MyFloat as NumCast>::from(25u8), Some(MyFloat(25.0))); +} + +#[test] +fn test_zero() { + assert_eq!(MyFloat::zero(), MyFloat(0.0)); +} + +#[test] +fn test_one() { + assert_eq!(MyFloat::one(), MyFloat(1.0)); +} + +#[test] +fn test_num() { + assert_eq!(MyFloat::from_str_radix("25", 10).ok(), Some(MyFloat(25.0))); +} + +#[test] +fn test_float() { + assert_eq!(MyFloat(4.0).log(MyFloat(2.0)), MyFloat(2.0)); +} diff --git a/third_party/rust/num-derive/tests/num_derive_without_num.rs b/third_party/rust/num-derive/tests/num_derive_without_num.rs new file mode 100644 index 0000000000..edebbecbfa --- /dev/null +++ b/third_party/rust/num-derive/tests/num_derive_without_num.rs @@ -0,0 +1,20 @@ +// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[macro_use] +extern crate num_derive; + +#[derive(Debug, FromPrimitive, ToPrimitive)] +enum Direction { + Up, + Down, + Left, + Right, +} diff --git a/third_party/rust/num-derive/tests/trivial-2015.rs b/third_party/rust/num-derive/tests/trivial-2015.rs new file mode 100644 index 0000000000..8309f0e889 --- /dev/null +++ b/third_party/rust/num-derive/tests/trivial-2015.rs @@ -0,0 +1,2 @@ +// Same source, just compiled for 2015 edition +include!("trivial.rs"); diff --git a/third_party/rust/num-derive/tests/trivial.rs b/third_party/rust/num-derive/tests/trivial.rs new file mode 100644 index 0000000000..d3b56b6a25 --- /dev/null +++ b/third_party/rust/num-derive/tests/trivial.rs @@ -0,0 +1,64 @@ +// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate num as num_renamed; +#[macro_use] +extern crate num_derive; + +#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] +enum Color { + Red, + Blue, + Green, +} + +#[test] +fn test_from_primitive_for_trivial_case() { + let v: [Option<Color>; 4] = [ + num_renamed::FromPrimitive::from_u64(0), + num_renamed::FromPrimitive::from_u64(1), + num_renamed::FromPrimitive::from_u64(2), + num_renamed::FromPrimitive::from_u64(3), + ]; + + assert_eq!( + v, + [ + Some(Color::Red), + Some(Color::Blue), + Some(Color::Green), + None + ] + ); +} + +#[test] +fn test_to_primitive_for_trivial_case() { + let v: [Option<u64>; 3] = [ + num_renamed::ToPrimitive::to_u64(&Color::Red), + num_renamed::ToPrimitive::to_u64(&Color::Blue), + num_renamed::ToPrimitive::to_u64(&Color::Green), + ]; + + assert_eq!(v, [Some(0), Some(1), Some(2)]); +} + +#[test] +fn test_reflexive_for_trivial_case() { + let before: [u64; 3] = [0, 1, 2]; + let after: Vec<Option<u64>> = before + .iter() + .map(|&x| -> Option<Color> { num_renamed::FromPrimitive::from_u64(x) }) + .map(|x| x.and_then(|x| num_renamed::ToPrimitive::to_u64(&x))) + .collect(); + let before = before.iter().cloned().map(Some).collect::<Vec<_>>(); + + assert_eq!(before, after); +} diff --git a/third_party/rust/num-derive/tests/with_custom_values.rs b/third_party/rust/num-derive/tests/with_custom_values.rs new file mode 100644 index 0000000000..94c84458be --- /dev/null +++ b/third_party/rust/num-derive/tests/with_custom_values.rs @@ -0,0 +1,70 @@ +// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![cfg(feature = "full-syntax")] + +extern crate num as num_renamed; +#[macro_use] +extern crate num_derive; + +#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] +enum Color { + Red, + Blue = 5, + Green, + Alpha = (-3 - (-5isize)) - 10, +} + +#[test] +fn test_from_primitive_for_enum_with_custom_value() { + let v: [Option<Color>; 5] = [ + num_renamed::FromPrimitive::from_u64(0), + num_renamed::FromPrimitive::from_u64(5), + num_renamed::FromPrimitive::from_u64(6), + num_renamed::FromPrimitive::from_u64(-8isize as u64), + num_renamed::FromPrimitive::from_u64(3), + ]; + + assert_eq!( + v, + [ + Some(Color::Red), + Some(Color::Blue), + Some(Color::Green), + Some(Color::Alpha), + None + ] + ); +} + +#[test] +fn test_to_primitive_for_enum_with_custom_value() { + let v: [Option<u64>; 4] = [ + num_renamed::ToPrimitive::to_u64(&Color::Red), + num_renamed::ToPrimitive::to_u64(&Color::Blue), + num_renamed::ToPrimitive::to_u64(&Color::Green), + num_renamed::ToPrimitive::to_u64(&Color::Alpha), + ]; + + assert_eq!(v, [Some(0), Some(5), Some(6), Some(-8isize as u64)]); +} + +#[test] +fn test_reflexive_for_enum_with_custom_value() { + let before: [u64; 3] = [0, 5, 6]; + let after: Vec<Option<u64>> = before + .iter() + .map(|&x| -> Option<Color> { num_renamed::FromPrimitive::from_u64(x) }) + .map(|x| x.and_then(|x| num_renamed::ToPrimitive::to_u64(&x))) + .collect(); + let before = before.iter().cloned().map(Some).collect::<Vec<_>>(); + + assert_eq!(before, after); +} |