Vector.java

1
package com.takenoko.vector;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Objects;
6
7
/** The Vector class represents a vector in a 2D hexagonal grid. */
8
public class Vector {
9
    private final double q;
10
    private final double r;
11
    private final double s;
12
13
    /**
14
     * Constructor for the Vector class. The vector is represented by its coordinates in a 2D
15
     * hexagonal grid. The vector must respect q+r+s=0.
16
     *
17
     * @param q The q coordinate of the vector.
18
     * @param r The r coordinate of the vector.
19
     * @param s The s coordinate of the vector.
20
     */
21
    public Vector(double q, double r, double s) {
22 3 1. <init> : Replaced double addition with subtraction → KILLED
2. <init> : Replaced double addition with subtraction → KILLED
3. <init> : negated conditional → KILLED
        if (q + r + s != 0) {
23
            throw new IllegalArgumentException("q + r + s must be 0");
24
        }
25
        this.q = q;
26
        this.r = r;
27
        this.s = s;
28
    }
29
30
    /**
31
     * Add two vectors.
32
     *
33
     * @param other The vector to add to this vector.
34
     * @return The sum of this vector and the other vector.
35
     */
36
    public Vector add(Vector other) {
37 4 1. add : Replaced double addition with subtraction → KILLED
2. add : Replaced double addition with subtraction → KILLED
3. add : Replaced double addition with subtraction → KILLED
4. add : replaced return value with null for com/takenoko/vector/Vector::add → KILLED
        return new Vector(q() + other.q(), r() + other.r(), s() + other.s());
38
    }
39
40
    /**
41
     * Subtract two vectors.
42
     *
43
     * @param other The vector to subtract to this vector.
44
     * @return The difference of this vector and the other vector.
45
     */
46
    public Vector sub(Vector other) {
47 4 1. sub : Replaced double subtraction with addition → KILLED
2. sub : Replaced double subtraction with addition → KILLED
3. sub : Replaced double subtraction with addition → KILLED
4. sub : replaced return value with null for com/takenoko/vector/Vector::sub → KILLED
        return new Vector(q() - other.q(), r() - other.r(), s() - other.s());
48
    }
49
50
    /**
51
     * Rotate the vector by 60 degrees.
52
     *
53
     * @return The rotated vector.
54
     */
55
    public Vector rotate60() {
56 4 1. rotate60 : removed negation → KILLED
2. rotate60 : removed negation → KILLED
3. rotate60 : removed negation → KILLED
4. rotate60 : replaced return value with null for com/takenoko/vector/Vector::rotate60 → KILLED
        return new Vector(-r(), -s(), -q());
57
    }
58
59
    /**
60
     * Determine the length of the vector.
61
     *
62
     * @return The length of the vector.
63
     */
64
    public double length() {
65 4 1. length : Replaced double addition with subtraction → KILLED
2. length : Replaced double addition with subtraction → KILLED
3. length : Replaced double division with multiplication → KILLED
4. length : replaced double return with 0.0d for com/takenoko/vector/Vector::length → KILLED
        return ((Math.abs(q()) + Math.abs(r()) + Math.abs(s())) / 2.0f);
66
    }
67
68
    /**
69
     * Determine the distance between two vectors.
70
     *
71
     * @param other The other vector.
72
     * @return The distance between the two vectors.
73
     */
74
    public double distance(Vector other) {
75 1 1. distance : replaced double return with 0.0d for com/takenoko/vector/Vector::distance → KILLED
        return sub(other).length();
76
    }
77
78
    @Override
79
    public boolean equals(Object o) {
80 2 1. equals : negated conditional → KILLED
2. equals : replaced boolean return with false for com/takenoko/vector/Vector::equals → KILLED
        if (this == o) return true;
81 3 1. equals : negated conditional → KILLED
2. equals : negated conditional → KILLED
3. equals : replaced boolean return with true for com/takenoko/vector/Vector::equals → KILLED
        if (o == null || getClass() != o.getClass()) return false;
82
        Vector vector = (Vector) o;
83 4 1. equals : negated conditional → KILLED
2. equals : negated conditional → KILLED
3. equals : negated conditional → KILLED
4. equals : replaced boolean return with true for com/takenoko/vector/Vector::equals → KILLED
        return q() == vector.q() && r() == vector.r() && s() == vector.s();
84
    }
85
86
    @Override
87
    public int hashCode() {
88 1 1. hashCode : replaced int return with 0 for com/takenoko/vector/Vector::hashCode → KILLED
        return Objects.hash(q(), r(), s());
89
    }
90
91
    /**
92
     * Determine the neighbors of the vector.
93
     *
94
     * @return The neighbors around the vector.
95
     */
96
    public List<Vector> getNeighbors() {
97
        ArrayList<Vector> neighbors = new ArrayList<>();
98
        for (Direction direction : Direction.values()) {
99
            neighbors.add(add(direction.getVector()));
100
        }
101 1 1. getNeighbors : replaced return value with Collections.emptyList for com/takenoko/vector/Vector::getNeighbors → KILLED
        return neighbors;
102
    }
103
104
    /**
105
     * Multiply the vector by a scalar.
106
     *
107
     * @param i The number of times to multiply the vector.
108
     * @return The multiplied vector.
109
     */
110
    public Vector multiply(double i) {
111 4 1. multiply : Replaced double multiplication with division → KILLED
2. multiply : Replaced double multiplication with division → KILLED
3. multiply : Replaced double multiplication with division → KILLED
4. multiply : replaced return value with null for com/takenoko/vector/Vector::multiply → KILLED
        return new Vector(q() * i, r() * i, s() * i);
112
    }
113
114
    /**
115
     * Normalize the vector.
116
     *
117
     * @return The normalized vector.
118
     */
119
    public Vector normalize() {
120
        double length = length();
121 1 1. normalize : negated conditional → KILLED
        if (length == 0) {
122 1 1. normalize : replaced return value with null for com/takenoko/vector/Vector::normalize → KILLED
            return new Vector(0, 0, 0);
123
        }
124 4 1. normalize : Replaced double division with multiplication → KILLED
2. normalize : Replaced double division with multiplication → KILLED
3. normalize : Replaced double division with multiplication → KILLED
4. normalize : replaced return value with null for com/takenoko/vector/Vector::normalize → KILLED
        return new Vector(q() / length, r() / length, s() / length);
125
    }
126
127
    public double q() {
128 1 1. q : replaced double return with 0.0d for com/takenoko/vector/Vector::q → KILLED
        return q;
129
    }
130
131
    public double r() {
132 1 1. r : replaced double return with 0.0d for com/takenoko/vector/Vector::r → KILLED
        return r;
133
    }
134
135
    public double s() {
136 1 1. s : replaced double return with 0.0d for com/takenoko/vector/Vector::s → KILLED
        return s;
137
    }
138
139
    @Override
140
    public String toString() {
141 1 1. toString : replaced return value with "" for com/takenoko/vector/Vector::toString → KILLED
        return "Vector[" + "q=" + q + ", " + "r=" + r + ", " + "s=" + s + ']';
142
    }
143
144
    public PositionVector toPositionVector() {
145 1 1. toPositionVector : replaced return value with null for com/takenoko/vector/Vector::toPositionVector → KILLED
        return new PositionVector(this);
146
    }
147
}

Mutations

22

1.1
Location : <init>
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestConstructor]/[method:constructor_WhenGivenCorrectCoordinates_CreatesVector()]
Replaced double addition with subtraction → KILLED

2.2
Location : <init>
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestConstructor]/[method:constructor_WhenGivenCorrectCoordinates_CreatesVector()]
Replaced double addition with subtraction → KILLED

3.3
Location : <init>
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestConstructor]/[method:constructor_WhenGivenCorrectCoordinates_CreatesVector()]
negated conditional → KILLED

37

1.1
Location : add
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestAdd]/[method:add_shouldAddTheTwoVectors()]
Replaced double addition with subtraction → KILLED

2.2
Location : add
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestAdd]/[method:add_shouldAddTheTwoVectors()]
Replaced double addition with subtraction → KILLED

3.3
Location : add
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestAdd]/[method:add_shouldAddTheTwoVectors()]
Replaced double addition with subtraction → KILLED

4.4
Location : add
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestAdd]/[method:add_shouldAddTheTwoVectors()]
replaced return value with null for com/takenoko/vector/Vector::add → KILLED

47

1.1
Location : sub
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestDistance]/[method:distance_shouldReturnTheDistanceBetweenTheTwoVectors()]
Replaced double subtraction with addition → KILLED

2.2
Location : sub
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestDistance]/[method:distance_shouldReturnTheDistanceBetweenTheTwoVectors()]
Replaced double subtraction with addition → KILLED

3.3
Location : sub
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestDistance]/[method:distance_shouldReturnTheDistanceBetweenTheTwoVectors()]
Replaced double subtraction with addition → KILLED

4.4
Location : sub
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestDistance]/[method:distance_shouldReturnTheDistanceBetweenTheTwoVectors()]
replaced return value with null for com/takenoko/vector/Vector::sub → KILLED

56

1.1
Location : rotate60
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestRotate60]/[method:rotate60_shouldRotateTheVectorBy60Degrees()]
removed negation → KILLED

2.2
Location : rotate60
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestRotate60]/[method:rotate60_shouldRotateTheVectorBy60Degrees()]
removed negation → KILLED

3.3
Location : rotate60
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestRotate60]/[method:rotate60_shouldRotateTheVectorBy60Degrees()]
removed negation → KILLED

4.4
Location : rotate60
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestRotate60]/[method:rotate60_shouldRotateTheVectorBy60Degrees()]
replaced return value with null for com/takenoko/vector/Vector::rotate60 → KILLED

65

1.1
Location : length
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestLength]/[method:length_shouldReturnTheLengthOfTheVector()]
Replaced double addition with subtraction → KILLED

2.2
Location : length
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestLength]/[method:length_shouldReturnTheLengthOfTheVector()]
Replaced double addition with subtraction → KILLED

3.3
Location : length
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestLength]/[method:length_shouldReturnTheLengthOfTheVector()]
Replaced double division with multiplication → KILLED

4.4
Location : length
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestLength]/[method:length_shouldReturnTheLengthOfTheVector()]
replaced double return with 0.0d for com/takenoko/vector/Vector::length → KILLED

75

1.1
Location : distance
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestDistance]/[method:distance_shouldReturnTheDistanceBetweenTheTwoVectors()]
replaced double return with 0.0d for com/takenoko/vector/Vector::distance → KILLED

80

1.1
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenCalledOnAnotherClass_ReturnsFalse()]
negated conditional → KILLED

2.2
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenCalledOnSelf_ReturnsTrue()]
replaced boolean return with false for com/takenoko/vector/Vector::equals → KILLED

81

1.1
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenVectorsAreEqual_ReturnsTrue()]
negated conditional → KILLED

2.2
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenCalledOnAnotherClass_ReturnsFalse()]
negated conditional → KILLED

3.3
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenCalledOnAnotherClass_ReturnsFalse()]
replaced boolean return with true for com/takenoko/vector/Vector::equals → KILLED

83

1.1
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenVectorsAreEqual_ReturnsTrue()]
negated conditional → KILLED

2.2
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenVectorsAreEqual_ReturnsTrue()]
negated conditional → KILLED

3.3
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenVectorsAreEqual_ReturnsTrue()]
negated conditional → KILLED

4.4
Location : equals
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestEquals]/[method:equals_WhenVectorsAreNotEqual_ReturnsFalse()]
replaced boolean return with true for com/takenoko/vector/Vector::equals → KILLED

88

1.1
Location : hashCode
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestHashcode]/[method:hashcode_shouldReturnADifferentHashcodeForDifferentVectors()]
replaced int return with 0 for com/takenoko/vector/Vector::hashCode → KILLED

101

1.1
Location : getNeighbors
Killed by : com.takenoko.layers.irrigation.EdgePositionTest.[engine:junit-jupiter]/[class:com.takenoko.layers.irrigation.EdgePositionTest]/[method:methodGetNeighbours()]
replaced return value with Collections.emptyList for com/takenoko/vector/Vector::getNeighbors → KILLED

111

1.1
Location : multiply
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestMultiply]/[method:multiply_shouldReturnTheZeroVectorIfTheFactorIs0()]
Replaced double multiplication with division → KILLED

2.2
Location : multiply
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestMultiply]/[method:multiply_shouldReturnTheZeroVectorIfTheFactorIs0()]
Replaced double multiplication with division → KILLED

3.3
Location : multiply
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestMultiply]/[method:multiply_shouldReturnTheZeroVectorIfTheFactorIs0()]
Replaced double multiplication with division → KILLED

4.4
Location : multiply
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestMultiply]/[method:multiply_shouldReturnTheSameVectorIfTheFactorIs1()]
replaced return value with null for com/takenoko/vector/Vector::multiply → KILLED

121

1.1
Location : normalize
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestNormalize]/[method:normalize_shouldReturnTheNormalizedVector()]
negated conditional → KILLED

122

1.1
Location : normalize
Killed by : com.takenoko.actors.PandaTest.[engine:junit-jupiter]/[class:com.takenoko.actors.PandaTest]/[nested-class:TestGetPossibleMoves]/[method:shouldReturnAListOfPossibleMoves()]
replaced return value with null for com/takenoko/vector/Vector::normalize → KILLED

124

1.1
Location : normalize
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestNormalize]/[method:normalize_shouldReturnTheNormalizedVector()]
Replaced double division with multiplication → KILLED

2.2
Location : normalize
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestNormalize]/[method:normalize_shouldReturnTheNormalizedVector()]
Replaced double division with multiplication → KILLED

3.3
Location : normalize
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestNormalize]/[method:normalize_shouldReturnTheNormalizedVector()]
Replaced double division with multiplication → KILLED

4.4
Location : normalize
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestNormalize]/[method:normalize_shouldReturnTheNormalizedVector()]
replaced return value with null for com/takenoko/vector/Vector::normalize → KILLED

128

1.1
Location : q
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestConstructor]/[method:constructor_WhenGivenCorrectCoordinates_CreatesVector()]
replaced double return with 0.0d for com/takenoko/vector/Vector::q → KILLED

132

1.1
Location : r
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestConstructor]/[method:constructor_WhenGivenCorrectCoordinates_CreatesVector()]
replaced double return with 0.0d for com/takenoko/vector/Vector::r → KILLED

136

1.1
Location : s
Killed by : com.takenoko.vector.VectorTest.[engine:junit-jupiter]/[class:com.takenoko.vector.VectorTest]/[nested-class:TestConstructor]/[method:constructor_WhenGivenCorrectCoordinates_CreatesVector()]
replaced double return with 0.0d for com/takenoko/vector/Vector::s → KILLED

141

1.1
Location : toString
Killed by : com.takenoko.actors.GardenerTest.[engine:junit-jupiter]/[class:com.takenoko.actors.GardenerTest]/[nested-class:TestPositionMessage]/[method:shouldReturnAStringExplainingWhereTheGardenerIsOnTheBoard()]
replaced return value with "" for com/takenoko/vector/Vector::toString → KILLED

145

1.1
Location : toPositionVector
Killed by : com.takenoko.shape.ShapeTest.[engine:junit-jupiter]/[class:com.takenoko.shape.ShapeTest]/[nested-class:TestTranslate]/[method:translate_shouldReturnNewShapeWithSameSize()]
replaced return value with null for com/takenoko/vector/Vector::toPositionVector → KILLED

Active mutators

Tests examined


Report generated by PIT 1.8.0