aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJoel Galenson <jgalenson@google.com>2020-08-05 11:50:19 -0700
committerJoel Galenson <jgalenson@google.com>2020-08-05 11:50:52 -0700
commit75ab7e10d2ade06072e920d5824c012961565611 (patch)
tree3f574d32667e1823310c92cdacce52b4fa0cad6b /tests
parent15a8a05fb76ee97c116096255a40bdfc3af410b6 (diff)
downloadnum-derive-75ab7e10d2ade06072e920d5824c012961565611.tar.gz
Import num-derive-0.3.0
Test: None Change-Id: Ieefcea803e41ca38f563ee8e4c1ffde303426c97
Diffstat (limited to 'tests')
-rw-r--r--tests/empty_enum.rs23
-rw-r--r--tests/issue-6.rs17
-rw-r--r--tests/issue-9.rs18
-rw-r--r--tests/newtype-2015.rs2
-rw-r--r--tests/newtype.rs89
-rw-r--r--tests/num_derive_without_num.rs20
-rw-r--r--tests/trivial-2015.rs2
-rw-r--r--tests/trivial.rs64
-rw-r--r--tests/with_custom_values.rs70
9 files changed, 305 insertions, 0 deletions
diff --git a/tests/empty_enum.rs b/tests/empty_enum.rs
new file mode 100644
index 0000000..173996c
--- /dev/null
+++ b/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/tests/issue-6.rs b/tests/issue-6.rs
new file mode 100644
index 0000000..9eae7fb
--- /dev/null
+++ b/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/tests/issue-9.rs b/tests/issue-9.rs
new file mode 100644
index 0000000..30c04d2
--- /dev/null
+++ b/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/tests/newtype-2015.rs b/tests/newtype-2015.rs
new file mode 100644
index 0000000..2b58190
--- /dev/null
+++ b/tests/newtype-2015.rs
@@ -0,0 +1,2 @@
+// Same source, just compiled for 2015 edition
+include!("newtype.rs");
diff --git a/tests/newtype.rs b/tests/newtype.rs
new file mode 100644
index 0000000..6eafd09
--- /dev/null
+++ b/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/tests/num_derive_without_num.rs b/tests/num_derive_without_num.rs
new file mode 100644
index 0000000..edebbec
--- /dev/null
+++ b/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/tests/trivial-2015.rs b/tests/trivial-2015.rs
new file mode 100644
index 0000000..8309f0e
--- /dev/null
+++ b/tests/trivial-2015.rs
@@ -0,0 +1,2 @@
+// Same source, just compiled for 2015 edition
+include!("trivial.rs");
diff --git a/tests/trivial.rs b/tests/trivial.rs
new file mode 100644
index 0000000..d3b56b6
--- /dev/null
+++ b/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/tests/with_custom_values.rs b/tests/with_custom_values.rs
new file mode 100644
index 0000000..94c8445
--- /dev/null
+++ b/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);
+}