Exemplo n.º 1
0
        public void TestToInt()
        {
            HugeNumber result = new HugeNumber()
            {
                true, true, false, false, true
            };
            long expected = 15;

            Assert.AreEqual(expected, result.ToLong(), "ToLong failed for 15.");
            result = new HugeNumber()
            {
                true, false, true, false, true, true
            };
            expected = -11;
            Assert.AreEqual(expected, result.ToLong(), "ToLong failed for 15.");
        }
Exemplo n.º 2
0
        public void TestAddPerformance()
        {
            Random rnd = new Random(23);

            for (int i = 0; i < 100; ++i)
            {
                int        a = rnd.Next(short.MaxValue);
                int        b = rnd.Next() * rnd.Next(short.MaxValue);
                HugeNumber x = new HugeNumber(a);
                HugeNumber y = new HugeNumber(b);
                HugeNumber z = x + y;
                Assert.AreEqual(a + b, z.ToLong(), "Addition failed.");
                Assert.AreEqual(a + b, (y + x).ToLong(), "Addition failed.");
            }
            for (int i = 0; i < 100; ++i)
            {                     // big numbers
                long       a = rnd.Next(short.MaxValue) * int.MaxValue;
                long       b = rnd.Next(short.MaxValue) * rnd.Next(short.MaxValue) * int.MaxValue;
                HugeNumber x = new HugeNumber(a);
                HugeNumber y = new HugeNumber(b);
                HugeNumber z = x + y;
                Assert.AreEqual(0, (z).ToLong() - (a + b), "Addition failed.");
            }
            for (int i = 0; i < 1000; ++i)
            {
                int        a = -rnd.Next(short.MaxValue);           // test with negatives
                int        b = rnd.Next(short.MaxValue) * rnd.Next(short.MaxValue);
                HugeNumber x = new HugeNumber(a);
                HugeNumber y = new HugeNumber(b);
                HugeNumber z = x + y;
                Assert.AreEqual(a + b, z.ToLong(), "Addition failed.");
                Assert.AreEqual(a + b, (y + x).ToLong(), "Addition failed.");
            }
            int        v = rnd.Next(short.MaxValue);
            HugeNumber V = new HugeNumber(v);

            Assert.AreEqual(0, (V + V.Negate()).ToLong(), "Addition to 0 failed.");
        }