示例#1
0
        /**
         * Test to see if the NFA was generated correctly
         */
        public static Boolean NFAGenerationTest(NFA nfa)
        {
            // Min./Max. Index Test
            // All node indexes are less than the terms maximum index
            var maxErrors = nfa.GetAllowableErrors();
            var p         = maxErrors + 1;

            var minIndex = nfa.GetStartState();
            var maxIndex = (2 << (nfa.GetTerm().Length + p)) + (2 << maxErrors);

            var nodes = nfa.getNodes().ToArray();

            // Each node must be less than or equal to the maximum index in order to pass
            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i] < minIndex)
                {
                    Console.WriteLine("Minimum Index Test Failed: " + "\n" + "\t" +
                                      "Node Index: " + nodes[i] + " is under the minimum index of " + minIndex);
                    return(false);
                }
                else if (nodes[i] > maxIndex)
                {
                    Console.WriteLine("Maximum Index Test Failed: " + "\n" + "\t" +
                                      "Node Index: " + nodes[i] + " is over the maximum index of " + maxIndex);
                    return(false);
                }
            }

            Console.WriteLine("Min./Max. Index Test Passed");

            // Max Errors Test
            int numErrors = 0;
            int num       = 0;
            int power;

            for (int i = 0; i < nodes.Length; i++)
            {
                power     = nfa.GetPower(nodes[i]);
                numErrors = nfa.GetPower((int)(nodes[i] - Math.Pow(2, power)));
                if (numErrors > maxErrors)
                {
                    Console.WriteLine("Max Errors Test Failed: " + "\n" + "\t" +
                                      "Node Index: " + nodes[i] + " has " + ((nodes[i] % 10) - maxErrors)
                                      + " more error(s) than is allowed (" + maxErrors + ")");
                    return(false);
                }
            }

            Console.WriteLine("Max Errors Test Passed");

            // Valid Next States

            var index = nfa.nextStates.Keys.ToArray();

            string[] input;
            int[]    stateSet;

            for (int i = 0; i < index.Length; i++)
            {
                power     = nfa.GetPower(index[i]);
                num       = power - (p + 1);
                numErrors = nfa.GetPower((int)(nodes[i] - Math.Pow(2, power)));

                input = nfa.nextStates[index[i]].Keys.ToArray();

                for (int j = 0; j < input.Length; j++)
                {
                    stateSet = nfa.nextStates[index[i]][input[j]].ToArray();

                    for (int k = 0; k < stateSet.Length; k++)
                    {
                        var test = (2 << power) + (2 << (numErrors + (maxErrors - numErrors)));
                        if (stateSet[k] > test)
                        {
                            Console.WriteLine("Valid Next States Test Failed: " + "\n" + "\t" +
                                              "Node Index: " + index[i] + " has a next state of " + stateSet[k]);
                            return(false);
                        }
                    }
                }
            }

            Console.WriteLine("Valid Next States Passed");

            Console.WriteLine("NFA Generation Tests Passed");

            return(true);
        }