Example #1
0
        public void PowTest()
        {
            var rand = new XorShift(Seed);

            Assert.Equal(1, ModInt.Raw(100).Pow(0));
            Assert.Equal(100, ModInt.Raw(100).Pow(1));

            for (int i = 0; i < N; i++)
            {
                var x = rand.Next(Mod);
                var n = rand.Next(Mod);

                var actual = ModInt.Raw(x).Pow(n);

                long expected = 1;
                while (n > 0)
                {
                    if ((n & 1) > 0)
                    {
                        expected *= x;
                        expected %= Mod;
                    }
                    x   = (int)((long)x * x % Mod);
                    n >>= 1;
                }

                Assert.Equal(expected, actual);
            }
        }
Example #2
0
        public void DivisionTest()
        {
            var rand = new XorShift(Seed);

            for (int i = 0; i < N; i++)
            {
                var a       = rand.Next(Mod);
                var b       = rand.Next(Mod);
                var divided = new ModInt(a) / b;

                var actual = ((long)divided.Value * b) % Mod;

                Assert.Equal(a, actual);
            }
        }
Example #3
0
        public void EqualTest()
        {
            var rand = new XorShift(Seed);

            for (int i = 0; i < N; i++)
            {
                var a = rand.Next(Mod);
                var b = a + (long)Mod * rand.Next(Mod);

                var ma = new ModInt(a);
                var mb = new ModInt(b);

                Assert.True(ma == mb);
                Assert.False(ma != mb);
            }
        }
Example #4
0
        public void RawTest()
        {
            var rand = new XorShift(Seed);

            RawSubTest(0);
            RawSubTest(Mod - 1);

            for (int i = 0; i < N; i++)
            {
                RawSubTest((int)(rand.Next() % Mod));
            }
        }
Example #5
0
        public void NotEqualTest()
        {
            var rand = new XorShift(Seed);

            for (int i = 0; i < N; i++)
            {
                var a = (long)(rand.Next() % long.MaxValue);
                var b = (long)(rand.Next() % long.MaxValue);

                if (a % Mod == b % Mod)
                {
                    continue;
                }

                var ma = new ModInt(a);
                var mb = new ModInt(b);

                Assert.False(ma == mb);
                Assert.True(ma != mb);
            }
        }
Example #6
0
        public void ToStringTest()
        {
            var rand = new XorShift(Seed);

            for (int i = 0; i < N; i++)
            {
                var a = (long)(rand.Next() % long.MaxValue);

                var m = new ModInt(a);

                Assert.Equal((a % Mod).ToString(), m.ToString());
            }
        }
Example #7
0
        public void NegateTest()
        {
            var rand = new XorShift(Seed);

            for (int i = 0; i < N; i++)
            {
                var m = rand.Next(Mod);

                var actual = +m + -m;

                Assert.Equal(0, actual);
            }
        }
Example #8
0
        public void AddTest()
        {
            var        rand = new XorShift(Seed);
            const long max  = 1L << 60;

            for (int i = 0; i < N; i++)
            {
                var a  = (long)(rand.Next() % (max >> 1) - max);
                var b  = (long)(rand.Next() % (max >> 1) - max);
                var ma = new ModInt(a);
                var mb = new ModInt(b);

                var expected = (a + b) % Mod;
                if (expected < 0)
                {
                    expected += Mod;
                }

                var actual = ma + mb;

                Assert.Equal(expected, actual);
            }
        }
Example #9
0
        public void ConstructorTest()
        {
            var rand = new XorShift(Seed);

            ConstructorSubTest(0);
            ConstructorSubTest(-1);
            ConstructorSubTest(Mod);
            ConstructorSubTest(-Mod);

            for (int i = 0; i < N; i++)
            {
                ConstructorSubTest((long)rand.Next());
            }
        }
Example #10
0
        public void MultiplicationTest()
        {
            var       rand = new XorShift(Seed);
            const int max  = 1 << 30;

            for (int i = 0; i < N; i++)
            {
                var a  = (int)(rand.Next() % (max >> 1)) - max;
                var b  = (int)(rand.Next() % (max >> 1)) - max;
                var ma = new ModInt(a);
                var mb = new ModInt(b);

                var expected = (long)a * b % Mod;
                if (expected < 0)
                {
                    expected += Mod;
                }

                var actual = ma * mb;

                Assert.Equal(expected, actual);
            }
        }