Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding junit test to test bubble sort case #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@
<artifactId>interview</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>interview</name>
<url>http://maven.apache.org</url>
</project>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

</project>
38 changes: 19 additions & 19 deletions src/main/java/com/zhokhov/interview/data/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
*/
package com.zhokhov.interview.data;

import java.util.logging.Logger;

/**
* @author <a href='mailto:[email protected]'>Alexey Zhokhov</a>
*/

public class ArrayList {

private static final int SIZE_FACTOR = 5;

private Object data[];
private static final Logger logger = Logger.getLogger(ArrayList.class.getName());
private Object[] data;

private int index;

Expand All @@ -29,7 +33,6 @@ public ArrayList() {

public void add(Object item) {
System.out.println("index:" + this.index + "size:" + this.size + "data size:" + this.data.length);

if (this.index == this.size - 1) {
//we need to increase the size of data[]
increaseSizeAndReallocate();
Expand All @@ -43,46 +46,43 @@ private void increaseSizeAndReallocate() {
this.size = this.size + SIZE_FACTOR;

// TODO: Arrays.copyOf()
Object newData[] = new Object[this.size];
Object[] newData = new Object[this.size];

// TODO: System.arraycopy(data, 0, newData, 0, data.length);
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
System.arraycopy(data, 0, newData, 0, data.length);

this.data = newData;

System.out.println("***index:" + this.index + "size:" + this.size + "data size:" + this.data.length);
logger.info("***index:" + this.index + "size:" + this.size + "data size:" + this.data.length);
}

public Object get(int index) throws Exception {
public Object get(int index) {
if (index > this.index - 1) {
throw new Exception("ArrayIndexOutOfBound");
throw new IllegalStateException("ArrayIndexOutOfBound");
}
if (index < 0) {
throw new Exception("Negative Value");
throw new IllegalStateException("Negative Value");
}

return this.data[index];
}

public void remove(int removeIndex) throws Exception {
public void remove(int removeIndex) {
if (removeIndex > this.index - 1) {
throw new Exception("ArrayIndexOutOfBound");
throw new IllegalStateException("ArrayIndexOutOfBound");
}
if (removeIndex < 0) {
throw new Exception("Negative Value");
throw new IllegalStateException("Negative Value");
}
System.out.println("Object getting removed:" + this.data[removeIndex]);
logger.fine("Object getting removed: " + this.data[removeIndex]);

for (int i = removeIndex; i < this.data.length - 1; i++) {
data[i] = data[i + 1];
}
if (this.data.length - 1 - removeIndex >= 0)
System.arraycopy(data, removeIndex + 1, data, removeIndex, this.data.length - 1 - removeIndex);

this.index--;
}

public static void main(String[] args) throws Exception {
public static void main(String[] args) {
ArrayList mal = new ArrayList();
mal.add("1");
mal.add("2");
Expand All @@ -98,7 +98,7 @@ public static void main(String[] args) throws Exception {
// remove by index
mal.remove(5);

System.out.println(mal.get(7));
logger.info((String) mal.get(7));
}

}
38 changes: 38 additions & 0 deletions src/test/java/sorting/BubbleSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sorting;

import com.zhokhov.interview.sorting.BubbleSort;
import com.zhokhov.interview.util.Console;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;


@RunWith(JUnit4.class)
public class BubbleSortTest extends TestCase {


private static int[] array;


@Test
public void bubbleSortIntegerArray(){
array = new int[]{12, 2, 8, 5, 1, 6, 4, 15};
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.sort(array);
Console.__red("Index 1 is value in this array => " + array[1]);
assertEquals(2, array[1]);
}

@Test
public void checkLengthAfterUsingBubbleSort(){
array = new int[]{12, 2, 8, 5, 1, 6, 4, 15};
int lengthBeforeSort = array.length;
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.sort(array);
int lengthAfterSort = array.length;
assertEquals(lengthBeforeSort, lengthAfterSort);
}


}