1use alloc::string::ToString;
2use core::cmp::Ordering;
3use core::fmt::{self, Write};
4use core::ops::{
5 Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
6};
7use core::str::FromStr;
8use serde::{de, ser, Deserialize, Deserializer, Serialize};
9
10use crate::errors::{
11 CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, ErrorKind, OverflowError,
12 OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
13};
14use crate::forward_ref::{forward_ref_binop, forward_ref_op_assign};
15use crate::{Decimal, Decimal256, Int512, SignedDecimal, __internal::forward_ref_partial_eq};
16
17use super::Fraction;
18use super::Int256;
19
20#[derive(
30 Copy,
31 Clone,
32 Default,
33 PartialEq,
34 Eq,
35 PartialOrd,
36 Ord,
37 schemars::JsonSchema,
38 cw_schema::Schemaifier,
39)]
40#[schemaifier(type = cw_schema::NodeType::Decimal { precision: 256, signed: true })]
41pub struct SignedDecimal256(#[schemars(with = "String")] Int256);
42
43forward_ref_partial_eq!(SignedDecimal256, SignedDecimal256);
44
45#[derive(Debug, PartialEq, Eq, thiserror::Error)]
46#[error("SignedDecimal256 range exceeded")]
47pub struct SignedDecimal256RangeExceeded;
48
49impl SignedDecimal256 {
50 const DECIMAL_FRACTIONAL: Int256 = Int256::new(1_000_000_000_000_000_000);
52 const DECIMAL_FRACTIONAL_SQUARED: Int256 = Int256::new(1_000_000_000_000_000_000_000_000_000_000_000_000);
54
55 pub const DECIMAL_PLACES: u32 = 18; pub const MAX: Self = Self(Int256::MAX);
71
72 pub const MIN: Self = Self(Int256::MIN);
84
85 #[inline]
99 #[must_use]
100 pub const fn new(value: Int256) -> Self {
101 Self(value)
102 }
103
104 #[deprecated(
114 since = "3.0.0",
115 note = "Use SignedDecimal256::new(Int256::new(value)) instead"
116 )]
117 pub const fn raw(value: i128) -> Self {
118 Self(Int256::new(value))
119 }
120
121 #[inline]
123 pub const fn one() -> Self {
124 Self(Self::DECIMAL_FRACTIONAL)
125 }
126
127 #[inline]
129 pub const fn negative_one() -> Self {
130 Self(Int256::new(-1_000_000_000_000_000_000))
132 }
133
134 #[inline]
136 pub const fn zero() -> Self {
137 Self(Int256::zero())
138 }
139
140 pub fn percent(x: i64) -> Self {
142 Self(((x as i128) * 10_000_000_000_000_000).into())
143 }
144
145 pub fn permille(x: i64) -> Self {
147 Self(((x as i128) * 1_000_000_000_000_000).into())
148 }
149
150 pub fn bps(x: i64) -> Self {
152 Self(((x as i128) * 100_000_000_000_000).into())
153 }
154
155 pub fn from_atomics(
180 atomics: impl Into<Int256>,
181 decimal_places: u32,
182 ) -> Result<Self, SignedDecimal256RangeExceeded> {
183 let atomics = atomics.into();
184 let ten = Int256::from(10u64);
185 Ok(match decimal_places.cmp(&(Self::DECIMAL_PLACES)) {
186 Ordering::Less => {
187 let digits = (Self::DECIMAL_PLACES) - decimal_places; let factor = ten.checked_pow(digits).unwrap(); Self(
190 atomics
191 .checked_mul(factor)
192 .map_err(|_| SignedDecimal256RangeExceeded)?,
193 )
194 }
195 Ordering::Equal => Self(atomics),
196 Ordering::Greater => {
197 let digits = decimal_places - (Self::DECIMAL_PLACES); if let Ok(factor) = ten.checked_pow(digits) {
199 Self(atomics.checked_div(factor).unwrap()) } else {
201 Self(Int256::zero())
205 }
206 }
207 })
208 }
209
210 pub fn from_ratio(numerator: impl Into<Int256>, denominator: impl Into<Int256>) -> Self {
222 match SignedDecimal256::checked_from_ratio(numerator, denominator) {
223 Ok(value) => value,
224 Err(CheckedFromRatioError::DivideByZero) => {
225 panic!("Denominator must not be zero")
226 }
227 Err(CheckedFromRatioError::Overflow) => panic!("Multiplication overflow"),
228 }
229 }
230
231 pub fn checked_from_ratio(
247 numerator: impl Into<Int256>,
248 denominator: impl Into<Int256>,
249 ) -> Result<Self, CheckedFromRatioError> {
250 let numerator: Int256 = numerator.into();
251 let denominator: Int256 = denominator.into();
252 match numerator.checked_multiply_ratio(Self::DECIMAL_FRACTIONAL, denominator) {
253 Ok(ratio) => {
254 Ok(SignedDecimal256(ratio))
256 }
257 Err(CheckedMultiplyRatioError::Overflow) => Err(CheckedFromRatioError::Overflow),
258 Err(CheckedMultiplyRatioError::DivideByZero) => {
259 Err(CheckedFromRatioError::DivideByZero)
260 }
261 }
262 }
263
264 #[must_use]
266 pub const fn is_zero(&self) -> bool {
267 self.0.is_zero()
268 }
269
270 #[must_use]
272 pub const fn is_negative(&self) -> bool {
273 self.0.is_negative()
274 }
275
276 #[must_use]
295 #[inline]
296 pub const fn atomics(&self) -> Int256 {
297 self.0
298 }
299
300 #[must_use]
305 #[inline]
306 pub const fn decimal_places(&self) -> u32 {
307 Self::DECIMAL_PLACES
308 }
309
310 #[must_use = "this returns the result of the operation, without modifying the original"]
321 pub fn trunc(&self) -> Self {
322 Self((self.0 / Self::DECIMAL_FRACTIONAL) * Self::DECIMAL_FRACTIONAL)
323 }
324
325 #[must_use = "this returns the result of the operation, without modifying the original"]
336 pub fn floor(&self) -> Self {
337 match self.checked_floor() {
338 Ok(value) => value,
339 Err(_) => panic!("attempt to floor with overflow"),
340 }
341 }
342
343 pub fn checked_floor(&self) -> Result<Self, RoundDownOverflowError> {
345 if self.is_negative() {
346 let truncated = self.trunc();
347
348 if truncated != self {
349 truncated
350 .checked_sub(SignedDecimal256::one())
351 .map_err(|_| RoundDownOverflowError)
352 } else {
353 Ok(truncated)
354 }
355 } else {
356 Ok(self.trunc())
357 }
358 }
359
360 #[must_use = "this returns the result of the operation, without modifying the original"]
371 pub fn ceil(&self) -> Self {
372 match self.checked_ceil() {
373 Ok(value) => value,
374 Err(_) => panic!("attempt to ceil with overflow"),
375 }
376 }
377
378 pub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError> {
380 let floor = self.floor();
381 if floor == self {
382 Ok(floor)
383 } else {
384 floor
385 .checked_add(SignedDecimal256::one())
386 .map_err(|_| RoundUpOverflowError)
387 }
388 }
389
390 pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
392 self.0
393 .checked_add(other.0)
394 .map(Self)
395 .map_err(|_| OverflowError::new(OverflowOperation::Add))
396 }
397
398 pub fn checked_sub(self, other: Self) -> Result<Self, OverflowError> {
400 self.0
401 .checked_sub(other.0)
402 .map(Self)
403 .map_err(|_| OverflowError::new(OverflowOperation::Sub))
404 }
405
406 pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError> {
408 let result_as_int512 =
409 self.numerator().full_mul(other.numerator()) / Int512::from(Self::DECIMAL_FRACTIONAL);
410 result_as_int512
411 .try_into()
412 .map(Self)
413 .map_err(|_| OverflowError::new(OverflowOperation::Mul))
414 }
415
416 #[must_use = "this returns the result of the operation, without modifying the original"]
418 pub fn pow(self, exp: u32) -> Self {
419 match self.checked_pow(exp) {
420 Ok(value) => value,
421 Err(_) => panic!("Multiplication overflow"),
422 }
423 }
424
425 pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError> {
427 fn inner(mut x: SignedDecimal256, mut n: u32) -> Result<SignedDecimal256, OverflowError> {
431 if n == 0 {
432 return Ok(SignedDecimal256::one());
433 }
434
435 let mut y = SignedDecimal256::one();
436
437 while n > 1 {
438 if n % 2 == 0 {
439 x = x.checked_mul(x)?;
440 n /= 2;
441 } else {
442 y = x.checked_mul(y)?;
443 x = x.checked_mul(x)?;
444 n = (n - 1) / 2;
445 }
446 }
447
448 Ok(x * y)
449 }
450
451 inner(self, exp).map_err(|_| OverflowError::new(OverflowOperation::Pow))
452 }
453
454 pub fn checked_div(self, other: Self) -> Result<Self, CheckedFromRatioError> {
455 SignedDecimal256::checked_from_ratio(self.numerator(), other.numerator())
456 }
457
458 pub fn checked_rem(self, other: Self) -> Result<Self, DivideByZeroError> {
460 self.0
461 .checked_rem(other.0)
462 .map(Self)
463 .map_err(|_| DivideByZeroError)
464 }
465
466 #[must_use = "this returns the result of the operation, without modifying the original"]
467 pub const fn abs_diff(self, other: Self) -> Decimal256 {
468 Decimal256::new(self.0.abs_diff(other.0))
469 }
470
471 #[must_use = "this returns the result of the operation, without modifying the original"]
472 pub fn saturating_add(self, other: Self) -> Self {
473 Self(self.0.saturating_add(other.0))
474 }
475
476 #[must_use = "this returns the result of the operation, without modifying the original"]
477 pub fn saturating_sub(self, other: Self) -> Self {
478 Self(self.0.saturating_sub(other.0))
479 }
480
481 #[must_use = "this returns the result of the operation, without modifying the original"]
482 pub fn saturating_mul(self, other: Self) -> Self {
483 match self.checked_mul(other) {
484 Ok(value) => value,
485 Err(_) => {
486 if self.is_negative() == other.is_negative() {
488 Self::MAX
489 } else {
490 Self::MIN
491 }
492 }
493 }
494 }
495
496 #[must_use = "this returns the result of the operation, without modifying the original"]
497 pub fn saturating_pow(self, exp: u32) -> Self {
498 match self.checked_pow(exp) {
499 Ok(value) => value,
500 Err(_) => {
501 if self.is_negative() && exp % 2 == 1 {
504 Self::MIN
505 } else {
506 Self::MAX
507 }
508 }
509 }
510 }
511
512 #[must_use = "this returns the result of the operation, without modifying the original"]
531 pub fn to_int_floor(self) -> Int256 {
532 if self.is_negative() {
533 let x = self.0;
536 let y = Self::DECIMAL_FRACTIONAL;
537 -Int256::one() - ((-Int256::one() - x) / y)
539 } else {
540 self.to_int_trunc()
541 }
542 }
543
544 #[must_use = "this returns the result of the operation, without modifying the original"]
563 pub fn to_int_trunc(self) -> Int256 {
564 self.0 / Self::DECIMAL_FRACTIONAL
565 }
566
567 #[must_use = "this returns the result of the operation, without modifying the original"]
586 pub fn to_int_ceil(self) -> Int256 {
587 if self.is_negative() {
588 self.to_int_trunc()
589 } else {
590 let x = self.0;
593 let y = Self::DECIMAL_FRACTIONAL;
594 if x.is_zero() {
595 Int256::zero()
596 } else {
597 Int256::one() + ((x - Int256::one()) / y)
598 }
599 }
600 }
601}
602
603impl Fraction<Int256> for SignedDecimal256 {
604 #[inline]
605 fn numerator(&self) -> Int256 {
606 self.0
607 }
608
609 #[inline]
610 fn denominator(&self) -> Int256 {
611 Self::DECIMAL_FRACTIONAL
612 }
613
614 fn inv(&self) -> Option<Self> {
618 if self.is_zero() {
619 None
620 } else {
621 Some(SignedDecimal256(Self::DECIMAL_FRACTIONAL_SQUARED / self.0))
625 }
626 }
627}
628
629impl Neg for SignedDecimal256 {
630 type Output = Self;
631
632 fn neg(self) -> Self::Output {
633 Self(-self.0)
634 }
635}
636
637impl From<SignedDecimal> for SignedDecimal256 {
638 fn from(value: SignedDecimal) -> Self {
639 Self::new(value.atomics().into())
640 }
641}
642
643impl From<Decimal> for SignedDecimal256 {
644 fn from(value: Decimal) -> Self {
645 Self::new(value.atomics().into())
646 }
647}
648
649impl TryFrom<Decimal256> for SignedDecimal256 {
650 type Error = SignedDecimal256RangeExceeded;
651
652 fn try_from(value: Decimal256) -> Result<Self, Self::Error> {
653 value
654 .atomics()
655 .try_into()
656 .map(SignedDecimal256)
657 .map_err(|_| SignedDecimal256RangeExceeded)
658 }
659}
660
661impl TryFrom<Int256> for SignedDecimal256 {
662 type Error = SignedDecimal256RangeExceeded;
663
664 #[inline]
665 fn try_from(value: Int256) -> Result<Self, Self::Error> {
666 Self::from_atomics(value, 0)
667 }
668}
669
670impl FromStr for SignedDecimal256 {
671 type Err = StdError;
672
673 fn from_str(input: &str) -> Result<Self, Self::Err> {
680 let mut parts_iter = input.split('.');
681
682 let whole_part = parts_iter.next().unwrap(); let is_neg = whole_part.starts_with('-');
684
685 let whole = whole_part.parse::<Int256>()?;
686 let mut atomics = whole.checked_mul(Self::DECIMAL_FRACTIONAL)?;
687
688 if let Some(fractional_part) = parts_iter.next() {
689 let fractional = fractional_part.parse::<u64>()?; let exp = (Self::DECIMAL_PLACES.checked_sub(fractional_part.len() as u32)).ok_or_else(
691 || {
692 StdError::msg(format_args!(
693 "Cannot parse more than {} fractional digits",
694 Self::DECIMAL_PLACES
695 ))
696 },
697 )?;
698 debug_assert!(exp <= Self::DECIMAL_PLACES);
699 let fractional_factor = Int256::from(10i128.pow(exp));
700
701 let fractional_part = Int256::from(fractional)
704 .checked_mul(fractional_factor)
705 .unwrap();
706
707 atomics = if is_neg {
709 atomics.checked_sub(fractional_part)
710 } else {
711 atomics.checked_add(fractional_part)
712 }?;
713 }
714
715 if parts_iter.next().is_some() {
716 return Err(StdError::msg("Unexpected number of dots").with_kind(ErrorKind::Parsing));
717 }
718
719 Ok(SignedDecimal256(atomics))
720 }
721}
722
723impl fmt::Display for SignedDecimal256 {
724 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
725 let whole = (self.0) / Self::DECIMAL_FRACTIONAL;
726 let fractional = (self.0).checked_rem(Self::DECIMAL_FRACTIONAL).unwrap();
727
728 if fractional.is_zero() {
729 write!(f, "{whole}")
730 } else {
731 let fractional_string = format!(
732 "{:0>padding$}",
733 fractional.abs(), padding = Self::DECIMAL_PLACES as usize
735 );
736 if self.is_negative() {
737 f.write_char('-')?;
738 }
739 write!(
740 f,
741 "{whole}.{fractional}",
742 whole = whole.abs(),
743 fractional = fractional_string.trim_end_matches('0')
744 )
745 }
746 }
747}
748
749impl fmt::Debug for SignedDecimal256 {
750 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
751 write!(f, "SignedDecimal256({self})")
752 }
753}
754
755impl Add for SignedDecimal256 {
756 type Output = Self;
757
758 fn add(self, other: Self) -> Self {
759 SignedDecimal256(self.0 + other.0)
760 }
761}
762forward_ref_binop!(impl Add, add for SignedDecimal256, SignedDecimal256);
763
764impl AddAssign for SignedDecimal256 {
765 fn add_assign(&mut self, rhs: SignedDecimal256) {
766 *self = *self + rhs;
767 }
768}
769forward_ref_op_assign!(impl AddAssign, add_assign for SignedDecimal256, SignedDecimal256);
770
771impl Sub for SignedDecimal256 {
772 type Output = Self;
773
774 fn sub(self, other: Self) -> Self {
775 SignedDecimal256(self.0 - other.0)
776 }
777}
778forward_ref_binop!(impl Sub, sub for SignedDecimal256, SignedDecimal256);
779
780impl SubAssign for SignedDecimal256 {
781 fn sub_assign(&mut self, rhs: SignedDecimal256) {
782 *self = *self - rhs;
783 }
784}
785forward_ref_op_assign!(impl SubAssign, sub_assign for SignedDecimal256, SignedDecimal256);
786
787impl Mul for SignedDecimal256 {
788 type Output = Self;
789
790 #[allow(clippy::suspicious_arithmetic_impl)]
791 fn mul(self, other: Self) -> Self {
792 let result_as_int512 =
798 self.numerator().full_mul(other.numerator()) / Int512::from(Self::DECIMAL_FRACTIONAL);
799 match result_as_int512.try_into() {
800 Ok(result) => Self(result),
801 Err(_) => panic!("attempt to multiply with overflow"),
802 }
803 }
804}
805forward_ref_binop!(impl Mul, mul for SignedDecimal256, SignedDecimal256);
806
807impl MulAssign for SignedDecimal256 {
808 fn mul_assign(&mut self, rhs: SignedDecimal256) {
809 *self = *self * rhs;
810 }
811}
812forward_ref_op_assign!(impl MulAssign, mul_assign for SignedDecimal256, SignedDecimal256);
813
814impl Div for SignedDecimal256 {
815 type Output = Self;
816
817 fn div(self, other: Self) -> Self {
818 match SignedDecimal256::checked_from_ratio(self.numerator(), other.numerator()) {
819 Ok(ratio) => ratio,
820 Err(CheckedFromRatioError::DivideByZero) => {
821 panic!("Division failed - denominator must not be zero")
822 }
823 Err(CheckedFromRatioError::Overflow) => {
824 panic!("Division failed - multiplication overflow")
825 }
826 }
827 }
828}
829forward_ref_binop!(impl Div, div for SignedDecimal256, SignedDecimal256);
830
831impl DivAssign for SignedDecimal256 {
832 fn div_assign(&mut self, rhs: SignedDecimal256) {
833 *self = *self / rhs;
834 }
835}
836forward_ref_op_assign!(impl DivAssign, div_assign for SignedDecimal256, SignedDecimal256);
837
838impl Div<Int256> for SignedDecimal256 {
839 type Output = Self;
840
841 fn div(self, rhs: Int256) -> Self::Output {
842 SignedDecimal256(self.0 / rhs)
843 }
844}
845
846impl DivAssign<Int256> for SignedDecimal256 {
847 fn div_assign(&mut self, rhs: Int256) {
848 self.0 /= rhs;
849 }
850}
851
852impl Rem for SignedDecimal256 {
853 type Output = Self;
854
855 #[inline]
859 fn rem(self, rhs: Self) -> Self {
860 Self(self.0.rem(rhs.0))
861 }
862}
863forward_ref_binop!(impl Rem, rem for SignedDecimal256, SignedDecimal256);
864
865impl RemAssign<SignedDecimal256> for SignedDecimal256 {
866 fn rem_assign(&mut self, rhs: SignedDecimal256) {
867 *self = *self % rhs;
868 }
869}
870forward_ref_op_assign!(impl RemAssign, rem_assign for SignedDecimal256, SignedDecimal256);
871
872impl<A> core::iter::Sum<A> for SignedDecimal256
873where
874 Self: Add<A, Output = Self>,
875{
876 fn sum<I: Iterator<Item = A>>(iter: I) -> Self {
877 iter.fold(Self::zero(), Add::add)
878 }
879}
880
881impl Serialize for SignedDecimal256 {
883 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
884 where
885 S: ser::Serializer,
886 {
887 serializer.serialize_str(&self.to_string())
888 }
889}
890
891impl<'de> Deserialize<'de> for SignedDecimal256 {
893 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
894 where
895 D: Deserializer<'de>,
896 {
897 deserializer.deserialize_str(SignedDecimal256Visitor)
898 }
899}
900
901struct SignedDecimal256Visitor;
902
903impl de::Visitor<'_> for SignedDecimal256Visitor {
904 type Value = SignedDecimal256;
905
906 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
907 formatter.write_str("string-encoded decimal")
908 }
909
910 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
911 where
912 E: de::Error,
913 {
914 match SignedDecimal256::from_str(v) {
915 Ok(d) => Ok(d),
916 Err(e) => Err(E::custom(format_args!("Error parsing decimal '{v}': {e}"))),
917 }
918 }
919}
920
921#[cfg(test)]
922mod tests {
923 use super::*;
924
925 use alloc::vec::Vec;
926
927 fn dec(input: &str) -> SignedDecimal256 {
928 SignedDecimal256::from_str(input).unwrap()
929 }
930
931 #[test]
932 fn try_from_integer() {
933 let int = Int256::new(0xDEADBEEF);
934 let decimal = SignedDecimal256::try_from(int).unwrap();
935 assert_eq!(int.to_string(), decimal.to_string());
936 }
937
938 #[test]
939 fn signed_decimal_256_new() {
940 let expected = Int256::from(300i128);
941 assert_eq!(SignedDecimal256::new(expected).0, expected);
942
943 let expected = Int256::from(-300i128);
944 assert_eq!(SignedDecimal256::new(expected).0, expected);
945 }
946
947 #[test]
948 #[allow(deprecated)]
949 fn signed_decimal_256_raw() {
950 let value = 300i128;
951 assert_eq!(SignedDecimal256::raw(value).0, Int256::from(value));
952
953 let value = -300i128;
954 assert_eq!(SignedDecimal256::raw(value).0, Int256::from(value));
955 }
956
957 #[test]
958 fn signed_decimal_256_one() {
959 let value = SignedDecimal256::one();
960 assert_eq!(value.0, SignedDecimal256::DECIMAL_FRACTIONAL);
961 }
962
963 #[test]
964 fn signed_decimal_256_zero() {
965 let value = SignedDecimal256::zero();
966 assert!(value.0.is_zero());
967 }
968
969 #[test]
970 fn signed_decimal_256_percent() {
971 let value = SignedDecimal256::percent(50);
972 assert_eq!(
973 value.0,
974 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(2u8)
975 );
976
977 let value = SignedDecimal256::percent(-50);
978 assert_eq!(
979 value.0,
980 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(-2i8)
981 );
982 }
983
984 #[test]
985 fn signed_decimal_256_permille() {
986 let value = SignedDecimal256::permille(125);
987 assert_eq!(
988 value.0,
989 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(8u8)
990 );
991
992 let value = SignedDecimal256::permille(-125);
993 assert_eq!(
994 value.0,
995 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(-8i8)
996 );
997 }
998
999 #[test]
1000 fn signed_decimal_256_bps() {
1001 let value = SignedDecimal256::bps(125);
1002 assert_eq!(
1003 value.0,
1004 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(80u8)
1005 );
1006
1007 let value = SignedDecimal256::bps(-125);
1008 assert_eq!(
1009 value.0,
1010 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(-80i8)
1011 );
1012 }
1013
1014 #[test]
1015 fn signed_decimal_256_from_atomics_works() {
1016 let one = SignedDecimal256::one();
1017 let two = one + one;
1018 let neg_one = SignedDecimal256::negative_one();
1019
1020 assert_eq!(SignedDecimal256::from_atomics(1i128, 0).unwrap(), one);
1021 assert_eq!(SignedDecimal256::from_atomics(10i128, 1).unwrap(), one);
1022 assert_eq!(SignedDecimal256::from_atomics(100i128, 2).unwrap(), one);
1023 assert_eq!(SignedDecimal256::from_atomics(1000i128, 3).unwrap(), one);
1024 assert_eq!(
1025 SignedDecimal256::from_atomics(1000000000000000000i128, 18).unwrap(),
1026 one
1027 );
1028 assert_eq!(
1029 SignedDecimal256::from_atomics(10000000000000000000i128, 19).unwrap(),
1030 one
1031 );
1032 assert_eq!(
1033 SignedDecimal256::from_atomics(100000000000000000000i128, 20).unwrap(),
1034 one
1035 );
1036
1037 assert_eq!(SignedDecimal256::from_atomics(2i128, 0).unwrap(), two);
1038 assert_eq!(SignedDecimal256::from_atomics(20i128, 1).unwrap(), two);
1039 assert_eq!(SignedDecimal256::from_atomics(200i128, 2).unwrap(), two);
1040 assert_eq!(SignedDecimal256::from_atomics(2000i128, 3).unwrap(), two);
1041 assert_eq!(
1042 SignedDecimal256::from_atomics(2000000000000000000i128, 18).unwrap(),
1043 two
1044 );
1045 assert_eq!(
1046 SignedDecimal256::from_atomics(20000000000000000000i128, 19).unwrap(),
1047 two
1048 );
1049 assert_eq!(
1050 SignedDecimal256::from_atomics(200000000000000000000i128, 20).unwrap(),
1051 two
1052 );
1053
1054 assert_eq!(SignedDecimal256::from_atomics(-1i128, 0).unwrap(), neg_one);
1055 assert_eq!(SignedDecimal256::from_atomics(-10i128, 1).unwrap(), neg_one);
1056 assert_eq!(
1057 SignedDecimal256::from_atomics(-100000000000000000000i128, 20).unwrap(),
1058 neg_one
1059 );
1060
1061 assert_eq!(
1063 SignedDecimal256::from_atomics(4321i128, 20).unwrap(),
1064 SignedDecimal256::from_str("0.000000000000000043").unwrap()
1065 );
1066 assert_eq!(
1067 SignedDecimal256::from_atomics(-4321i128, 20).unwrap(),
1068 SignedDecimal256::from_str("-0.000000000000000043").unwrap()
1069 );
1070 assert_eq!(
1071 SignedDecimal256::from_atomics(6789i128, 20).unwrap(),
1072 SignedDecimal256::from_str("0.000000000000000067").unwrap()
1073 );
1074 assert_eq!(
1075 SignedDecimal256::from_atomics(i128::MAX, 38).unwrap(),
1076 SignedDecimal256::from_str("1.701411834604692317").unwrap()
1077 );
1078 assert_eq!(
1079 SignedDecimal256::from_atomics(i128::MAX, 39).unwrap(),
1080 SignedDecimal256::from_str("0.170141183460469231").unwrap()
1081 );
1082 assert_eq!(
1083 SignedDecimal256::from_atomics(i128::MAX, 45).unwrap(),
1084 SignedDecimal256::from_str("0.000000170141183460").unwrap()
1085 );
1086 assert_eq!(
1087 SignedDecimal256::from_atomics(i128::MAX, 51).unwrap(),
1088 SignedDecimal256::from_str("0.000000000000170141").unwrap()
1089 );
1090 assert_eq!(
1091 SignedDecimal256::from_atomics(i128::MAX, 56).unwrap(),
1092 SignedDecimal256::from_str("0.000000000000000001").unwrap()
1093 );
1094 assert_eq!(
1095 SignedDecimal256::from_atomics(i128::MAX, 57).unwrap(),
1096 SignedDecimal256::from_str("0.000000000000000000").unwrap()
1097 );
1098 assert_eq!(
1099 SignedDecimal256::from_atomics(i128::MAX, u32::MAX).unwrap(),
1100 SignedDecimal256::from_str("0.000000000000000000").unwrap()
1101 );
1102
1103 let max = SignedDecimal256::MAX;
1105 assert_eq!(
1106 SignedDecimal256::from_atomics(max.atomics(), max.decimal_places()).unwrap(),
1107 max
1108 );
1109
1110 let min = SignedDecimal256::MIN;
1112 assert_eq!(
1113 SignedDecimal256::from_atomics(min.atomics(), min.decimal_places()).unwrap(),
1114 min
1115 );
1116
1117 let result = SignedDecimal256::from_atomics(Int256::MAX, 17);
1119 assert_eq!(result.unwrap_err(), SignedDecimal256RangeExceeded);
1120 }
1121
1122 #[test]
1123 fn signed_decimal_256_from_ratio_works() {
1124 assert_eq!(
1126 SignedDecimal256::from_ratio(1i128, 1i128),
1127 SignedDecimal256::one()
1128 );
1129 assert_eq!(
1130 SignedDecimal256::from_ratio(53i128, 53i128),
1131 SignedDecimal256::one()
1132 );
1133 assert_eq!(
1134 SignedDecimal256::from_ratio(125i128, 125i128),
1135 SignedDecimal256::one()
1136 );
1137
1138 assert_eq!(
1140 SignedDecimal256::from_ratio(-1i128, 1i128),
1141 SignedDecimal256::negative_one()
1142 );
1143 assert_eq!(
1144 SignedDecimal256::from_ratio(-53i128, 53i128),
1145 SignedDecimal256::negative_one()
1146 );
1147 assert_eq!(
1148 SignedDecimal256::from_ratio(125i128, -125i128),
1149 SignedDecimal256::negative_one()
1150 );
1151
1152 assert_eq!(
1154 SignedDecimal256::from_ratio(3i128, 2i128),
1155 SignedDecimal256::percent(150)
1156 );
1157 assert_eq!(
1158 SignedDecimal256::from_ratio(150i128, 100i128),
1159 SignedDecimal256::percent(150)
1160 );
1161 assert_eq!(
1162 SignedDecimal256::from_ratio(333i128, 222i128),
1163 SignedDecimal256::percent(150)
1164 );
1165
1166 assert_eq!(
1168 SignedDecimal256::from_ratio(1i64, 8i64),
1169 SignedDecimal256::permille(125)
1170 );
1171 assert_eq!(
1172 SignedDecimal256::from_ratio(125i64, 1000i64),
1173 SignedDecimal256::permille(125)
1174 );
1175
1176 assert_eq!(
1178 SignedDecimal256::from_ratio(-1i64, 8i64),
1179 SignedDecimal256::permille(-125)
1180 );
1181 assert_eq!(
1182 SignedDecimal256::from_ratio(125i64, -1000i64),
1183 SignedDecimal256::permille(-125)
1184 );
1185
1186 assert_eq!(
1188 SignedDecimal256::from_ratio(1i64, 3i64),
1189 SignedDecimal256(Int256::from(333_333_333_333_333_333i128))
1190 );
1191
1192 assert_eq!(
1194 SignedDecimal256::from_ratio(2i64, 3i64),
1195 SignedDecimal256(Int256::from(666_666_666_666_666_666i128))
1196 );
1197
1198 assert_eq!(
1200 SignedDecimal256::from_ratio(0i128, i128::MAX),
1201 SignedDecimal256::zero()
1202 );
1203 assert_eq!(
1204 SignedDecimal256::from_ratio(i128::MAX, i128::MAX),
1205 SignedDecimal256::one()
1206 );
1207 assert_eq!(
1209 SignedDecimal256::from_ratio(170141183460469231731i128, 1i128),
1210 SignedDecimal256::from_str("170141183460469231731").unwrap()
1211 );
1212 }
1213
1214 #[test]
1215 #[should_panic(expected = "Denominator must not be zero")]
1216 fn signed_decimal_256_from_ratio_panics_for_zero_denominator() {
1217 SignedDecimal256::from_ratio(1i128, 0i128);
1218 }
1219
1220 #[test]
1221 #[should_panic(expected = "Multiplication overflow")]
1222 fn signed_decimal_256_from_ratio_panics_for_mul_overflow() {
1223 SignedDecimal256::from_ratio(Int256::MAX, 1i128);
1224 }
1225
1226 #[test]
1227 fn signed_decimal_256_checked_from_ratio_does_not_panic() {
1228 assert_eq!(
1229 SignedDecimal256::checked_from_ratio(1i128, 0i128),
1230 Err(CheckedFromRatioError::DivideByZero)
1231 );
1232
1233 assert_eq!(
1234 SignedDecimal256::checked_from_ratio(Int256::MAX, 1i128),
1235 Err(CheckedFromRatioError::Overflow)
1236 );
1237 }
1238
1239 #[test]
1240 fn signed_decimal_256_implements_fraction() {
1241 let fraction = SignedDecimal256::from_str("1234.567").unwrap();
1242 assert_eq!(
1243 fraction.numerator(),
1244 Int256::from(1_234_567_000_000_000_000_000i128)
1245 );
1246 assert_eq!(
1247 fraction.denominator(),
1248 Int256::from(1_000_000_000_000_000_000i128)
1249 );
1250
1251 let fraction = SignedDecimal256::from_str("-1234.567").unwrap();
1252 assert_eq!(
1253 fraction.numerator(),
1254 Int256::from(-1_234_567_000_000_000_000_000i128)
1255 );
1256 assert_eq!(
1257 fraction.denominator(),
1258 Int256::from(1_000_000_000_000_000_000i128)
1259 );
1260 }
1261
1262 #[test]
1263 fn signed_decimal_256_from_str_works() {
1264 assert_eq!(
1266 SignedDecimal256::from_str("0").unwrap(),
1267 SignedDecimal256::percent(0)
1268 );
1269 assert_eq!(
1270 SignedDecimal256::from_str("1").unwrap(),
1271 SignedDecimal256::percent(100)
1272 );
1273 assert_eq!(
1274 SignedDecimal256::from_str("5").unwrap(),
1275 SignedDecimal256::percent(500)
1276 );
1277 assert_eq!(
1278 SignedDecimal256::from_str("42").unwrap(),
1279 SignedDecimal256::percent(4200)
1280 );
1281 assert_eq!(
1282 SignedDecimal256::from_str("000").unwrap(),
1283 SignedDecimal256::percent(0)
1284 );
1285 assert_eq!(
1286 SignedDecimal256::from_str("001").unwrap(),
1287 SignedDecimal256::percent(100)
1288 );
1289 assert_eq!(
1290 SignedDecimal256::from_str("005").unwrap(),
1291 SignedDecimal256::percent(500)
1292 );
1293 assert_eq!(
1294 SignedDecimal256::from_str("0042").unwrap(),
1295 SignedDecimal256::percent(4200)
1296 );
1297
1298 assert_eq!(
1300 SignedDecimal256::from_str("1.0").unwrap(),
1301 SignedDecimal256::percent(100)
1302 );
1303 assert_eq!(
1304 SignedDecimal256::from_str("1.5").unwrap(),
1305 SignedDecimal256::percent(150)
1306 );
1307 assert_eq!(
1308 SignedDecimal256::from_str("0.5").unwrap(),
1309 SignedDecimal256::percent(50)
1310 );
1311 assert_eq!(
1312 SignedDecimal256::from_str("0.123").unwrap(),
1313 SignedDecimal256::permille(123)
1314 );
1315
1316 assert_eq!(
1317 SignedDecimal256::from_str("40.00").unwrap(),
1318 SignedDecimal256::percent(4000)
1319 );
1320 assert_eq!(
1321 SignedDecimal256::from_str("04.00").unwrap(),
1322 SignedDecimal256::percent(400)
1323 );
1324 assert_eq!(
1325 SignedDecimal256::from_str("00.40").unwrap(),
1326 SignedDecimal256::percent(40)
1327 );
1328 assert_eq!(
1329 SignedDecimal256::from_str("00.04").unwrap(),
1330 SignedDecimal256::percent(4)
1331 );
1332 assert_eq!(
1334 SignedDecimal256::from_str("-00.04").unwrap(),
1335 SignedDecimal256::percent(-4)
1336 );
1337 assert_eq!(
1338 SignedDecimal256::from_str("-00.40").unwrap(),
1339 SignedDecimal256::percent(-40)
1340 );
1341 assert_eq!(
1342 SignedDecimal256::from_str("-04.00").unwrap(),
1343 SignedDecimal256::percent(-400)
1344 );
1345
1346 assert_eq!(
1348 SignedDecimal256::from_str("7.123456789012345678").unwrap(),
1349 SignedDecimal256(Int256::from(7123456789012345678i128))
1350 );
1351 assert_eq!(
1352 SignedDecimal256::from_str("7.999999999999999999").unwrap(),
1353 SignedDecimal256(Int256::from(7999999999999999999i128))
1354 );
1355
1356 assert_eq!(
1358 SignedDecimal256::from_str(
1359 "57896044618658097711785492504343953926634992332820282019728.792003956564819967"
1360 )
1361 .unwrap(),
1362 SignedDecimal256::MAX
1363 );
1364 assert_eq!(
1366 SignedDecimal256::from_str(
1367 "-57896044618658097711785492504343953926634992332820282019728.792003956564819968"
1368 )
1369 .unwrap(),
1370 SignedDecimal256::MIN
1371 );
1372 assert_eq!(
1373 SignedDecimal256::from_str("-1").unwrap(),
1374 SignedDecimal256::negative_one()
1375 );
1376 }
1377
1378 #[test]
1379 fn signed_decimal_256_from_str_errors_for_broken_whole_part() {
1380 assert!(SignedDecimal256::from_str("").is_err());
1381 assert!(SignedDecimal256::from_str(" ").is_err());
1382 assert!(SignedDecimal256::from_str("-").is_err());
1383 }
1384
1385 #[test]
1386 fn signed_decimal_256_from_str_errors_for_broken_fractional_part() {
1387 assert!(SignedDecimal256::from_str("1.").is_err());
1388 assert!(SignedDecimal256::from_str("1. ").is_err());
1389 assert!(SignedDecimal256::from_str("1.e").is_err());
1390 assert!(SignedDecimal256::from_str("1.2e3").is_err());
1391 assert!(SignedDecimal256::from_str("1.-2").is_err());
1392 }
1393
1394 #[test]
1395 fn signed_decimal_256_from_str_errors_for_more_than_18_fractional_digits() {
1396 assert!(SignedDecimal256::from_str("7.1234567890123456789").is_err());
1397 assert!(SignedDecimal256::from_str("7.1230000000000000000").is_err());
1399 }
1400
1401 #[test]
1402 fn signed_decimal_256_from_str_errors_for_invalid_number_of_dots() {
1403 assert!(SignedDecimal256::from_str("1.2.3")
1404 .unwrap_err()
1405 .to_string()
1406 .ends_with("Unexpected number of dots"));
1407
1408 assert!(SignedDecimal256::from_str("1.2.3.4")
1409 .unwrap_err()
1410 .to_string()
1411 .ends_with("Unexpected number of dots"));
1412 }
1413
1414 #[test]
1415 fn signed_decimal_256_from_str_errors_for_more_than_max_value() {
1416 assert!(SignedDecimal256::from_str(
1418 "57896044618658097711785492504343953926634992332820282019729",
1419 )
1420 .is_err());
1421 assert!(SignedDecimal256::from_str(
1422 "-57896044618658097711785492504343953926634992332820282019729",
1423 )
1424 .is_err());
1425
1426 assert!(SignedDecimal256::from_str(
1428 "57896044618658097711785492504343953926634992332820282019729.0",
1429 )
1430 .is_err());
1431 assert!(SignedDecimal256::from_str(
1432 "57896044618658097711785492504343953926634992332820282019728.792003956564819968",
1433 )
1434 .is_err());
1435 assert!(SignedDecimal256::from_str(
1436 "-57896044618658097711785492504343953926634992332820282019728.792003956564819969",
1437 )
1438 .is_err());
1439 }
1440
1441 #[test]
1442 fn signed_decimal_256_conversions_work() {
1443 assert_eq!(
1444 SignedDecimal256::from(SignedDecimal::zero()),
1445 SignedDecimal256::zero()
1446 );
1447 assert_eq!(
1448 SignedDecimal256::from(SignedDecimal::one()),
1449 SignedDecimal256::one()
1450 );
1451 assert_eq!(
1452 SignedDecimal256::from(SignedDecimal::percent(50)),
1453 SignedDecimal256::percent(50)
1454 );
1455 assert_eq!(
1456 SignedDecimal256::from(SignedDecimal::MAX),
1457 SignedDecimal256::new(Int256::new(i128::MAX))
1458 );
1459 assert_eq!(
1460 SignedDecimal256::from(SignedDecimal::percent(-50)),
1461 SignedDecimal256::percent(-50)
1462 );
1463 assert_eq!(
1464 SignedDecimal256::from(SignedDecimal::MIN),
1465 SignedDecimal256::new(Int256::new(i128::MIN))
1466 );
1467 }
1468
1469 #[test]
1470 fn signed_decimal_256_atomics_works() {
1471 let zero = SignedDecimal256::zero();
1472 let one = SignedDecimal256::one();
1473 let half = SignedDecimal256::percent(50);
1474 let two = SignedDecimal256::percent(200);
1475 let max = SignedDecimal256::MAX;
1476 let neg_half = SignedDecimal256::percent(-50);
1477 let neg_two = SignedDecimal256::percent(-200);
1478 let min = SignedDecimal256::MIN;
1479
1480 assert_eq!(zero.atomics(), Int256::from(0));
1481 assert_eq!(one.atomics(), Int256::from(1000000000000000000i128));
1482 assert_eq!(half.atomics(), Int256::from(500000000000000000i128));
1483 assert_eq!(two.atomics(), Int256::from(2000000000000000000i128));
1484 assert_eq!(max.atomics(), Int256::MAX);
1485 assert_eq!(neg_half.atomics(), Int256::from(-500000000000000000i128));
1486 assert_eq!(neg_two.atomics(), Int256::from(-2000000000000000000i128));
1487 assert_eq!(min.atomics(), Int256::MIN);
1488 }
1489
1490 #[test]
1491 fn signed_decimal_256_decimal_places_works() {
1492 let zero = SignedDecimal256::zero();
1493 let one = SignedDecimal256::one();
1494 let half = SignedDecimal256::percent(50);
1495 let two = SignedDecimal256::percent(200);
1496 let max = SignedDecimal256::MAX;
1497 let neg_one = SignedDecimal256::negative_one();
1498
1499 assert_eq!(zero.decimal_places(), 18);
1500 assert_eq!(one.decimal_places(), 18);
1501 assert_eq!(half.decimal_places(), 18);
1502 assert_eq!(two.decimal_places(), 18);
1503 assert_eq!(max.decimal_places(), 18);
1504 assert_eq!(neg_one.decimal_places(), 18);
1505 }
1506
1507 #[test]
1508 fn signed_decimal_256_is_zero_works() {
1509 assert!(SignedDecimal256::zero().is_zero());
1510 assert!(SignedDecimal256::percent(0).is_zero());
1511 assert!(SignedDecimal256::permille(0).is_zero());
1512
1513 assert!(!SignedDecimal256::one().is_zero());
1514 assert!(!SignedDecimal256::percent(123).is_zero());
1515 assert!(!SignedDecimal256::permille(-1234).is_zero());
1516 }
1517
1518 #[test]
1519 fn signed_decimal_256_inv_works() {
1520 assert_eq!(SignedDecimal256::zero().inv(), None);
1522
1523 assert_eq!(SignedDecimal256::one().inv(), Some(SignedDecimal256::one()));
1525
1526 assert_eq!(
1528 SignedDecimal256::negative_one().inv(),
1529 Some(SignedDecimal256::negative_one())
1530 );
1531
1532 assert_eq!(
1534 SignedDecimal256::from_str("2").unwrap().inv(),
1535 Some(SignedDecimal256::from_str("0.5").unwrap())
1536 );
1537 assert_eq!(
1538 SignedDecimal256::from_str("20").unwrap().inv(),
1539 Some(SignedDecimal256::from_str("0.05").unwrap())
1540 );
1541 assert_eq!(
1542 SignedDecimal256::from_str("200").unwrap().inv(),
1543 Some(SignedDecimal256::from_str("0.005").unwrap())
1544 );
1545 assert_eq!(
1546 SignedDecimal256::from_str("2000").unwrap().inv(),
1547 Some(SignedDecimal256::from_str("0.0005").unwrap())
1548 );
1549
1550 assert_eq!(
1552 SignedDecimal256::from_str("3").unwrap().inv(),
1553 Some(SignedDecimal256::from_str("0.333333333333333333").unwrap())
1554 );
1555 assert_eq!(
1556 SignedDecimal256::from_str("6").unwrap().inv(),
1557 Some(SignedDecimal256::from_str("0.166666666666666666").unwrap())
1558 );
1559
1560 assert_eq!(
1562 SignedDecimal256::from_str("0.5").unwrap().inv(),
1563 Some(SignedDecimal256::from_str("2").unwrap())
1564 );
1565 assert_eq!(
1566 SignedDecimal256::from_str("0.05").unwrap().inv(),
1567 Some(SignedDecimal256::from_str("20").unwrap())
1568 );
1569 assert_eq!(
1570 SignedDecimal256::from_str("0.005").unwrap().inv(),
1571 Some(SignedDecimal256::from_str("200").unwrap())
1572 );
1573 assert_eq!(
1574 SignedDecimal256::from_str("0.0005").unwrap().inv(),
1575 Some(SignedDecimal256::from_str("2000").unwrap())
1576 );
1577
1578 assert_eq!(
1580 SignedDecimal256::from_str("-0.5").unwrap().inv(),
1581 Some(SignedDecimal256::from_str("-2").unwrap())
1582 );
1583 assert_eq!(
1585 SignedDecimal256::from_str("-3").unwrap().inv(),
1586 Some(SignedDecimal256::from_str("-0.333333333333333333").unwrap())
1587 );
1588 }
1589
1590 #[test]
1591 #[allow(clippy::op_ref)]
1592 fn signed_decimal_256_add_works() {
1593 let value = SignedDecimal256::one() + SignedDecimal256::percent(50); assert_eq!(
1595 value.0,
1596 SignedDecimal256::DECIMAL_FRACTIONAL * Int256::from(3u8) / Int256::from(2u8)
1597 );
1598
1599 assert_eq!(
1600 SignedDecimal256::percent(5) + SignedDecimal256::percent(4),
1601 SignedDecimal256::percent(9)
1602 );
1603 assert_eq!(
1604 SignedDecimal256::percent(5) + SignedDecimal256::zero(),
1605 SignedDecimal256::percent(5)
1606 );
1607 assert_eq!(
1608 SignedDecimal256::zero() + SignedDecimal256::zero(),
1609 SignedDecimal256::zero()
1610 );
1611 assert_eq!(
1613 SignedDecimal256::percent(-5) + SignedDecimal256::percent(-4),
1614 SignedDecimal256::percent(-9)
1615 );
1616 assert_eq!(
1617 SignedDecimal256::percent(-5) + SignedDecimal256::percent(4),
1618 SignedDecimal256::percent(-1)
1619 );
1620 assert_eq!(
1621 SignedDecimal256::percent(5) + SignedDecimal256::percent(-4),
1622 SignedDecimal256::percent(1)
1623 );
1624
1625 let a = SignedDecimal256::percent(15);
1627 let b = SignedDecimal256::percent(25);
1628 let expected = SignedDecimal256::percent(40);
1629 assert_eq!(a + b, expected);
1630 assert_eq!(&a + b, expected);
1631 assert_eq!(a + &b, expected);
1632 assert_eq!(&a + &b, expected);
1633 }
1634
1635 #[test]
1636 #[should_panic]
1637 fn signed_decimal_256_add_overflow_panics() {
1638 let _value = SignedDecimal256::MAX + SignedDecimal256::percent(50);
1639 }
1640
1641 #[test]
1642 fn signed_decimal_256_add_assign_works() {
1643 let mut a = SignedDecimal256::percent(30);
1644 a += SignedDecimal256::percent(20);
1645 assert_eq!(a, SignedDecimal256::percent(50));
1646
1647 let mut a = SignedDecimal256::percent(15);
1649 let b = SignedDecimal256::percent(3);
1650 let expected = SignedDecimal256::percent(18);
1651 a += &b;
1652 assert_eq!(a, expected);
1653 }
1654
1655 #[test]
1656 #[allow(clippy::op_ref)]
1657 fn signed_decimal_256_sub_works() {
1658 let value = SignedDecimal256::one() - SignedDecimal256::percent(50); assert_eq!(
1660 value.0,
1661 SignedDecimal256::DECIMAL_FRACTIONAL / Int256::from(2u8)
1662 );
1663
1664 assert_eq!(
1665 SignedDecimal256::percent(9) - SignedDecimal256::percent(4),
1666 SignedDecimal256::percent(5)
1667 );
1668 assert_eq!(
1669 SignedDecimal256::percent(16) - SignedDecimal256::zero(),
1670 SignedDecimal256::percent(16)
1671 );
1672 assert_eq!(
1673 SignedDecimal256::percent(16) - SignedDecimal256::percent(16),
1674 SignedDecimal256::zero()
1675 );
1676 assert_eq!(
1677 SignedDecimal256::zero() - SignedDecimal256::zero(),
1678 SignedDecimal256::zero()
1679 );
1680
1681 assert_eq!(
1683 SignedDecimal256::percent(-5) - SignedDecimal256::percent(-4),
1684 SignedDecimal256::percent(-1)
1685 );
1686 assert_eq!(
1687 SignedDecimal256::percent(-5) - SignedDecimal256::percent(4),
1688 SignedDecimal256::percent(-9)
1689 );
1690 assert_eq!(
1691 SignedDecimal256::percent(500) - SignedDecimal256::percent(-4),
1692 SignedDecimal256::percent(504)
1693 );
1694
1695 let a = SignedDecimal256::percent(13);
1697 let b = SignedDecimal256::percent(6);
1698 let expected = SignedDecimal256::percent(7);
1699 assert_eq!(a - b, expected);
1700 assert_eq!(&a - b, expected);
1701 assert_eq!(a - &b, expected);
1702 assert_eq!(&a - &b, expected);
1703 }
1704
1705 #[test]
1706 #[should_panic]
1707 fn signed_decimal_256_sub_overflow_panics() {
1708 let _value = SignedDecimal256::MIN - SignedDecimal256::percent(50);
1709 }
1710
1711 #[test]
1712 fn signed_decimal_256_sub_assign_works() {
1713 let mut a = SignedDecimal256::percent(20);
1714 a -= SignedDecimal256::percent(2);
1715 assert_eq!(a, SignedDecimal256::percent(18));
1716
1717 let mut a = SignedDecimal256::percent(33);
1719 let b = SignedDecimal256::percent(13);
1720 let expected = SignedDecimal256::percent(20);
1721 a -= &b;
1722 assert_eq!(a, expected);
1723 }
1724
1725 #[test]
1726 #[allow(clippy::op_ref)]
1727 fn signed_decimal_256_implements_mul() {
1728 let one = SignedDecimal256::one();
1729 let two = one + one;
1730 let half = SignedDecimal256::percent(50);
1731
1732 assert_eq!(
1734 one * SignedDecimal256::percent(0),
1735 SignedDecimal256::percent(0)
1736 );
1737 assert_eq!(
1738 one * SignedDecimal256::percent(1),
1739 SignedDecimal256::percent(1)
1740 );
1741 assert_eq!(
1742 one * SignedDecimal256::percent(10),
1743 SignedDecimal256::percent(10)
1744 );
1745 assert_eq!(
1746 one * SignedDecimal256::percent(100),
1747 SignedDecimal256::percent(100)
1748 );
1749 assert_eq!(
1750 one * SignedDecimal256::percent(1000),
1751 SignedDecimal256::percent(1000)
1752 );
1753 assert_eq!(one * SignedDecimal256::MAX, SignedDecimal256::MAX);
1754 assert_eq!(
1755 SignedDecimal256::percent(0) * one,
1756 SignedDecimal256::percent(0)
1757 );
1758 assert_eq!(
1759 SignedDecimal256::percent(1) * one,
1760 SignedDecimal256::percent(1)
1761 );
1762 assert_eq!(
1763 SignedDecimal256::percent(10) * one,
1764 SignedDecimal256::percent(10)
1765 );
1766 assert_eq!(
1767 SignedDecimal256::percent(100) * one,
1768 SignedDecimal256::percent(100)
1769 );
1770 assert_eq!(
1771 SignedDecimal256::percent(1000) * one,
1772 SignedDecimal256::percent(1000)
1773 );
1774 assert_eq!(SignedDecimal256::MAX * one, SignedDecimal256::MAX);
1775 assert_eq!(
1776 SignedDecimal256::percent(-1) * one,
1777 SignedDecimal256::percent(-1)
1778 );
1779 assert_eq!(
1780 one * SignedDecimal256::percent(-10),
1781 SignedDecimal256::percent(-10)
1782 );
1783
1784 assert_eq!(
1786 two * SignedDecimal256::percent(0),
1787 SignedDecimal256::percent(0)
1788 );
1789 assert_eq!(
1790 two * SignedDecimal256::percent(1),
1791 SignedDecimal256::percent(2)
1792 );
1793 assert_eq!(
1794 two * SignedDecimal256::percent(10),
1795 SignedDecimal256::percent(20)
1796 );
1797 assert_eq!(
1798 two * SignedDecimal256::percent(100),
1799 SignedDecimal256::percent(200)
1800 );
1801 assert_eq!(
1802 two * SignedDecimal256::percent(1000),
1803 SignedDecimal256::percent(2000)
1804 );
1805 assert_eq!(
1806 SignedDecimal256::percent(0) * two,
1807 SignedDecimal256::percent(0)
1808 );
1809 assert_eq!(
1810 SignedDecimal256::percent(1) * two,
1811 SignedDecimal256::percent(2)
1812 );
1813 assert_eq!(
1814 SignedDecimal256::percent(10) * two,
1815 SignedDecimal256::percent(20)
1816 );
1817 assert_eq!(
1818 SignedDecimal256::percent(100) * two,
1819 SignedDecimal256::percent(200)
1820 );
1821 assert_eq!(
1822 SignedDecimal256::percent(1000) * two,
1823 SignedDecimal256::percent(2000)
1824 );
1825 assert_eq!(
1826 SignedDecimal256::percent(-1) * two,
1827 SignedDecimal256::percent(-2)
1828 );
1829 assert_eq!(
1830 two * SignedDecimal256::new(Int256::MIN / Int256::from(2)),
1831 SignedDecimal256::MIN
1832 );
1833
1834 assert_eq!(
1836 half * SignedDecimal256::percent(0),
1837 SignedDecimal256::percent(0)
1838 );
1839 assert_eq!(
1840 half * SignedDecimal256::percent(1),
1841 SignedDecimal256::permille(5)
1842 );
1843 assert_eq!(
1844 half * SignedDecimal256::percent(10),
1845 SignedDecimal256::percent(5)
1846 );
1847 assert_eq!(
1848 half * SignedDecimal256::percent(100),
1849 SignedDecimal256::percent(50)
1850 );
1851 assert_eq!(
1852 half * SignedDecimal256::percent(1000),
1853 SignedDecimal256::percent(500)
1854 );
1855 assert_eq!(
1856 SignedDecimal256::percent(0) * half,
1857 SignedDecimal256::percent(0)
1858 );
1859 assert_eq!(
1860 SignedDecimal256::percent(1) * half,
1861 SignedDecimal256::permille(5)
1862 );
1863 assert_eq!(
1864 SignedDecimal256::percent(10) * half,
1865 SignedDecimal256::percent(5)
1866 );
1867 assert_eq!(
1868 SignedDecimal256::percent(100) * half,
1869 SignedDecimal256::percent(50)
1870 );
1871 assert_eq!(
1872 SignedDecimal256::percent(1000) * half,
1873 SignedDecimal256::percent(500)
1874 );
1875
1876 let a = dec("123.127726548762582");
1878 assert_eq!(a * dec("1"), dec("123.127726548762582"));
1879 assert_eq!(a * dec("10"), dec("1231.27726548762582"));
1880 assert_eq!(a * dec("100"), dec("12312.7726548762582"));
1881 assert_eq!(a * dec("1000"), dec("123127.726548762582"));
1882 assert_eq!(a * dec("1000000"), dec("123127726.548762582"));
1883 assert_eq!(a * dec("1000000000"), dec("123127726548.762582"));
1884 assert_eq!(a * dec("1000000000000"), dec("123127726548762.582"));
1885 assert_eq!(a * dec("1000000000000000"), dec("123127726548762582"));
1886 assert_eq!(a * dec("1000000000000000000"), dec("123127726548762582000"));
1887 assert_eq!(dec("1") * a, dec("123.127726548762582"));
1888 assert_eq!(dec("10") * a, dec("1231.27726548762582"));
1889 assert_eq!(dec("100") * a, dec("12312.7726548762582"));
1890 assert_eq!(dec("1000") * a, dec("123127.726548762582"));
1891 assert_eq!(dec("1000000") * a, dec("123127726.548762582"));
1892 assert_eq!(dec("1000000000") * a, dec("123127726548.762582"));
1893 assert_eq!(dec("1000000000000") * a, dec("123127726548762.582"));
1894 assert_eq!(dec("1000000000000000") * a, dec("123127726548762582"));
1895 assert_eq!(dec("1000000000000000000") * a, dec("123127726548762582000"));
1896 assert_eq!(
1897 dec("-1000000000000000000") * a,
1898 dec("-123127726548762582000")
1899 );
1900
1901 let max = SignedDecimal256::MAX;
1903 assert_eq!(
1904 max * dec("1.0"),
1905 dec("57896044618658097711785492504343953926634992332820282019728.792003956564819967")
1906 );
1907 assert_eq!(
1908 max * dec("0.1"),
1909 dec("5789604461865809771178549250434395392663499233282028201972.879200395656481996")
1910 );
1911 assert_eq!(
1912 max * dec("0.01"),
1913 dec("578960446186580977117854925043439539266349923328202820197.287920039565648199")
1914 );
1915 assert_eq!(
1916 max * dec("0.001"),
1917 dec("57896044618658097711785492504343953926634992332820282019.728792003956564819")
1918 );
1919 assert_eq!(
1920 max * dec("0.000001"),
1921 dec("57896044618658097711785492504343953926634992332820282.019728792003956564")
1922 );
1923 assert_eq!(
1924 max * dec("0.000000001"),
1925 dec("57896044618658097711785492504343953926634992332820.282019728792003956")
1926 );
1927 assert_eq!(
1928 max * dec("0.000000000001"),
1929 dec("57896044618658097711785492504343953926634992332.820282019728792003")
1930 );
1931 assert_eq!(
1932 max * dec("0.000000000000001"),
1933 dec("57896044618658097711785492504343953926634992.332820282019728792")
1934 );
1935 assert_eq!(
1936 max * dec("0.000000000000000001"),
1937 dec("57896044618658097711785492504343953926634.992332820282019728")
1938 );
1939
1940 let a = SignedDecimal256::percent(20);
1942 let b = SignedDecimal256::percent(30);
1943 let expected = SignedDecimal256::percent(6);
1944 assert_eq!(a * b, expected);
1945 assert_eq!(&a * b, expected);
1946 assert_eq!(a * &b, expected);
1947 assert_eq!(&a * &b, expected);
1948 }
1949
1950 #[test]
1951 fn signed_decimal_256_mul_assign_works() {
1952 let mut a = SignedDecimal256::percent(15);
1953 a *= SignedDecimal256::percent(60);
1954 assert_eq!(a, SignedDecimal256::percent(9));
1955
1956 let mut a = SignedDecimal256::percent(50);
1958 let b = SignedDecimal256::percent(20);
1959 a *= &b;
1960 assert_eq!(a, SignedDecimal256::percent(10));
1961 }
1962
1963 #[test]
1964 #[should_panic(expected = "attempt to multiply with overflow")]
1965 fn signed_decimal_256_mul_overflow_panics() {
1966 let _value = SignedDecimal256::MAX * SignedDecimal256::percent(101);
1967 }
1968
1969 #[test]
1970 fn signed_decimal_256_checked_mul() {
1971 let test_data = [
1972 (SignedDecimal256::zero(), SignedDecimal256::zero()),
1973 (SignedDecimal256::zero(), SignedDecimal256::one()),
1974 (SignedDecimal256::one(), SignedDecimal256::zero()),
1975 (SignedDecimal256::percent(10), SignedDecimal256::zero()),
1976 (SignedDecimal256::percent(10), SignedDecimal256::percent(5)),
1977 (SignedDecimal256::MAX, SignedDecimal256::one()),
1978 (
1979 SignedDecimal256::MAX / Int256::from(2),
1980 SignedDecimal256::percent(200),
1981 ),
1982 (
1983 SignedDecimal256::permille(6),
1984 SignedDecimal256::permille(13),
1985 ),
1986 (
1987 SignedDecimal256::permille(-6),
1988 SignedDecimal256::permille(0),
1989 ),
1990 (SignedDecimal256::MAX, SignedDecimal256::negative_one()),
1991 ];
1992
1993 for (x, y) in test_data.into_iter() {
1995 assert_eq!(x * y, x.checked_mul(y).unwrap());
1996 }
1997 }
1998
1999 #[test]
2000 fn signed_decimal_256_checked_mul_overflow() {
2001 assert_eq!(
2002 SignedDecimal256::MAX.checked_mul(SignedDecimal256::percent(200)),
2003 Err(OverflowError::new(OverflowOperation::Mul))
2004 );
2005 }
2006
2007 #[test]
2008 #[allow(clippy::op_ref)]
2009 fn signed_decimal_256_implements_div() {
2010 let one = SignedDecimal256::one();
2011 let two = one + one;
2012 let half = SignedDecimal256::percent(50);
2013
2014 assert_eq!(
2016 one / SignedDecimal256::percent(1),
2017 SignedDecimal256::percent(10_000)
2018 );
2019 assert_eq!(
2020 one / SignedDecimal256::percent(10),
2021 SignedDecimal256::percent(1_000)
2022 );
2023 assert_eq!(
2024 one / SignedDecimal256::percent(100),
2025 SignedDecimal256::percent(100)
2026 );
2027 assert_eq!(
2028 one / SignedDecimal256::percent(1000),
2029 SignedDecimal256::percent(10)
2030 );
2031 assert_eq!(
2032 SignedDecimal256::percent(0) / one,
2033 SignedDecimal256::percent(0)
2034 );
2035 assert_eq!(
2036 SignedDecimal256::percent(1) / one,
2037 SignedDecimal256::percent(1)
2038 );
2039 assert_eq!(
2040 SignedDecimal256::percent(10) / one,
2041 SignedDecimal256::percent(10)
2042 );
2043 assert_eq!(
2044 SignedDecimal256::percent(100) / one,
2045 SignedDecimal256::percent(100)
2046 );
2047 assert_eq!(
2048 SignedDecimal256::percent(1000) / one,
2049 SignedDecimal256::percent(1000)
2050 );
2051 assert_eq!(
2052 one / SignedDecimal256::percent(-1),
2053 SignedDecimal256::percent(-10_000)
2054 );
2055 assert_eq!(
2056 one / SignedDecimal256::percent(-10),
2057 SignedDecimal256::percent(-1_000)
2058 );
2059
2060 assert_eq!(
2062 two / SignedDecimal256::percent(1),
2063 SignedDecimal256::percent(20_000)
2064 );
2065 assert_eq!(
2066 two / SignedDecimal256::percent(10),
2067 SignedDecimal256::percent(2_000)
2068 );
2069 assert_eq!(
2070 two / SignedDecimal256::percent(100),
2071 SignedDecimal256::percent(200)
2072 );
2073 assert_eq!(
2074 two / SignedDecimal256::percent(1000),
2075 SignedDecimal256::percent(20)
2076 );
2077 assert_eq!(
2078 SignedDecimal256::percent(0) / two,
2079 SignedDecimal256::percent(0)
2080 );
2081 assert_eq!(SignedDecimal256::percent(1) / two, dec("0.005"));
2082 assert_eq!(
2083 SignedDecimal256::percent(10) / two,
2084 SignedDecimal256::percent(5)
2085 );
2086 assert_eq!(
2087 SignedDecimal256::percent(100) / two,
2088 SignedDecimal256::percent(50)
2089 );
2090 assert_eq!(
2091 SignedDecimal256::percent(1000) / two,
2092 SignedDecimal256::percent(500)
2093 );
2094 assert_eq!(
2095 two / SignedDecimal256::percent(-1),
2096 SignedDecimal256::percent(-20_000)
2097 );
2098 assert_eq!(
2099 SignedDecimal256::percent(-10000) / two,
2100 SignedDecimal256::percent(-5000)
2101 );
2102
2103 assert_eq!(
2105 half / SignedDecimal256::percent(1),
2106 SignedDecimal256::percent(5_000)
2107 );
2108 assert_eq!(
2109 half / SignedDecimal256::percent(10),
2110 SignedDecimal256::percent(500)
2111 );
2112 assert_eq!(
2113 half / SignedDecimal256::percent(100),
2114 SignedDecimal256::percent(50)
2115 );
2116 assert_eq!(
2117 half / SignedDecimal256::percent(1000),
2118 SignedDecimal256::percent(5)
2119 );
2120 assert_eq!(
2121 SignedDecimal256::percent(0) / half,
2122 SignedDecimal256::percent(0)
2123 );
2124 assert_eq!(
2125 SignedDecimal256::percent(1) / half,
2126 SignedDecimal256::percent(2)
2127 );
2128 assert_eq!(
2129 SignedDecimal256::percent(10) / half,
2130 SignedDecimal256::percent(20)
2131 );
2132 assert_eq!(
2133 SignedDecimal256::percent(100) / half,
2134 SignedDecimal256::percent(200)
2135 );
2136 assert_eq!(
2137 SignedDecimal256::percent(1000) / half,
2138 SignedDecimal256::percent(2000)
2139 );
2140
2141 let a = dec("123127726548762582");
2143 assert_eq!(a / dec("1"), dec("123127726548762582"));
2144 assert_eq!(a / dec("10"), dec("12312772654876258.2"));
2145 assert_eq!(a / dec("100"), dec("1231277265487625.82"));
2146 assert_eq!(a / dec("1000"), dec("123127726548762.582"));
2147 assert_eq!(a / dec("1000000"), dec("123127726548.762582"));
2148 assert_eq!(a / dec("1000000000"), dec("123127726.548762582"));
2149 assert_eq!(a / dec("1000000000000"), dec("123127.726548762582"));
2150 assert_eq!(a / dec("1000000000000000"), dec("123.127726548762582"));
2151 assert_eq!(a / dec("1000000000000000000"), dec("0.123127726548762582"));
2152 assert_eq!(dec("1") / a, dec("0.000000000000000008"));
2153 assert_eq!(dec("10") / a, dec("0.000000000000000081"));
2154 assert_eq!(dec("100") / a, dec("0.000000000000000812"));
2155 assert_eq!(dec("1000") / a, dec("0.000000000000008121"));
2156 assert_eq!(dec("1000000") / a, dec("0.000000000008121647"));
2157 assert_eq!(dec("1000000000") / a, dec("0.000000008121647560"));
2158 assert_eq!(dec("1000000000000") / a, dec("0.000008121647560868"));
2159 assert_eq!(dec("1000000000000000") / a, dec("0.008121647560868164"));
2160 assert_eq!(dec("1000000000000000000") / a, dec("8.121647560868164773"));
2161 let a = dec("-123127726548762582");
2163 assert_eq!(a / dec("1"), dec("-123127726548762582"));
2164 assert_eq!(a / dec("10"), dec("-12312772654876258.2"));
2165 assert_eq!(a / dec("100"), dec("-1231277265487625.82"));
2166 assert_eq!(a / dec("1000"), dec("-123127726548762.582"));
2167 assert_eq!(a / dec("1000000"), dec("-123127726548.762582"));
2168 assert_eq!(a / dec("1000000000"), dec("-123127726.548762582"));
2169 assert_eq!(a / dec("1000000000000"), dec("-123127.726548762582"));
2170 assert_eq!(a / dec("1000000000000000"), dec("-123.127726548762582"));
2171 assert_eq!(a / dec("1000000000000000000"), dec("-0.123127726548762582"));
2172 assert_eq!(dec("1") / a, dec("-0.000000000000000008"));
2173
2174 let a = dec("0.123127726548762582");
2176 assert_eq!(a / dec("1.0"), dec("0.123127726548762582"));
2177 assert_eq!(a / dec("0.1"), dec("1.23127726548762582"));
2178 assert_eq!(a / dec("0.01"), dec("12.3127726548762582"));
2179 assert_eq!(a / dec("0.001"), dec("123.127726548762582"));
2180 assert_eq!(a / dec("0.000001"), dec("123127.726548762582"));
2181 assert_eq!(a / dec("0.000000001"), dec("123127726.548762582"));
2182 assert_eq!(a / dec("0.000000000001"), dec("123127726548.762582"));
2183 assert_eq!(a / dec("0.000000000000001"), dec("123127726548762.582"));
2184 assert_eq!(a / dec("0.000000000000000001"), dec("123127726548762582"));
2185 let a = dec("-0.123127726548762582");
2187 assert_eq!(a / dec("1.0"), dec("-0.123127726548762582"));
2188 assert_eq!(a / dec("0.1"), dec("-1.23127726548762582"));
2189 assert_eq!(a / dec("0.01"), dec("-12.3127726548762582"));
2190 assert_eq!(a / dec("0.001"), dec("-123.127726548762582"));
2191 assert_eq!(a / dec("0.000001"), dec("-123127.726548762582"));
2192 assert_eq!(a / dec("0.000000001"), dec("-123127726.548762582"));
2193
2194 assert_eq!(
2195 SignedDecimal256::percent(15) / SignedDecimal256::percent(60),
2196 SignedDecimal256::percent(25)
2197 );
2198
2199 let a = SignedDecimal256::percent(100);
2201 let b = SignedDecimal256::percent(20);
2202 let expected = SignedDecimal256::percent(500);
2203 assert_eq!(a / b, expected);
2204 assert_eq!(&a / b, expected);
2205 assert_eq!(a / &b, expected);
2206 assert_eq!(&a / &b, expected);
2207 }
2208
2209 #[test]
2210 fn signed_decimal_256_div_assign_works() {
2211 let mut a = SignedDecimal256::percent(15);
2212 a /= SignedDecimal256::percent(20);
2213 assert_eq!(a, SignedDecimal256::percent(75));
2214
2215 let mut a = SignedDecimal256::percent(50);
2217 let b = SignedDecimal256::percent(20);
2218 a /= &b;
2219 assert_eq!(a, SignedDecimal256::percent(250));
2220 }
2221
2222 #[test]
2223 #[should_panic(expected = "Division failed - multiplication overflow")]
2224 fn signed_decimal_256_div_overflow_panics() {
2225 let _value = SignedDecimal256::MAX / SignedDecimal256::percent(10);
2226 }
2227
2228 #[test]
2229 #[should_panic(expected = "Division failed - denominator must not be zero")]
2230 fn signed_decimal_256_div_by_zero_panics() {
2231 let _value = SignedDecimal256::one() / SignedDecimal256::zero();
2232 }
2233
2234 #[test]
2235 fn signed_decimal_256_int128_division() {
2236 let left = SignedDecimal256::percent(150); let right = Int256::from(3);
2239 assert_eq!(left / right, SignedDecimal256::percent(50));
2240
2241 let left = SignedDecimal256::percent(-150); let right = Int256::from(3);
2244 assert_eq!(left / right, SignedDecimal256::percent(-50));
2245
2246 let left = SignedDecimal256::zero();
2248 let right = Int256::from(300);
2249 assert_eq!(left / right, SignedDecimal256::zero());
2250 }
2251
2252 #[test]
2253 #[should_panic]
2254 fn signed_decimal_256_int128_divide_by_zero() {
2255 let left = SignedDecimal256::percent(150); let right = Int256::from(0);
2257 let _result = left / right;
2258 }
2259
2260 #[test]
2261 fn signed_decimal_256_int128_div_assign() {
2262 let mut dec = SignedDecimal256::percent(150); dec /= Int256::from(3);
2265 assert_eq!(dec, SignedDecimal256::percent(50));
2266
2267 let mut dec = SignedDecimal256::zero();
2269 dec /= Int256::from(300);
2270 assert_eq!(dec, SignedDecimal256::zero());
2271 }
2272
2273 #[test]
2274 #[should_panic]
2275 fn signed_decimal_256_int128_div_assign_by_zero() {
2276 let mut dec = SignedDecimal256::percent(50);
2278 dec /= Int256::from(0);
2279 }
2280
2281 #[test]
2282 fn signed_decimal_256_checked_pow() {
2283 for exp in 0..10 {
2284 assert_eq!(
2285 SignedDecimal256::one().checked_pow(exp).unwrap(),
2286 SignedDecimal256::one()
2287 );
2288 }
2289
2290 assert_eq!(
2293 SignedDecimal256::zero().checked_pow(0).unwrap(),
2294 SignedDecimal256::one()
2295 );
2296
2297 for exp in 1..10 {
2298 assert_eq!(
2299 SignedDecimal256::zero().checked_pow(exp).unwrap(),
2300 SignedDecimal256::zero()
2301 );
2302 }
2303
2304 for exp in 1..10 {
2305 assert_eq!(
2306 SignedDecimal256::negative_one().checked_pow(exp).unwrap(),
2307 if exp % 2 == 0 {
2309 SignedDecimal256::one()
2310 } else {
2311 SignedDecimal256::negative_one()
2312 }
2313 )
2314 }
2315
2316 for num in &[
2317 SignedDecimal256::percent(50),
2318 SignedDecimal256::percent(99),
2319 SignedDecimal256::percent(200),
2320 ] {
2321 assert_eq!(num.checked_pow(0).unwrap(), SignedDecimal256::one())
2322 }
2323
2324 assert_eq!(
2325 SignedDecimal256::percent(20).checked_pow(2).unwrap(),
2326 SignedDecimal256::percent(4)
2327 );
2328
2329 assert_eq!(
2330 SignedDecimal256::percent(20).checked_pow(3).unwrap(),
2331 SignedDecimal256::permille(8)
2332 );
2333
2334 assert_eq!(
2335 SignedDecimal256::percent(200).checked_pow(4).unwrap(),
2336 SignedDecimal256::percent(1600)
2337 );
2338
2339 assert_eq!(
2340 SignedDecimal256::percent(200).checked_pow(4).unwrap(),
2341 SignedDecimal256::percent(1600)
2342 );
2343
2344 assert_eq!(
2345 SignedDecimal256::percent(700).checked_pow(5).unwrap(),
2346 SignedDecimal256::percent(1680700)
2347 );
2348
2349 assert_eq!(
2350 SignedDecimal256::percent(700).checked_pow(8).unwrap(),
2351 SignedDecimal256::percent(576480100)
2352 );
2353
2354 assert_eq!(
2355 SignedDecimal256::percent(700).checked_pow(10).unwrap(),
2356 SignedDecimal256::percent(28247524900)
2357 );
2358
2359 assert_eq!(
2360 SignedDecimal256::percent(120).checked_pow(123).unwrap(),
2361 SignedDecimal256(5486473221892422150877397607i128.into())
2362 );
2363
2364 assert_eq!(
2365 SignedDecimal256::percent(10).checked_pow(2).unwrap(),
2366 SignedDecimal256(10000000000000000i128.into())
2367 );
2368
2369 assert_eq!(
2370 SignedDecimal256::percent(10).checked_pow(18).unwrap(),
2371 SignedDecimal256(1i128.into())
2372 );
2373
2374 let decimals = [
2375 SignedDecimal256::percent(-50),
2376 SignedDecimal256::percent(-99),
2377 SignedDecimal256::percent(-200),
2378 ];
2379 let exponents = [1, 2, 3, 4, 5, 8, 10];
2380
2381 for d in decimals {
2382 for e in exponents {
2383 let mut mul = Ok(d);
2385 for _ in 1..e {
2386 mul = mul.and_then(|mul| mul.checked_mul(d));
2387 }
2388 assert_eq!(mul, d.checked_pow(e));
2389 }
2390 }
2391 }
2392
2393 #[test]
2394 fn signed_decimal_256_checked_pow_overflow() {
2395 assert_eq!(
2396 SignedDecimal256::MAX.checked_pow(2),
2397 Err(OverflowError::new(OverflowOperation::Pow))
2398 );
2399 }
2400
2401 #[test]
2402 fn signed_decimal_256_to_string() {
2403 assert_eq!(SignedDecimal256::zero().to_string(), "0");
2405 assert_eq!(SignedDecimal256::one().to_string(), "1");
2406 assert_eq!(SignedDecimal256::percent(500).to_string(), "5");
2407 assert_eq!(SignedDecimal256::percent(-500).to_string(), "-5");
2408
2409 assert_eq!(SignedDecimal256::percent(125).to_string(), "1.25");
2411 assert_eq!(SignedDecimal256::percent(42638).to_string(), "426.38");
2412 assert_eq!(SignedDecimal256::percent(3).to_string(), "0.03");
2413 assert_eq!(SignedDecimal256::permille(987).to_string(), "0.987");
2414 assert_eq!(SignedDecimal256::percent(-125).to_string(), "-1.25");
2415 assert_eq!(SignedDecimal256::percent(-42638).to_string(), "-426.38");
2416 assert_eq!(SignedDecimal256::percent(-3).to_string(), "-0.03");
2417 assert_eq!(SignedDecimal256::permille(-987).to_string(), "-0.987");
2418
2419 assert_eq!(
2420 SignedDecimal256(Int256::from(1i128)).to_string(),
2421 "0.000000000000000001"
2422 );
2423 assert_eq!(
2424 SignedDecimal256(Int256::from(10i128)).to_string(),
2425 "0.00000000000000001"
2426 );
2427 assert_eq!(
2428 SignedDecimal256(Int256::from(100i128)).to_string(),
2429 "0.0000000000000001"
2430 );
2431 assert_eq!(
2432 SignedDecimal256(Int256::from(1000i128)).to_string(),
2433 "0.000000000000001"
2434 );
2435 assert_eq!(
2436 SignedDecimal256(Int256::from(10000i128)).to_string(),
2437 "0.00000000000001"
2438 );
2439 assert_eq!(
2440 SignedDecimal256(Int256::from(100000i128)).to_string(),
2441 "0.0000000000001"
2442 );
2443 assert_eq!(
2444 SignedDecimal256(Int256::from(1000000i128)).to_string(),
2445 "0.000000000001"
2446 );
2447 assert_eq!(
2448 SignedDecimal256(Int256::from(10000000i128)).to_string(),
2449 "0.00000000001"
2450 );
2451 assert_eq!(
2452 SignedDecimal256(Int256::from(100000000i128)).to_string(),
2453 "0.0000000001"
2454 );
2455 assert_eq!(
2456 SignedDecimal256(Int256::from(1000000000i128)).to_string(),
2457 "0.000000001"
2458 );
2459 assert_eq!(
2460 SignedDecimal256(Int256::from(10000000000i128)).to_string(),
2461 "0.00000001"
2462 );
2463 assert_eq!(
2464 SignedDecimal256(Int256::from(100000000000i128)).to_string(),
2465 "0.0000001"
2466 );
2467 assert_eq!(
2468 SignedDecimal256(Int256::from(10000000000000i128)).to_string(),
2469 "0.00001"
2470 );
2471 assert_eq!(
2472 SignedDecimal256(Int256::from(100000000000000i128)).to_string(),
2473 "0.0001"
2474 );
2475 assert_eq!(
2476 SignedDecimal256(Int256::from(1000000000000000i128)).to_string(),
2477 "0.001"
2478 );
2479 assert_eq!(
2480 SignedDecimal256(Int256::from(10000000000000000i128)).to_string(),
2481 "0.01"
2482 );
2483 assert_eq!(
2484 SignedDecimal256(Int256::from(100000000000000000i128)).to_string(),
2485 "0.1"
2486 );
2487 assert_eq!(
2488 SignedDecimal256(Int256::from(-1i128)).to_string(),
2489 "-0.000000000000000001"
2490 );
2491 assert_eq!(
2492 SignedDecimal256(Int256::from(-100000000000000i128)).to_string(),
2493 "-0.0001"
2494 );
2495 assert_eq!(
2496 SignedDecimal256(Int256::from(-100000000000000000i128)).to_string(),
2497 "-0.1"
2498 );
2499 }
2500
2501 #[test]
2502 fn signed_decimal_256_iter_sum() {
2503 let items = vec![
2504 SignedDecimal256::zero(),
2505 SignedDecimal256(Int256::from(2i128)),
2506 SignedDecimal256(Int256::from(2i128)),
2507 SignedDecimal256(Int256::from(-2i128)),
2508 ];
2509 assert_eq!(
2510 items.iter().sum::<SignedDecimal256>(),
2511 SignedDecimal256(Int256::from(2i128))
2512 );
2513 assert_eq!(
2514 items.into_iter().sum::<SignedDecimal256>(),
2515 SignedDecimal256(Int256::from(2i128))
2516 );
2517
2518 let empty: Vec<SignedDecimal256> = vec![];
2519 assert_eq!(
2520 SignedDecimal256::zero(),
2521 empty.iter().sum::<SignedDecimal256>()
2522 );
2523 }
2524
2525 #[test]
2526 fn signed_decimal_256_serialize() {
2527 assert_eq!(
2528 serde_json::to_vec(&SignedDecimal256::zero()).unwrap(),
2529 br#""0""#
2530 );
2531 assert_eq!(
2532 serde_json::to_vec(&SignedDecimal256::one()).unwrap(),
2533 br#""1""#
2534 );
2535 assert_eq!(
2536 serde_json::to_vec(&SignedDecimal256::percent(8)).unwrap(),
2537 br#""0.08""#
2538 );
2539 assert_eq!(
2540 serde_json::to_vec(&SignedDecimal256::percent(87)).unwrap(),
2541 br#""0.87""#
2542 );
2543 assert_eq!(
2544 serde_json::to_vec(&SignedDecimal256::percent(876)).unwrap(),
2545 br#""8.76""#
2546 );
2547 assert_eq!(
2548 serde_json::to_vec(&SignedDecimal256::percent(8765)).unwrap(),
2549 br#""87.65""#
2550 );
2551 assert_eq!(
2552 serde_json::to_vec(&SignedDecimal256::percent(-87654)).unwrap(),
2553 br#""-876.54""#
2554 );
2555 assert_eq!(
2556 serde_json::to_vec(&SignedDecimal256::negative_one()).unwrap(),
2557 br#""-1""#
2558 );
2559 assert_eq!(
2560 serde_json::to_vec(&-SignedDecimal256::percent(8)).unwrap(),
2561 br#""-0.08""#
2562 );
2563 }
2564
2565 #[test]
2566 fn signed_decimal_256_deserialize() {
2567 assert_eq!(
2568 serde_json::from_slice::<SignedDecimal256>(br#""0""#).unwrap(),
2569 SignedDecimal256::zero()
2570 );
2571 assert_eq!(
2572 serde_json::from_slice::<SignedDecimal256>(br#""1""#).unwrap(),
2573 SignedDecimal256::one()
2574 );
2575 assert_eq!(
2576 serde_json::from_slice::<SignedDecimal256>(br#""000""#).unwrap(),
2577 SignedDecimal256::zero()
2578 );
2579 assert_eq!(
2580 serde_json::from_slice::<SignedDecimal256>(br#""001""#).unwrap(),
2581 SignedDecimal256::one()
2582 );
2583
2584 assert_eq!(
2585 serde_json::from_slice::<SignedDecimal256>(br#""0.08""#).unwrap(),
2586 SignedDecimal256::percent(8)
2587 );
2588 assert_eq!(
2589 serde_json::from_slice::<SignedDecimal256>(br#""0.87""#).unwrap(),
2590 SignedDecimal256::percent(87)
2591 );
2592 assert_eq!(
2593 serde_json::from_slice::<SignedDecimal256>(br#""8.76""#).unwrap(),
2594 SignedDecimal256::percent(876)
2595 );
2596 assert_eq!(
2597 serde_json::from_slice::<SignedDecimal256>(br#""87.65""#).unwrap(),
2598 SignedDecimal256::percent(8765)
2599 );
2600
2601 assert_eq!(
2603 serde_json::from_slice::<SignedDecimal256>(br#""-0""#).unwrap(),
2604 SignedDecimal256::zero()
2605 );
2606 assert_eq!(
2607 serde_json::from_slice::<SignedDecimal256>(br#""-1""#).unwrap(),
2608 SignedDecimal256::negative_one()
2609 );
2610 assert_eq!(
2611 serde_json::from_slice::<SignedDecimal256>(br#""-001""#).unwrap(),
2612 SignedDecimal256::negative_one()
2613 );
2614 assert_eq!(
2615 serde_json::from_slice::<SignedDecimal256>(br#""-0.08""#).unwrap(),
2616 SignedDecimal256::percent(-8)
2617 );
2618 }
2619
2620 #[test]
2621 fn signed_decimal_256_abs_diff_works() {
2622 let a = SignedDecimal256::percent(285);
2623 let b = SignedDecimal256::percent(200);
2624 let expected = Decimal256::percent(85);
2625 assert_eq!(a.abs_diff(b), expected);
2626 assert_eq!(b.abs_diff(a), expected);
2627
2628 let a = SignedDecimal256::percent(-200);
2629 let b = SignedDecimal256::percent(200);
2630 let expected = Decimal256::percent(400);
2631 assert_eq!(a.abs_diff(b), expected);
2632 assert_eq!(b.abs_diff(a), expected);
2633
2634 let a = SignedDecimal256::percent(-200);
2635 let b = SignedDecimal256::percent(-240);
2636 let expected = Decimal256::percent(40);
2637 assert_eq!(a.abs_diff(b), expected);
2638 assert_eq!(b.abs_diff(a), expected);
2639 }
2640
2641 #[test]
2642 #[allow(clippy::op_ref)]
2643 fn signed_decimal_256_rem_works() {
2644 assert_eq!(
2646 SignedDecimal256::percent(402) % SignedDecimal256::percent(111),
2647 SignedDecimal256::percent(69)
2648 );
2649
2650 assert_eq!(
2652 SignedDecimal256::percent(1525) % SignedDecimal256::percent(400),
2653 SignedDecimal256::percent(325)
2654 );
2655
2656 assert_eq!(
2658 SignedDecimal256::percent(-2025) % SignedDecimal256::percent(500),
2659 SignedDecimal256::percent(-25)
2660 );
2661
2662 let a = SignedDecimal256::percent(318);
2663 let b = SignedDecimal256::percent(317);
2664 let expected = SignedDecimal256::percent(1);
2665 assert_eq!(a % b, expected);
2666 assert_eq!(a % &b, expected);
2667 assert_eq!(&a % b, expected);
2668 assert_eq!(&a % &b, expected);
2669 }
2670
2671 #[test]
2672 fn signed_decimal_256_rem_assign_works() {
2673 let mut a = SignedDecimal256::percent(17673);
2674 a %= SignedDecimal256::percent(2362);
2675 assert_eq!(a, SignedDecimal256::percent(1139)); let mut a = SignedDecimal256::percent(4262);
2678 let b = SignedDecimal256::percent(1270);
2679 a %= &b;
2680 assert_eq!(a, SignedDecimal256::percent(452)); let mut a = SignedDecimal256::percent(-4262);
2683 let b = SignedDecimal256::percent(1270);
2684 a %= &b;
2685 assert_eq!(a, SignedDecimal256::percent(-452)); }
2687
2688 #[test]
2689 #[should_panic(expected = "divisor of zero")]
2690 fn signed_decimal_256_rem_panics_for_zero() {
2691 let _ = SignedDecimal256::percent(777) % SignedDecimal256::zero();
2692 }
2693
2694 #[test]
2695 fn signed_decimal_256_checked_methods() {
2696 assert_eq!(
2698 SignedDecimal256::percent(402)
2699 .checked_add(SignedDecimal256::percent(111))
2700 .unwrap(),
2701 SignedDecimal256::percent(513)
2702 );
2703 assert!(matches!(
2704 SignedDecimal256::MAX.checked_add(SignedDecimal256::percent(1)),
2705 Err(OverflowError { .. })
2706 ));
2707 assert!(matches!(
2708 SignedDecimal256::MIN.checked_add(SignedDecimal256::percent(-1)),
2709 Err(OverflowError { .. })
2710 ));
2711
2712 assert_eq!(
2714 SignedDecimal256::percent(1111)
2715 .checked_sub(SignedDecimal256::percent(111))
2716 .unwrap(),
2717 SignedDecimal256::percent(1000)
2718 );
2719 assert_eq!(
2720 SignedDecimal256::zero()
2721 .checked_sub(SignedDecimal256::percent(1))
2722 .unwrap(),
2723 SignedDecimal256::percent(-1)
2724 );
2725 assert!(matches!(
2726 SignedDecimal256::MIN.checked_sub(SignedDecimal256::percent(1)),
2727 Err(OverflowError { .. })
2728 ));
2729 assert!(matches!(
2730 SignedDecimal256::MAX.checked_sub(SignedDecimal256::percent(-1)),
2731 Err(OverflowError { .. })
2732 ));
2733
2734 assert_eq!(
2736 SignedDecimal256::percent(30)
2737 .checked_div(SignedDecimal256::percent(200))
2738 .unwrap(),
2739 SignedDecimal256::percent(15)
2740 );
2741 assert_eq!(
2742 SignedDecimal256::percent(88)
2743 .checked_div(SignedDecimal256::percent(20))
2744 .unwrap(),
2745 SignedDecimal256::percent(440)
2746 );
2747 assert!(matches!(
2748 SignedDecimal256::MAX.checked_div(SignedDecimal256::zero()),
2749 Err(CheckedFromRatioError::DivideByZero)
2750 ));
2751 assert!(matches!(
2752 SignedDecimal256::MAX.checked_div(SignedDecimal256::percent(1)),
2753 Err(CheckedFromRatioError::Overflow)
2754 ));
2755 assert_eq!(
2756 SignedDecimal256::percent(-88)
2757 .checked_div(SignedDecimal256::percent(20))
2758 .unwrap(),
2759 SignedDecimal256::percent(-440)
2760 );
2761 assert_eq!(
2762 SignedDecimal256::percent(-88)
2763 .checked_div(SignedDecimal256::percent(-20))
2764 .unwrap(),
2765 SignedDecimal256::percent(440)
2766 );
2767
2768 assert_eq!(
2770 SignedDecimal256::percent(402)
2771 .checked_rem(SignedDecimal256::percent(111))
2772 .unwrap(),
2773 SignedDecimal256::percent(69)
2774 );
2775 assert_eq!(
2776 SignedDecimal256::percent(1525)
2777 .checked_rem(SignedDecimal256::percent(400))
2778 .unwrap(),
2779 SignedDecimal256::percent(325)
2780 );
2781 assert_eq!(
2782 SignedDecimal256::percent(-1525)
2783 .checked_rem(SignedDecimal256::percent(400))
2784 .unwrap(),
2785 SignedDecimal256::percent(-325)
2786 );
2787 assert_eq!(
2788 SignedDecimal256::percent(-1525)
2789 .checked_rem(SignedDecimal256::percent(-400))
2790 .unwrap(),
2791 SignedDecimal256::percent(-325)
2792 );
2793 assert!(matches!(
2794 SignedDecimal256::MAX.checked_rem(SignedDecimal256::zero()),
2795 Err(DivideByZeroError { .. })
2796 ));
2797 }
2798
2799 #[test]
2800 fn signed_decimal_256_pow_works() {
2801 assert_eq!(
2802 SignedDecimal256::percent(200).pow(2),
2803 SignedDecimal256::percent(400)
2804 );
2805 assert_eq!(
2806 SignedDecimal256::percent(-200).pow(2),
2807 SignedDecimal256::percent(400)
2808 );
2809 assert_eq!(
2810 SignedDecimal256::percent(-200).pow(3),
2811 SignedDecimal256::percent(-800)
2812 );
2813 assert_eq!(
2814 SignedDecimal256::percent(200).pow(10),
2815 SignedDecimal256::percent(102400)
2816 );
2817 }
2818
2819 #[test]
2820 #[should_panic]
2821 fn signed_decimal_256_pow_overflow_panics() {
2822 _ = SignedDecimal256::MAX.pow(2u32);
2823 }
2824
2825 #[test]
2826 fn signed_decimal_256_saturating_works() {
2827 assert_eq!(
2828 SignedDecimal256::percent(200).saturating_add(SignedDecimal256::percent(200)),
2829 SignedDecimal256::percent(400)
2830 );
2831 assert_eq!(
2832 SignedDecimal256::percent(-200).saturating_add(SignedDecimal256::percent(200)),
2833 SignedDecimal256::zero()
2834 );
2835 assert_eq!(
2836 SignedDecimal256::percent(-200).saturating_add(SignedDecimal256::percent(-200)),
2837 SignedDecimal256::percent(-400)
2838 );
2839 assert_eq!(
2840 SignedDecimal256::MAX.saturating_add(SignedDecimal256::percent(200)),
2841 SignedDecimal256::MAX
2842 );
2843 assert_eq!(
2844 SignedDecimal256::MIN.saturating_add(SignedDecimal256::percent(-1)),
2845 SignedDecimal256::MIN
2846 );
2847 assert_eq!(
2848 SignedDecimal256::percent(200).saturating_sub(SignedDecimal256::percent(100)),
2849 SignedDecimal256::percent(100)
2850 );
2851 assert_eq!(
2852 SignedDecimal256::percent(-200).saturating_sub(SignedDecimal256::percent(100)),
2853 SignedDecimal256::percent(-300)
2854 );
2855 assert_eq!(
2856 SignedDecimal256::percent(-200).saturating_sub(SignedDecimal256::percent(-100)),
2857 SignedDecimal256::percent(-100)
2858 );
2859 assert_eq!(
2860 SignedDecimal256::zero().saturating_sub(SignedDecimal256::percent(200)),
2861 SignedDecimal256::from_str("-2").unwrap()
2862 );
2863 assert_eq!(
2864 SignedDecimal256::MIN.saturating_sub(SignedDecimal256::percent(200)),
2865 SignedDecimal256::MIN
2866 );
2867 assert_eq!(
2868 SignedDecimal256::MAX.saturating_sub(SignedDecimal256::percent(-200)),
2869 SignedDecimal256::MAX
2870 );
2871 assert_eq!(
2872 SignedDecimal256::percent(200).saturating_mul(SignedDecimal256::percent(50)),
2873 SignedDecimal256::percent(100)
2874 );
2875 assert_eq!(
2876 SignedDecimal256::percent(-200).saturating_mul(SignedDecimal256::percent(50)),
2877 SignedDecimal256::percent(-100)
2878 );
2879 assert_eq!(
2880 SignedDecimal256::percent(-200).saturating_mul(SignedDecimal256::percent(-50)),
2881 SignedDecimal256::percent(100)
2882 );
2883 assert_eq!(
2884 SignedDecimal256::MAX.saturating_mul(SignedDecimal256::percent(200)),
2885 SignedDecimal256::MAX
2886 );
2887 assert_eq!(
2888 SignedDecimal256::MIN.saturating_mul(SignedDecimal256::percent(200)),
2889 SignedDecimal256::MIN
2890 );
2891 assert_eq!(
2892 SignedDecimal256::MIN.saturating_mul(SignedDecimal256::percent(-200)),
2893 SignedDecimal256::MAX
2894 );
2895 assert_eq!(
2896 SignedDecimal256::percent(400).saturating_pow(2u32),
2897 SignedDecimal256::percent(1600)
2898 );
2899 assert_eq!(
2900 SignedDecimal256::MAX.saturating_pow(2u32),
2901 SignedDecimal256::MAX
2902 );
2903 assert_eq!(
2904 SignedDecimal256::MAX.saturating_pow(3u32),
2905 SignedDecimal256::MAX
2906 );
2907 assert_eq!(
2908 SignedDecimal256::MIN.saturating_pow(2u32),
2909 SignedDecimal256::MAX
2910 );
2911 assert_eq!(
2912 SignedDecimal256::MIN.saturating_pow(3u32),
2913 SignedDecimal256::MIN
2914 );
2915 }
2916
2917 #[test]
2918 fn signed_decimal_256_rounding() {
2919 assert_eq!(SignedDecimal256::one().floor(), SignedDecimal256::one());
2920 assert_eq!(
2921 SignedDecimal256::percent(150).floor(),
2922 SignedDecimal256::one()
2923 );
2924 assert_eq!(
2925 SignedDecimal256::percent(199).floor(),
2926 SignedDecimal256::one()
2927 );
2928 assert_eq!(
2929 SignedDecimal256::percent(200).floor(),
2930 SignedDecimal256::percent(200)
2931 );
2932 assert_eq!(
2933 SignedDecimal256::percent(99).floor(),
2934 SignedDecimal256::zero()
2935 );
2936 assert_eq!(
2937 SignedDecimal256(Int256::from(1i128)).floor(),
2938 SignedDecimal256::zero()
2939 );
2940 assert_eq!(
2941 SignedDecimal256(Int256::from(-1i128)).floor(),
2942 SignedDecimal256::negative_one()
2943 );
2944 assert_eq!(
2945 SignedDecimal256::permille(-1234).floor(),
2946 SignedDecimal256::percent(-200)
2947 );
2948
2949 assert_eq!(SignedDecimal256::one().ceil(), SignedDecimal256::one());
2950 assert_eq!(
2951 SignedDecimal256::percent(150).ceil(),
2952 SignedDecimal256::percent(200)
2953 );
2954 assert_eq!(
2955 SignedDecimal256::percent(199).ceil(),
2956 SignedDecimal256::percent(200)
2957 );
2958 assert_eq!(
2959 SignedDecimal256::percent(99).ceil(),
2960 SignedDecimal256::one()
2961 );
2962 assert_eq!(
2963 SignedDecimal256(Int256::from(1i128)).ceil(),
2964 SignedDecimal256::one()
2965 );
2966 assert_eq!(
2967 SignedDecimal256(Int256::from(-1i128)).ceil(),
2968 SignedDecimal256::zero()
2969 );
2970 assert_eq!(
2971 SignedDecimal256::permille(-1234).ceil(),
2972 SignedDecimal256::negative_one()
2973 );
2974
2975 assert_eq!(SignedDecimal256::one().trunc(), SignedDecimal256::one());
2976 assert_eq!(
2977 SignedDecimal256::percent(150).trunc(),
2978 SignedDecimal256::one()
2979 );
2980 assert_eq!(
2981 SignedDecimal256::percent(199).trunc(),
2982 SignedDecimal256::one()
2983 );
2984 assert_eq!(
2985 SignedDecimal256::percent(200).trunc(),
2986 SignedDecimal256::percent(200)
2987 );
2988 assert_eq!(
2989 SignedDecimal256::percent(99).trunc(),
2990 SignedDecimal256::zero()
2991 );
2992 assert_eq!(
2993 SignedDecimal256(Int256::from(1i128)).trunc(),
2994 SignedDecimal256::zero()
2995 );
2996 assert_eq!(
2997 SignedDecimal256(Int256::from(-1i128)).trunc(),
2998 SignedDecimal256::zero()
2999 );
3000 assert_eq!(
3001 SignedDecimal256::permille(-1234).trunc(),
3002 SignedDecimal256::negative_one()
3003 );
3004 }
3005
3006 #[test]
3007 #[should_panic(expected = "attempt to ceil with overflow")]
3008 fn signed_decimal_256_ceil_panics() {
3009 let _ = SignedDecimal256::MAX.ceil();
3010 }
3011
3012 #[test]
3013 #[should_panic(expected = "attempt to floor with overflow")]
3014 fn signed_decimal_256_floor_panics() {
3015 let _ = SignedDecimal256::MIN.floor();
3016 }
3017
3018 #[test]
3019 fn signed_decimal_256_checked_ceil() {
3020 assert_eq!(
3021 SignedDecimal256::percent(199).checked_ceil(),
3022 Ok(SignedDecimal256::percent(200))
3023 );
3024 assert_eq!(
3025 SignedDecimal256::MAX.checked_ceil(),
3026 Err(RoundUpOverflowError)
3027 );
3028 }
3029
3030 #[test]
3031 fn signed_decimal_256_checked_floor() {
3032 assert_eq!(
3033 SignedDecimal256::percent(199).checked_floor(),
3034 Ok(SignedDecimal256::one())
3035 );
3036 assert_eq!(
3037 SignedDecimal256::percent(-199).checked_floor(),
3038 Ok(SignedDecimal256::percent(-200))
3039 );
3040 assert_eq!(
3041 SignedDecimal256::MIN.checked_floor(),
3042 Err(RoundDownOverflowError)
3043 );
3044 assert_eq!(
3045 SignedDecimal256::negative_one().checked_floor(),
3046 Ok(SignedDecimal256::negative_one())
3047 );
3048 }
3049
3050 #[test]
3051 fn signed_decimal_256_to_int_floor_works() {
3052 let d = SignedDecimal256::from_str("12.000000000000000001").unwrap();
3053 assert_eq!(d.to_int_floor(), Int256::from(12));
3054 let d = SignedDecimal256::from_str("12.345").unwrap();
3055 assert_eq!(d.to_int_floor(), Int256::from(12));
3056 let d = SignedDecimal256::from_str("12.999").unwrap();
3057 assert_eq!(d.to_int_floor(), Int256::from(12));
3058 let d = SignedDecimal256::from_str("0.98451384").unwrap();
3059 assert_eq!(d.to_int_floor(), Int256::from(0));
3060 let d = SignedDecimal256::from_str("-12.000000000000000001").unwrap();
3061 assert_eq!(d.to_int_floor(), Int256::from(-13));
3062 let d = SignedDecimal256::from_str("-12.345").unwrap();
3063 assert_eq!(d.to_int_floor(), Int256::from(-13));
3064 let d = SignedDecimal256::from_str("0.0001").unwrap();
3065 assert_eq!(d.to_int_floor(), Int256::from(0));
3066 let d = SignedDecimal256::from_str("75.0").unwrap();
3067 assert_eq!(d.to_int_floor(), Int256::from(75));
3068 let d = SignedDecimal256::from_str("0.0").unwrap();
3069 assert_eq!(d.to_int_floor(), Int256::from(0));
3070 let d = SignedDecimal256::from_str("-0.0").unwrap();
3071 assert_eq!(d.to_int_floor(), Int256::from(0));
3072 let d = SignedDecimal256::from_str("-0.0001").unwrap();
3073 assert_eq!(d.to_int_floor(), Int256::from(-1));
3074 let d = SignedDecimal256::from_str("-75.0").unwrap();
3075 assert_eq!(d.to_int_floor(), Int256::from(-75));
3076
3077 let d = SignedDecimal256::MAX;
3078 assert_eq!(
3079 d.to_int_floor(),
3080 Int256::from_str("57896044618658097711785492504343953926634992332820282019728")
3081 .unwrap()
3082 );
3083 let d = SignedDecimal256::MIN;
3084 assert_eq!(
3085 d.to_int_floor(),
3086 Int256::from_str("-57896044618658097711785492504343953926634992332820282019729")
3087 .unwrap()
3088 );
3089 }
3090
3091 #[test]
3092 fn signed_decimal_256_to_int_ceil_works() {
3093 let d = SignedDecimal256::from_str("12.000000000000000001").unwrap();
3094 assert_eq!(d.to_int_ceil(), Int256::from(13));
3095 let d = SignedDecimal256::from_str("12.345").unwrap();
3096 assert_eq!(d.to_int_ceil(), Int256::from(13));
3097 let d = SignedDecimal256::from_str("12.999").unwrap();
3098 assert_eq!(d.to_int_ceil(), Int256::from(13));
3099 let d = SignedDecimal256::from_str("-12.000000000000000001").unwrap();
3100 assert_eq!(d.to_int_ceil(), Int256::from(-12));
3101 let d = SignedDecimal256::from_str("-12.345").unwrap();
3102 assert_eq!(d.to_int_ceil(), Int256::from(-12));
3103
3104 let d = SignedDecimal256::from_str("75.0").unwrap();
3105 assert_eq!(d.to_int_ceil(), Int256::from(75));
3106 let d = SignedDecimal256::from_str("0.0").unwrap();
3107 assert_eq!(d.to_int_ceil(), Int256::from(0));
3108 let d = SignedDecimal256::from_str("-75.0").unwrap();
3109 assert_eq!(d.to_int_ceil(), Int256::from(-75));
3110
3111 let d = SignedDecimal256::MAX;
3112 assert_eq!(
3113 d.to_int_ceil(),
3114 Int256::from_str("57896044618658097711785492504343953926634992332820282019729")
3115 .unwrap()
3116 );
3117 let d = SignedDecimal256::MIN;
3118 assert_eq!(
3119 d.to_int_ceil(),
3120 Int256::from_str("-57896044618658097711785492504343953926634992332820282019728")
3121 .unwrap()
3122 );
3123 }
3124
3125 #[test]
3126 fn signed_decimal_256_to_int_trunc_works() {
3127 let d = SignedDecimal256::from_str("12.000000000000000001").unwrap();
3128 assert_eq!(d.to_int_trunc(), Int256::from(12));
3129 let d = SignedDecimal256::from_str("12.345").unwrap();
3130 assert_eq!(d.to_int_trunc(), Int256::from(12));
3131 let d = SignedDecimal256::from_str("12.999").unwrap();
3132 assert_eq!(d.to_int_trunc(), Int256::from(12));
3133 let d = SignedDecimal256::from_str("-12.000000000000000001").unwrap();
3134 assert_eq!(d.to_int_trunc(), Int256::from(-12));
3135 let d = SignedDecimal256::from_str("-12.345").unwrap();
3136 assert_eq!(d.to_int_trunc(), Int256::from(-12));
3137
3138 let d = SignedDecimal256::from_str("75.0").unwrap();
3139 assert_eq!(d.to_int_trunc(), Int256::from(75));
3140 let d = SignedDecimal256::from_str("0.0").unwrap();
3141 assert_eq!(d.to_int_trunc(), Int256::from(0));
3142 let d = SignedDecimal256::from_str("-75.0").unwrap();
3143 assert_eq!(d.to_int_trunc(), Int256::from(-75));
3144
3145 let d = SignedDecimal256::MAX;
3146 assert_eq!(
3147 d.to_int_trunc(),
3148 Int256::from_str("57896044618658097711785492504343953926634992332820282019728")
3149 .unwrap()
3150 );
3151 let d = SignedDecimal256::MIN;
3152 assert_eq!(
3153 d.to_int_trunc(),
3154 Int256::from_str("-57896044618658097711785492504343953926634992332820282019728")
3155 .unwrap()
3156 );
3157 }
3158
3159 #[test]
3160 fn signed_decimal_256_neg_works() {
3161 assert_eq!(
3162 -SignedDecimal256::percent(50),
3163 SignedDecimal256::percent(-50)
3164 );
3165 assert_eq!(-SignedDecimal256::one(), SignedDecimal256::negative_one());
3166 }
3167
3168 #[test]
3169 fn signed_decimal_256_partial_eq() {
3170 let test_cases = [
3171 ("1", "1", true),
3172 ("0.5", "0.5", true),
3173 ("0.5", "0.51", false),
3174 ("0", "0.00000", true),
3175 ("-1", "-1", true),
3176 ("-0.5", "-0.5", true),
3177 ("-0.5", "0.5", false),
3178 ("-0.5", "-0.51", false),
3179 ("-0", "-0.00000", true),
3180 ]
3181 .into_iter()
3182 .map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));
3183
3184 #[allow(clippy::op_ref)]
3185 for (lhs, rhs, expected) in test_cases {
3186 assert_eq!(lhs == rhs, expected);
3187 assert_eq!(&lhs == rhs, expected);
3188 assert_eq!(lhs == &rhs, expected);
3189 assert_eq!(&lhs == &rhs, expected);
3190 }
3191 }
3192
3193 #[test]
3194 fn signed_decimal_256_implements_debug() {
3195 let decimal = SignedDecimal256::from_str("123.45").unwrap();
3196 assert_eq!(format!("{decimal:?}"), "SignedDecimal256(123.45)");
3197
3198 let test_cases = ["5", "5.01", "42", "0", "2", "-0.000001"];
3199 for s in test_cases {
3200 let decimal = SignedDecimal256::from_str(s).unwrap();
3201 let expected = format!("SignedDecimal256({s})");
3202 assert_eq!(format!("{decimal:?}"), expected);
3203 }
3204 }
3205
3206 #[test]
3207 fn signed_decimal_256_can_be_instantiated_from_decimal() {
3208 let d: SignedDecimal256 = Decimal::one().into();
3209 assert_eq!(d, SignedDecimal256::one());
3210 }
3211
3212 #[test]
3213 fn signed_decimal_256_can_be_instantiated_from_decimal_256() {
3214 let d: SignedDecimal256 = Decimal256::zero().try_into().unwrap();
3215 assert_eq!(d, SignedDecimal256::zero());
3216 }
3217
3218 #[test]
3219 fn signed_decimal_256_may_fail_when_instantiated_from_decimal_256() {
3220 let err = <Decimal256 as TryInto<SignedDecimal256>>::try_into(Decimal256::MAX).unwrap_err();
3221 assert_eq!("SignedDecimal256RangeExceeded", format!("{err:?}"));
3222 assert_eq!("SignedDecimal256 range exceeded", format!("{err}"));
3223 }
3224
3225 #[test]
3226 fn signed_decimal_256_can_be_serialized_and_deserialized() {
3227 let value: SignedDecimal256 = serde_json::from_str(r#""123""#).unwrap();
3229 assert_eq!(SignedDecimal256::from_str("123").unwrap(), value);
3230
3231 let value = SignedDecimal256::from_str("456").unwrap();
3233 assert_eq!(r#""456""#, serde_json::to_string(&value).unwrap());
3234
3235 assert_eq!(
3237 "invalid type: integer `123`, expected string-encoded decimal at line 1 column 3",
3238 serde_json::from_str::<SignedDecimal256>("123")
3239 .err()
3240 .unwrap()
3241 .to_string()
3242 );
3243
3244 assert_eq!(
3246 "Error parsing decimal '1.e': kind: Parsing, error: invalid digit found in string at line 1 column 5",
3247 serde_json::from_str::<SignedDecimal256>(r#""1.e""#)
3248 .err()
3249 .unwrap()
3250 .to_string()
3251 );
3252 }
3253
3254 #[test]
3255 fn signed_decimal_256_has_defined_json_schema() {
3256 let schema = schemars::schema_for!(SignedDecimal256);
3257 assert_eq!(
3258 "SignedDecimal256",
3259 schema.schema.metadata.unwrap().title.unwrap()
3260 );
3261 }
3262}