Code covered by the BSD License  

Highlights from
Benchmark Problem 02 - Matlab Code

image thumbnail

Benchmark Problem 02 - Matlab Code

by

 

21 May 2013 (Updated )

This is the Matlab code for the model Benchmark Problem 02.

insert_mutants.m
% InsertMutants is used to insert various mutants in a .m file.
%
% Inputs:
% scriptfile -- The original .m files in which the mutants are to added.
%               The mutants are inserted only between the comment lines
%               saying 'START HERE' and 'STOP HERE'
% MutantList -- A cell array of mutants
%               The script replaces each occurance of an element with all other
%               elements.
% MutantName -- Name of the output mutant file will start with the name
%               specified in the varible along with a Mutant Number.
% Outputs:
% Mutants files -- Each mutant is stored as a new .m file and stored in the
%                  'MatlabMutants' folder.
% ListMutants   -- Text file containing the list of Information reagarding
%                   all the mutants generated.

% (c) Chethan C U <chethan.cu@gmail.com>

scriptfile = 'Original.m';        % Your file

MutantName = 'Mutant';
if ~exist('MatlabMutants','dir')
    mkdir('MatlabMutants');
end
dirctPath = [pwd '\' 'MatlabMutants\'];   % This variable can be updated to any desired user path
%dirctPath = 'C:\matlab_mutants\';  % Your path
% Counter to keep track of the no. of mutants.
% The same counter is used to name the mutant files.
MutantCnt=0;

for imut = 1:5
    % Arithmetic Mutants
    if imut==1
        MutantList={'+','-','*','+1+','-1+','/'};

        % Relational Operator Mutants
    elseif imut==2
        MutantList={'>','<','>=','<=','==','~='};

        % Variable Mutants
    elseif imut==3
        MutantList={'inp1','inp2','inp3','inp4','inp5','inp6','inp7','inp8','inp9','inp10','inp11','inp12','inp13','inp14','inp15','inp16','inp17','inp18','inp19','inp20','inp21','DtOn1','DtOff1','DtOn','DtOff','ftime2','ftime1','ftime7','ftime3',...
            'ftime4','a1','a0','sn1','sn0','up','lo','LO3','ul','ll','outRL','I1toRL','ySwitch1','S1','ySwitch','S3','outper','S2','Product2','Sum2','y1','Sum3','pinp11','F2','delt','out_2','y','out1','LFPro1','LFPro3','LFSum2','LFout',...
            'SOP1','Sum5','out2','Product1','Sum5','Gain3','sw1','integout','LF1out','LF1Sum2','Product4','F7','Sum4','dtx1','INP3','Sum5','y4','out6','Sumx1','y3','SOP4'};

        % Logical Mutants
    elseif imut==4
        MutantList={'and(','or(','xor(','~and(','~or(','~xor('};

        % Data Mutants
    elseif imut==5
        MutantList={'50','200','150','2.5','51','49','10.0','1.5','1.9','7.5','3.5','6.2','12.4','8.0','0.39','1.32','1.56','3.25','5.0','0.45','1.0'};
    end
    % Read the Original .m file as a cell array
    scriptdata = textread(scriptfile,'%s','delimiter','\n');

    % Find line Number containing 'START HERE'. Mutants are inserted after this line.
    StartHere=strfind(upper(scriptdata),'START HERE');
    for q=1:length(StartHere)
        if(~isempty(StartHere{q}))
            StrtLineNo=q;
        end
    end
    % Find line Number containing 'END HERE'. Mutants are inserted until this
    % line.
    EndHere=strfind(upper(scriptdata),'END HERE');
    for q=1:length(EndHere)
        if(~isempty(EndHere{q}))
            EndLineNo=q;
        end
    end
llmut=length(MutantList);
llmut=2;  % Comment this if you want all the 34000 odd mutants

% This can generate 34000 mutants at a go. Be careful. At present the for
% loop is generating 162.
    for ii=1:llmut
        % Find element of MutantList in the Original .m file between 'START HERE' and 'END HERE'
        SrchRes=strfind(scriptdata(StrtLineNo:EndLineNo),MutantList{ii});

        % Check if element of MutantList is found
        if ~isempty(SrchRes)
            lineNos=[];
            % Find all Line Numbers containing element of MutantList
            for kk= 1: length(SrchRes)
                if ~isempty(SrchRes{kk})
                    lineNos=[lineNos;kk+StrtLineNo-1];
                end
            end

            for kk= 1:length(lineNos)
                % Replace the element of MutantList with other elements of
                % MutantList
                for jj= 1:llmut
                    % Skip for same element
                    if ii~=jj
                        mutant = scriptdata;
                        NoBlnks=cell2mat(strtrim(mutant(lineNos(kk))));
                        % Skip if the line is a comment
                        if NoBlnks(1)=='%'
                            break;
                        end
                        if ~isempty(strfind(NoBlnks,'TAPOUT'))
                            break;
                        end
                        % find if the same element is repeated in the same line
                        RepsInsameLine=strfind(mutant(lineNos(kk)),MutantList{ii});
                        RepsInsameLine=cell2mat(RepsInsameLine);
                        % for each occurance of element in a line.
                        for lll=1:length(RepsInsameLine)
                            mutant = scriptdata;
                            temp=mutant{lineNos(kk)};
                            temp(RepsInsameLine(lll):RepsInsameLine(lll)+length(MutantList{ii})-1)=[];
                            temp=[temp(1:RepsInsameLine(lll)-1) '$' temp(RepsInsameLine(lll):end)];
                            mutant(lineNos(kk))={strrep(temp,'$',MutantList{jj})};
                            % Mutant Inserted. Increment count
                            MutantCnt=MutantCnt+1;
                            % ListMutants containes a list of all mutants inserted
                            diary ListMutants.txt
                            disp(['% Changed line: ' num2str(lineNos(kk)+1) ' from ' MutantList{ii} ' to ' MutantList{jj}]);
                            diary off
                            % Use diary to store the mutant file
                            eval(['diary ''' dirctPath MutantName num2str(MutantCnt) '.m''']);
                            disp(['% Changed line: ' num2str(lineNos(kk)+1) ' from ' MutantList{ii} ' to ' MutantList{jj}]);
                            for cc=1:length(mutant)
                                disp(mutant{cc})
                            end
                            diary off
                        end
                    end
                end
            end
        end
    end
end







Contact us