RandomEInteger() 공개 정적인 메소드

public static RandomEInteger ( RandomGenerator r ) : PeterO.Numbers.EInteger
r PeterO.RandomGenerator
리턴 PeterO.Numbers.EInteger
예제 #1
0
        public static CBORObject RandomNumberOrRational(RandomGenerator rand)
        {
            switch (rand.UniformInt(7))
            {
            case 0:
                return(CBORObject.FromObject(
                           RandomObjects.RandomDouble(
                               rand,
                               Int32.MaxValue)));

            case 1:
                return(CBORObject.FromObject(
                           RandomObjects.RandomSingle(
                               rand,
                               Int32.MaxValue)));

            case 2:
                return(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(RandomObjects.RandomEFloat(rand)));

            case 4:
                return
                    (CBORObject.FromObject(RandomObjects.RandomEDecimal(rand)));

            case 5:
                return(CBORObject.FromObject(RandomObjects.RandomInt64(rand)));

            case 6:
                return(CBORObject.FromObject(RandomObjects.RandomERational(rand)));

            default: throw new ArgumentException();
            }
        }
예제 #2
0
        public void TestCompareTo()
        {
            var r = new RandomGenerator();

            for (var i = 0; i < 500; ++i)
            {
                ERational bigintA = RandomObjects.RandomERational(r);
                ERational bigintB = RandomObjects.RandomERational(r);
                ERational bigintC = RandomObjects.RandomERational(r);
                TestCommon.CompareTestRelations(bigintA, bigintB, bigintC);
            }
            TestCommon.CompareTestLess(ERational.Zero, ERational.NaN);
            ERational rat, rat2;

            for (var i = 0; i < 100; ++i)
            {
                EInteger num = RandomObjects.RandomEInteger(r);
                if (num.IsZero)
                {
                    // Skip if number is 0; 0/1 and 0/2 are
                    // equal in that case
                    continue;
                }
                num  = num.Abs();
                rat  = ERational.Create(num, EInteger.One);
                rat2 = ERational.Create(num, (EInteger)2);
                TestCommon.CompareTestLess(rat2, rat);
                TestCommon.CompareTestGreater(rat, rat2);
            }
            TestCommon.CompareTestLess(
                ERational.Create(EInteger.One, (EInteger)2),
                ERational.Create((EInteger)4, EInteger.One));
            for (var i = 0; i < 100; ++i)
            {
                EInteger num = RandomObjects.RandomEInteger(r);
                EInteger den = RandomObjects.RandomEInteger(r);
                if (den.IsZero)
                {
                    den = EInteger.One;
                }
                rat = ERational.Create(num, den);
                for (int j = 0; j < 10; ++j)
                {
                    EInteger num2 = num;
                    EInteger den2 = den;
                    EInteger mult = RandomObjects.RandomEInteger(r);
                    if (mult.IsZero || mult.Equals(EInteger.One))
                    {
                        mult = (EInteger)2;
                    }
                    num2 *= (EInteger)mult;
                    den2 *= (EInteger)mult;
                    rat2  = ERational.Create(num2, den2);
                    TestCommon.CompareTestEqual(rat, rat2);
                }
            }
        }
예제 #3
0
        public void TestCompareToDecimal()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 100; ++i)
            {
                ERational er  = RandomObjects.RandomERational(fr);
                int       exp = -100000 + fr.UniformInt(200000);
                EDecimal  ed  = EDecimal.Create(
                    RandomObjects.RandomEInteger(fr),
                    (EInteger)exp);
                ERational er2 = ERational.FromEDecimal(ed);
                int       c2r = er.CompareTo(er2);
                int       c2d = er.CompareToDecimal(ed);
                Assert.AreEqual(c2r, c2d);
            }
        }
예제 #4
0
        public static CBORObject RandomNumberOrRational(IRandomGenExtended rand)
        {
            object o = null;

            switch (rand.GetInt32(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 2:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(CBORObject.FromObject(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(CBORObject.FromObject(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(CBORObject.FromObject(o));

            default: throw new InvalidOperationException();
            }
        }
예제 #5
0
        public static CBORObject RandomNumberOrRational(RandomGenerator rand)
        {
            object o = null;

            switch (rand.UniformInt(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 2:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            default: throw new InvalidOperationException();
            }
        }
예제 #6
0
        public void TestRemainder()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 100; ++i)
            {
                ERational er;
                ERational er2;
                er = ERational.Create(
                    RandomObjects.RandomEInteger(fr),
                    EInteger.One);
                er2 = ERational.Create(
                    RandomObjects.RandomEInteger(fr),
                    EInteger.One);
                if (er2.IsZero || !er2.IsFinite)
                {
                    continue;
                }
                if (er.IsZero || !er.IsFinite)
                {
                    // Code below will divide by "er",
                    // so skip if "er" is zero
                    continue;
                }
                ERational ermult = er.Multiply(er2);
                ERational erdiv  = ermult.Divide(er);
                erdiv = ermult.Remainder(er);
                if (!erdiv.IsZero)
                {
                    Assert.Fail(ermult + "; " + er);
                }
                erdiv = ermult.Remainder(er2);
                if (!erdiv.IsZero)
                {
                    Assert.Fail(er + "; " + er2);
                }
            }
        }
예제 #7
0
        public static CBORObject RandomCBORTaggedObject(
            IRandomGenExtended rand,
            int depth)
        {
            var tag = 0;

            if (rand.GetInt32(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.GetInt32(tagselection.Length)];
            }
            else if (rand.GetInt32(100) < 90)
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           rand.GetInt32(0x100000)));
            }
            else
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           RandomEIntegerMajorType0(rand)));
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(CBORObject.FromObject(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = CBORObject.FromObject(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                return(CBORObject.FromObjectAndTag(cbor, tag));
            }
        }
예제 #8
0
        public static CBORObject RandomCBORTaggedObject(
            RandomGenerator rand,
            int depth)
        {
            var tag = 0;

            if (rand.UniformInt(2) == 0)
            {
                int[] tagselection = { 2,  2, 2,  3,  3, 3, 4, 4, 4, 5, 5, 5, 30, 30,
                                       30, 0, 1, 25, 26, 27 };
                tag = tagselection[rand.UniformInt(tagselection.Length)];
            }
            else
            {
                tag = rand.UniformInt(0x1000000);
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                return(CBORObject.FromObject(RandomObjects.RandomByteString(rand)));
            }
            for (var i = 0; i < 15; ++i)
            {
                CBORObject o;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    o = CBORObject.FromObject(RandomObjects.RandomByteStringShort(rand));
                }
                else if (tag == 4 || tag == 5)
                {
                    o = CBORObject.NewArray();
                    o.Add(CBORObject.FromObject(RandomObjects.RandomSmallIntegral(rand)));
                    o.Add(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));
                }
                else if (tag == 30)
                {
                    o = CBORObject.NewArray();
                    o.Add(CBORObject.FromObject(RandomObjects.RandomSmallIntegral(rand)));
                    o.Add(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));
                }
                else
                {
                    o = RandomCBORObject(rand, depth + 1);
                }
                try {
                    o = CBORObject.FromObjectAndTag(o, tag);
                    // Console.WriteLine("done");
                    return(o);
                } catch (Exception) {
                    continue;
                }
            }
            // Console.WriteLine("Failed "+tag);
            return(CBORObject.Null);
        }
예제 #9
0
        public void TestFromString()
        {
            ERational er;

            er = ERational.FromString("-2/4");
            Assert.AreEqual(EInteger.FromInt32(-2), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("2/4");
            Assert.AreEqual(EInteger.FromInt32(2), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("293939393939/4");
            Assert.AreEqual(EInteger.FromString("293939393939"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("-293939393939/4");
            Assert.AreEqual(EInteger.FromString("-293939393939"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("-2/293939393939");
            Assert.AreEqual(EInteger.FromInt32(-2), er.Numerator);
            Assert.AreEqual(EInteger.FromString("293939393939"), er.Denominator);
            er = ERational.FromString("-2");
            Assert.AreEqual(EInteger.FromString("-2"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(1), er.Denominator);
            er = ERational.FromString("2");
            Assert.AreEqual(EInteger.FromString("2"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(1), er.Denominator);
            try {
                ERational.FromString("-2x");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ERational.FromString("-2/");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ERational.FromString("-2/x");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ERational.FromString("-2/2x");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            var fr = new RandomGenerator();

            for (var i = 0; i < 1000; ++i)
            {
                EInteger ei1 = RandomObjects.RandomEInteger(fr);
                EInteger ei2 = RandomObjects.RandomEInteger(fr).Abs();
                if (ei2.IsZero)
                {
                    ei2 = EInteger.One;
                }
                er = ERational.FromString(ei1 + "/" + ei2);
                Assert.AreEqual(ei1, er.Numerator);
                Assert.AreEqual(ei2, er.Denominator);
            }
        }
예제 #10
0
        public static CBORObject RandomCBORTaggedObject(
            RandomGenerator rand,
            int depth)
        {
            var tag = 0;

            if (rand.UniformInt(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.UniformInt(tagselection.Length)];
            }
            else
            {
                tag = rand.UniformInt(0x1000000);
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = ToObjectTest.TestToFromObjectRoundTrip(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                try {
                    cbor = CBORObject.FromObjectAndTag(cbor, tag);
                    // Console.WriteLine("done");
                    return(cbor);
                } catch (Exception) {
                    return(CBORObject.FromObjectAndTag(cbor, 999));
                }
            }
        }