diff --git a/pom.xml b/pom.xml index 979cb1f..31d4a33 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,28 @@ interview jar 1.0-SNAPSHOT - interview - http://maven.apache.org - + + + junit + junit + 4.4 + test + + + junit + junit + 4.13.2 + compile + + + log4j + log4j + 1.2.17 + + + + 11 + 11 + + + \ No newline at end of file diff --git a/src/main/java/com/zhokhov/interview/data/ArrayList.java b/src/main/java/com/zhokhov/interview/data/ArrayList.java index d27c621..c3610e7 100644 --- a/src/main/java/com/zhokhov/interview/data/ArrayList.java +++ b/src/main/java/com/zhokhov/interview/data/ArrayList.java @@ -9,14 +9,18 @@ */ package com.zhokhov.interview.data; +import java.util.logging.Logger; + /** * @author Alexey Zhokhov */ + 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; @@ -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(); @@ -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"); @@ -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)); } } \ No newline at end of file diff --git a/src/test/java/sorting/BubbleSortTest.java b/src/test/java/sorting/BubbleSortTest.java new file mode 100644 index 0000000..13e2f3f --- /dev/null +++ b/src/test/java/sorting/BubbleSortTest.java @@ -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); + } + + +} \ No newline at end of file