@@ -490,6 +490,50 @@ TEST(DecimalTest, FromBigEndianInvalid) {
490490 IsError (ErrorKind::kInvalidArgument ));
491491}
492492
493+ TEST (DecimalTest, ToBigEndian) {
494+ std::vector<int64_t > high_values = {0 ,
495+ 1 ,
496+ -1 ,
497+ INT32_MAX,
498+ INT32_MIN,
499+ static_cast <int64_t >(INT32_MAX) + 1 ,
500+ static_cast <int64_t >(INT32_MIN) - 1 ,
501+ INT64_MAX,
502+ INT64_MIN};
503+ std::vector<uint64_t > low_values = {0 ,
504+ 1 ,
505+ 255 ,
506+ UINT32_MAX,
507+ static_cast <uint64_t >(UINT32_MAX) + 1 ,
508+ static_cast <uint64_t >(UINT32_MAX) + 2 ,
509+ static_cast <uint64_t >(UINT32_MAX) + 3 ,
510+ static_cast <uint64_t >(UINT32_MAX) + 4 ,
511+ static_cast <uint64_t >(UINT32_MAX) + 5 ,
512+ static_cast <uint64_t >(UINT32_MAX) + 6 ,
513+ static_cast <uint64_t >(UINT32_MAX) + 7 ,
514+ static_cast <uint64_t >(UINT32_MAX) + 8 ,
515+ UINT64_MAX};
516+
517+ for (int64_t high : high_values) {
518+ for (uint64_t low : low_values) {
519+ Decimal decimal (high, low);
520+ auto bytes = decimal.ToBigEndian ();
521+ auto result = Decimal::FromBigEndian (bytes.data (), bytes.size ());
522+ ASSERT_THAT (result, IsOk ());
523+ EXPECT_EQ (result.value (), decimal);
524+ }
525+ }
526+
527+ for (int128_t value : std::vector<int128_t >{-INT64_MAX, -INT32_MAX, -255 , -1 , 0 , 1 , 255 ,
528+ 256 , INT32_MAX, INT64_MAX}) {
529+ Decimal decimal (value);
530+ auto bytes = decimal.ToBigEndian ();
531+ auto result = Decimal::FromBigEndian (bytes.data (), bytes.size ());
532+ ASSERT_THAT (result, IsOk ());
533+ EXPECT_EQ (result.value (), decimal);
534+ }
535+ }
536+
493537TEST (DecimalTestFunctionality, Multiply) {
494538 ASSERT_EQ (Decimal (60501 ), Decimal (301 ) * Decimal (201 ));
495539 ASSERT_EQ (Decimal (-60501 ), Decimal (-301 ) * Decimal (201 ));
@@ -671,4 +715,58 @@ TEST(DecimalTest, Rescale) {
671715 ASSERT_THAT (Decimal (5555555 ).Rescale (6 , 1 ), IsError (ErrorKind::kInvalid ));
672716}
673717
718+ TEST (DecimalTest, Compare) {
719+ // max positive unscaled value
720+ // 10^38 - 1 scale cause overflow
721+ ASSERT_EQ (Decimal::Compare (Decimal (" 99999999999999999999999999999999999999" ),
722+ Decimal (" 99999999999999999999999999999999999999" ), 2 , 3 ),
723+ std::partial_ordering::greater);
724+ // 10^37 - 1 scale no overflow
725+ ASSERT_EQ (Decimal::Compare (Decimal (" 9999999999999999999999999999999999999" ),
726+ Decimal (" 99999999999999999999999999999999999999" ), 2 , 3 ),
727+ std::partial_ordering::less);
728+
729+ // min negative unscaled value
730+ // -10^38 + 1 scale cause overflow
731+ ASSERT_EQ (Decimal::Compare (Decimal (" -99999999999999999999999999999999999999" ),
732+ Decimal (" -99999999999999999999999999999999999999" ), 2 , 3 ),
733+ std::partial_ordering::less);
734+ // -10^37 + 1 scale no overflow
735+ ASSERT_EQ (Decimal::Compare (Decimal (" -9999999999999999999999999999999999999" ),
736+ Decimal (" -99999999999999999999999999999999999999" ), 2 , 3 ),
737+ std::partial_ordering::greater);
738+
739+ // equal values with different scales
740+ ASSERT_EQ (Decimal::Compare (Decimal (" 123456789" ), Decimal (" 1234567890" ), 2 , 3 ),
741+ std::partial_ordering::equivalent);
742+ ASSERT_EQ (Decimal::Compare (Decimal (" -1234567890" ), Decimal (" -123456789" ), 3 , 2 ),
743+ std::partial_ordering::equivalent);
744+
745+ // different values with different scales
746+ ASSERT_EQ (Decimal::Compare (Decimal (" 123456788" ), Decimal (" 1234567890" ), 2 , 3 ),
747+ std::partial_ordering::less);
748+ ASSERT_EQ (Decimal::Compare (Decimal (" -1234567890" ), Decimal (" -123456788" ), 2 , 3 ),
749+ std::partial_ordering::less);
750+
751+ // different values with same scales
752+ ASSERT_EQ (Decimal::Compare (Decimal (" 123456790" ), Decimal (" 123456789" ), 2 , 2 ),
753+ std::partial_ordering::greater);
754+ ASSERT_EQ (Decimal::Compare (Decimal (" -123456790" ), Decimal (" -123456789" ), 2 , 2 ),
755+ std::partial_ordering::less);
756+
757+ // different signs
758+ ASSERT_EQ (Decimal::Compare (Decimal (" 123456789" ), Decimal (" -123456789" ), 2 , 3 ),
759+ std::partial_ordering::greater);
760+ ASSERT_EQ (Decimal::Compare (Decimal (" -123456789" ), Decimal (" 123456789" ), 2 , 3 ),
761+ std::partial_ordering::less);
762+
763+ // zero comparisons
764+ ASSERT_EQ (Decimal::Compare (Decimal (" 0" ), Decimal (" 0" ), 2 , 3 ),
765+ std::partial_ordering::equivalent);
766+ ASSERT_EQ (Decimal::Compare (Decimal (" 0" ), Decimal (" 123456789" ), 2 , 3 ),
767+ std::partial_ordering::less);
768+ ASSERT_EQ (Decimal::Compare (Decimal (" -123456789" ), Decimal (" 0" ), 2 , 3 ),
769+ std::partial_ordering::less);
770+ }
771+
674772} // namespace iceberg
0 commit comments