Skip to content

Commit

Permalink
Update lab3.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jasper-hyj committed Oct 30, 2023
1 parent c51fb8f commit 750d133
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
93 changes: 93 additions & 0 deletions lab3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Lab Report 3

## Part 1

### Associated Code

```java
// Changes the input array to be in reversed order
static void reverseInPlace(int[] arr) {
for(int i = 0; i < arr.length; i += 1) {
arr[i] = arr[arr.length - i - 1];
}
}
```

Failure-inducing Input (Test)

```java
@Test
public void testReverseInPlace() {
int[] input1 = {1, 2, 3, 4, 5, 6};
ArrayExamples.reverseInPlace(input1);
assertArrayEquals(new int[]{6, 5, 4, 3, 2, 1}, input1);
}
```

Non Failure-inducing Input

```java
@Test
public void testReverseOneElementInPlace() {
int[] input1 = {1};
ArrayExamples.reverseInPlace(input1);
assertArrayEquals(new int[]{1}, input1);
}
```

### Symptoms

```
JUnit version 4.13.2
..E
Time: 0.044
There was 1 failure:
1) testReverseInPlace(ArrayTests)
arrays first differed at element [3]; expected:<3> but was:<4>
at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:78)
at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:28)
at org.junit.Assert.internalArrayEquals(Assert.java:534)
at org.junit.Assert.assertArrayEquals(Assert.java:418)
at org.junit.Assert.assertArrayEquals(Assert.java:429)
at ArrayTests.testReverseInPlace(ArrayTests.java:11)
... 32 trimmed
Caused by: java.lang.AssertionError: expected:<3> but was:<4>
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:120)
at org.junit.Assert.assertEquals(Assert.java:146)
at org.junit.internal.ExactComparisonCriteria.assertElementsEqual(ExactComparisonCriteria.java:8)
at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:76)
... 38 more
FAILURES!!!
Tests run: 2, Failures: 1
```

### Fix the Bug

#### Before:

```java
static void reverseInPlace(int[] arr) {
for(int i = 0; i < arr.length; i += 1) {
arr[i] = arr[arr.length - i - 1];
}
}
```

#### After:

```java
static void reverseInPlace(int[] arr) {
for (int i = 0; i < arr.length / 2; i += 1) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
}
```

> The fix addresses the issue because the value is updated without storing the previous value. Before the temp variable is added and the loop is loop only for half of the array, the `arr[i] = arr[arr.length - i - 1];` directly override the value that we need to save it and interchange it. By loop through only half of the array and switch the elements between each other with the value stored in the `temp` first address the problem that the method change the array value with the wrong way.
## Part 2
1 change: 1 addition & 0 deletions lab4
Submodule lab4 added at 0638ac
1 change: 1 addition & 0 deletions lab5/docsearch
Submodule docsearch added at c43f6d
1 change: 1 addition & 0 deletions path-examples
Submodule path-examples added at a34d98

0 comments on commit 750d133

Please sign in to comment.