From 46c6603821722176ce1be17eba16d471cbb7a653 Mon Sep 17 00:00:00 2001 From: Suraj Singh Date: Sat, 14 Apr 2018 15:06:44 +0530 Subject: [PATCH] Program complexity measure & analysis --- ...ted_Calculation_Cyclomatic_Complexity.java | 68 +++++ CyclomaticComplexity.java | 57 +++++ CyclomaticComplexity_Using_GraphMatrix.java | 239 ++++++++++++++++++ Decision_Table_Testing.java | 217 ++++++++++++++++ Definition_Paths.java | 228 +++++++++++++++++ EquivalenceTesting.java | 142 +++++++++++ Matcher.java | 27 ++ MutationTesting.java | 125 +++++++++ Mutation_Testing2.java | 82 ++++++ SW_BVA_Auto.java | 78 ++++++ SW_BVA_Robust.java | 74 ++++++ SW_BVA_WorstCaseTesting.java | 127 ++++++++++ SW_BoundaryValueAnalysis.java | 48 ++++ Test.java | 105 ++++++++ Triangle.java | 128 ++++++++++ 15 files changed, 1745 insertions(+) create mode 100644 Automated_Calculation_Cyclomatic_Complexity.java create mode 100644 CyclomaticComplexity.java create mode 100644 CyclomaticComplexity_Using_GraphMatrix.java create mode 100644 Decision_Table_Testing.java create mode 100644 Definition_Paths.java create mode 100644 EquivalenceTesting.java create mode 100644 Matcher.java create mode 100644 MutationTesting.java create mode 100644 Mutation_Testing2.java create mode 100644 SW_BVA_Auto.java create mode 100644 SW_BVA_Robust.java create mode 100644 SW_BVA_WorstCaseTesting.java create mode 100644 SW_BoundaryValueAnalysis.java create mode 100644 Test.java create mode 100644 Triangle.java diff --git a/Automated_Calculation_Cyclomatic_Complexity.java b/Automated_Calculation_Cyclomatic_Complexity.java new file mode 100644 index 0000000..383882b --- /dev/null +++ b/Automated_Calculation_Cyclomatic_Complexity.java @@ -0,0 +1,68 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Scanner; + +/** + * + * @author Suraj Singh + */ +class Automated_Calculation_Cyclomatic_Complexity { + static String[] matching_Expression = new String[]{""}; + public static void main(String arfs[]) throws IOException + { + Scanner s = new Scanner(System.in); + String line,path; + int counter=0,isSwitch=0;// store the number of decision nodes in the DD graph + char choice; + StringBuffer inputFile, matcherFile; + do + { + counter = 0; + System.out.print("Enter file name : "); + path = "C:\\Users\\Suraj Singh\\Desktop\\"+s.next()+".java"; + System.out.print("Do you want source code : "); + choice = s.next().charAt(0); + try + { + BufferedReader r = new BufferedReader(new FileReader(path)); + if(choice=='y'||choice=='Y') + System.out.println("\n*************************************SOURCE CODE*************************************"); + while( (line=r.readLine())!=null) + { + if(choice=='y'||choice=='Y') System.out.println(line); + if(line.matches("(^\\s*)(.*)(if|for|while)(\\(.*)")) + counter++; + else if(line.matches("(^\\s*)switch(\\(.*)")) + isSwitch++; // Beginning of switch statement + else if(line.matches("(^\\s*)case(\\s+)(\\w+):(.*)") && isSwitch>0) // support nested sw + counter++; + else if(line.matches("(^\\s*)default(\\s*):(.*)") && isSwitch>0)//end of switch further case will not be cosidered + { + isSwitch--; + counter++; + } + } + } + catch(FileNotFoundException e) + { + System.out.println(e.getMessage()); + return; + } + if(choice=='y'||choice=='Y') System.out.println("**************************************************************************************"); + System.out.println("\nCYCLOMATIC COMPLEXITY => "+counter); + System.out.print("\nDo you want to continue (y/n)? "); + choice = s.next().charAt(0); + System.out.println(); + }while(choice=='Y'||choice=='y'); + } +} diff --git a/CyclomaticComplexity.java b/CyclomaticComplexity.java new file mode 100644 index 0000000..84facd6 --- /dev/null +++ b/CyclomaticComplexity.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.util.Scanner; + +/** + * + * @author Suraj Singh + */ +//class CyclomaticComplexity { +// public static void main(String arfs[]) +// { +// Scanner s = new Scanner(System.in); +// int i; +// String input = s.next(); +// for(i =0;i 0) + { + System.out.println("Marks obtained "+n); + if(n>50) + { + System.out.println("Student Passed "); + } + else + { + System.out.println("Student Failed"); + } + } + System.out.println("The End !!!"); + } +} \ No newline at end of file diff --git a/CyclomaticComplexity_Using_GraphMatrix.java b/CyclomaticComplexity_Using_GraphMatrix.java new file mode 100644 index 0000000..1326cce --- /dev/null +++ b/CyclomaticComplexity_Using_GraphMatrix.java @@ -0,0 +1,239 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Scanner; +import java.util.Stack; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author Guarav Shah + */ +class graph +{ + graph[] adjacents = new graph[2]; // each node can have two outgoing branches out of it. + String type; // will store the line numbers which this node comprise + int start; + int end; + int index=0; // will represent the number of outgoing edges out of a node. + public String toString() + { + return " N"+this.index+" "+this.start+" "+this.end; + } +} +class CyclomaticComplexity_Using_GraphMatrix { + static ArrayList nodes; + static int number; + public static void print_Graph_Matrix() + { + graph node,left,right; + int graphMatrix[][] = new int[nodes.size()][nodes.size()],value[] = new int[nodes.size()],verticalSum=0; + System.out.println("\n"); + System.out.println("******************** CONNECTION MATRIX ********************"); + System.out.print(" "); + for(int i=0;i scope = new Stack(); // 1 means top element is opening bracket, 2 means closing bracket + Stack stack = new Stack(); + nodes = new ArrayList(); + + int counter=0,lastConditionPoint=1; // lastConditionPoint will store the last time before a condition node + // Pattern definition section + Pattern decision = Pattern.compile("\\b(if|for|while|do)\\b(.*)"); // decision + // left node will store the node on left of conditional node, right node will store the node on right side of the conditional node. + graph node,last=null,left=null,right=null,temp=null; // last node will store the last condition node found in order to trace the path along two outgoing edges. + // Matcher definition section + Matcher dM,bM; + do + { + System.out.print("Enter file name : "); + path = "C:\\Users\\Suraj Singh\\Desktop\\"+s.next()+".txt"; + System.out.println("\n\n******************** FILE CONTENT ********************"); + try + { + BufferedReader r = new BufferedReader(new FileReader(path)); + while( (line=r.readLine())!=null) + { + counter++;// store the line number in the sampled program. + dM = decision.matcher(line); + System.out.print(counter+" "); + System.out.println(line); + if(dM.find()) // We found a conditional node + { +// conditionFound = true; + node = new graph(); + node.start=lastConditionPoint; + node.end = counter;// will store the starting and ending line numbers for which this node is defined. + lastConditionPoint = counter+1; + node.index = ++number; + if(stack.size()!=0) // nested conditional statements + { + temp = stack.peek(); + temp.adjacents[0] = node; // set the left outgoing edge + } + // last = node; // last node is stored in order to set the pointers when scope of this condition ends + stack.push(node); + nodes.add(node); + conditionFound = true; + //nodes.add(new graph()) + // Add code for this node + } + else if(line.contains("{") && conditionFound) // beginning of new block scope + { + conditionFound=false; + scope.add(1); + } + else if(line.contains("else")) + { + alternateNode = true; + } + else if(line.contains("}") && scope.size()!=0 && !alternateNode) // we found the complete block scope with open and closing brackets + { + //inside the scope we need to create one single graph node + node = new graph(); + node.start = lastConditionPoint; + node.end = counter; + node.index = ++number; + last = stack.peek(); + if(left!=null && right!=null) + left.adjacents[0] = right.adjacents[0] = node; + if(last.adjacents[0]==null) + last.adjacents[0] = node; // if condition is true then statements inside scope are executed and form the sibling of last node. + left = node; + nodes.add(node); + mergeNode = false; + lastConditionPoint = counter+1; + scope.pop(); + } + else if(line.contains("}") && alternateNode) // take care of else part + { + node = new graph(); + node.start = lastConditionPoint; + node.end = counter; + node.index = ++number; + nodes.add(node); + last = stack.pop(); + right = node; + //last.adjacents[1] = new graph(); + if(last.adjacents[1]==null) + last.adjacents[1] = node; + lastConditionPoint = counter+1; + alternateNode = false; + mergeNode = true; + last = null; // both left and right are set of the conditional node + } + } // end of while loop + + // To handle the case when last statements are part of some condition statements + node = new graph(); + node.start = lastConditionPoint; + node.end = counter; + node.index = ++number; + nodes.add(node); + if(!mergeNode) // alternateNode = true, both outgoing edges hasn't been taken care of + { + last.adjacents[1] = new graph(); + last.adjacents[1] = node; + } + else // if alternateNode is present, then this is the merge node + { + // set the link of the nodes inside the conditional statements. + left.adjacents[0] = node; + right.adjacents[0] = node; + } + } + catch(Exception e) + { + e.printStackTrace(); + System.out.println(e.toString()); + } + }while(false); // run only once :) + print_Nodes_Lines(); + print_Graph_Matrix(); + } +} + + + + + + \ No newline at end of file diff --git a/Decision_Table_Testing.java b/Decision_Table_Testing.java new file mode 100644 index 0000000..447693b --- /dev/null +++ b/Decision_Table_Testing.java @@ -0,0 +1,217 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.util.Random; +import java.util.Scanner; + +/** + * + * @author Gaurav Kumar Shah + */ +class Decision_Table_Testing { + static String conditions[][] = new String[11][11]; + static StringBuffer sb = new StringBuffer(); + static int numberer; + static String[] stubs = new String[]{"C1: x0) + { + condition1 = condition2 = condition3 = false; + x = m.nextInt(range); + y = m.nextInt(range); + z = m.nextInt(range); + if(x>y+z) + { + condition1=true; + if(!executed[0]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Not a Triangle"); + conditions[0][0] = "0"; + for(j=1;j<6;j++) + conditions[j][0] = "-"; + executed[0]=true; + number--; + conditions[6][0] = "*"; + } + } + if(y > x+z) + { + condition2 = true; + if(!executed[1]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Not a Triangle"); + conditions[0][1] = "1"; + conditions[1][1] = "0"; + for(j=2;j<6;j++) + conditions[j][1] = "-"; + executed[1]=true; + conditions[6][1] = "*"; + number--; + } + } + if(z > x+y) + { + condition3 = true; + if(!executed[2]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Not a Triangle"); + for(j=0;j<2;j++) + conditions[j][2] = "1"; + conditions[2][2] = "0"; + for(j=3;j<6;j++) + conditions[j][2] = "-"; + executed[2]=true; + conditions[6][2] = "*"; + number--; + } + } + if(!condition1 && !condition2 && !condition3 ) + { + if(x==y && y==z && x==z) + { + if(!executed[3]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Equilateral "); + for(j=0;j<6;j++) + conditions[j][3] = "1"; + executed[3]=true; + conditions[7][3] = "*"; + number--; + } + // Make impossible Entries. + else if(!executed[4]) + { + System.out.printf("\n%-9d %-3s %-3s %-3s %-30s",numberer++,"X","X","X","Impossible"); + //System.out.println() + executed[4] = true; + number--; + } + else if(!executed[5]) + { + System.out.printf("\n%-9d %-3s %-3s %-3s %-30s",numberer++,"X","X","X","Impossible"); + executed[5] = true; + number--; + } + else if(!executed[7]) + { + System.out.printf("\n%-9d %-3s %-3s %-3s %-30s",numberer++,"X","X","X","Impossible"); + executed[7] = true; + number--; + } + } + else if(x==y && y!=z && x!=z) + { + if(!executed[6]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Isosceles "); + for(j=0;j<4;j++) + conditions[j][6] = "1"; + for(;j<6;j++) + conditions[j][6] = "0"; + conditions[8][6] = "*"; + executed[6]=true; + number--; + } + } + else if(x!=y && y==z && x!=z) + { + if(!executed[8]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Isosceles "); + executed[8]=true; + for(j=0;j<6;j++) + conditions[j][8] = "1"; + conditions[8][8]="*"; + conditions[3][8] = "0"; + conditions[5][8] = "0"; + number--; + } + } + else if(x!=y && y!=z && x==z) + { + if(!executed[9]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Isosceles "); + executed[9]=true; + for(j=0;j<6;j++) + conditions[j][9] = "1"; + conditions[3][9] = "0"; + conditions[4][9] = "0"; + conditions[8][9] = "*"; + number--; + } + } + else if(x!=y && y!=z && x!=z) + { + if(!executed[10]) + { + System.out.printf("\n%-8d %3d %3d %-4d %-30s",numberer++,x,y,z,"Scalene "); + executed[10]=true; + for(j=0;j<3;j++) + conditions[j][10] = "1"; + for(;j<6;j++) + conditions[j][10] = "0"; + conditions[9][10] = "*"; + number--; + } + } + } + } + } +} diff --git a/Definition_Paths.java b/Definition_Paths.java new file mode 100644 index 0000000..4c231b8 --- /dev/null +++ b/Definition_Paths.java @@ -0,0 +1,228 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author Gaurav Shah + */ +class External_Exam { + public static void main(String arfs[]) throws IOException + { + Scanner s = new Scanner(System.in); + String line,path,defined[],variable,position,assignment,usages,input; + int counter=0; + char choice; + boolean declare; + StringBuffer output = new StringBuffer(); + String[] counts; + // It should include all the data types of the language used. + Pattern declara = Pattern.compile("\\b(int|float|String|long|boolean|double|byte)\\b(.*)"); // declara + //Pattern assignment = Pattern.compile("(\\b[a-z0-9]+\\b)=(.*)"); + Pattern def = Pattern.compile("([a-z][a-z0-9]*)(\\[])?(\\s*=\\s*)?(\\bnew\\b)?(\\b[a-z0-9]+)?(\\[[a-z0-9]+])?[,;]"); +// int coins[] = new int[n]; coins[i] = r.nextInt(); + Pattern assign_definition_via_object = Pattern.compile("([a-z][a-z0-9]*)(\\s*=\\s*)(\\b[a-z0-9]+.[a-z0-9]+)(\\.*)"); + Pattern simple_assign = Pattern.compile("([a-zA-z]+)(\\s*=\\s*)(.*);"); + Pattern usage = Pattern.compile("\\b[a-zA-Z][a-zA-Z0-9]*\\b"); + Matcher findDeclaration,findGeneralize,findSimpleAssign,findGenrealizeViaObject,findUsage; + Set variable_declaration = new HashSet(); + Map definition = new HashMap(); // first parameter is the variable name, second parameter gives the place of definition. + Map used = new HashMap(); + do + { + System.out.print("Enter file name : "); + path = "C:\\Users\\Suraj Singh\\Desktop\\"+s.next()+".txt"; + System.out.println("\n******************************* FILE CONTENT *******************************"); + try + { + BufferedReader r = new BufferedReader(new FileReader(path)); + while( (line=r.readLine())!=null) + { + counter++; /// to keep track of line numbers + findDeclaration = declara.matcher(line); + findGeneralize = def.matcher(line); + findSimpleAssign = simple_assign.matcher(line); + System.out.println(line); + // DEFINITION NODES + if(findDeclaration.find())// This is a variable declara line. Process it and check for assignments. + { + declare = true; + input = findDeclaration.group(2); + findGeneralize = def.matcher(input); + findGenrealizeViaObject = assign_definition_via_object.matcher(input); + // first check for declara with assignment through fucntion call or arithmetic operators + if(findGenrealizeViaObject.find()) + { + variable = findGenrealizeViaObject.group(1); + if(findGenrealizeViaObject.group(3)!=null)// this is definition and assignment + definition.put(findGenrealizeViaObject.group(1), Integer.toString(counter)); + variable_declaration.add(findGenrealizeViaObject.group(1)); + } + else + { + while(findGeneralize.find()) + { + if(findGeneralize.group(3)!=null)// this is definition and assignment + definition.put(findGeneralize.group(1), Integer.toString(counter)); + variable_declaration.add(findGeneralize.group(1)); + } + } + } + else if(findGeneralize.find())//this matches the variable assignment point. + { + variable = findGeneralize.group(1); + if(variable_declaration.contains(variable)) // variable must be in definition list + { + if(!definition.containsKey(findGeneralize.group(1))) + definition.put(findGeneralize.group(1), Integer.toString(counter)); + else + { + assignment = definition.get(findGeneralize.group(1)); + definition.remove(findGeneralize.group(1)); + definition.put(findGeneralize.group(1), assignment+" "+counter); + } + } + } + else if(findSimpleAssign.find()) + { + if(variable_declaration.contains(findSimpleAssign.group(1))) // variable must be in definition list + { + if(!definition.containsKey(findSimpleAssign.group(1))) + definition.put(findSimpleAssign.group(1), Integer.toString(counter)); + else + { + assignment = definition.get(findSimpleAssign.group(1)); + definition.remove(findSimpleAssign.group(1)); + definition.put(findSimpleAssign.group(1), assignment+" "+counter); + } + } + } + // USAGE NODES + if(line.contains("=")) + line = line.substring(line.indexOf("=")); + findUsage = usage.matcher(line); + while(findUsage.find()) + { + variable = findUsage.group(0); + if(variable_declaration.contains(variable)) // this variable must first be assigned somewhere or else ignore this word + { + if(used.containsKey(variable)) // it is already present in usage path + { + usages = used.get(variable); + used.remove(variable); + used.put(variable, usages+" "+counter); + } + else + { + used.put(variable, Integer.toString(counter)+" "); + } + } + } + } + } + catch(Exception e) + { + System.out.println("Error Occured"); + } + Iterator iterator = definition.entrySet().iterator(); + System.out.println("\n******************************* DU PATHS *******************************\n"); + System.out.printf("%10s %20s %20s", "S.NO","VARIABLE(S)","DU-PATH\n"); + counter=0; + while(iterator.hasNext()) + { + System.out.println("-----------------------------------------------------------------------"); + counter++; + Map.Entry e = (Map.Entry) iterator.next(); + variable = e.getKey(); + defined = e.getValue().split("\\s+"); + if(defined.length>1) + { + for(int j=0;j e = (Map.Entry) iterator.next(); + variable = e.getKey(); + defined = e.getValue().split("\\s+"); + position = defined[defined.length-1]; + if(used.containsKey(variable)) + { + counts = used.get(variable).split("\\s+"); + for(int sn = 0;snhigh || a high || c < low || c > high) + return false; + else + return true; + } + public static void generateOutputEquivalence() + { + Random m = new Random(); + int count,a,b,c,d; + boolean done[] = new boolean[4]; + if(low<=0) + count=4; + else + { + count = 3; + done[3]= true; + } + while(count>0) + { + a = m.nextInt(high+1); + b = m.nextInt(high+1); + c = m.nextInt(high+1); + d = b*b - 4*a*c; + if(d == 0 && a!=0 && valuesInRange(a,b,c) && !done[0]) + { + calculateDiscriminant(a,b,c); + done[0] = true; + } + else if(d > 0 && valuesInRange(a,b,c)&& !done[1]) + { + calculateDiscriminant(a,b,c); + done[1] = true; + } + else if(d < 0 && valuesInRange(a,b,c)&& !done[2]) + { + calculateDiscriminant(a,b,c); + done[2] = true; + } + else if(a==0 && valuesInRange(a,b,c)&& !done[3]) + { + calculateDiscriminant(a,b,c); + done[3] = true; + } + else + continue; + count--; + } + } + public static void calculateDiscriminant(int a, int b, int c) + { + discriminant = b*b - 4*a*c; + if(a>high || a high || c < low || c > high) + System.out.printf("\n%-8d %3d %3d %-4d %-50s",counter,a,b,c,"Invalid Input... Values Out of Range !!!"); + else if(a==0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Not a Quadratic Equation"); + else if(discriminant>0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Real Roots"); + else if(discriminant==0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Equal Roots"); + else if(discriminant<0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Imaginary Roots"); + counter++; + } + public static void main(String args[]) + { + Scanner s = new Scanner(System.in); + int a,b,c,count=0,inputCount; + char choice; + do + { + System.out.print("Enter the range for [a,b,c] => "); + low = s.nextInt(); + high = s.nextInt(); + average = (low+high)/2; + counter = 1; + System.out.println(low+"<= a,b,c <="+high); + System.out.println(); + // Auto generation of values of a,b anc c + setBoundaryValues(low,high); + System.out.print("\nINPUT EQUIVALENCE CLASSES"); + + System.out.println("\nTestCase a b c Output"); + if(low<=0) + calculateDiscriminant(0,average,average);//First input equivalence class + for(int i=0;i<3;i++) + { + for(int j=0;j"+count); + System.out.println("Number of Redundant Cases : 2"); + System.out.println("Total number of different test cases : "+(count-2)); + System.out.println("\nDo you want to continue ? \nY/y to continue. Other key to stop."); + choice = s.next().charAt(0); + System.out.println(); + }while(choice=='Y'||choice=='y'); + } +} diff --git a/Matcher.java b/Matcher.java new file mode 100644 index 0000000..46a37d3 --- /dev/null +++ b/Matcher.java @@ -0,0 +1,27 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +/** + * + * @author Suraj Singh + */ +class Matcher +{ + static int a,b; + public static void Greater() + { + if(a>b) + System.out.println("a is greater than b"); + else + System.out.println("b is greater than a"); + } + public static void main(String arfs[]) + { + System.out.println("Matcher Called"); + } +} \ No newline at end of file diff --git a/MutationTesting.java b/MutationTesting.java new file mode 100644 index 0000000..5e981e8 --- /dev/null +++ b/MutationTesting.java @@ -0,0 +1,125 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.awt.Color; +import java.awt.Font; +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author Suraj Singh + */ +class MutationTesting { + static StringBuffer operand; + static StringBuffer operator; + static StringBuffer stat; + public static void Mutant_Output(StringBuffer output) + { + System.out.println(output); + } + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + Pattern decision = Pattern.compile("\\b(if)\\b(.*)"); // decision + Pattern mutationChange = Pattern.compile("\\(([a-zA-Z]+)(<|>|<=|>=|==)([a-zA-Z]+)\\)"); + Matcher decisionMatch,conditionMatch; + String path, line,condition,var1,var2,op; + int counter; + boolean done=false; + char choice; + + Color c = new Color(0, 0, 255); + //Font f = new Font(); + do + { + + operand = new StringBuffer(); + operator = new StringBuffer(); + stat = new StringBuffer(); + counter=0; + System.out.print("Enter file name : "); + path = "C:\\Users\\Suraj Singh\\Desktop\\matcher1.txt"; + System.out.println("\n\n******************** FILE CONTENT ********************\n"); + try + { + BufferedReader r = new BufferedReader(new FileReader(path)); + while( (line=r.readLine())!=null) + { + counter++;// store the line number in the sampled program. + System.out.println(counter+" "+line); + decisionMatch = decision.matcher(line); + if(decisionMatch.find() && !done) + { + condition = decisionMatch.group(2); + conditionMatch = mutationChange.matcher(condition); + if(conditionMatch.find()) + { + var1 = conditionMatch.group(1); + var2 = conditionMatch.group(3); + op = conditionMatch.group(2); + operand.append("\tif("+var2+op+var1+")\n"); + operator.append("\tif("+var1+"<"+var2+")\n"); + done = true; + } + } + else //if(!done) + { + operand.append(counter+" "+line+"\n"); + operator.append(counter+" "+line+"\n"); + } + if(line.trim().startsWith("static ")) + { + //System.out.println("Static found"); + stat.append(counter+"\t"+line.trim().substring(6)+"\n"); + } + else + stat.append(counter+" "+line+"\n"); + } + } + catch(Exception e) + { + System.out.println(e.getMessage()); + } + System.out.println("\nOutput : a is greater than b\n"); + System.out.println("******** Enter the type of mutant to check **********"); + System.out.println("\t1. Operand Mutant \n\t2. Operator Mutant\n\t3. static keyword"); + counter = s.nextInt(); + if(counter==1) + { + System.out.println("\n************************ Operand Mutant **********************\n"); + Mutant_Output(operand); + System.out.println("Output : b is greater than a"); + } + else if(counter==2) + { + System.out.println("\n************************ Operator Mutant **********************\n"); + Mutant_Output(operator); + System.out.println("Output : b is greater than a"); + } + else if(counter==3) + { + System.out.println("\n************************ static keyword change **********************\n"); + Mutant_Output(stat); + System.out.println("Output : Compilation Error : Non-static variable in static context "); + } + else + { + System.out.println("Please enter correct choice !!!"); + } + operand.delete(0, operand.length()); + System.out.println("\nDo you want to continue (y/Y) ?"); + choice = s.next().charAt(0); + }while(choice=='Y'||choice=='y'); + } +} + + + diff --git a/Mutation_Testing2.java b/Mutation_Testing2.java new file mode 100644 index 0000000..91fc06e --- /dev/null +++ b/Mutation_Testing2.java @@ -0,0 +1,82 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.applet.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.event.ActionListener; +import java.net.*; +import javax.swing.JLabel; + +/** + * + * @author Suraj Singh + */ +class Mutation_Testing2 extends Applet implements ActionListener +{ + TextField input,output; + Label label1,label2; + Button b1; + JLabel lbl; + int num, sum = 0; + public void init() + { + String link = "google"; + Button b = new Button(link); + //b.add(); + b.addActionListener(this); + add(b); + Label label1,label2; + TextField input,output; + Button b1; + JLabel lbl; + label1 = new Label("please enter number : "); + add(label1); + label1.setBackground(Color.yellow); + label1.setForeground(Color.magenta); + input = new TextField(5); + add(input); + label2 = new Label("Sum : "); + add(label2); + label2.setBackground(Color.yellow); + label2.setForeground(Color.magenta); + output = new TextField(20); + add(output); + b1 = new Button("Add"); + add(b1); + b1.addActionListener(this); + lbl = new JLabel("Swing Applet Example. "); + add(lbl); + setBackground(Color.yellow); + } + public void actionPerformed(ActionEvent ae){ + try{ + num = Integer.parseInt(input.getText()); + sum = sum+num; + input.setText(""); + output.setText(Integer.toString(sum)); + lbl.setForeground(Color.blue); + lbl.setText("Output of the second Text Box : " + + output.getText()); + } + catch(NumberFormatException e){ + lbl.setForeground(Color.red); + lbl.setText("Invalid Entry!"); + } + Button source = (Button)ae.getSource(); + String link = "http://www."+source.getLabel()+".com"; + try{ + AppletContext a = getAppletContext(); + URL url = new URL(link); + a.showDocument(url, "_blank"); + } + catch(MalformedURLException e) + { + System.out.println(e.getMessage()); + } +} diff --git a/SW_BVA_Auto.java b/SW_BVA_Auto.java new file mode 100644 index 0000000..9280a36 --- /dev/null +++ b/SW_BVA_Auto.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +import java.util.Scanner; + +/** + * + * @author Suraj Singh + */ +class SW_BVA_Auto { + static int BoundaryValues[]; + static int high,low,discriminant,counter; + public static void setBoundaryValues(int low, int high) + { + BoundaryValues = new int[]{low,low+1,high-1,high};// Assuming four boundary values + } + public static void calculateDiscriminant(int a, int b, int c) + { + System.out.print(counter+++". "+a+"x\u00b2+"+b+"x+"+c+" => "); + discriminant = b*b - 4*a*c; + if(a>high || a high || c < low || c > high) + System.out.println("Invalid Input... Values Out of Range !!!"); + else if(a==0) + System.out.println("Not a Quadratic Equation"); + else if(discriminant>0) + System.out.println("Real Roots"); + else if(discriminant==0) + System.out.println("Equal Roots"); + else if(discriminant<0) + System.out.println("Imaginary Roots"); + } + public static void main(String args[]) + { + Scanner s = new Scanner(System.in); + int a,b,c,average; + char choice; + do + { + System.out.print("Enter the range for [a,b,c] => "); + low = s.nextInt(); + high = s.nextInt(); + counter = 1; + average = (low+high)/2; + System.out.println(low+"<= a,b,c <="+high); + System.out.println(); + // Auto generation of values of a,b anc c + setBoundaryValues(low,high); + for(int i=0;i<3;i++) + { + for(int j=0;j "); + discriminant = b*b - 4*a*c; + if(a>high || a high || c < low || c > high) + System.out.println("Invalid Input... Values Out of Range !!!"); + else if(a==0) + System.out.println("Not a Quadratic Equation"); + else if(discriminant>0) + System.out.println("Real Roots"); + else if(discriminant==0) + System.out.println("Equal Roots"); + else if(discriminant<0) + System.out.println("Imaginary Roots"); + } + public static void main(String args[]) + { + Scanner s = new Scanner(System.in); + int a,b,c,average; + char choice; + do + { + System.out.print("Enter the range for [a,b,c] => "); + low = s.nextInt(); + high = s.nextInt(); + counter = 1; + average = (low+high)/2; + System.out.println(low+"<= a,b,c <="+high); + System.out.println(); + // Auto generation of values of a,b anc c + setBoundaryValues(low,high); + for(int i=0;i<3;i++) + { + for(int j=0;jhigh || a high || c < low || c > high) + System.out.printf("\n%-8d %3d %3d %-4d %-50s",counter,a,b,c,"Invalid Input... Values Out of Range !!!"); + else if(a==0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Not a Quadratic Equation"); + else if(discriminant>0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Real Roots"); + else if(discriminant==0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Equal Roots"); + else if(discriminant<0) + System.out.printf("\n%-8d %3d %3d %-4d %-30s",counter,a,b,c,"Imaginary Roots"); + counter++; + } + public static void main(String args[]) + { + Scanner s = new Scanner(System.in); + int a,b,c,count=0,type,average; + char choice; + do + { + System.out.println("\nEnter the type of testing to be performed !!!"); + System.out.println("\n1.\tBoundary Value Analysis."); + System.out.println("\n2.\tRobustness Testing"); + System.out.println("\n3.\tWorst Case Testing\n\nEnter your choice !!!"); + type = s.nextInt(); + System.out.print("Enter the range for [a,b,c] => "); + low = s.nextInt(); + high = s.nextInt(); + average = (low+high)/2; + counter = 1; + System.out.println(low+"<= a,b,c <="+high); + System.out.println(); + System.out.println("\nTestCase a b c Output"); + // Auto generation of values of a,b anc c + setBoundaryValues(low,high); + switch(type) + { + case 1 : + for(int i=0;i<3;i++) + { + for(int j=0;j "); + low = s.nextInt(); + high = s.nextInt(); + System.out.println(low+"<= a,b,c <="+high); + System.out.println("\nEnter the values of a, b and c "); + a = s.nextInt(); + b = s.nextInt(); + c = s.nextInt(); + D = b*b - 4*a*c; + System.out.print("\n"+a+"x\u00b2+"+b+"x+"+c+" => "); + if(a>high || a high || c < low || c > high) + System.out.println("Invalid Input... Values Out of Range !!!"); + else if(a==0) + System.out.println("Not a Quadratic Equation"); + else if(D>0) + System.out.println("Real Roots"); + else if(D==0) + System.out.println("Equal Roots"); + else if(D<0) + System.out.println("Imaginary Roots"); + System.out.println("\nDo you want to continue ? \nY/y to continue. Other key to stop."); + choice = s.next().charAt(0); + System.out.println(); + }while(choice=='Y'||choice=='y'); + } +} diff --git a/Test.java b/Test.java new file mode 100644 index 0000000..82e0c7d --- /dev/null +++ b/Test.java @@ -0,0 +1,105 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package software_testing_meaures; + +/** + * + * @author Suraj Singh + */ + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +//p 20507 +//It is prime +//q 19927 +//It is prime +//n 408642989 +//it is not prime and is divisible by 19927 20507.0 +//phi 408602556 +//it is not prime and is divisible by 2 2.0430128E8 +//e 11 +//It is prime +//d 1494103 + +public class Test +{ + public static void main( String args[] ){ + + // String to be scanned to find the pattern. + //String line = "int a,b,c=100,e=200;"; + // String line = "if(a>10) System.out.println(ab);"; to find keywords + String line = " int a, sum = 5, b = 13;";//" int a,sum=1;"; // int a,sum = 5; + String variables = "12 12"; + System.out.println(1494103*11); + //System.out.println(variables.split(" ")[0] + variables.split(" ")[1]+ variables.split(" ")[2]); + System.out.println(Integer.toBinaryString(57)); + System.out.println((730-561)); + System.out.println(Integer.toBinaryString(730-561)); + // + String input = "(a>b)"; + Pattern mutationChange = Pattern.compile("\\(([a-zA-Z]+)(<|>|<=|>=|==)([a-zA-Z]+)\\)"); + Matcher operator = mutationChange.matcher(input); + String var1,operator1,var2; + if(operator.find()) + { + var1 = operator.group(1); + operator1 = operator.group(2); + var2 = operator.group(3); + System.out.println(var1+" "+operator1+" "+var2); + } + + Pattern declaration = Pattern.compile("\\b(int|float|String|long)\\b(.*)"); // declaration + //Pattern assignment = Pattern.compile("(\\b[a-z0-9]+\\b)=(.*)"); + Pattern generalize = Pattern.compile("([a-z][a-z0-9]*)(\\[])?(\\s*=\\s*)?(\\b[a-z0-9]+)?[,;]"); + Pattern assign_definition_via_object = Pattern.compile("([a-z][a-z0-9]*)(\\s*=\\s*)?([a-z0-9])+.([a-z0-9])+(\\(\\))?[,;]"); // (\\([a-zA-Z]+))? + Pattern usage = Pattern.compile("\\b[a-zA-Z0-9]*\\b"); + //Pattern justVar = Pattern.compile("[a-z][a-z0-9]*[,;]"); + // Pattern varWithVal = Pattern.compile(line) + // Now create matcher object. + + Matcher dec = declaration.matcher(line); + //Matcher ass = assignment.matcher(line); + Matcher var,used,varWithObject; + System.out.println(line); + used = usage.matcher(line); + Pattern simple_assign = Pattern.compile("([a-zA-z]+)(\\s*=\\s*)(.*);"); + Matcher simp = simple_assign.matcher("sum = a+4;"); + if(simp.find()) + { + System.out.println(simp.group(1)+" "+simp.group(2)+" "+simp.group(3)); + System.out.println(simp.group(0)); + } +// while(used.find()) +// {S +// System.out.println(used.group(0));//+" "+used.start()+" "+used.end()); +// } +// if (dec.find( )) { +// //System.out.println(dec.group(2)); +// variables = dec.group(2); +// var = generalize.matcher(variables); +// //justVariable = justVar.matcher(variables); +// while(var.find()) +// { +// System.out.println(var.group(1)+" "+var.group(2)+" "+var.group(3)+" "+var.group(4)); +// if(var.group(3)==null) +// { +// System.out.println("Variable declared only "+var.group(1)); +// } +// else +// { +// System.out.println("Variables declared with value "+var.group(1)+"=>"+var.group(4)); +// } +// } +// varWithObject = assign_definition_via_object.matcher(variables); +// while(varWithObject.find()) +// { +// System.out.println(varWithObject.group(1)+" "+varWithObject.group(2)+" "+varWithObject.group(3)); +// } +// } + } +} \ No newline at end of file diff --git a/Triangle.java b/Triangle.java new file mode 100644 index 0000000..62750a2 --- /dev/null +++ b/Triangle.java @@ -0,0 +1,128 @@ +package software_testing_meaures; + +///* +// * To change this license header, choose License Headers in Project Properties. +// * To change this template file, choose Tools | Templates +// * and open the template in the editor. +// */ +// +//package college_lab; +// +///** +// * +// * @author Suraj Singh +// */ +//import java.util.Scanner; +// +///** +// * +// * @author Suraj Singh +// */ +//class Triangle { +//static int BoundaryValues[],RobustnessValues[],WorstCase[]; +// static int high,low,discriminant,counter; +// public static void setBoundaryValues(int low, int high) +// { +// BoundaryValues = new int[]{low,low+1,high-1,high};// Assuming four boundary values +// RobustnessValues= new int[]{low-1,low,low+1,(low+high)/2,high-1,high};// Assuming six boundary values +// WorstCase = new int[]{low,low+1,(low+high)/2,high-1,high};// Assuming five boundary values +// } +// public static boolean isValidTriangle(int a, int b, int c) +// { +// if(a > b+c) +// return false; +// if(b > a+c ) +// return false; +// if(c > a+b) +// return false; +// return false; +// } +// public static void main(String args[]) +// { +// Scanner s = new Scanner(System.in); +// int a,b,c,count=0,type,average; +// char choice; +// do +// { +// System.out.println("\nEnter the type of testing to be performed !!!"); +// System.out.println("\n1.\tBoundary Value Analysis."); +// System.out.println("\n2.\tRobustness Testing"); +// System.out.println("\n3.\tWorst Case Testing\n\nEnter your choice !!!"); +// type = s.nextInt(); +// System.out.println("\nEnter the range of a,b,c"); +// System.out.print("Enter the values for [a,b,c] => "); +// a = s.nextInt(); +// b = s.nextInt(); +// c = s.nextInt(); +// counter = 1; +// System.out.println(); +// System.out.println("\nTestCase a b c Output"); +// // Auto generation of values of a,b anc c +// setBoundaryValues(low,high); +// switch(type) +// { +// case 1 : +// for(int i=0;i<3;i++) +// { +// for(int j=0;j