import java.util.Collections; import java.util.LinkedList; import java.util.Random; import java.util.Arrays; public class HashMapTester { /** * Testing put and resize */ public static void test1(){ //Adds some values, makes sure they're in the Hashmap, makes sure it resizes System.out.println("-".repeat(30) + "\nTest1: "); String expected = "Hashmap after adding 5, 100, 3\nbin 0: <100 -> 2> | \nbin 1: <5 -> 1> | \nbin 2: <30 -> 3> | \nbin 3: \n\nHashmap after adding 12\nbin 0: \nbin 1: \nbin 2: \nbin 3: \nbin 4: <100 -> 2> | <12 -> 4> | \nbin 5: <5 -> 1> | \nbin 6: <30 -> 3> | \nbin 7: \n\nHashmap after adding 4, 6\nbin 0: \nbin 1: \nbin 2: \nbin 3: \nbin 4: <100 -> 2> | <12 -> 4> | <4 -> 5> | \nbin 5: <5 -> 1> | \nbin 6: <30 -> 3> | <6 -> 6> | \nbin 7: \n"; System.out.println("Expected output: " + expected ); System.out.println("-".repeat(15) + "\n"); String actual = ""; //Start it with 4 bins so it's small. MapSet map = new HashMap<>(4, 0.75); map.put( 5, "" + 1 ); map.put( 100, "" + 2 ); map.put( 30, "" + 3 ); actual += "Hashmap after adding 5, 100, 3\n" + map ; map.put( 12, "" + 4 ); actual += "\nHashmap after adding 12\n" + map ; map.put( 4, "" + 5 ); map.put( 6, "" + 6 ); actual += "\nHashmap after adding 4, 6\n" + map ; System.out.println("Actual output: " + actual ); System.out.println( "For this test to pass, each bin should have the same entries, but the order doesn't matter" ); //This code checks if each line has the same characters regardless of order boolean areEquivalent = true; String[] expectedLines = expected.split("\n"); String[] actualLines = actual.split("\n"); if ( expectedLines.length == actualLines.length ) { for ( int i = 0 ; i < expectedLines.length ; i ++ ) { //Split each line into characters, then sort String[] expectedLineChars = expectedLines[ i ].split(""); String[] actualLineChars = actualLines[ i ].split(""); Arrays.sort( expectedLineChars ); Arrays.sort( actualLineChars ); boolean charsEqual = Arrays.equals( expectedLineChars , actualLineChars ); if ( !charsEqual ){ areEquivalent = false ; } } } else { areEquivalent = false ; } System.out.println("For test 1: expected and actual output are equivalent: " + areEquivalent ); } /** * Testing get */ public static void test2(){ //Adds some values, gets them, and makes sure the key is correct System.out.println("-".repeat(30) + "\nTest2: "); MapSet map = new HashMap<>(4, 0.75); map.put( 5, "" + 1 ); map.put( 100, "" + 2 ); map.put( 30, "" + 3 ); map.put( 12, "" + 4 ); System.out.println("For test 2: the following values should be equal: " ); System.out.println( "Get 5 --- Key is " + map.get( 5 ) + ", should be 1" ); System.out.println( "Get 100 --- Key is " + map.get( 100 ) + ", should be 2" ); System.out.println( "Get 30 --- Key is " + map.get( 30 ) + ", should be 3" ); System.out.println( "Get 12 --- Key is " + map.get( 12 ) + ", should be 4" ); } /** * Testing that size, capacity entry set are correct after put and resize */ public static void test3(){ //TODO //Make a HashMap of capacity 3 and loadFactor 0.5 //Put some keys and values into the hashmap //Print out the map, size, capacity and entryset and validate that they are all correct } /** * Testing remove */ public static void test4(){ //TODO //Make a HashMap with default capacity and loadFactor //Put around 15 values values into the hashmap //Remove a few of them at a time and print the size after //Make sure it resizes when it's supposed to } /** * Testing remove at scale */ public static void test5(){ //TODO //Make a HashMap of capacity 4 and loadFactor 0.5 //Add a lot of key/value pairs (e.g. 1000). //Remove them one at a time //make sure the size stays between the bounds its supposed to //Make sure the associated values are correct } public static void main(String[] args){ test1(); test2(); //test3(); //test4(); //test5(); } }