From f863ee513b4c7ab7ddff9f35be7d0ef08e40177a Mon Sep 17 00:00:00 2001 From: Pavel Solikov Date: Wed, 27 Sep 2017 11:01:02 +0300 Subject: [PATCH 1/5] Unit-Test for hw1 --- hw1/hw1.iml | 31 +++ .../hw1/src/primitive/test/HashTableTest.java | 41 ++++ .../hw1/src/primitive/test/ListTest.java | 33 ++++ .../spbau/solikov/hw1/test/HashTableTest.java | 177 ++++++++++++++++-- hw1/ru/spbau/solikov/hw1/test/ListTest.java | 76 ++++++-- 5 files changed, 324 insertions(+), 34 deletions(-) create mode 100644 hw1/hw1.iml create mode 100644 hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java create mode 100644 hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java diff --git a/hw1/hw1.iml b/hw1/hw1.iml new file mode 100644 index 0000000..bf5ed58 --- /dev/null +++ b/hw1/hw1.iml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java b/hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java new file mode 100644 index 0000000..6a881ca --- /dev/null +++ b/hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java @@ -0,0 +1,41 @@ +package ru.spbau.solikov.hw1.src.primitive.test; + +import ru.spbau.solikov.hw1.src.HashTable; + +/** + * Class for testing all the HashTable functions + */ + +public class HashTableTest { + public static void main(String[] args) { + HashTable hashTable = new HashTable(); + + for (int i = 0; i < 100; i++){ + hashTable.put(String.valueOf(i), String.valueOf(i * i)); + } + + System.out.println(hashTable.size() + " - size of hashtable (must be 100)"); + + System.out.println(); + + System.out.println(hashTable.get("10") + " - square of 10 (must be 100)"); + + System.out.println(); + + System.out.println(hashTable.put("10", "200") + " - changed square of 10 (must have been 100)"); + + System.out.println(); + + System.out.println(hashTable.contains("10") + " - contains '10' (must be true)"); + + System.out.println(); + + System.out.println(hashTable.remove("10") + " - removed '10'"); + + System.out.println(); + + hashTable.clear(); + + System.out.println(hashTable.size() + " - size of hashtable (must be 0)"); + } +} diff --git a/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java b/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java new file mode 100644 index 0000000..4572b49 --- /dev/null +++ b/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java @@ -0,0 +1,33 @@ +package ru.spbau.solikov.hw1.src.primitive.test; + +import ru.spbau.solikov.hw1.src.List; + +/** + * Class for testing all the List functions + */ + +public class ListTest { + public static void main(String[] args) { + List l = new List(); + + for (int i = 0; i < 10; i++) { + l.insert(String.valueOf(i), String.valueOf(i)); + } + + for (List.Node node = l.getHead(); node != null; node = node.getNext()) { + System.out.println(node.getKey()); + } + + System.out.println(); + + System.out.println(l.find("5") + " - found key '5'."); + l.delete("5"); + System.out.println(l.delete("5") + " - deleted key '5'."); + + System.out.println(); + + l.clear(); + System.out.print(l.getHead() == null); + System.out.println(" - cleared the list."); + } +} diff --git a/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java b/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java index 2c14322..e66599b 100644 --- a/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java @@ -1,41 +1,178 @@ package ru.spbau.solikov.hw1.test; +import org.junit.Before; +import org.junit.Test; import ru.spbau.solikov.hw1.src.HashTable; -/** - * Class for testing all the HashTable functions - */ +import static org.junit.Assert.*; public class HashTableTest { - public static void main(String[] args) { - HashTable hashTable = new HashTable(); - for (int i = 0; i < 100; i++){ - hashTable.put(String.valueOf(i), String.valueOf(i * i)); + private HashTable table; + + @Before + public void setUp() { + table = new HashTable(); + } + + @Test + public void testSizeOfEmptyHashTable() { + assertEquals(0, table.size()); + } + + @Test + public void testPut() { + table.put("27", "09"); + assertEquals(true, table.contains("27")); + } + + @Test + public void testSizeAfterPut() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); } + assertEquals(10, table.size()); + } - System.out.println(hashTable.size() + " - size of hashtable (must be 100)"); + @Test + public void testSizeAfterPutExistingKey() { + table.put("123", "321"); + table.put("123", "333"); + assertEquals(1, table.size()); + } - System.out.println(); + @Test + public void testPutExistingKey() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + table.put("4", "99"); + assertEquals("99", table.get("4")); + } - System.out.println(hashTable.get("10") + " - square of 10 (must be 100)"); + @Test + public void testClearEmpty() { + table.clear(); + assertEquals(0, table.size()); + } - System.out.println(); + @Test + public void testClear() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + table.clear(); + assertEquals(0, table.size()); + } - System.out.println(hashTable.put("10", "200") + " - changed square of 10 (must have been 100)"); + @Test + public void testGetFromEmpty() { + assertEquals(null, table.get("Deadline")); + } - System.out.println(); + @Test + public void testGetElementFromOneElementTable() { + table.put("1", "2"); + assertEquals("2", table.get("1")); + } - System.out.println(hashTable.contains("10") + " - contains '10' (must be true)"); + @Test + public void testGetElementFromManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + assertEquals("5", table.get("5")); + } - System.out.println(); + @Test + public void testGetDoesNotChangeSize() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + table.get("5"); + assertEquals(10, table.size()); + } - System.out.println(hashTable.remove("10") + " - removed '10'"); + @Test + public void testSizeAfterRemovingFromEmptyTable() { + table.remove("123"); + assertEquals(0, table.size()); + } - System.out.println(); + @Test + public void testRemoveFromEmptyTable() { + assertEquals(null, table.remove("123")); + } - hashTable.clear(); + @Test + public void testRemoveFromOneElementTable() { + table.put("123", "321"); + assertEquals("321", table.get("123")); + } + + @Test + public void testSizeAfterRemovingFromOneElementTable() { + table.put("123", "321"); + table.remove("123"); + assertEquals(0, table.size()); + } + + @Test + public void testSizeAfterRemovingNonexistingFromManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + table.remove("100"); + assertEquals(10, table.size()); + } + + @Test + public void testSizeAfterRemoveFromManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + table.remove("5"); + assertEquals(9, table.size()); + } + + @Test + public void testRemoveFromManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + assertEquals("4", table.remove("4")); + } - System.out.println(hashTable.size() + " - size of hashtable (must be 0)"); + @Test + public void testContainsFromEmptyTable() { + assertEquals(false, table.contains("some")); + } + + @Test + public void testContainsFromOneElementTable() { + table.put("Wednesday", "27.09"); + assertEquals(true, table.contains("Wednesday")); + } + + @Test + public void testContainsNonexistingElementFromOneElementTable() { + table.put("Wednesday", "27.09"); + assertEquals(false, table.contains("Sunday")); + } + + @Test + public void testContainsManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + assertEquals(true, table.contains("4")); + } + + @Test + public void testContainsNonexistingElementFromManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + assertEquals(false, table.contains("123")); } -} +} \ No newline at end of file diff --git a/hw1/ru/spbau/solikov/hw1/test/ListTest.java b/hw1/ru/spbau/solikov/hw1/test/ListTest.java index 4711288..8d8b694 100644 --- a/hw1/ru/spbau/solikov/hw1/test/ListTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/ListTest.java @@ -1,33 +1,81 @@ package ru.spbau.solikov.hw1.test; +import org.junit.Before; +import org.junit.Test; import ru.spbau.solikov.hw1.src.List; -/** - * Class for testing all the List functions - */ +import static org.junit.Assert.*; public class ListTest { - public static void main(String[] args) { - List l = new List(); + private List l; + + @Before + public void setUp() { + l = new List(); + } + + @Test + public void testGetHeadOfEmptyList() { + assertEquals(null, l.getHead()); + } + + @Test + public void testInsert() { + l.insert("SPbAU", "Java"); + assertEquals("Java", l.getHead().getData()); + } + + @Test + public void testManyInserts() { for (int i = 0; i < 10; i++) { l.insert(String.valueOf(i), String.valueOf(i)); } - for (List.Node node = l.getHead(); node != null; node = node.getNext()) { - System.out.println(node.getKey()); + int number = 0; + for (List.Node n = l.getHead(); n != null; n = n.getNext()) { + assertEquals(Integer.toString(number), n.getData()); + number++; } + } + + @Test + public void testFindNonexistingElement() { + assertEquals(null, l.find("Something")); + } - System.out.println(); + @Test + public void testFindExistingElement() { + l.insert("SPbAU", "Java"); + assertEquals("Java", l.find("SPbAU")); + } - System.out.println(l.find("5") + " - found key '5'."); + @Test + public void testDeleteElementFromEmptyList() { + l.delete("SomeKey"); + assertEquals(null, l.getHead()); + } + + @Test + public void testDeleteNonexistingElement() { + l.insert("1", "2"); l.delete("5"); - System.out.println(l.delete("5") + " - deleted key '5'."); + assertNotEquals(null, l.getHead()); + } - System.out.println(); + @Test + public void testDeleteExistingElement() { + l.insert("SPbAU", "Java"); + l.delete("SPbAU"); + assertEquals(null, l.getHead()); + } + @Test + public void testClear() { + for (int i = 0; i < 10; i++) { + l.insert(String.valueOf(i), String.valueOf(i)); + } l.clear(); - System.out.print(l.getHead() == null); - System.out.println(" - cleared the list."); + assertEquals(null, l.getHead()); } -} +} \ No newline at end of file From 4beb1572a366cfc323cfaf2b843bdf566a793b9d Mon Sep 17 00:00:00 2001 From: Pavel Solikov Date: Wed, 27 Sep 2017 19:52:33 +0300 Subject: [PATCH 2/5] Added new Home Work --- hw2/.idea/compiler.xml | 9 ++ hw2/.idea/gradle.xml | 17 +++ .../libraries/Gradle__junit_junit_4_12.xml | 11 ++ ...Gradle__org_hamcrest_hamcrest_core_1_3.xml | 11 ++ hw2/.idea/modules.xml | 10 ++ hw2/.idea/modules/hw2.iml | 13 ++ hw2/.idea/modules/hw2_main.iml | 13 ++ hw2/.idea/modules/hw2_test.iml | 17 +++ hw2/build.gradle | 14 +++ hw2/settings.gradle | 2 + .../java/ru/spbau/solikov/hw2/src/Spiral.java | 112 ++++++++++++++++++ .../solikov/hw2/src/test/SpiralTest.java | 49 ++++++++ 12 files changed, 278 insertions(+) create mode 100644 hw2/.idea/compiler.xml create mode 100644 hw2/.idea/gradle.xml create mode 100644 hw2/.idea/libraries/Gradle__junit_junit_4_12.xml create mode 100644 hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 hw2/.idea/modules.xml create mode 100644 hw2/.idea/modules/hw2.iml create mode 100644 hw2/.idea/modules/hw2_main.iml create mode 100644 hw2/.idea/modules/hw2_test.iml create mode 100644 hw2/build.gradle create mode 100644 hw2/settings.gradle create mode 100644 hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java create mode 100644 hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java diff --git a/hw2/.idea/compiler.xml b/hw2/.idea/compiler.xml new file mode 100644 index 0000000..f68cfe7 --- /dev/null +++ b/hw2/.idea/compiler.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/hw2/.idea/gradle.xml b/hw2/.idea/gradle.xml new file mode 100644 index 0000000..a5c3ae1 --- /dev/null +++ b/hw2/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/hw2/.idea/libraries/Gradle__junit_junit_4_12.xml b/hw2/.idea/libraries/Gradle__junit_junit_4_12.xml new file mode 100644 index 0000000..04c10dd --- /dev/null +++ b/hw2/.idea/libraries/Gradle__junit_junit_4_12.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..8262f72 --- /dev/null +++ b/hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/hw2/.idea/modules.xml b/hw2/.idea/modules.xml new file mode 100644 index 0000000..0a515eb --- /dev/null +++ b/hw2/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/hw2/.idea/modules/hw2.iml b/hw2/.idea/modules/hw2.iml new file mode 100644 index 0000000..df6b05c --- /dev/null +++ b/hw2/.idea/modules/hw2.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw2/.idea/modules/hw2_main.iml b/hw2/.idea/modules/hw2_main.iml new file mode 100644 index 0000000..47d7e3c --- /dev/null +++ b/hw2/.idea/modules/hw2_main.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw2/.idea/modules/hw2_test.iml b/hw2/.idea/modules/hw2_test.iml new file mode 100644 index 0000000..1975b36 --- /dev/null +++ b/hw2/.idea/modules/hw2_test.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw2/build.gradle b/hw2/build.gradle new file mode 100644 index 0000000..4d6a793 --- /dev/null +++ b/hw2/build.gradle @@ -0,0 +1,14 @@ +group 'spbau' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/hw2/settings.gradle b/hw2/settings.gradle new file mode 100644 index 0000000..c182db6 --- /dev/null +++ b/hw2/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'hw2' + diff --git a/hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java b/hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java new file mode 100644 index 0000000..9c1ce9f --- /dev/null +++ b/hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java @@ -0,0 +1,112 @@ +package ru.spbau.solikov.hw2.src; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Scanner; + +/** + * Matrix of size N * N that could be printed as spiral and sorted by first elements in column + */ +public class Spiral { + + private final int size; + private int matrix[][]; + + /** + * Generates matrix of given size + * Transposes it in order to easily sort by columns + * + * @param N odd integer + * @param other matrix + */ + public Spiral(int N, int other[][]) { + size = N; + matrix = new int[N][N]; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + matrix[j][i] = other[i][j]; + } + } + } + + /** + * Method for initializing matrix's fields from standard input + * Transposes it in order to easily sort by columns + */ + public void fill() { + Scanner in = new Scanner(System.in); + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + matrix[j][i] = in.nextInt(); + } + } + } + + /** + * Sorting the matrix by first element in column using custom comparator + */ + public void sort() { + Arrays.sort(matrix, new Comparator() { + @Override + public int compare(final int[] first, final int[] second) { + Integer f = first[0]; + Integer s = second[0]; + return f.compareTo(s); + } + }); + } + + /** + * Prints matrix's fields into standard output + * Transposes matrix back to the original view + */ + public void print() { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + System.out.print(matrix[j][i] + " "); + } + System.out.println(); + } + } + + public int[][] getMatrix() { + return matrix; + } + + /** + * Transposes matrix back to the original view + * Prints matrix's fields into standard output by spiral order. Integer 'c' is the center of matrix + * External 'for' loop implements the layer of matrix + * 4 internal 'for' loops implement round way on sides of current rectangle + */ + public void printSpiral() { + final int c = size / 2; + System.out.print(matrix[c][c] + " "); + if (size > 1) { + int x = c; + int y = c; + int layer = 0; + for (int side = 2; side < size - layer; side++) { + y++; + System.out.print(matrix[y][x] + " "); + for (int i = 1; i < side + layer; i++) { + System.out.print(matrix[y][x - i] + " "); + } + x -= side - 1 + layer; + for (int i = 1; i < side + layer + 1; i++) { + System.out.print(matrix[y - i][x] + " "); + } + y -= side + layer; + for (int i = 1; i < side + layer + 1; i++) { + System.out.print(matrix[y][x + i] + " "); + } + x += side + layer; + for (int i = 1; i < side + layer + 1; i++) { + System.out.print(matrix[y + i][x] + " "); + } + y += side + layer; + layer++; + } + } + } +} \ No newline at end of file diff --git a/hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java b/hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java new file mode 100644 index 0000000..497577a --- /dev/null +++ b/hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java @@ -0,0 +1,49 @@ +package ru.spbau.solikov.hw2.src.test; + +import org.junit.Before; +import org.junit.Test; +import ru.spbau.solikov.hw2.src.Spiral; + +import java.util.Arrays; +import java.util.Comparator; + +import static org.junit.Assert.*; + + +public class SpiralTest { + + private int[][] elements = { + {4, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + + private Spiral s = new Spiral(3, elements); + + /** + * Tests if matrix is equal to transposed input matrix + */ + @Test + public void testConstructor() { + int[][] matrix = s.getMatrix(); + for (int i = 0; i < matrix.length; i++){ + for (int j = 0; j < matrix.length; j++){ + assertEquals(true, matrix[j][i] == elements[i][j]); + } + } + } + + /** + * Tests if matrix is sorted from left to right + */ + @Test + public void testSort() { + s.sort(); + int[][] matrix = s.getMatrix(); + int prev = matrix[0][0]; + for (int i = 1; i < matrix.length; i++){ + assertEquals(true, prev <= matrix[i][0]); + } + } + +} \ No newline at end of file From 36facc9f1331415738dfaa3652b2eadd9a4642b3 Mon Sep 17 00:00:00 2001 From: Pavel Solikov Date: Wed, 27 Sep 2017 20:30:13 +0300 Subject: [PATCH 3/5] Added JavaDoc --- hw1/ru/spbau/solikov/hw1/src/HashTable.java | 4 +- .../spbau/solikov/hw1/test/HashTableTest.java | 83 ++++++++++++++++++- hw1/ru/spbau/solikov/hw1/test/ListTest.java | 45 ++++++++++ 3 files changed, 126 insertions(+), 6 deletions(-) diff --git a/hw1/ru/spbau/solikov/hw1/src/HashTable.java b/hw1/ru/spbau/solikov/hw1/src/HashTable.java index dc8722d..fe9b463 100644 --- a/hw1/ru/spbau/solikov/hw1/src/HashTable.java +++ b/hw1/ru/spbau/solikov/hw1/src/HashTable.java @@ -1,12 +1,12 @@ package ru.spbau.solikov.hw1.src; /** - * Implementation of hash table structure using separate chaining + * Implementation of hash table structure using separate chaining */ public class HashTable { /** - * Number used in hash function + * Number used in hash function */ private static final int DEFAULT_HASH_PRIME_NUMBER = 31; diff --git a/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java b/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java index e66599b..4e01672 100644 --- a/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java @@ -6,26 +6,41 @@ import static org.junit.Assert.*; +/** + * Test class for HashTable with separate chaining + */ public class HashTableTest { private HashTable table; + /** + * Constructs new hash table for every test + */ @Before public void setUp() { table = new HashTable(); } + /** + * Tests if the size of empty table is 0 + */ @Test public void testSizeOfEmptyHashTable() { assertEquals(0, table.size()); } + /** + * Tests if 'put' puts the right key + */ @Test public void testPut() { table.put("27", "09"); assertEquals(true, table.contains("27")); } + /** + * Tests if the size after many 'puts' is correct + */ @Test public void testSizeAfterPut() { for (int i = 0; i < 10; i++) { @@ -34,6 +49,9 @@ public void testSizeAfterPut() { assertEquals(10, table.size()); } + /** + * Tests if the size after 'put' in the same key is correct + */ @Test public void testSizeAfterPutExistingKey() { table.put("123", "321"); @@ -41,6 +59,9 @@ public void testSizeAfterPutExistingKey() { assertEquals(1, table.size()); } + /** + * Tests if 'put' inserts new value instead of previous + */ @Test public void testPutExistingKey() { for (int i = 0; i < 10; i++) { @@ -50,12 +71,18 @@ public void testPutExistingKey() { assertEquals("99", table.get("4")); } + /** + * Tests if 'size' on empty table is 0 + */ @Test public void testClearEmpty() { table.clear(); assertEquals(0, table.size()); } + /** + * Tests if 'clear' works right on table with many keys + */ @Test public void testClear() { for (int i = 0; i < 10; i++) { @@ -65,17 +92,26 @@ public void testClear() { assertEquals(0, table.size()); } + /** + * Tests if 'get' from empty table returns null + */ @Test public void testGetFromEmpty() { assertEquals(null, table.get("Deadline")); } + /** + * Tests if 'get' from one-element table returns the right value + */ @Test public void testGetElementFromOneElementTable() { table.put("1", "2"); assertEquals("2", table.get("1")); } + /** + * Tests if 'get' on many-element table returns the right value + */ @Test public void testGetElementFromManyElementTable() { for (int i = 0; i < 10; i++) { @@ -84,6 +120,9 @@ public void testGetElementFromManyElementTable() { assertEquals("5", table.get("5")); } + /** + * Tests if 'get' does not change the size of hash table + */ @Test public void testGetDoesNotChangeSize() { for (int i = 0; i < 10; i++) { @@ -93,39 +132,57 @@ public void testGetDoesNotChangeSize() { assertEquals(10, table.size()); } + /** + * Tests if 'remove' does nothing with table after removing from empty table + */ @Test public void testSizeAfterRemovingFromEmptyTable() { table.remove("123"); assertEquals(0, table.size()); } + /** + * Tests if 'remove' on empty table returns null + */ @Test public void testRemoveFromEmptyTable() { assertEquals(null, table.remove("123")); } + /** + * Tests if 'remove' on one-element table returns the right value + */ @Test - public void testRemoveFromOneElementTable() { + public void testRemoveFromOneElementTable() { table.put("123", "321"); assertEquals("321", table.get("123")); } + /** + * Tests if the size of table becomes zero after removing from one-element table + */ @Test - public void testSizeAfterRemovingFromOneElementTable() { + public void testSizeAfterRemovingFromOneElementTable() { table.put("123", "321"); table.remove("123"); assertEquals(0, table.size()); } + /** + * Tests if the size of hash table is not affected by removing element that does not exist + */ @Test - public void testSizeAfterRemovingNonexistingFromManyElementTable() { + public void testSizeAfterRemovingNonexistingFromManyElementTable() { for (int i = 0; i < 10; i++) { table.put(String.valueOf(i), String.valueOf(i)); } table.remove("100"); assertEquals(10, table.size()); } - + + /** + * Tests if size after removing existing element decreases + */ @Test public void testSizeAfterRemoveFromManyElementTable() { for (int i = 0; i < 10; i++) { @@ -135,6 +192,9 @@ public void testSizeAfterRemoveFromManyElementTable() { assertEquals(9, table.size()); } + /** + * Tests if 'remove' from many-element table returns the right value + */ @Test public void testRemoveFromManyElementTable() { for (int i = 0; i < 10; i++) { @@ -143,23 +203,35 @@ public void testRemoveFromManyElementTable() { assertEquals("4", table.remove("4")); } + /** + * Tests if 'contains' returns false on empty table + */ @Test public void testContainsFromEmptyTable() { assertEquals(false, table.contains("some")); } + /** + * Tests if 'contains' returns true on element that exists in one-element table + */ @Test public void testContainsFromOneElementTable() { table.put("Wednesday", "27.09"); assertEquals(true, table.contains("Wednesday")); } + /** + * Tests if 'contains' returns false on element that does not exist in one-element table + */ @Test public void testContainsNonexistingElementFromOneElementTable() { table.put("Wednesday", "27.09"); assertEquals(false, table.contains("Sunday")); } + /** + * Tests if 'contains' returns true on many-element table + */ @Test public void testContainsManyElementTable() { for (int i = 0; i < 10; i++) { @@ -168,6 +240,9 @@ public void testContainsManyElementTable() { assertEquals(true, table.contains("4")); } + /** + * Tests if 'contains' returns false on elements that don't exist in many-element table + */ @Test public void testContainsNonexistingElementFromManyElementTable() { for (int i = 0; i < 10; i++) { diff --git a/hw1/ru/spbau/solikov/hw1/test/ListTest.java b/hw1/ru/spbau/solikov/hw1/test/ListTest.java index 8d8b694..11d094e 100644 --- a/hw1/ru/spbau/solikov/hw1/test/ListTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/ListTest.java @@ -6,26 +6,41 @@ import static org.junit.Assert.*; +/** + * Test class for class List that implements single-linked list + */ public class ListTest { private List l; + /** + * Constructs new list for every test + */ @Before public void setUp() { l = new List(); } + /** + * Tests if 'getHead' on empty list returns null + */ @Test public void testGetHeadOfEmptyList() { assertEquals(null, l.getHead()); } + /** + * Tests if 'insert' on empty list inserts the right data + */ @Test public void testInsert() { l.insert("SPbAU", "Java"); assertEquals("Java", l.getHead().getData()); } + /** + * Tests if many 'insert's insert the right data + */ @Test public void testManyInserts() { for (int i = 0; i < 10; i++) { @@ -39,23 +54,47 @@ public void testManyInserts() { } } + /** + * Tests if 'find' on empty list returns null + */ @Test public void testFindNonexistingElement() { assertEquals(null, l.find("Something")); } + /** + * Tests if 'find' on one-element list returns right value + */ @Test public void testFindExistingElement() { l.insert("SPbAU", "Java"); assertEquals("Java", l.find("SPbAU")); } + /** + * Tests if 'find' on many-element list returns right value + */ + @Test + public void testFindExistingElementManyElements() { + for (int i = 0; i < 10; i++) { + l.insert(String.valueOf(i), String.valueOf(i)); + } + l.insert("SPbAU", "Java"); + assertEquals("Java", l.find("SPbAU")); + } + + /** + * Tests if 'delete' on empty list does nothing + */ @Test public void testDeleteElementFromEmptyList() { l.delete("SomeKey"); assertEquals(null, l.getHead()); } + /** + * Tests if 'delete' nonexisting element on one-element list does nothing + */ @Test public void testDeleteNonexistingElement() { l.insert("1", "2"); @@ -63,6 +102,9 @@ public void testDeleteNonexistingElement() { assertNotEquals(null, l.getHead()); } + /** + * Tests if 'delete' from one-element list truly deletes element + */ @Test public void testDeleteExistingElement() { l.insert("SPbAU", "Java"); @@ -70,6 +112,9 @@ public void testDeleteExistingElement() { assertEquals(null, l.getHead()); } + /** + * Tests if 'clear' deletes all elements from many-element list + */ @Test public void testClear() { for (int i = 0; i < 10; i++) { From dc6efaeb11e89f3b46fc117e8e349b254c9614e0 Mon Sep 17 00:00:00 2001 From: Pavel Solikov Date: Sat, 30 Sep 2017 03:24:11 +0300 Subject: [PATCH 4/5] Fixed remarks from to 1st branch --- hw1/ru/spbau/solikov/hw1/src/HashTable.java | 23 +++----- hw1/ru/spbau/solikov/hw1/src/List.java | 58 ++++++++++--------- .../hw1/src/primitive/test/ListTest.java | 4 -- hw1/ru/spbau/solikov/hw1/test/ListTest.java | 21 +++---- 4 files changed, 52 insertions(+), 54 deletions(-) diff --git a/hw1/ru/spbau/solikov/hw1/src/HashTable.java b/hw1/ru/spbau/solikov/hw1/src/HashTable.java index fe9b463..f71fafd 100644 --- a/hw1/ru/spbau/solikov/hw1/src/HashTable.java +++ b/hw1/ru/spbau/solikov/hw1/src/HashTable.java @@ -11,14 +11,13 @@ public class HashTable { private static final int DEFAULT_HASH_PRIME_NUMBER = 31; private List[] table; - private int capacity; + private int capacity = 2; private int size; /** - * Creates a HashTable of capacity 2 + * Creates a HashTable of default capacity */ public HashTable() { - capacity = 2; table = new List[2]; size = 0; } @@ -45,7 +44,6 @@ public void clear() { size = 0; } - /** * Hash function for obtaining a code from key * @@ -60,7 +58,6 @@ private int getHash(String key) { return hash; } - /** * Returns data stored by key or null if not found * @@ -69,11 +66,12 @@ private int getHash(String key) { */ public String get(String key) { int hash = getHash(key); - if (table[hash] == null) return null; + if (table[hash] == null){ + return null; + } return table[hash].find(key); } - /** * Increases the capacity of HashTable by 2 times */ @@ -84,15 +82,14 @@ private void increase() { for (List list : oldTable) { if (list != null) { - List.Node node = list.getHead(); - while (node != null) { - String key = node.getKey(); + while (!list.isEmpty()) { + String key = list.getHeadsKey(); int hash = getHash(key); if (table[hash] == null) { table[hash] = new List(); } - table[hash].insert(key, node.getData()); - node = node.getNext(); + table[hash].insert(key, list.getHeadsData()); + list.delete(key); } } } @@ -126,7 +123,6 @@ public String put(String key, String data) { return deleted; } - /** * Allows to remove data stored by key * @@ -149,7 +145,6 @@ public String remove(String key) { return null; } - /** * Checks whether HashTable contains some data by passed key * diff --git a/hw1/ru/spbau/solikov/hw1/src/List.java b/hw1/ru/spbau/solikov/hw1/src/List.java index 82fbfea..04f8d48 100644 --- a/hw1/ru/spbau/solikov/hw1/src/List.java +++ b/hw1/ru/spbau/solikov/hw1/src/List.java @@ -8,10 +8,12 @@ public class List { /** * Class that stores keys and data and has link for next Node */ - public class Node { + private class Node { private Node next; + private String key; + private String data; public Node(Node n, String k, String d) { @@ -19,21 +21,10 @@ public Node(Node n, String k, String d) { key = k; data = d; } - - public Node getNext() { - return next; - } - - public String getKey() { - return key; - } - - public String getData() { - return data; - } } private Node head; + private Node tail; public Node getHead() { @@ -47,16 +38,23 @@ public Node getHead() { * @param data */ public void insert(String key, String data) { - if (head != null){ + if (head != null) { tail.next = new Node(null, key, data); tail = tail.next; - } - - else{ + } else { tail = head = new Node(null, key, data); } } + /** + * Checks if the list does not contain any elements + * + * @return boolean - true if the list is empty + */ + public boolean isEmpty() { + return head == null; + } + /** * Searches for data by key in List * @@ -65,16 +63,24 @@ public void insert(String key, String data) { */ public String find(String key) { Node current = head; - while (current != null){ - if (current.getKey().equals(key)){ - return current.getData(); + while (current != null) { + if (current.key.equals(key)) { + return current.data; } - current = current.getNext(); + current = current.next; } return null; } + public String getHeadsKey() { + return head.key; + } + + public String getHeadsData() { + return head.data; + } + /** * Deletes a data stored by key in List * @@ -82,19 +88,19 @@ public String find(String key) { * @return data that was deleted */ public String delete(String key) { - if (head == null){ + if (head == null) { return null; } - if (head.getKey().equals(key)){ + if (head.key.equals(key)) { String deleted = head.data; head = head.next; return deleted; } Node current = head; - while (current.next != null){ - if (current.next.getKey().equals(key)){ + while (current.next != null) { + if (current.next.key.equals(key)) { String deleted = current.next.data; current.next = current.next.next; return deleted; @@ -108,7 +114,7 @@ public String delete(String key) { /** * Clears the List */ - public void clear(){ + public void clear() { head = null; tail = null; } diff --git a/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java b/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java index 4572b49..be54d90 100644 --- a/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java +++ b/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java @@ -14,10 +14,6 @@ public static void main(String[] args) { l.insert(String.valueOf(i), String.valueOf(i)); } - for (List.Node node = l.getHead(); node != null; node = node.getNext()) { - System.out.println(node.getKey()); - } - System.out.println(); System.out.println(l.find("5") + " - found key '5'."); diff --git a/hw1/ru/spbau/solikov/hw1/test/ListTest.java b/hw1/ru/spbau/solikov/hw1/test/ListTest.java index 11d094e..7100040 100644 --- a/hw1/ru/spbau/solikov/hw1/test/ListTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/ListTest.java @@ -22,11 +22,11 @@ public void setUp() { } /** - * Tests if 'getHead' on empty list returns null + * Tests if 'isEmpty' on empty list returns true */ @Test - public void testGetHeadOfEmptyList() { - assertEquals(null, l.getHead()); + public void testIsEmptyOfEmptyList() { + assertEquals(true, l.isEmpty()); } /** @@ -35,7 +35,7 @@ public void testGetHeadOfEmptyList() { @Test public void testInsert() { l.insert("SPbAU", "Java"); - assertEquals("Java", l.getHead().getData()); + assertEquals("Java", l.getHeadsData()); } /** @@ -48,9 +48,10 @@ public void testManyInserts() { } int number = 0; - for (List.Node n = l.getHead(); n != null; n = n.getNext()) { - assertEquals(Integer.toString(number), n.getData()); + while (!l.isEmpty()) { + assertEquals(Integer.toString(number), l.getHeadsData()); number++; + l.delete(l.getHeadsKey()); } } @@ -89,7 +90,7 @@ public void testFindExistingElementManyElements() { @Test public void testDeleteElementFromEmptyList() { l.delete("SomeKey"); - assertEquals(null, l.getHead()); + assertEquals(true, l.isEmpty()); } /** @@ -99,7 +100,7 @@ public void testDeleteElementFromEmptyList() { public void testDeleteNonexistingElement() { l.insert("1", "2"); l.delete("5"); - assertNotEquals(null, l.getHead()); + assertNotEquals(true, l.isEmpty()); } /** @@ -109,7 +110,7 @@ public void testDeleteNonexistingElement() { public void testDeleteExistingElement() { l.insert("SPbAU", "Java"); l.delete("SPbAU"); - assertEquals(null, l.getHead()); + assertEquals(true, l.isEmpty()); } /** @@ -121,6 +122,6 @@ public void testClear() { l.insert(String.valueOf(i), String.valueOf(i)); } l.clear(); - assertEquals(null, l.getHead()); + assertEquals(true, l.isEmpty()); } } \ No newline at end of file From 48e5f3d02930915d0bc52285438368cda235b7f2 Mon Sep 17 00:00:00 2001 From: Pavel Solikov Date: Wed, 8 Nov 2017 20:40:06 +0300 Subject: [PATCH 5/5] Deleted hw2 --- hw2/.idea/compiler.xml | 9 -- hw2/.idea/gradle.xml | 17 --- .../libraries/Gradle__junit_junit_4_12.xml | 11 -- ...Gradle__org_hamcrest_hamcrest_core_1_3.xml | 11 -- hw2/.idea/modules.xml | 10 -- hw2/.idea/modules/hw2.iml | 13 -- hw2/.idea/modules/hw2_main.iml | 13 -- hw2/.idea/modules/hw2_test.iml | 17 --- hw2/build.gradle | 14 --- hw2/settings.gradle | 2 - .../java/ru/spbau/solikov/hw2/src/Spiral.java | 112 ------------------ .../solikov/hw2/src/test/SpiralTest.java | 49 -------- 12 files changed, 278 deletions(-) delete mode 100644 hw2/.idea/compiler.xml delete mode 100644 hw2/.idea/gradle.xml delete mode 100644 hw2/.idea/libraries/Gradle__junit_junit_4_12.xml delete mode 100644 hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml delete mode 100644 hw2/.idea/modules.xml delete mode 100644 hw2/.idea/modules/hw2.iml delete mode 100644 hw2/.idea/modules/hw2_main.iml delete mode 100644 hw2/.idea/modules/hw2_test.iml delete mode 100644 hw2/build.gradle delete mode 100644 hw2/settings.gradle delete mode 100644 hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java delete mode 100644 hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java diff --git a/hw2/.idea/compiler.xml b/hw2/.idea/compiler.xml deleted file mode 100644 index f68cfe7..0000000 --- a/hw2/.idea/compiler.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/hw2/.idea/gradle.xml b/hw2/.idea/gradle.xml deleted file mode 100644 index a5c3ae1..0000000 --- a/hw2/.idea/gradle.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/hw2/.idea/libraries/Gradle__junit_junit_4_12.xml b/hw2/.idea/libraries/Gradle__junit_junit_4_12.xml deleted file mode 100644 index 04c10dd..0000000 --- a/hw2/.idea/libraries/Gradle__junit_junit_4_12.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml deleted file mode 100644 index 8262f72..0000000 --- a/hw2/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/hw2/.idea/modules.xml b/hw2/.idea/modules.xml deleted file mode 100644 index 0a515eb..0000000 --- a/hw2/.idea/modules.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/hw2/.idea/modules/hw2.iml b/hw2/.idea/modules/hw2.iml deleted file mode 100644 index df6b05c..0000000 --- a/hw2/.idea/modules/hw2.iml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/hw2/.idea/modules/hw2_main.iml b/hw2/.idea/modules/hw2_main.iml deleted file mode 100644 index 47d7e3c..0000000 --- a/hw2/.idea/modules/hw2_main.iml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/hw2/.idea/modules/hw2_test.iml b/hw2/.idea/modules/hw2_test.iml deleted file mode 100644 index 1975b36..0000000 --- a/hw2/.idea/modules/hw2_test.iml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/hw2/build.gradle b/hw2/build.gradle deleted file mode 100644 index 4d6a793..0000000 --- a/hw2/build.gradle +++ /dev/null @@ -1,14 +0,0 @@ -group 'spbau' -version '1.0-SNAPSHOT' - -apply plugin: 'java' - -sourceCompatibility = 1.8 - -repositories { - mavenCentral() -} - -dependencies { - testCompile group: 'junit', name: 'junit', version: '4.12' -} diff --git a/hw2/settings.gradle b/hw2/settings.gradle deleted file mode 100644 index c182db6..0000000 --- a/hw2/settings.gradle +++ /dev/null @@ -1,2 +0,0 @@ -rootProject.name = 'hw2' - diff --git a/hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java b/hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java deleted file mode 100644 index 9c1ce9f..0000000 --- a/hw2/src/main/java/ru/spbau/solikov/hw2/src/Spiral.java +++ /dev/null @@ -1,112 +0,0 @@ -package ru.spbau.solikov.hw2.src; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.Scanner; - -/** - * Matrix of size N * N that could be printed as spiral and sorted by first elements in column - */ -public class Spiral { - - private final int size; - private int matrix[][]; - - /** - * Generates matrix of given size - * Transposes it in order to easily sort by columns - * - * @param N odd integer - * @param other matrix - */ - public Spiral(int N, int other[][]) { - size = N; - matrix = new int[N][N]; - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - matrix[j][i] = other[i][j]; - } - } - } - - /** - * Method for initializing matrix's fields from standard input - * Transposes it in order to easily sort by columns - */ - public void fill() { - Scanner in = new Scanner(System.in); - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - matrix[j][i] = in.nextInt(); - } - } - } - - /** - * Sorting the matrix by first element in column using custom comparator - */ - public void sort() { - Arrays.sort(matrix, new Comparator() { - @Override - public int compare(final int[] first, final int[] second) { - Integer f = first[0]; - Integer s = second[0]; - return f.compareTo(s); - } - }); - } - - /** - * Prints matrix's fields into standard output - * Transposes matrix back to the original view - */ - public void print() { - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - System.out.print(matrix[j][i] + " "); - } - System.out.println(); - } - } - - public int[][] getMatrix() { - return matrix; - } - - /** - * Transposes matrix back to the original view - * Prints matrix's fields into standard output by spiral order. Integer 'c' is the center of matrix - * External 'for' loop implements the layer of matrix - * 4 internal 'for' loops implement round way on sides of current rectangle - */ - public void printSpiral() { - final int c = size / 2; - System.out.print(matrix[c][c] + " "); - if (size > 1) { - int x = c; - int y = c; - int layer = 0; - for (int side = 2; side < size - layer; side++) { - y++; - System.out.print(matrix[y][x] + " "); - for (int i = 1; i < side + layer; i++) { - System.out.print(matrix[y][x - i] + " "); - } - x -= side - 1 + layer; - for (int i = 1; i < side + layer + 1; i++) { - System.out.print(matrix[y - i][x] + " "); - } - y -= side + layer; - for (int i = 1; i < side + layer + 1; i++) { - System.out.print(matrix[y][x + i] + " "); - } - x += side + layer; - for (int i = 1; i < side + layer + 1; i++) { - System.out.print(matrix[y + i][x] + " "); - } - y += side + layer; - layer++; - } - } - } -} \ No newline at end of file diff --git a/hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java b/hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java deleted file mode 100644 index 497577a..0000000 --- a/hw2/src/test/java/ru/spbau/solikov/hw2/src/test/SpiralTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package ru.spbau.solikov.hw2.src.test; - -import org.junit.Before; -import org.junit.Test; -import ru.spbau.solikov.hw2.src.Spiral; - -import java.util.Arrays; -import java.util.Comparator; - -import static org.junit.Assert.*; - - -public class SpiralTest { - - private int[][] elements = { - {4, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - - private Spiral s = new Spiral(3, elements); - - /** - * Tests if matrix is equal to transposed input matrix - */ - @Test - public void testConstructor() { - int[][] matrix = s.getMatrix(); - for (int i = 0; i < matrix.length; i++){ - for (int j = 0; j < matrix.length; j++){ - assertEquals(true, matrix[j][i] == elements[i][j]); - } - } - } - - /** - * Tests if matrix is sorted from left to right - */ - @Test - public void testSort() { - s.sort(); - int[][] matrix = s.getMatrix(); - int prev = matrix[0][0]; - for (int i = 1; i < matrix.length; i++){ - assertEquals(true, prev <= matrix[i][0]); - } - } - -} \ No newline at end of file