From 8ccbc821df3cc35a711b719372ed98b0912891f0 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 13 Sep 2017 20:30:29 +0300 Subject: [PATCH 01/11] init commit --- .idea/HashTable.iml | 9 ++ .idea/compiler.xml | 23 ++++ .idea/description.html | 1 + .idea/encodings.xml | 5 + .idea/misc.xml | 12 ++ .idea/modules.xml | 8 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 7 + src/Main.java | 29 ++++ .../mit/kazakov/HashTable/HashTable.java | 106 +++++++++++++++ .../mit/kazakov/HashTable/LinkedList.java | 94 +++++++++++++ src/ru/spbau/mit/kazakov/HashTable/Node.java | 21 +++ 12 files changed, 439 insertions(+) create mode 100644 .idea/HashTable.iml create mode 100644 .idea/compiler.xml create mode 100644 .idea/description.html create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 src/Main.java create mode 100644 src/ru/spbau/mit/kazakov/HashTable/HashTable.java create mode 100644 src/ru/spbau/mit/kazakov/HashTable/LinkedList.java create mode 100644 src/ru/spbau/mit/kazakov/HashTable/Node.java diff --git a/.idea/HashTable.iml b/.idea/HashTable.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/HashTable.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..217af47 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f83681f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..61b8683 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..dde6899 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,29 @@ +import ru.spbau.mit.kazakov.HashTable.*; + +public class Main { + + public static void main(String[] args) { + HashTable a = new HashTable(); + a.put("1", "44"); + a.put("2", "55"); + System.out.println(a.put("3", "66")); + System.out.println(a.remove("2")); + System.out.println(a.contains("3")); + System.out.println(a.get("1")); + System.out.println(a.get("1d")); + System.out.println(a.put("3", "33")); + System.out.println(a.get("3")); + System.out.println(a.size()); + a.clear(); + System.out.println(a.size()); + System.out.println(a.contains("3")); + + System.out.println(); + for (int i = 0; i < 16; i++) { + a.put(Integer.toString(i), Integer.toString(i)); + } + System.out.println(a.size()); + HashTable olya = new HashTable(); + olya.remove("2"); + } +} diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java new file mode 100644 index 0000000..664e3ae --- /dev/null +++ b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java @@ -0,0 +1,106 @@ +package ru.spbau.mit.kazakov.HashTable; + +/** + * HashTable + * Key: String + * Value: String + * Collision resolution: Separate chaining with linked lists + */ +public class HashTable { + private int capacity = 16; + private int size = 0; + private LinkedList[] hashtable = new LinkedList[capacity]; + + /** + * Initialises all linked lists. + */ + public HashTable() { + for (int i = 0; i < capacity; i++) + hashtable[i] = new LinkedList(); + } + + /** + * @return number of keys in hashtable + */ + public int size() { + return size; + } + + /** + * Tests if the specified string is a key in this hashtable. + */ + public boolean contains(String key) { + int hash = key.hashCode() % capacity; + return hashtable[hash].contains(key); + } + + /** + * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key + */ + public String get(String key) { + int hash = key.hashCode() % capacity; + return hashtable[hash].get(key); + } + + /** + * Maps the specified key to the specified value in this hashtable. + * + * @return previous value to which the specified key was mapped, or null if this map contained no mapping for the key + */ + public String put(String key, String value) { + int hash = key.hashCode() % capacity; + String return_value = hashtable[hash].put(key, value); + + if (return_value == null) { + size++; + if (size == capacity) + rebuild(); + } + + return return_value; + } + + /** + * Removes the specified key and its corresponding value from this hashtable. + * + * @return removed value, or null if this map contained no mapping for the key + */ + public String remove(String key) { + int hash = key.hashCode() % capacity; + String return_value = hashtable[hash].remove(key); + if (return_value != null) + size--; + return return_value; + } + + /** + * Clears this hashtable so that it contains no keys. + */ + public void clear() { + for (int i = 0; i < capacity; i++) + hashtable[i] = new LinkedList(); + size = 0; + } + + /** + * Increases capacity by copying all entries in twice and rehashes all elements. + */ + private void rebuild() { + capacity *= 2; + size = 0; + LinkedList[] old_hashtable = hashtable; + + hashtable = new LinkedList[capacity]; + for (int i = 0; i < capacity; i++) + hashtable[i] = new LinkedList(); + + for (int i = 0; i < capacity / 2; i++) { + Node current = old_hashtable[i].head.next; + while (current != null) { + put(current.key, current.value); + current = current.next; + } + } + } + +} diff --git a/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java b/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java new file mode 100644 index 0000000..4707b8c --- /dev/null +++ b/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java @@ -0,0 +1,94 @@ +package ru.spbau.mit.kazakov.HashTable; + +/** + * Singly linked list for hashtable. + */ +class LinkedList { + Node head = new Node(null, null); + + /** + * Adds new node to list, increments size. + */ + private void add(Node new_node) { + Node current = head; + + while (current.next != null) + current = current.next; + + current.next = new_node; + } + + /** + * Find node with specified key in list. + * + * @return null if there is no such node and found node otherwise + */ + private Node find(String key) { + Node current = head; + + while (current.next != null) { + current = current.next; + if (current.key.equals(key)) + return current; + } + + return null; + } + + /** + * @return true if list contains node with specified key and false otherwise + */ + boolean contains(String key) { + return find(key) != null; + } + + /** + * @return the node's value with specified key, or null there is no such node in list + */ + String get(String key) { + Node found_node = find(key); + + if (found_node != null) + return found_node.value; + else + return null; + } + + /** + * Assigns specified value to node with specified key. + * + * @return previous node's value or null if there was no node with specified key + */ + String put(String key, String value) { + Node found_node = find(key); + + if (found_node != null) { + String previous_value = found_node.value; + found_node.value = value; + return previous_value; + } else { + add(new Node(key, value)); + return null; + } + } + + /** + * Removes node with specified key from list. + * + * @return removed node's value or null if there was no node with specified key + */ + String remove(String key) { + Node current = head; + + while (current.next != null) { + if (current.next.key.equals(key)) { + String return_value = current.next.value; + current.next = current.next.next; + return return_value; + } + current = current.next; + } + + return null; + } +} diff --git a/src/ru/spbau/mit/kazakov/HashTable/Node.java b/src/ru/spbau/mit/kazakov/HashTable/Node.java new file mode 100644 index 0000000..9b3a260 --- /dev/null +++ b/src/ru/spbau/mit/kazakov/HashTable/Node.java @@ -0,0 +1,21 @@ +package ru.spbau.mit.kazakov.HashTable; + +/** + * Node for linked list. + * Store hashtable's key and value + */ +class Node { + Node next; + String key, value; + + /** + * Node constructor + * + * @param key key in hash table + * @param value value in hash table + */ + Node(String key, String value) { + this.key = key; + this.value = value; + } +} From c39cbde3d3e34ec2bb74d2d96cc8e96f094f471b Mon Sep 17 00:00:00 2001 From: DmitryAu Date: Wed, 13 Sep 2017 20:33:40 +0300 Subject: [PATCH 02/11] Delete Main.java --- src/Main.java | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index dde6899..0000000 --- a/src/Main.java +++ /dev/null @@ -1,29 +0,0 @@ -import ru.spbau.mit.kazakov.HashTable.*; - -public class Main { - - public static void main(String[] args) { - HashTable a = new HashTable(); - a.put("1", "44"); - a.put("2", "55"); - System.out.println(a.put("3", "66")); - System.out.println(a.remove("2")); - System.out.println(a.contains("3")); - System.out.println(a.get("1")); - System.out.println(a.get("1d")); - System.out.println(a.put("3", "33")); - System.out.println(a.get("3")); - System.out.println(a.size()); - a.clear(); - System.out.println(a.size()); - System.out.println(a.contains("3")); - - System.out.println(); - for (int i = 0; i < 16; i++) { - a.put(Integer.toString(i), Integer.toString(i)); - } - System.out.println(a.size()); - HashTable olya = new HashTable(); - olya.remove("2"); - } -} From cbcbd30468c4df231302d07226bb19ca5a6bf059 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 14 Sep 2017 02:11:36 +0300 Subject: [PATCH 03/11] more tests --- .gitignore | 49 ++++++ HashTable.iml | 31 ++++ .../mit/kazakov/HashTable/HashTable.java | 8 +- .../mit/kazakov/HashTable/HashTableTest.java | 154 ++++++++++++++++++ .../mit/kazakov/HashTable/LinkedListTest.java | 82 ++++++++++ 5 files changed, 320 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 HashTable.iml create mode 100644 src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java create mode 100644 src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..345e61a --- /dev/null +++ b/.gitignore @@ -0,0 +1,49 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/HashTable.iml b/HashTable.iml new file mode 100644 index 0000000..4e51204 --- /dev/null +++ b/HashTable.iml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java index 664e3ae..3e6d192 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java +++ b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java @@ -30,7 +30,7 @@ public int size() { * Tests if the specified string is a key in this hashtable. */ public boolean contains(String key) { - int hash = key.hashCode() % capacity; + int hash = Math.floorMod(key.hashCode(), capacity); return hashtable[hash].contains(key); } @@ -38,7 +38,7 @@ public boolean contains(String key) { * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key */ public String get(String key) { - int hash = key.hashCode() % capacity; + int hash = Math.floorMod(key.hashCode(), capacity); return hashtable[hash].get(key); } @@ -48,7 +48,7 @@ public String get(String key) { * @return previous value to which the specified key was mapped, or null if this map contained no mapping for the key */ public String put(String key, String value) { - int hash = key.hashCode() % capacity; + int hash = Math.floorMod(key.hashCode(), capacity); String return_value = hashtable[hash].put(key, value); if (return_value == null) { @@ -66,7 +66,7 @@ public String put(String key, String value) { * @return removed value, or null if this map contained no mapping for the key */ public String remove(String key) { - int hash = key.hashCode() % capacity; + int hash = Math.floorMod(key.hashCode(), capacity); String return_value = hashtable[hash].remove(key); if (return_value != null) size--; diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java b/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java new file mode 100644 index 0000000..169cec5 --- /dev/null +++ b/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java @@ -0,0 +1,154 @@ +package ru.spbau.mit.kazakov.HashTable; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class HashTableTest { + @Test + void testConstructor(){ + HashTable hashtable = new HashTable(); + } + + @Test + void testPutNew() { + HashTable hashtable = new HashTable(); + assertNull(hashtable.put("some key", "some value")); + assertEquals("some value", hashtable.get("some key")); + } + + @Test + void testPutReassign() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + assertEquals("some value", hashtable.put("some key", "another value")); + assertEquals("another value", hashtable.get("some key")); + } + + @Test + void testPutOneHundredElements() { + HashTable hashtable = new HashTable(); + for (int i = 0; i < 100; i++) { + hashtable.put(Integer.toString(i), Integer.toString(i)); + } + } + + @Test + void testContainsNotFound() { + HashTable hashtable = new HashTable(); + assertFalse(hashtable.contains("some key")); + } + + @Test + void testContainsFound() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + assertTrue(hashtable.contains("some key")); + } + + @Test + void testGetExisting() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + assertEquals("some value", hashtable.get("some key")); + } + + @Test + void testGetNotExisting() { + HashTable hashtable = new HashTable(); + assertNull(hashtable.get("some key")); + } + + + @Test + void testRemoveEmpty() { + HashTable hashtable = new HashTable(); + assertNull(hashtable.remove("some key")); + } + + @Test + void testRemoveNotExisting() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + assertNull(hashtable.remove("another key")); + } + + @Test + void testRemoveExisting() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + assertEquals("some value", hashtable.remove("some key")); + } + + @Test + void testSizeEmpty() { + HashTable hashtable = new HashTable(); + assertEquals(0, hashtable.size()); + } + + @Test + void testSizePutNew() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + assertEquals(1, hashtable.size()); + } + + @Test + void testSizePutReassign() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + hashtable.put("some key", "another value"); + assertEquals(1, hashtable.size()); + } + + @Test + void testSizeOneHundredElements() { + HashTable hashtable = new HashTable(); + for (int i = 0; i < 100; i++) { + hashtable.put(Integer.toString(i), Integer.toString(i)); + } + assertEquals(100, hashtable.size()); + } + + @Test + void testSizeRemoveNotExisting() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + hashtable.remove("another key"); + assertEquals(1, hashtable.size()); + } + + @Test + void testSizeRemoveExisting() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + hashtable.remove("some key"); + assertEquals(0, hashtable.size()); + } + + @Test + void testClearEmpty() { + HashTable hashtable = new HashTable(); + hashtable.clear(); + assertEquals(0, hashtable.size()); + } + + @Test + void testClearNotEmpty() { + HashTable hashtable = new HashTable(); + hashtable.put("some key", "some value"); + hashtable.clear(); + assertEquals(0, hashtable.size()); + } + + @Test + void testClearOneHundredElements() { + HashTable hashtable = new HashTable(); + for (int i = 0; i < 100; i++) { + hashtable.put(Integer.toString(i), Integer.toString(i)); + } + hashtable.clear(); + assertEquals(0, hashtable.size()); + } + +} \ No newline at end of file diff --git a/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java b/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java new file mode 100644 index 0000000..b61482f --- /dev/null +++ b/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java @@ -0,0 +1,82 @@ +package ru.spbau.mit.kazakov.HashTable; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + @Test + void testConstructor(){ + LinkedList list = new LinkedList(); + } + + @Test + void testPutNew() { + LinkedList list = new LinkedList(); + assertNull(list.put("some key", "some value")); + assertEquals("some value", list.get("some key")); + } + + @Test + void testPutReassign() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.put("some key", "other value")); + assertEquals("other value", list.get("some key")); + } + + @Test + void testPutOneHundredElements() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.put(Integer.toString(i), Integer.toString(i)); + } + } + + @Test + void testContainsNotFound() { + LinkedList list = new LinkedList(); + assertFalse(list.contains("some key")); + } + + @Test + void testContainsFound() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertTrue(list.contains("some key")); + } + + @Test + void testGetExisting() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.get("some key")); + } + + @Test + void testGetNotExisting() { + LinkedList list = new LinkedList(); + assertNull(list.get("some key")); + } + + + @Test + void testRemoveEmpty() { + LinkedList list = new LinkedList(); + assertNull(list.remove("some key")); + } + + @Test + void testRemoveNotExisting() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertNull(list.remove("another key")); + } + + @Test + void testRemoveExisting() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.remove("some key")); + } +} \ No newline at end of file From 817799d291ffa76fb82402d1a2dd7225db49dd58 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 14 Sep 2017 02:16:59 +0300 Subject: [PATCH 04/11] rm --- .idea/HashTable.iml | 9 --- .idea/compiler.xml | 23 -------- .idea/description.html | 1 - .idea/encodings.xml | 5 -- .idea/misc.xml | 12 ---- .idea/modules.xml | 8 --- .idea/uiDesigner.xml | 124 ----------------------------------------- .idea/vcs.xml | 7 --- 8 files changed, 189 deletions(-) delete mode 100644 .idea/HashTable.iml delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/description.html delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/uiDesigner.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/HashTable.iml b/.idea/HashTable.iml deleted file mode 100644 index d6ebd48..0000000 --- a/.idea/HashTable.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 217af47..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - diff --git a/.idea/description.html b/.idea/description.html deleted file mode 100644 index db5f129..0000000 --- a/.idea/description.html +++ /dev/null @@ -1 +0,0 @@ -Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index e206d70..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index f83681f..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 61b8683..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index def6a6a..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - From 8eb6e4a5370f8134b6f9025a221fbd00950eafc3 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 14 Sep 2017 02:18:23 +0300 Subject: [PATCH 05/11] .idea --- .idea/HashTable.iml | 9 +++++++++ .idea/compiler.xml | 23 +++++++++++++++++++++++ .idea/description.html | 1 + .idea/encodings.xml | 5 +++++ .idea/misc.xml | 12 ++++++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 7 +++++++ 7 files changed, 65 insertions(+) create mode 100644 .idea/HashTable.iml create mode 100644 .idea/compiler.xml create mode 100644 .idea/description.html create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/HashTable.iml b/.idea/HashTable.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/HashTable.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..217af47 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f83681f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..61b8683 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + From a738d0dbfdecb1ab301a0a396289736de58b181f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 14 Sep 2017 02:21:41 +0300 Subject: [PATCH 06/11] reload --- src/Main.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..0bca45e --- /dev/null +++ b/src/Main.java @@ -0,0 +1,27 @@ +import ru.spbau.mit.kazakov.HashTable.*; + +public class Main { + + public static void main(String[] args) { + HashTable a = new HashTable(); + a.put("1", "44"); + a.put("2", "55"); + System.out.println(a.put("3", "66")); + System.out.println(a.remove("2")); + System.out.println(a.contains("3")); + System.out.println(a.get("1")); + System.out.println(a.get("1d")); + System.out.println(a.put("3", "33")); + System.out.println(a.get("3")); + System.out.println(a.size()); + a.clear(); + System.out.println(a.get("3")); + System.out.println(a.size()); + System.out.println(a.contains("3")); + + System.out.println(); + for (int i = 0; i < 16; i++) { + a.put(Integer.toString(i), Integer.toString(i)); + } + } +} From 74c03b910b2cbf21ebf1ad4514f2c9f57642d1fe Mon Sep 17 00:00:00 2001 From: DmitryAu Date: Thu, 14 Sep 2017 02:23:14 +0300 Subject: [PATCH 07/11] Delete Main.java --- src/Main.java | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 0bca45e..0000000 --- a/src/Main.java +++ /dev/null @@ -1,27 +0,0 @@ -import ru.spbau.mit.kazakov.HashTable.*; - -public class Main { - - public static void main(String[] args) { - HashTable a = new HashTable(); - a.put("1", "44"); - a.put("2", "55"); - System.out.println(a.put("3", "66")); - System.out.println(a.remove("2")); - System.out.println(a.contains("3")); - System.out.println(a.get("1")); - System.out.println(a.get("1d")); - System.out.println(a.put("3", "33")); - System.out.println(a.get("3")); - System.out.println(a.size()); - a.clear(); - System.out.println(a.get("3")); - System.out.println(a.size()); - System.out.println(a.contains("3")); - - System.out.println(); - for (int i = 0; i < 16; i++) { - a.put(Integer.toString(i), Integer.toString(i)); - } - } -} From c87ceab306e9b789f5a74ab9be0fb8bcc7df178f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 14 Sep 2017 02:26:27 +0300 Subject: [PATCH 08/11] line format --- src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java | 4 ++-- src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java b/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java index 169cec5..b2d131f 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java +++ b/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java @@ -6,7 +6,7 @@ class HashTableTest { @Test - void testConstructor(){ + void testConstructor() { HashTable hashtable = new HashTable(); } @@ -79,7 +79,7 @@ void testRemoveExisting() { hashtable.put("some key", "some value"); assertEquals("some value", hashtable.remove("some key")); } - + @Test void testSizeEmpty() { HashTable hashtable = new HashTable(); diff --git a/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java b/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java index b61482f..119d1df 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java +++ b/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java @@ -6,7 +6,7 @@ class LinkedListTest { @Test - void testConstructor(){ + void testConstructor() { LinkedList list = new LinkedList(); } From 4ffb9ccb16d076c4132fad1a46901b8b675ffb0d Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 21 Sep 2017 10:59:58 +0300 Subject: [PATCH 09/11] tests moved to another package --- src/Main.java | 27 +++++++++++++++++++ .../mit/kazakov/HashTable/HashTable.java | 3 +++ .../mit/kazakov/HashTable/LinkedList.java | 10 +++---- .../HashTable/{ => test}/HashTableTest.java | 3 ++- .../HashTable/{ => test}/LinkedListTest.java | 3 ++- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/Main.java rename src/ru/spbau/mit/kazakov/HashTable/{ => test}/HashTableTest.java (97%) rename src/ru/spbau/mit/kazakov/HashTable/{ => test}/LinkedListTest.java (95%) diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..929d11a --- /dev/null +++ b/src/Main.java @@ -0,0 +1,27 @@ +import ru.spbau.mit.kazakov.HashTable.HashTable; + +public class Main { + + public static void main(String[] args) { + HashTable a = new HashTable(); + a.put("1", "44"); + a.put("2", "55"); + System.out.println(a.put("3", "66")); + System.out.println(a.remove("2")); + System.out.println(a.contains("3")); + System.out.println(a.get("1")); + System.out.println(a.get("1d")); + System.out.println(a.put("3", "33")); + System.out.println(a.get("3")); + System.out.println(a.size()); + a.clear(); + System.out.println(a.get("3")); + System.out.println(a.size()); + System.out.println(a.contains("3")); + + System.out.println(); + for (int i = 0; i < 16; i++) { + a.put(Integer.toString(i), Integer.toString(i)); + } + } +} diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java index 3e6d192..155e89c 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java +++ b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java @@ -1,5 +1,8 @@ package ru.spbau.mit.kazakov.HashTable; +import ru.spbau.mit.kazakov.HashTable.LinkedList; +import ru.spbau.mit.kazakov.HashTable.Node; + /** * HashTable * Key: String diff --git a/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java b/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java index 4707b8c..17170ea 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java +++ b/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java @@ -3,7 +3,7 @@ /** * Singly linked list for hashtable. */ -class LinkedList { +public class LinkedList { Node head = new Node(null, null); /** @@ -38,14 +38,14 @@ private Node find(String key) { /** * @return true if list contains node with specified key and false otherwise */ - boolean contains(String key) { + public boolean contains(String key) { return find(key) != null; } /** * @return the node's value with specified key, or null there is no such node in list */ - String get(String key) { + public String get(String key) { Node found_node = find(key); if (found_node != null) @@ -59,7 +59,7 @@ String get(String key) { * * @return previous node's value or null if there was no node with specified key */ - String put(String key, String value) { + public String put(String key, String value) { Node found_node = find(key); if (found_node != null) { @@ -77,7 +77,7 @@ String put(String key, String value) { * * @return removed node's value or null if there was no node with specified key */ - String remove(String key) { + public String remove(String key) { Node current = head; while (current.next != null) { diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java b/src/ru/spbau/mit/kazakov/HashTable/test/HashTableTest.java similarity index 97% rename from src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java rename to src/ru/spbau/mit/kazakov/HashTable/test/HashTableTest.java index b2d131f..5c20c05 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/HashTableTest.java +++ b/src/ru/spbau/mit/kazakov/HashTable/test/HashTableTest.java @@ -1,6 +1,7 @@ -package ru.spbau.mit.kazakov.HashTable; +package ru.spbau.mit.kazakov.HashTable.test; import org.junit.jupiter.api.Test; +import ru.spbau.mit.kazakov.HashTable.HashTable; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java b/src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java similarity index 95% rename from src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java rename to src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java index 119d1df..a905883 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/LinkedListTest.java +++ b/src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java @@ -1,6 +1,7 @@ -package ru.spbau.mit.kazakov.HashTable; +package ru.spbau.mit.kazakov.HashTable.test; import org.junit.jupiter.api.Test; +import ru.spbau.mit.kazakov.HashTable.LinkedList; import static org.junit.jupiter.api.Assertions.*; From 9f09c77b55684f67511f4376976320515ede7630 Mon Sep 17 00:00:00 2001 From: DmitryAu Date: Thu, 21 Sep 2017 11:01:52 +0300 Subject: [PATCH 10/11] delete Main.java --- src/Main.java | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 929d11a..0000000 --- a/src/Main.java +++ /dev/null @@ -1,27 +0,0 @@ -import ru.spbau.mit.kazakov.HashTable.HashTable; - -public class Main { - - public static void main(String[] args) { - HashTable a = new HashTable(); - a.put("1", "44"); - a.put("2", "55"); - System.out.println(a.put("3", "66")); - System.out.println(a.remove("2")); - System.out.println(a.contains("3")); - System.out.println(a.get("1")); - System.out.println(a.get("1d")); - System.out.println(a.put("3", "33")); - System.out.println(a.get("3")); - System.out.println(a.size()); - a.clear(); - System.out.println(a.get("3")); - System.out.println(a.size()); - System.out.println(a.contains("3")); - - System.out.println(); - for (int i = 0; i < 16; i++) { - a.put(Integer.toString(i), Integer.toString(i)); - } - } -} From f7f8d8fdd4b678ecea62afb20e0a49eae3fee70f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 24 Sep 2017 20:32:23 +0300 Subject: [PATCH 11/11] Node moved to LinkedList --- HashTable.iml | 1 + .../mit/kazakov/HashTable/HashTable.java | 47 +++-- .../mit/kazakov/HashTable/LinkedList.java | 89 +++++++--- src/ru/spbau/mit/kazakov/HashTable/Node.java | 21 --- .../HashTable/test/LinkedListTest.java | 83 --------- .../test => test}/HashTableTest.java | 2 - test/LinkedListTest.java | 161 ++++++++++++++++++ 7 files changed, 250 insertions(+), 154 deletions(-) delete mode 100644 src/ru/spbau/mit/kazakov/HashTable/Node.java delete mode 100644 src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java rename {src/ru/spbau/mit/kazakov/HashTable/test => test}/HashTableTest.java (98%) create mode 100644 test/LinkedListTest.java diff --git a/HashTable.iml b/HashTable.iml index 4e51204..1b93dee 100644 --- a/HashTable.iml +++ b/HashTable.iml @@ -4,6 +4,7 @@ + diff --git a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java index 155e89c..9d20fd0 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/HashTable.java +++ b/src/ru/spbau/mit/kazakov/HashTable/HashTable.java @@ -1,8 +1,5 @@ package ru.spbau.mit.kazakov.HashTable; -import ru.spbau.mit.kazakov.HashTable.LinkedList; -import ru.spbau.mit.kazakov.HashTable.Node; - /** * HashTable * Key: String @@ -18,12 +15,13 @@ public class HashTable { * Initialises all linked lists. */ public HashTable() { - for (int i = 0; i < capacity; i++) + for (int i = 0; i < capacity; i++) { hashtable[i] = new LinkedList(); + } } /** - * @return number of keys in hashtable + * Returns number of keys in hashtable. */ public int size() { return size; @@ -38,7 +36,7 @@ public boolean contains(String key) { } /** - * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key + * Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. */ public String get(String key) { int hash = Math.floorMod(key.hashCode(), capacity); @@ -47,41 +45,42 @@ public String get(String key) { /** * Maps the specified key to the specified value in this hashtable. - * - * @return previous value to which the specified key was mapped, or null if this map contained no mapping for the key + * Returns previous value to which the specified key was mapped, or null if this map contained no mapping for the key. */ public String put(String key, String value) { int hash = Math.floorMod(key.hashCode(), capacity); - String return_value = hashtable[hash].put(key, value); + String returnValue = hashtable[hash].put(key, value); - if (return_value == null) { + if (returnValue == null) { size++; - if (size == capacity) + if (size == capacity) { rebuild(); + } } - return return_value; + return returnValue; } /** * Removes the specified key and its corresponding value from this hashtable. - * - * @return removed value, or null if this map contained no mapping for the key + * Returns removed value, or null if this map contained no mapping for the key. */ public String remove(String key) { int hash = Math.floorMod(key.hashCode(), capacity); - String return_value = hashtable[hash].remove(key); - if (return_value != null) + String returnValue = hashtable[hash].remove(key); + if (returnValue != null) { size--; - return return_value; + } + return returnValue; } /** * Clears this hashtable so that it contains no keys. */ public void clear() { - for (int i = 0; i < capacity; i++) + for (int i = 0; i < capacity; i++) { hashtable[i] = new LinkedList(); + } size = 0; } @@ -91,17 +90,17 @@ public void clear() { private void rebuild() { capacity *= 2; size = 0; - LinkedList[] old_hashtable = hashtable; + LinkedList[] oldHashtable = hashtable; hashtable = new LinkedList[capacity]; - for (int i = 0; i < capacity; i++) + for (int i = 0; i < capacity; i++) { hashtable[i] = new LinkedList(); + } for (int i = 0; i < capacity / 2; i++) { - Node current = old_hashtable[i].head.next; - while (current != null) { - put(current.key, current.value); - current = current.next; + while (!oldHashtable[i].empty()) { + put(oldHashtable[i].firstElementKey(), oldHashtable[i].firstElementValue()); + oldHashtable[i].remove(oldHashtable[i].firstElementKey()); } } } diff --git a/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java b/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java index 17170ea..029afda 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java +++ b/src/ru/spbau/mit/kazakov/HashTable/LinkedList.java @@ -4,68 +4,90 @@ * Singly linked list for hashtable. */ public class LinkedList { - Node head = new Node(null, null); + private Node head = new Node(null, null); /** * Adds new node to list, increments size. */ - private void add(Node new_node) { + private void add(Node newNode) { Node current = head; - while (current.next != null) + while (current.next != null) { current = current.next; + } - current.next = new_node; + current.next = newNode; } /** * Find node with specified key in list. - * - * @return null if there is no such node and found node otherwise + * Returns null if there is no such node and found node otherwise. */ private Node find(String key) { Node current = head; while (current.next != null) { current = current.next; - if (current.key.equals(key)) + if (current.key.equals(key)) { return current; + } } return null; } /** - * @return true if list contains node with specified key and false otherwise + * Returns first element's key. + */ + public String firstElementKey() { + return head.next.key; + } + + /** + * Returns first element's value. + */ + public String firstElementValue() { + return head.next.value; + } + + /** + * Returns false if there is no elements in list and true otherwise, + */ + public boolean empty() { + return head.next == null; + } + + /** + * Returns true if list contains node with specified key and false otherwise. */ public boolean contains(String key) { return find(key) != null; } /** - * @return the node's value with specified key, or null there is no such node in list + * Returns the node's value with specified key, or null there is no such node in list. */ public String get(String key) { - Node found_node = find(key); + Node foundNode = find(key); - if (found_node != null) - return found_node.value; - else + if (foundNode != null) { + return foundNode.value; + } else { return null; + } } /** * Assigns specified value to node with specified key. - * - * @return previous node's value or null if there was no node with specified key + * Returns previous node's value or null if there was no node with specified key. */ public String put(String key, String value) { - Node found_node = find(key); + Node foundNode = find(key); - if (found_node != null) { - String previous_value = found_node.value; - found_node.value = value; - return previous_value; + if (foundNode != null) { + String previousValue = foundNode.value; + foundNode.value = value; + return previousValue; } else { add(new Node(key, value)); return null; @@ -74,21 +96,40 @@ public String put(String key, String value) { /** * Removes node with specified key from list. - * - * @return removed node's value or null if there was no node with specified key + * Returns removed node's value or null if there was no node with specified key. */ public String remove(String key) { Node current = head; while (current.next != null) { if (current.next.key.equals(key)) { - String return_value = current.next.value; + String returnValue = current.next.value; current.next = current.next.next; - return return_value; + return returnValue; } current = current.next; } return null; } + + /** + * Node for linked list. + * Store hashtable's key and value + */ + class Node { + private Node next; + private String key, value; + + /** + * Node constructor + * + * @param key key in hash table + * @param value value in hash table + */ + Node(String key, String value) { + this.key = key; + this.value = value; + } + } } diff --git a/src/ru/spbau/mit/kazakov/HashTable/Node.java b/src/ru/spbau/mit/kazakov/HashTable/Node.java deleted file mode 100644 index 9b3a260..0000000 --- a/src/ru/spbau/mit/kazakov/HashTable/Node.java +++ /dev/null @@ -1,21 +0,0 @@ -package ru.spbau.mit.kazakov.HashTable; - -/** - * Node for linked list. - * Store hashtable's key and value - */ -class Node { - Node next; - String key, value; - - /** - * Node constructor - * - * @param key key in hash table - * @param value value in hash table - */ - Node(String key, String value) { - this.key = key; - this.value = value; - } -} diff --git a/src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java b/src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java deleted file mode 100644 index a905883..0000000 --- a/src/ru/spbau/mit/kazakov/HashTable/test/LinkedListTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package ru.spbau.mit.kazakov.HashTable.test; - -import org.junit.jupiter.api.Test; -import ru.spbau.mit.kazakov.HashTable.LinkedList; - -import static org.junit.jupiter.api.Assertions.*; - -class LinkedListTest { - @Test - void testConstructor() { - LinkedList list = new LinkedList(); - } - - @Test - void testPutNew() { - LinkedList list = new LinkedList(); - assertNull(list.put("some key", "some value")); - assertEquals("some value", list.get("some key")); - } - - @Test - void testPutReassign() { - LinkedList list = new LinkedList(); - list.put("some key", "some value"); - assertEquals("some value", list.put("some key", "other value")); - assertEquals("other value", list.get("some key")); - } - - @Test - void testPutOneHundredElements() { - LinkedList list = new LinkedList(); - for (int i = 0; i < 100; i++) { - list.put(Integer.toString(i), Integer.toString(i)); - } - } - - @Test - void testContainsNotFound() { - LinkedList list = new LinkedList(); - assertFalse(list.contains("some key")); - } - - @Test - void testContainsFound() { - LinkedList list = new LinkedList(); - list.put("some key", "some value"); - assertTrue(list.contains("some key")); - } - - @Test - void testGetExisting() { - LinkedList list = new LinkedList(); - list.put("some key", "some value"); - assertEquals("some value", list.get("some key")); - } - - @Test - void testGetNotExisting() { - LinkedList list = new LinkedList(); - assertNull(list.get("some key")); - } - - - @Test - void testRemoveEmpty() { - LinkedList list = new LinkedList(); - assertNull(list.remove("some key")); - } - - @Test - void testRemoveNotExisting() { - LinkedList list = new LinkedList(); - list.put("some key", "some value"); - assertNull(list.remove("another key")); - } - - @Test - void testRemoveExisting() { - LinkedList list = new LinkedList(); - list.put("some key", "some value"); - assertEquals("some value", list.remove("some key")); - } -} \ No newline at end of file diff --git a/src/ru/spbau/mit/kazakov/HashTable/test/HashTableTest.java b/test/HashTableTest.java similarity index 98% rename from src/ru/spbau/mit/kazakov/HashTable/test/HashTableTest.java rename to test/HashTableTest.java index 5c20c05..43ffe10 100644 --- a/src/ru/spbau/mit/kazakov/HashTable/test/HashTableTest.java +++ b/test/HashTableTest.java @@ -1,5 +1,3 @@ -package ru.spbau.mit.kazakov.HashTable.test; - import org.junit.jupiter.api.Test; import ru.spbau.mit.kazakov.HashTable.HashTable; diff --git a/test/LinkedListTest.java b/test/LinkedListTest.java new file mode 100644 index 0000000..d2d3280 --- /dev/null +++ b/test/LinkedListTest.java @@ -0,0 +1,161 @@ +import org.junit.jupiter.api.Test; +import ru.spbau.mit.kazakov.HashTable.LinkedList; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + @Test + void testConstructor() { + LinkedList list = new LinkedList(); + } + + @Test + void testPutNew() { + LinkedList list = new LinkedList(); + assertNull(list.put("some key", "some value")); + assertEquals("some value", list.get("some key")); + } + + @Test + void testPutReassign() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.put("some key", "other value")); + assertEquals("other value", list.get("some key")); + } + + @Test + void testPutOneHundredElements() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.put(Integer.toString(i), Integer.toString(i)); + } + } + + @Test + void testContainsNotFound() { + LinkedList list = new LinkedList(); + assertFalse(list.contains("some key")); + } + + @Test + void testContainsFound() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertTrue(list.contains("some key")); + } + + @Test + void testGetExisting() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.get("some key")); + } + + @Test + void testGetNotExisting() { + LinkedList list = new LinkedList(); + assertNull(list.get("some key")); + } + + + @Test + void testRemoveEmpty() { + LinkedList list = new LinkedList(); + assertNull(list.remove("some key")); + } + + @Test + void testRemoveNotExisting() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertNull(list.remove("another key")); + } + + @Test + void testRemoveExisting() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.remove("some key")); + } + + @Test + void testFirstElementKeyOneElement() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some key", list.firstElementKey()); + } + + @Test + void testFirstElementKeyAfterRemove() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + list.put("another key", "another value"); + list.remove("some key"); + assertEquals("another key", list.firstElementKey()); + } + + @Test + void testFirstElementKeyOneHundredElements() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.put(Integer.toString(i), Integer.toString(i)); + } + assertEquals("0", list.firstElementKey()); + } + + @Test + void testFirstElementValueOneElement() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertEquals("some value", list.firstElementValue()); + } + + @Test + void testFirstElementValueAfterRemove() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + list.put("another key", "another value"); + list.remove("some key"); + assertEquals("another value", list.firstElementValue()); + } + + @Test + void testFirstElementValueOneHundredElements() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.put(Integer.toString(i), Integer.toString(i)); + } + assertEquals("0", list.firstElementValue()); + } + + @Test + void testEmptyTrue() { + LinkedList list = new LinkedList(); + assertTrue(list.empty()); + } + + @Test + void testEmptyFalse() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + assertFalse(list.empty()); + } + + @Test + void testEmptyTrueAfterRemove() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + list.remove("some key"); + assertTrue(list.empty()); + } + + @Test + void testEmptyFalseAfterRemove() { + LinkedList list = new LinkedList(); + list.put("some key", "some value"); + list.put("another key", "another value"); + list.remove("some key"); + assertFalse(list.empty()); + } +} \ No newline at end of file