-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 2. Tests for hw1 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
psolikov
wants to merge
2
commits into
master
Choose a base branch
from
hw1_tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| <exclude-output /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| <orderEntry type="module-library" scope="TEST"> | ||
| <library name="JUnit4"> | ||
| <CLASSES> | ||
| <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" /> | ||
| <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" /> | ||
| </CLASSES> | ||
| <JAVADOC /> | ||
| <SOURCES /> | ||
| </library> | ||
| </orderEntry> | ||
| <orderEntry type="module-library"> | ||
| <library> | ||
| <CLASSES> | ||
| <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" /> | ||
| <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" /> | ||
| </CLASSES> | ||
| <JAVADOC /> | ||
| <SOURCES /> | ||
| </library> | ||
| </orderEntry> | ||
| </component> | ||
| </module> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package ru.spbau.solikov.hw1.src; | ||
|
|
||
| /** | ||
| * Implementation of hash table structure using separate chaining | ||
| */ | ||
| public class HashTable { | ||
|
|
||
| /** | ||
| * Number used in hash function | ||
| */ | ||
| private static final int DEFAULT_HASH_PRIME_NUMBER = 31; | ||
|
|
||
| private List[] table; | ||
| private int capacity = 2; | ||
| private int size; | ||
|
|
||
| /** | ||
| * Creates a HashTable of default capacity | ||
| */ | ||
| public HashTable() { | ||
| table = new List[2]; | ||
| size = 0; | ||
| } | ||
|
|
||
| /** | ||
| * Returns number of keys | ||
| * | ||
| * @return size | ||
| */ | ||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Iterates on each list in HashTable and clears it, capacity is the same | ||
| */ | ||
| public void clear() { | ||
| for (List list : table) { | ||
| if (list != null) { | ||
| list.clear(); | ||
| } | ||
| } | ||
| size = 0; | ||
| } | ||
|
|
||
| /** | ||
| * Hash function for obtaining a code from key | ||
| * | ||
| * @param key | ||
| * @return hash code | ||
| */ | ||
| private int getHash(String key) { | ||
| int hash = 0; | ||
| for (int i = 0; i < key.length(); i++) { | ||
| hash = (DEFAULT_HASH_PRIME_NUMBER * hash + key.charAt(i)) % capacity; | ||
| } | ||
| return hash; | ||
| } | ||
|
|
||
| /** | ||
| * Returns data stored by key or null if not found | ||
| * | ||
| * @param key | ||
| * @return data or null if not found | ||
| */ | ||
| public String get(String key) { | ||
| int hash = getHash(key); | ||
| if (table[hash] == null){ | ||
| return null; | ||
| } | ||
| return table[hash].find(key); | ||
| } | ||
|
|
||
| /** | ||
| * Increases the capacity of HashTable by 2 times | ||
| */ | ||
| private void increase() { | ||
| List[] oldTable = table; | ||
| table = new List[2 * capacity]; | ||
| capacity *= 2; | ||
|
|
||
| for (List list : oldTable) { | ||
| if (list != null) { | ||
| while (!list.isEmpty()) { | ||
| String key = list.getHeadsKey(); | ||
| int hash = getHash(key); | ||
| if (table[hash] == null) { | ||
| table[hash] = new List(); | ||
| } | ||
| table[hash].insert(key, list.getHeadsData()); | ||
| list.delete(key); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Allows to add the pair of key and data into HashTable, if there's not enough space calls increase() | ||
| * | ||
| * @param key | ||
| * @param data | ||
| * @return data that was stored before by the same key or null if this space was empty | ||
| */ | ||
| public String put(String key, String data) { | ||
| int hash = getHash(key); | ||
| if (table[hash] == null) { | ||
| table[hash] = new List(); | ||
| } | ||
|
|
||
| List current = table[hash]; | ||
| String deleted = current.delete(key); | ||
| current.insert(key, data); | ||
|
|
||
| if (deleted == null) { | ||
| size++; | ||
| } | ||
|
|
||
| if (size >= capacity) { | ||
| increase(); | ||
| } | ||
|
|
||
| return deleted; | ||
| } | ||
|
|
||
| /** | ||
| * Allows to remove data stored by key | ||
| * | ||
| * @param key | ||
| * @return data stored by key | ||
| */ | ||
| public String remove(String key) { | ||
| int hash = getHash(key); | ||
|
|
||
| if (table[hash] == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String deleted = table[hash].delete(key); | ||
| if (deleted != null) { | ||
| size--; | ||
| return deleted; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether HashTable contains some data by passed key | ||
| * | ||
| * @param key | ||
| * @return data or null if the HashTable doesn't contain key | ||
| */ | ||
| public boolean contains(String key) { | ||
| return get(key) != null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package ru.spbau.solikov.hw1.src; | ||
|
|
||
| /** | ||
| * Implementation of single-linked list that stores Nodes of keys and data | ||
| */ | ||
| public class List { | ||
|
|
||
| /** | ||
| * Class that stores keys and data and has link for next Node | ||
| */ | ||
| private class Node { | ||
|
|
||
| private Node next; | ||
|
|
||
| private String key; | ||
|
|
||
| private String data; | ||
|
|
||
| public Node(Node n, String k, String d) { | ||
| next = n; | ||
| key = k; | ||
| data = d; | ||
| } | ||
| } | ||
|
|
||
| private Node head; | ||
|
|
||
| private Node tail; | ||
|
|
||
| public Node getHead() { | ||
| return head; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a pair of key and data in List | ||
| * | ||
| * @param key | ||
| * @param data | ||
| */ | ||
| public void insert(String key, String data) { | ||
| if (head != null) { | ||
| tail.next = new Node(null, key, data); | ||
| tail = tail.next; | ||
| } 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 | ||
| * | ||
| * @param key | ||
| * @return data or null if not found | ||
| */ | ||
| public String find(String key) { | ||
| Node current = head; | ||
| while (current != null) { | ||
| if (current.key.equals(key)) { | ||
| return current.data; | ||
| } | ||
| 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 | ||
| * | ||
| * @param key | ||
| * @return data that was deleted | ||
| */ | ||
| public String delete(String key) { | ||
| if (head == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (head.key.equals(key)) { | ||
| String deleted = head.data; | ||
| head = head.next; | ||
| return deleted; | ||
| } | ||
|
|
||
| Node current = head; | ||
| while (current.next != null) { | ||
| if (current.next.key.equals(key)) { | ||
| String deleted = current.next.data; | ||
| current.next = current.next.next; | ||
| return deleted; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Clears the List | ||
| */ | ||
| public void clear() { | ||
| head = null; | ||
| tail = null; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ну и зачем это тут? тут только тесты должны быть.
-1