removeUniqueElementsWithEqualHashesDecrementsSize test nor workin with hastable

88
June 14, 2022, at 06:40 AM

I have one test in this testclass that is not going through. I have implemented how I think it should be written, but I get a message :

java.lang.AssertionError: 
Expected: <-1>
     but: was <0>
Expected :<-1>
Actual   :<0>

I dont know what I am doing wrong. I think that my Hashtable code is correct however, this test does not work. Please look at the code.

//EDIT: I have added the new implementation of the test, where it goes through in JUNIT test.

//TODO: How should I write this test in the testclass.

Interface class:

/**
 * An interface describing a generic set. Duplicates are not allowed.
 */
public interface Set<T> {
    boolean add(T elem);
    boolean remove(T elem);
    boolean contains(T elem);
    int size();
}

Hashtable class:


import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import static java.util.Objects.hash;
/**
 * A hash table-based implementation of the Set interface.
 */
public class HashSet<T> implements Set<T> {
   private List<T>[] table;
    private int size;
    private int capacity;
    private int index;

     /**
     * Creates a hash table with the given capacity (amount of buckets).
     *
     * @throws IllegalArgumentException if capacity <= 0.
     */
    public HashSet(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException(
                "capacity must be a positive, non-zero value! Provided: " + capacity);
        } 
     @SuppressWarnings("unchecked") // for this declaration only
      List<T>[] t = new LinkedList[capacity];
      table = t;
      size = 0;
    }

    /**
     * Adds the given element to the set.
     *
     * Complexity: O(1) expected time.
     *
     * @param elem An element to add to the set.
     * @return true if the set did not contain the element, otherwise false.
     */
    @Override
    public boolean add(T elem) {
    if (elem == null || elem.equals("")){
            return false;
        }
        int hash = Math.abs(elem.hashCode() % table.length);
        if (table[hash]== null){
            table[hash] = new LinkedList<T>();
            table[hash].add(elem);
            size ++;
            return true;
        }
        if (table[hash].contains(elem)){
            return false;
        }else{
            table[hash].add(elem);
            size++;
            return true;
        }
    }
    /**
     * Removes the given element from the dictionary, if it is present.
     *
     * Complexity: O(1) expected time.
     *
     * @param elem An element to remove from the set.
     * @return true if the set contained the element, false otherwise.
     */
    @Override
    public boolean remove(T elem) {
        if (elem == null || elem.equals("")){
            return false;
        }
        int hash = Math.abs(elem.hashCode() % table.length);
        if (table[hash] != null && table[hash].contains(elem)){
            table[hash].remove(elem);
            size --;
            return true;
        }else{
            return false;
        }
    }

    /**
     * Check if an element is in the Set.
     *
     * Complexity: O(1) expected time.
     *
     * @param elem An element to look for.
     * @return true if the element is in the set, false otherwise.
     */
    @Override
    public boolean contains(T elem) {
        if (elem == null || elem .equals("")){
            return false;
        }
        int hash = Math.abs(elem.hashCode() % table.length);
        if (table[hash] != null && table[hash].contains(elem)){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        HashSet<?> hashSet = (HashSet<?>) o;
        return capacity == hashSet.capacity && size == hashSet.size && index == hashSet.index && Objects.equals(table, hashSet.table);
    }
    @Override
    public int hashCode() {
        return hash(capacity, size, index, table);
    }
    /**
     * Returns the number of elements in this set.
     *
     * Complexity: O(1) expected time.
     *
     * @return The amount of elements in this set.
     */
    @Override
    public int size() {
        return size;
    }
}

Test class:

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import java.util.Arrays;
/**
 * Test for the HashSet implementation of the Set interface. Runs both the
 * SetTest tests, as well as tests specific to hashing.
 *
 * This test class mostly contains only hash collision tests.
 */
public class HashSetTest extends SetTest {
    private Set<SingleHashUnequal> set;
    private SingleHashUnequal[] uniqueObjsWithEqualHashes;
    /**
     * Returns an implementation of Set that can hold at least 'minCapacity'
     * Integers.
     *
     * @param minCapacity The least amount of elements the Set must be able to
     * hold.
     * @return An implementation of Set.
     */
    @Override
    protected Set<Integer> getIntegerSet(int minCapacity) {
        return new HashSet<Integer>(minCapacity);
    }
    @Override
    @Before
    public void setUp() {
        // We are overriding the setUp method of SetTest, so we need to call
        // it explicitly to not break everything.
        super.setUp();
        int numDummies = 10;
        uniqueObjsWithEqualHashes = new SingleHashUnequal[numDummies];
        set = new HashSet<SingleHashUnequal>(numDummies * 2);
        for (int i = 0; i < numDummies; i++) {
            SingleHashUnequal dummy = new SingleHashUnequal();
            set.add(dummy);
            uniqueObjsWithEqualHashes[i] = dummy;
        }
    }
  @Test
    public void removeUniqueElementsWithEqualHashesDecrementsSize() { //TESTT
        SingleHashUnequal elem = new SingleHashUnequal();
        int expectedSize = set.size();
            // Act
            set.remove(elem);
            // Assert
            assertThat(set.size(), equalTo(expectedSize));
        }
    }
    /**
     * A helper class for testing hash collisions. Instances equal only
     * themselves, and all instances have the same hashCode.
     */
    private static class SingleHashUnequal {
        private static final int HASH = 0;
        @Override
        public boolean equals(Object o) {
            return this == o;
        }
        @Override
        public int hashCode() {
            return HASH;
        }
    }
}
Rent Charter Buses Company
READ ALSO
Conditional code based on Build/flavor - Identical to C# #if DEBUG

Conditional code based on Build/flavor - Identical to C# #if DEBUG

In C# there is a way to include/exclude parts of the code based on build type

85
Java.lang.NullPointerException error while exporting SSL Certificate BurpSuit

Java.lang.NullPointerException error while exporting SSL Certificate BurpSuit

I have burp suite installed on windows 10, I tried to configure the certificate and when I try to download it from the embedded browser I see that it is in cer format and can't be added to firefox or chrome, so I tried to export it using burp option in Der format,...

99