Example #1
0
 public void CastToULongOverflow()
 {
     Random random = new Random();
     UInt128 value;
     ulong overflow = random.NextULong(ulong.MaxValue);
     try
     {
         value = (UInt128)(((BigInteger)ulong.MaxValue) + overflow + 1);
     }
     catch (Exception)
     {
         Assert.Fail();
         return;
     }
     Assert.AreEqual(overflow, (ulong)value);
 }
Example #2
0
        public void UInt128Test()
        {
            Random random = new Random();
            for (int i = 0; i != 1000; ++i)
            {
                UInt128 value = random.NextUInt128();

                // Test comparisons.
                Assert.AreEqual(value, value);
                Assert.AreNotEqual(value, string.Empty);
                Assert.AreNotEqual(value, UInt128.MaxValue);
                Assert.AreNotEqual(value, UInt128.Zero);
                // ReSharper disable EqualExpressionComparison
                Assert.IsTrue(value == value);
                Assert.IsFalse(value != value);
                Assert.IsTrue(value <= value);
                Assert.IsTrue(value >= value);
                // ReSharper restore EqualExpressionComparison
                if (value != UInt128.MaxValue)
                {
                    Assert.IsTrue(value < value + 1);
                    Assert.IsTrue(value <= value + 1);
                    Assert.IsTrue(value + 1 > value);
                    Assert.IsTrue(value + 1 >= value);
                }

                // Test GetHashCode
                Assert.IsNotNull(value.GetHashCode());

                // Test Parse()
                Assert.AreEqual(value, UInt128.Parse(value.ToString()));
                Assert.AreEqual(value, UInt128.Parse(value.ToString(), CultureInfo.InvariantCulture));
                Assert.AreEqual(value, UInt128.Parse(value.ToString(), NumberStyles.Integer));

                // Test TryParse()
                UInt128 actualValue;
                Assert.IsTrue(UInt128.TryParse(value.ToString(), out actualValue));
                Assert.AreEqual(value, actualValue);
                Assert.IsTrue(UInt128.TryParse(value.ToString(CultureInfo.InvariantCulture), out actualValue));
                Assert.AreEqual(value, actualValue);

                // Cast to UInt64
                ulong smallValue = random.NextULong();
                Assert.AreEqual(smallValue, (ulong)((UInt128)smallValue));
            }
        }