Example #1
0
        private void VerifySpecificLines()
        {
            if (!string.IsNullOrWhiteSpace(SpecificLines))
            {
                const char separator = ':';
                var        range     = SpecificLines.Split(separator);
                if (range.Length == 2)
                {
                    var minValid = uint.TryParse(range[0], out var minimum);
                    var maxValid = uint.TryParse(range[1], out var maximum);

                    if (!minValid || !maxValid || maximum < minimum)
                    {
                        throw new MuTestInputException(ErrorMessage, $"Invalid Specific Line Range {CliOptions.SpecificLineRange.ArgumentDescription}");
                    }
                }
            }
        }
        public void Mutate(string sourceFile)
        {
            Mutants = new Collection <CppMutant>();
            if (sourceFile == null || !File.Exists(sourceFile))
            {
                return;
            }

            var codeLines = File.ReadAllLines(sourceFile);

            var skipList = new List <string>
            {
                "//",
                "#",
                BlockOpen,
                BlockClosed,
                "};",
                "});",
                ")",
                "public:",
                "private:",
                "protected:",
                Extern,
                VoId,
                Using,
                Catch,
                Namespace,
                Typedef,
                Static,
                Class,
                Trace,
                Delete
            };

            const char separator  = ':';
            var        lineNumber = 0;
            string     line;
            var        insideCommentedCode = false;
            var        id = 0;

            var minimum = -1;
            var maximum = int.MaxValue;

            if (!string.IsNullOrWhiteSpace(SpecificLines))
            {
                var range = SpecificLines.Split(separator);
                minimum = Convert.ToInt32(range[0]);
                maximum = Convert.ToInt32(range[1]);
            }

            for (var lineIndex = 0; lineIndex < codeLines.Length; lineIndex++)
            {
                lineNumber++;
                line = codeLines[lineIndex].Trim();

                if (lineNumber < minimum ||
                    lineNumber > maximum)
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(line) ||
                    skipList.Any(x => line.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)) ||
                    line.EndsWith("):"))
                {
                    continue;
                }

                if (line.StartsWith("/*") || line.EndsWith("/*"))
                {
                    insideCommentedCode = true;
                }

                if (line.EndsWith("*/"))
                {
                    insideCommentedCode = false;
                    continue;
                }

                if (!insideCommentedCode)
                {
                    var codeLine = new CodeLine
                    {
                        Line          = line,
                        LineNumber    = lineNumber,
                        EndLineNumber = lineNumber
                    };

                    AddStringsInsideLines(line, codeLine);
                    AddCommentsInsideLine(line, codeLine);
                    AddIfBlocks(line, codeLine, lineIndex, codeLines);

                    foreach (var mutator in Mutators)
                    {
                        var cppMutants = mutator.Mutate(codeLine).ToList();
                        foreach (var mutant in cppMutants)
                        {
                            mutant.Id = id++;
                            mutant.Mutation.EndLineNumber = codeLine.EndLineNumber;
                            Mutants.Add(mutant);
                        }
                    }
                }
            }
        }