示例#1
0
        public void CharUnitTest()
        {
            Random   rnd = new Random((int)DateTime.Now.Ticks);
            CharUnit cu  = new CharUnit();

            Assert.IsTrue(cu.Compare(new CharConst((char)rnd.Next(char.MaxValue + 1))));
            Assert.IsTrue(cu.Compare(new CharConst((char)rnd.Next(char.MaxValue + 1))));
            Assert.IsTrue(cu.Compare(new CharConst((char)rnd.Next(char.MaxValue + 1))));
            Assert.IsTrue(cu.Compare(new CharConst((char)rnd.Next(char.MaxValue + 1))));
            Assert.IsTrue(cu.Compare(new CharConst((char)rnd.Next(char.MaxValue + 1))));
            Assert.IsTrue(cu.Compare(new CharConst(char.MinValue)));
            Assert.IsTrue(cu.Compare(new CharConst(char.MaxValue)));
            Assert.IsFalse(cu.Compare(new StringConst("dd")));
            Assert.IsFalse(cu.Compare(new LongConst(7)));
            Assert.IsTrue(cu.Compare(new CharConst('\a')));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            TestContext.WriteLine(cu.Random().ToString());
            cu = new CharUnit('y');
            Assert.IsTrue(cu.Compare(new CharConst('y')));
            Assert.IsFalse(cu.Compare(new CharConst('z')));
            Assert.IsFalse(cu.Compare(new CharConst('x')));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            TestContext.WriteLine(cu.Random().ToString());
            cu = new CharUnit('d', 'n');
            Assert.IsFalse(cu.Compare(new CharConst('a')));
            Assert.IsTrue(cu.Compare(new CharConst('d')));
            Assert.IsTrue(cu.Compare(new CharConst('f')));
            Assert.IsTrue(cu.Compare(new CharConst('k')));
            Assert.IsTrue(cu.Compare(new CharConst('n')));
            Assert.IsFalse(cu.Compare(new CharConst('o')));
            Assert.IsFalse(cu.Compare(new CharConst('z')));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            TestContext.WriteLine(cu.Random().ToString());

            cu = new CharUnit('z', 'r');
            Assert.ThrowsException <ArgumentException>(() => cu.Random());

            cu = new CharUnit('a', CompareMethod.Not);
            Assert.IsTrue(cu.Compare(new CharConst('d')));
            Assert.IsTrue(cu.Compare(new CharConst('f')));
            Assert.IsFalse(cu.Compare(new CharConst('a')));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));
            Assert.IsTrue(cu.Compare(cu.Random()));

            cu = new CharUnit('a', 'z', CompareMethod.NotMinMax);
            Assert.IsFalse(cu.Compare(new CharConst('d')));
            Assert.IsTrue(cu.Compare(new CharConst('A')));
            Assert.IsFalse(cu.Compare(new CharConst('a')));
            Assert.IsFalse(cu.Compare(new CharConst('z')));
            Assert.IsTrue(cu.Compare(new CharConst('-')));

            char c;

            for (int i = 0; i < 500; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu = new CharUnit("dhfejuteATR");
            for (int i = 0; i < 1000; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu.CompareMethod = CompareMethod.NotSelect;
            for (int i = 0; i < 1000; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu = CharUnit.Letter;
            for (int i = 0; i < 1000; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu = CharUnit.Space;
            for (int i = 0; i < 300; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu = CharUnit.NotLetter;
            for (int i = 0; i < 3000; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu = CharUnit.NotSpace;
            for (int i = 0; i < 300; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            cu = CharUnit.NotDigit;
            for (int i = 0; i < 300; i++)
            {
                Assert.IsTrue(cu.Compare(cu.Random()));
            }

            //for (int i = 0; i < 300; i++)
            //{
            //    //TestContext.Write(cu.Random().ToString());
            //    TestContext.Write((Convert.ToInt32(cu.Random()).ToString() + ","));
            //    if (i % 50 == 0)
            //        TestContext.WriteLine("");
            //}
        }
示例#2
0
 internal bool Match(CharUnit charUnit)
 {
     return(charUnit.Char == _expectedChar);
 }