Esempio n. 1
0
        public void TestSubtract()
        {
            HugeNumber eleven = new HugeNumber()
            {
                true, true, true, true, true
            };
            HugeNumber twelve = new HugeNumber()
            {
                false, false, true, true, true
            };
            HugeNumber thirteen = new HugeNumber()
            {
                true, false, true, true, true
            };
            HugeNumber one = new HugeNumber {
                true
            };

            CollectionAssert.AreEqual(one.Negate(), eleven - twelve, "Add failed 11-12.");
            CollectionAssert.AreEqual(one, twelve - eleven, "Add failed 12-11.");
            HugeNumber twentythree = new HugeNumber {
                true, true, false, true, false, true, true
            };

            CollectionAssert.AreEqual(twentythree, twelve - eleven.Negate(), "Add failed 12--11.");
        }
Esempio n. 2
0
        public void TestAdd()
        {
            HugeNumber eleven = new HugeNumber()
            {
                true, true, true, true, true
            };
            HugeNumber twelve = new HugeNumber()
            {
                false, false, true, true, true
            };
            HugeNumber thirteen = new HugeNumber()
            {
                true, false, true, true, true
            };
            HugeNumber twentythree = new HugeNumber {
                true, true, false, true, false, true, true
            };
            HugeNumber minus_twentythree = twentythree.Negate();

            CollectionAssert.AreEqual(twentythree, eleven + twelve, "Add failed 11+12.");
            CollectionAssert.AreEqual(twentythree, twelve + eleven, "Add failed 12+11.");
            CollectionAssert.AreEqual(new HugeNumber()
            {
                true
            }, twelve + eleven.Negate(), "Add failed 12+-11.");
            CollectionAssert.AreEqual(new HugeNumber()
            {
            }, twentythree + minus_twentythree, "Add failed 23+-23.");
        }
Esempio n. 3
0
        public void TestNegate()
        {
            HugeNumber input = new HugeNumber()
            {
                true, true, false, false, true
            };                                                                                  // 15
            HugeNumber expected = new HugeNumber()
            {
                true, false, false, false, true, true
            };                                                                                            // -15
            HugeNumber result = input.Negate();

            CollectionAssert.AreEqual(expected, result, "Failed on negate of 15.");
            CollectionAssert.AreEqual(input, result.Negate(), "Failed on negate of -15.");
        }
Esempio n. 4
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.");
        }