Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions hw1/hw1.iml
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>
41 changes: 41 additions & 0 deletions hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java
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)");
}
}
29 changes: 29 additions & 0 deletions hw1/ru/spbau/solikov/hw1/src/primitive/test/ListTest.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
250 changes: 231 additions & 19 deletions hw1/ru/spbau/solikov/hw1/test/HashTableTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
Loading