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..be54d90 --- /dev/null +++ b/hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java @@ -0,0 +1,29 @@ +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)); + } + + 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 6a881ca..f7d5d17 100644 --- a/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/HashTableTest.java @@ -1,41 +1,253 @@ package ru.spbau.solikov.hw1.src.primitive.test; +import org.junit.Before; +import org.junit.Test; import ru.spbau.solikov.hw1.src.HashTable; +import static org.junit.Assert.*; + /** - * Class for testing all the HashTable functions + * Test class for HashTable with separate chaining */ - 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; + + /** + * 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++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + assertEquals(10, table.size()); + } + + /** + * Tests if the size after 'put' in the same key is correct + */ + @Test + public void testSizeAfterPutExistingKey() { + table.put("123", "321"); + table.put("123", "333"); + assertEquals(1, table.size()); + } + + /** + * Tests if 'put' inserts new value instead of previous + */ + @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")); + } + + /** + * 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++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + table.clear(); + 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++) { + table.put(String.valueOf(i), String.valueOf(i)); } + assertEquals("5", table.get("5")); + } - System.out.println(hashTable.size() + " - size of hashtable (must be 100)"); + /** + * Tests if 'get' does not change the size of hash table + */ + @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(); + /** + * Tests if 'remove' does nothing with table after removing from empty table + */ + @Test + public void testSizeAfterRemovingFromEmptyTable() { + table.remove("123"); + assertEquals(0, table.size()); + } - System.out.println(hashTable.get("10") + " - square of 10 (must be 100)"); + /** + * Tests if 'remove' on empty table returns null + */ + @Test + public void testRemoveFromEmptyTable() { + assertEquals(null, table.remove("123")); + } - System.out.println(); + /** + * Tests if 'remove' on one-element table returns the right value + */ + @Test + public void testRemoveFromOneElementTable() { + table.put("123", "321"); + assertEquals("321", table.get("123")); + } - System.out.println(hashTable.put("10", "200") + " - changed square of 10 (must have been 100)"); + /** + * Tests if the size of table becomes zero after removing from one-element table + */ + @Test + public void testSizeAfterRemovingFromOneElementTable() { + table.put("123", "321"); + table.remove("123"); + assertEquals(0, table.size()); + } - System.out.println(); + /** + * Tests if the size of hash table is not affected by removing element that does not exist + */ + @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()); + } - System.out.println(hashTable.contains("10") + " - contains '10' (must be true)"); + /** + * Tests if size after removing existing element decreases + */ + @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()); + } - System.out.println(); + /** + * Tests if 'remove' from many-element table returns the right value + */ + @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.remove("10") + " - removed '10'"); + /** + * Tests if 'contains' returns false on empty table + */ + @Test + public void testContainsFromEmptyTable() { + assertEquals(false, table.contains("some")); + } - System.out.println(); + /** + * 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")); + } - hashTable.clear(); + /** + * 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")); + } - System.out.println(hashTable.size() + " - size of hashtable (must be 0)"); + /** + * Tests if 'contains' returns true on many-element table + */ + @Test + public void testContainsManyElementTable() { + for (int i = 0; i < 10; i++) { + table.put(String.valueOf(i), String.valueOf(i)); + } + 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++) { + 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 be54d90..db8c8f1 100644 --- a/hw1/ru/spbau/solikov/hw1/test/ListTest.java +++ b/hw1/ru/spbau/solikov/hw1/test/ListTest.java @@ -1,29 +1,127 @@ package ru.spbau.solikov.hw1.src.primitive.test; +import org.junit.Before; +import org.junit.Test; import ru.spbau.solikov.hw1.src.List; +import static org.junit.Assert.*; + /** - * Class for testing all the List functions + * Test class for class List that implements single-linked list */ - public class ListTest { - public static void main(String[] args) { - List l = new List(); + private List l; + + /** + * Constructs new list for every test + */ + @Before + public void setUp() { + l = new List(); + } + + /** + * Tests if 'isEmpty' on empty list returns true + */ + @Test + public void testIsEmptyOfEmptyList() { + assertEquals(true, l.isEmpty()); + } + + /** + * Tests if 'insert' on empty list inserts the right data + */ + @Test + public void testInsert() { + l.insert("SPbAU", "Java"); + assertEquals("Java", l.getHeadsData()); + } + + /** + * Tests if many 'insert's insert the right data + */ + @Test + public void testManyInserts() { for (int i = 0; i < 10; i++) { l.insert(String.valueOf(i), String.valueOf(i)); } - System.out.println(); + int number = 0; + while (!l.isEmpty()) { + assertEquals(Integer.toString(number), l.getHeadsData()); + number++; + l.delete(l.getHeadsKey()); + } + } - System.out.println(l.find("5") + " - found key '5'."); + /** + * 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(true, l.isEmpty()); + } + + /** + * Tests if 'delete' nonexisting element on one-element list does nothing + */ + @Test + public void testDeleteNonexistingElement() { + l.insert("1", "2"); l.delete("5"); - System.out.println(l.delete("5") + " - deleted key '5'."); + assertNotEquals(true, l.isEmpty()); + } - System.out.println(); + /** + * Tests if 'delete' from one-element list truly deletes element + */ + @Test + public void testDeleteExistingElement() { + l.insert("SPbAU", "Java"); + l.delete("SPbAU"); + assertEquals(true, l.isEmpty()); + } + /** + * Tests if 'clear' deletes all elements from many-element list + */ + @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(true, l.isEmpty()); } -} +} \ No newline at end of file