Skip to content

Commit

Permalink
GP-145 Fix resizeTable method to calculate HashTable size correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tboychuk committed Jan 15, 2024
1 parent 0916202 commit 88e17bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bobocode.cs;

import static java.util.Objects.requireNonNull;

import lombok.ToString;

import static java.util.Objects.requireNonNull;

/**
* {@link HashTable} is a simple Hashtable-based implementation of {@link Map} interface with some additional methods.
* It is based on the array of {@link Node} objects. Both {@link HashTable} and {@link Node} have two type parameters:
Expand Down Expand Up @@ -271,6 +271,7 @@ public String toString() {
public void resizeTable(int newCapacity) {
verifyCapacity(newCapacity);
@SuppressWarnings("unchecked") Node<K, V>[] newTable = new Node[newCapacity];
size = 0;
for (var head : table) {
var current = head;
while (current != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,10 @@ class HashTableHelperMethodsTest {
@Order(1)
@DisplayName("resizeTable creates a new array and put there all elements")
void resizeTable() {
addToTable("madmax", 833);
addToTable("altea", 553);
addToTable("AaAa", 123);
addToTable("BBBB", 456);
hashTable.put("madmax", 833);
hashTable.put("altea", 553);
hashTable.put("AaAa", 123);
hashTable.put("BBBB", 456);

hashTable.resizeTable(16);

Expand All @@ -518,6 +518,20 @@ void resizeTable() {
}

@Test
@Order(2)
@DisplayName("resizeTable does not change the size")
void resizeTableDoesNotChangeSize() {
hashTable.put("madmax", 833);
hashTable.put("altea", 553);
hashTable.put("AaAa", 123);

hashTable.resizeTable(32);

assertThat(hashTable.size()).isEqualTo(3);
}

@Test
@Order(3)
@DisplayName("toString returns a string that represents an underlying table")
void toStringTest() {
addToTable("madmax", 833);
Expand Down

0 comments on commit 88e17bf

Please sign in to comment.