From 204742792c3de08ec726ae5c7c51672d68b15e45 Mon Sep 17 00:00:00 2001 From: Koen Wermer <koenwermer@gmail.com> Date: Mon, 13 Feb 2017 17:20:46 +0100 Subject: [PATCH] added wishnu's mutations --- Equivalent mutations/MinsMaxs.java | 38 +++++++++++++++ Equivalent mutations/Mutants/MinsMaxs_R1.java | 36 ++++++++++++++ Equivalent mutations/Mutants/MinsMaxs_R2.java | 36 ++++++++++++++ Equivalent mutations/Mutants/MinsMaxs_R3.java | 36 ++++++++++++++ .../Mutants/Normalizer_R1.java | 26 ++++++++++ .../Mutants/Normalizer_R2.java | 31 ++++++++++++ .../Mutants/Normalizer_R3.java | 24 ++++++++++ .../Mutants/Normalizer_R4.java | 34 ++++++++++++++ Equivalent mutations/Mutants/Vector_R1.java | 47 +++++++++++++++++++ Equivalent mutations/Mutants/Vector_R2.java | 46 ++++++++++++++++++ Equivalent mutations/Mutants/Vector_R3.java | 47 +++++++++++++++++++ Equivalent mutations/Normalizer.java | 25 ++++++++++ Equivalent mutations/Vector.java | 41 ++++++++++++++++ 13 files changed, 467 insertions(+) create mode 100644 Equivalent mutations/MinsMaxs.java create mode 100644 Equivalent mutations/Mutants/MinsMaxs_R1.java create mode 100644 Equivalent mutations/Mutants/MinsMaxs_R2.java create mode 100644 Equivalent mutations/Mutants/MinsMaxs_R3.java create mode 100644 Equivalent mutations/Mutants/Normalizer_R1.java create mode 100644 Equivalent mutations/Mutants/Normalizer_R2.java create mode 100644 Equivalent mutations/Mutants/Normalizer_R3.java create mode 100644 Equivalent mutations/Mutants/Normalizer_R4.java create mode 100644 Equivalent mutations/Mutants/Vector_R1.java create mode 100644 Equivalent mutations/Mutants/Vector_R2.java create mode 100644 Equivalent mutations/Mutants/Vector_R3.java create mode 100644 Equivalent mutations/Normalizer.java create mode 100644 Equivalent mutations/Vector.java diff --git a/Equivalent mutations/MinsMaxs.java b/Equivalent mutations/MinsMaxs.java new file mode 100644 index 0000000..d1808c8 --- /dev/null +++ b/Equivalent mutations/MinsMaxs.java @@ -0,0 +1,38 @@ +package Examples.NN_InputVector; + +public class MinsMaxs { + + /** + * Given an NxM matrix of double-values, calculate the minimum and maximum of every column in the matrix. + * The mins and maxs are returned as a pair of arrays. + */ + static public double[][] getMinsMaxs(double[][] data) { + int N = data.length ; + if (N < 1) throw new IllegalArgumentException() ; + int rowWidth = data[0].length ; + // check if every row has the same length: + for (int r=1; r<N; r++) + if (data[r].length != rowWidth) throw new IllegalArgumentException() ; + + double[] mins = new double[rowWidth] ; + double[] maxs = new double[rowWidth] ; + // initialize mins and maxs: + for (int c=0; c<rowWidth; c++) { + mins[c] =data[0][c] ; + maxs[c] =data[0][c] ; + } + // iterate over the matrix: + for (int r=1; r<N; r++) { + for (int c=0; c<rowWidth; c++) { + if (data[r][c] <= mins[c]) mins[c] = data[r][c] ; // mutate this + if (data[r][c] >= maxs[c]) maxs[c] = data[r][c] ; // mutate this + } + } + double[][] output = new double[2][] ; + output[0] = mins ; + output[1] = maxs ; + return output ; + } + + +} diff --git a/Equivalent mutations/Mutants/MinsMaxs_R1.java b/Equivalent mutations/Mutants/MinsMaxs_R1.java new file mode 100644 index 0000000..3cb0511 --- /dev/null +++ b/Equivalent mutations/Mutants/MinsMaxs_R1.java @@ -0,0 +1,36 @@ +package Examples.NN_InputVector; + +public class MinsMaxs_R1 { + + /** + * Refactoring R1: replacing some <= and >= to improve efficiency. + */ + static public double[][] getMinsMaxs(double[][] data) { + int N = data.length ; + if (N < 1) throw new IllegalArgumentException() ; + int rowWidth = data[0].length ; + // check if every row has the same lenght: + for (int r=1; r<N; r++) + if (data[r].length != rowWidth) throw new IllegalArgumentException() ; + + double[] mins = new double[rowWidth] ; + double[] maxs = new double[rowWidth] ; + // initialize mins and maxs: + for (int c=0; c<rowWidth; c++) { + mins[c] =data[0][c] ; + maxs[c] =data[0][c] ; + } + // iterate over the matrix: + for (int r=1; r<data.length; r++) { + for (int c=0; c<rowWidth; c++) { + if (data[r][c] < mins[c]) mins[c] = data[r][c] ; // more efficient + if (data[r][c] > maxs[c]) maxs[c] = data[r][c] ; // more efficient + } + } + double[][] output = new double[2][] ; + output[0] = mins ; + output[1] = maxs ; + return output ; + } + +} diff --git a/Equivalent mutations/Mutants/MinsMaxs_R2.java b/Equivalent mutations/Mutants/MinsMaxs_R2.java new file mode 100644 index 0000000..1603f1d --- /dev/null +++ b/Equivalent mutations/Mutants/MinsMaxs_R2.java @@ -0,0 +1,36 @@ +package Examples.NN_InputVector; + +public class MinsMaxs_R2 { + + /** + * Refactoring R2: moving the check for equal length rows to inside the main loop. + */ + static public double[][] getMinsMaxs(double[][] data) { + int N = data.length ; + if (N < 1) throw new IllegalArgumentException() ; + int rowWidth = data[0].length ; + + double[] mins = new double[rowWidth] ; + double[] maxs = new double[rowWidth] ; + + // initialize mins and maxs: + for (int c=0; c<rowWidth; c++) { + mins[c] =data[0][c] ; + maxs[c] =data[0][c] ; + } + // iterate over the matrix: + for (int r=1; r<data.length; r++) { + // first check if the row has the same length as data[0]: + if (data[r].length != rowWidth) throw new IllegalArgumentException() ; + for (int c=0; c<rowWidth; c++) { + if (data[r][c] <= mins[c]) mins[c] = data[r][c] ; + if (data[r][c] >= maxs[c]) maxs[c] = data[r][c] ; + } + } + double[][] output = new double[2][] ; + output[0] = mins ; + output[1] = maxs ; + return output ; + } + +} diff --git a/Equivalent mutations/Mutants/MinsMaxs_R3.java b/Equivalent mutations/Mutants/MinsMaxs_R3.java new file mode 100644 index 0000000..d89b304 --- /dev/null +++ b/Equivalent mutations/Mutants/MinsMaxs_R3.java @@ -0,0 +1,36 @@ +package Examples.NN_InputVector; + +public class MinsMaxs_R3 { + + /** + * Refactoring R3: changing the main loop from a row-column loop, to column-row loop. + */ + static public double[][] getMinsMaxs(double[][] data) { + int N = data.length ; + if (N < 1) throw new IllegalArgumentException() ; + int rowWidth = data[0].length ; + // check if every row has the same length: + for (int r=1; r<N; r++) + if (data[r].length != rowWidth) throw new IllegalArgumentException() ; + + double[] mins = new double[rowWidth] ; + double[] maxs = new double[rowWidth] ; + // initialize mins and maxs: + for (int c=0; c<rowWidth; c++) { + mins[c] =data[0][c] ; + maxs[c] =data[0][c] ; + } + // iterate over the matrix: + for (int c=0; c<rowWidth; c++) { + for (int r=1; r<N; r++) { + if (data[r][c] <= mins[c]) mins[c] = data[r][c] ; // mutate this + if (data[r][c] >= maxs[c]) maxs[c] = data[r][c] ; // mutate this + } + } + double[][] output = new double[2][] ; + output[0] = mins ; + output[1] = maxs ; + return output ; + } + +} diff --git a/Equivalent mutations/Mutants/Normalizer_R1.java b/Equivalent mutations/Mutants/Normalizer_R1.java new file mode 100644 index 0000000..62a7d0e --- /dev/null +++ b/Equivalent mutations/Mutants/Normalizer_R1.java @@ -0,0 +1,26 @@ +package Examples.NN_InputVector; + +public class Normalizer_R1 { + + /** + * R1: Restructuring the check that mins and max should be of the same length. + */ + static public void normalize(double[] mins, double[] maxs, double[][] data) { + int rowWidth = mins.length ; + if (maxs.length != rowWidth) { throw new IllegalArgumentException() ; } + int row = 0 ; + while (row < data.length) { + if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; } + int col = 0 ; + while (col < rowWidth) { + double delta = maxs[col] - mins[col] ; + if (delta == 0) throw new IllegalArgumentException() ; + double x = data[row][col] ; + data[row][col] = (x - mins[col])/delta ; + col++ ; + } + row++ ; + } + } + +} diff --git a/Equivalent mutations/Mutants/Normalizer_R2.java b/Equivalent mutations/Mutants/Normalizer_R2.java new file mode 100644 index 0000000..782e1f5 --- /dev/null +++ b/Equivalent mutations/Mutants/Normalizer_R2.java @@ -0,0 +1,31 @@ +package Examples.NN_InputVector; + +public class Normalizer_R2 { + + /** + * R2: moving the element-level normalization to a separate method. + */ + static public void normalize(double[] mins, double[] maxs, double[][] data) { + int rowWidth = mins.length ; + int row = 0 ; + if (maxs.length == rowWidth) { + while (row < data.length) { + if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; } + int col = 0 ; + while (col < rowWidth) { + data[row][col] = normalizeValue(data[row][col] ,mins[col],maxs[col]) ; + col++ ; + } + row++ ; + } + } + else { throw new IllegalArgumentException() ; } + } + + static private double normalizeValue(double x, double min, double max) throws IllegalArgumentException { + double delta = max - min ; + if (delta == 0) throw new IllegalArgumentException() ; + return (x - min)/delta ; + } + +} diff --git a/Equivalent mutations/Mutants/Normalizer_R3.java b/Equivalent mutations/Mutants/Normalizer_R3.java new file mode 100644 index 0000000..48bb8ba --- /dev/null +++ b/Equivalent mutations/Mutants/Normalizer_R3.java @@ -0,0 +1,24 @@ +package Examples.NN_InputVector; + +public class Normalizer_R3 { + + /** + * R3: converting while loop to for loop. + */ + static public void normalize(double[] mins, double[] maxs, double[][] data) { + int rowWidth = mins.length ; + if (maxs.length == rowWidth) { + for (int row=0; row < data.length; row++) { + if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; } + for (int col=0; col < rowWidth; col++) { + double delta = maxs[col] - mins[col] ; + if (delta == 0) throw new IllegalArgumentException() ; + double x = data[row][col] ; + data[row][col] = (x - mins[col])/delta ; + } + } + } + else { throw new IllegalArgumentException() ; } + } + +} diff --git a/Equivalent mutations/Mutants/Normalizer_R4.java b/Equivalent mutations/Mutants/Normalizer_R4.java new file mode 100644 index 0000000..367f70e --- /dev/null +++ b/Equivalent mutations/Mutants/Normalizer_R4.java @@ -0,0 +1,34 @@ +package Examples.NN_InputVector; + + +public class Normalizer_R4 { + + /** + * R4: Re-routing exceptional control flow via a different exception. + */ + static public void normalize(double[] mins, double[] maxs, double[][] data) { + int rowWidth = mins.length ; + int row = 0 ; + try { + if (maxs.length == rowWidth) { + while (row < data.length) { + if (data[row].length != rowWidth) { throw new IncompatibleSizeException() ; } + int col = 0 ; + while (col < rowWidth) { + double delta = maxs[col] - mins[col] ; + if (delta == 0) throw new IncompatibleSizeException() ; + double x = data[row][col] ; + data[row][col] = (x - mins[col])/delta ; + col++ ; + } + row++ ; + } + } + else { throw new IllegalArgumentException() ; } + } + catch (IncompatibleSizeException e) { + throw new IllegalArgumentException(e) ; + } + } + +} diff --git a/Equivalent mutations/Mutants/Vector_R1.java b/Equivalent mutations/Mutants/Vector_R1.java new file mode 100644 index 0000000..47bc86f --- /dev/null +++ b/Equivalent mutations/Mutants/Vector_R1.java @@ -0,0 +1,47 @@ +package Examples.NN_InputVector; + +public class Vector_R1 { + + private double[] vector; + + public Vector_R1(double[] vec) { + this.vector = new double[vec.length] ; + System.arraycopy(vec, 0, this.vector, 0, vec.length); + } + + public double[] getVector() { + int N = this.vector.length ; + double[] V = new double[N] ; + for (int k=0; k<N; k++) V[k] = this.vector[k] ; + return V ; + } + + public int getSize() { return this.vector.length ; } + + + /** + * R1 replaces the return in the INPRODUCT-case with a break. + */ + public Vector_R1 combine_R1(int operation, Vector_R1 Z) { + if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ; + double[] result = new double[this.vector.length] ; + double[] vector2 = Z.getVector() ; + switch (operation) { + case VectorCONST.ACCUMULATE: ; // does nothing, deliberately falling through to the code of INPRODUCT + case VectorCONST.INPRODUCT: { + int r = 0 ; + for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ; + double[] rr = {r} ; + result = rr ; + break ; + } + case VectorCONST.PLUS: { + for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ; + break ; + } + default: return null ; + } + return new Vector_R1(result) ; + } + +} diff --git a/Equivalent mutations/Mutants/Vector_R2.java b/Equivalent mutations/Mutants/Vector_R2.java new file mode 100644 index 0000000..532e278 --- /dev/null +++ b/Equivalent mutations/Mutants/Vector_R2.java @@ -0,0 +1,46 @@ +package Examples.NN_InputVector; + +public class Vector_R2 { + + private double[] vector; + + public Vector_R2(double[] vec) { + this.vector = new double[vec.length] ; + System.arraycopy(vec, 0, this.vector, 0, vec.length); + } + + public double[] getVector() { + int N = this.vector.length ; + double[] V = new double[N] ; + for (int k=0; k<N; k++) V[k] = this.vector[k] ; + return V ; + } + + public int getSize() { return this.vector.length ; } + + + /** + * R2 replaces the fall through in the ACCUMULATE case with a recursive call with INPRODUCT. + */ + public Vector_R2 combine_R2(int operation, Vector_R2 Z) { + if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ; + double[] result = new double[this.vector.length] ; + double[] vector2 = Z.getVector() ; + switch (operation) { + case VectorCONST.ACCUMULATE: return combine_R2(VectorCONST.INPRODUCT,Z) ; + case VectorCONST.INPRODUCT: { + int r = 0 ; + for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ; + double[] rr = {r} ; + return new Vector_R2(rr) ; + } + case VectorCONST.PLUS: { + for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ; + break ; + } + default: return null ; + } + return new Vector_R2(result) ; + } + +} diff --git a/Equivalent mutations/Mutants/Vector_R3.java b/Equivalent mutations/Mutants/Vector_R3.java new file mode 100644 index 0000000..4982918 --- /dev/null +++ b/Equivalent mutations/Mutants/Vector_R3.java @@ -0,0 +1,47 @@ +package Examples.NN_InputVector; + +public class Vector_R3 { + + private double[] vector; + + public Vector_R3(double[] vec) { + this.vector = new double[vec.length] ; + System.arraycopy(vec, 0, this.vector, 0, vec.length); + } + + public double[] getVector() { + int N = this.vector.length ; + double[] V = new double[N] ; + for (int k=0; k<N; k++) V[k] = this.vector[k] ; + return V ; + } + + public int getSize() { return this.vector.length ; } + + + /** + * R3 reorder the cases, and remove the use of return from the case arms. + */ + public Vector_R3 combine_R3(int operation, Vector_R3 Z) { + if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ; + double[] result = new double[this.vector.length] ; + double[] vector2 = Z.getVector() ; + Vector_R3 resultingVector = null ; + switch (operation) { + case VectorCONST.PLUS: { + for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ; + resultingVector = new Vector_R3(result) ; + break ; } + case VectorCONST.ACCUMULATE: ; // does nothing, deliberately falling through to the code of INPRODUCT + case VectorCONST.INPRODUCT: { + int r = 0 ; + for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ; + double[] rr = {r} ; + resultingVector = new Vector_R3(rr) ; // we will just let it fall through + } + default: ; + } + return resultingVector ; + } + +} diff --git a/Equivalent mutations/Normalizer.java b/Equivalent mutations/Normalizer.java new file mode 100644 index 0000000..61ecfca --- /dev/null +++ b/Equivalent mutations/Normalizer.java @@ -0,0 +1,25 @@ +package Examples.NN_InputVector; + +public class Normalizer { + + static public void normalize(double[] mins, double[] maxs, double[][] data) { + int rowWidth = mins.length ; + int row = 0 ; + if (maxs.length == rowWidth) { + while (row < data.length) { + if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; } + int col = 0 ; + while (col < rowWidth) { + double delta = maxs[col] - mins[col] ; + if (delta == 0) throw new IllegalArgumentException() ; + double x = data[row][col] ; + data[row][col] = (x - mins[col])/delta ; + col++ ; + } + row++ ; + } + } + else { throw new IllegalArgumentException() ; } + } + +} diff --git a/Equivalent mutations/Vector.java b/Equivalent mutations/Vector.java new file mode 100644 index 0000000..fedb202 --- /dev/null +++ b/Equivalent mutations/Vector.java @@ -0,0 +1,41 @@ +package Examples.NN_InputVector; + +public class Vector { + + private double[] vector; + + public Vector(double[] vec) { + this.vector = new double[vec.length] ; + System.arraycopy(vec, 0, this.vector, 0, vec.length); + } + + public double[] getVector() { + int N = this.vector.length ; + double[] V = new double[N] ; + for (int k=0; k<N; k++) V[k] = this.vector[k] ; + return V ; + } + + public int getSize() { return this.vector.length ; } + + public Vector combine(int operation, Vector Z) { + if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ; + double[] result = new double[this.vector.length] ; + double[] vector2 = Z.getVector() ; + switch (operation) { + case VectorCONST.ACCUMULATE: ; // does nothing, deliberately falling through to the code of INPRODUCT + case VectorCONST.INPRODUCT: { + int r = 0 ; + for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ; + double[] rr = {r} ; + return new Vector(rr) ; + } + case VectorCONST.PLUS: { + for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ; + break ; + } + default: return null ; + } + return new Vector(result) ; + } +} -- GitLab