Пример #1
0
        public void Union()
        {
            //begin
            //x = (find 'a(b|c)' in abc.txt) union (find 'bc in abc.txt)
            //end

            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            AssignmentStatement as1 = new AssignmentStatement();
            statement.AssignmentStatement = as1;

            Exp exp = new Exp();
            as1.Exp = exp;

            #region First find

            Term t1 = new Term();
            exp.Term = t1;

            Regex r1 = new Regex();
            r1.Pattern = "([a-zA-Z])*ment([a-zA-Z])*";
            t1.Regex = r1;

            Filename file1 = new Filename();
            file1.Path = "../../../TestFiles/file1.txt";
            t1.Filename = file1;

            #endregion

            ExpTail expTail = new ExpTail();
            exp.Tail = expTail;

            BinOp bop = new BinOp();
            expTail.Binop = bop;

            StringNode operation = new StringNode("union");
            bop.Operation = operation;

            #region Second find

            Term t2 = new Term();
            expTail.Term = t2;

            ExpTail expTail2 = new ExpTail();
            expTail.Tail = expTail2;

            Regex r2 = new Regex();
            r2.Pattern = "([a-zA-Z])*(a|A)([a-zA-Z])*";
            t2.Regex = r2;

            Filename file2 = new Filename();
            file2.Path = "../../../TestFiles/file2.txt";
            t2.Filename = file2;

            #endregion

            miniRe.Execute(table);
            StringMatchList matches = (StringMatchList) table["x"];

            Assert.AreEqual(8, matches.Length);

            String expected = "{\"mentor\"<'../../../TestFiles/file1.txt', 1>" +
                "\"filament\"<'../../../TestFiles/file1.txt', 1>" +
                "\"argument\"<'../../../TestFiles/file1.txt', 1>" +
                "\"argumentative\"<'../../../TestFiles/file1.txt', 1><'../../../TestFiles/file2.txt', 1>" +
                "\"Tournament\"<'../../../TestFiles/file1.txt', 1>" +
                "\"Argument\"<'../../../TestFiles/file2.txt', 1>" +
                "\"predicament\"<'../../../TestFiles/file2.txt', 1>" +
                "\"apple\"<'../../../TestFiles/file2.txt', 1>" +
            "}";

            Assert.AreEqual(expected, matches.ToString());
        }
Пример #2
0
        public void Replace()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            #region Statement 3

            Statement s3 = new Statement();
            tail2.Statement = s3;

            OtherStatement os = new OtherStatement();
            os.Mode = OtherStatementMode.Replace;
            os.ReplaceText = "b";
            s3.OtherStatement = os;

            Regex regex3 = new Regex();
            regex3.Pattern = "(a|A)";
            os.Regex = regex3;

            Filenames filenames = new Filenames();
            os.Filenames = filenames;

            Filename filename2 = new Filename();
            filename2.Path = "../../../TestFiles/file2.txt";
            filenames.Filename = filename2;

            Filename destimation = new Filename();
            destimation.Path = "../../../TestFiles/file3.txt";
            filenames.Destimation = destimation;

            #endregion

            miniRe.Execute(table);

            String expected = "brgument brgumentbtive predicbment mentoring bpple";
            StringBuilder actual = new StringBuilder();
            using(FileStream fs = new FileStream("../../../TestFiles/file3.txt", FileMode.Open))
            {
                using(StreamReader sr = new StreamReader(fs))
                {
                    actual.Append(sr.ReadToEnd());
                }
            }
            Assert.AreEqual(expected, actual.ToString());
        }
Пример #3
0
        public void MiniRE()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            miniRe.Execute(table);

            object value = table["x"];

            Assert.AreEqual(7, (int)table["y"]);
        }
Пример #4
0
        public void Print()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            #region Statement 3

            Statement s3 = new Statement();
            tail2.Statement = s3;

            OtherStatement print = new OtherStatement();
            print.Mode = OtherStatementMode.Print;
            s3.OtherStatement = print;

            ExpList expList = new ExpList();
            print.ExpList = expList;

            Exp exp3 = new Exp();
            exp3.Id = new StringNode("y");
            expList.Exp = exp3;

            #endregion

            using (MemoryStream ms = new MemoryStream())
            {
                StreamWriter sw = new StreamWriter(ms);
                Console.SetOut(sw);
                miniRe.Execute(table);
                sw.Flush();

                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms))
                {
                    String actual = sr.ReadToEnd();
                    Assert.AreEqual(7, Int32.Parse(actual));
                }
            }
        }
Пример #5
0
        public void Maxfreqstr()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            AssignmentStatement as1 = new AssignmentStatement();
            statement.AssignmentStatement = as1;

            Exp exp1 = new Exp();
            as1.Exp = exp1;

            Term term = new Term();
            exp1.Term = term;

            Regex r1 = new Regex();
            r1.Pattern = "[a-z]([a-z])*";
            term.Regex = r1;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/maxfreq.txt";
            term.Filename = filename;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.MaxFreqString;
            as2.Id = new StringNode("x");
            s2.AssignmentStatement = as2;

            miniRe.Execute(table);
            Assert.AreEqual("hey", table["y"]);
        }