hashtabletestcase.java

来自「自己上学编的Hashtable的java源码」· Java 代码 · 共 527 行 · 第 1/2 页

JAVA
527
字号
		testEntry1.setKey("ABCDELDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "division", "quadratic_probing");
		assertNotNull(table.insert(testEntry1));
		assertNotNull(table.delete(testEntry1.getKey()));
		assertNull(table.find(testEntry1.getKey()));	
	}
	
	@Test
	public void testDelete_FoldingQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("ABCDELDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "folding", "quadratic_probing");
		assertNotNull(table.insert(testEntry1));
		assertNotNull(table.delete(testEntry1.getKey()));
		assertNull(table.find(testEntry1.getKey()));
	}
	
	@Test
	public void testDelete_MidSquareQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("ABCDELDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "mid_square", "quadratic_probing");
		assertNotNull(table.insert(testEntry1));
		assertNotNull(table.delete(testEntry1.getKey()));
		assertNull(table.find(testEntry1.getKey()));		
	}
	
	@Test
	public void testHomeAdress_DivisionLinear() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "division", "linear_probing");
		table.insert(testEntry1);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry1.getKey());
		assertTrue(testEntry1.getKey()+" is not at home adress.", adresses == null);
	}
	
	@Test
	public void testHomeAdress_FoldingLinear() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "folding", "linear_probing");
		table.insert(testEntry1);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry1.getKey());
		assertTrue(testEntry1.getKey()+" is not at home adress.", adresses == null);
	}
	
	@Test
	public void testHomeAdress_MidSquareLinear() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "mid_square", "linear_probing");
		table.insert(testEntry1);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry1.getKey());
		assertTrue(testEntry1.getKey()+" is not at home adress.", adresses == null);
	}
	
	@Test
	public void testHomeAdress_DivisionQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "division", "quadratic_probing");
		table.insert(testEntry1);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry1.getKey());
		assertTrue(testEntry1.getKey()+" is not at home adress.", adresses == null);
	}
	
	@Test
	public void testHomeAdress_FoldingQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "folding", "quadratic_probing");
		table.insert(testEntry1);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry1.getKey());
		assertTrue(testEntry1.getKey()+" is not at home adress.", adresses == null);
	}
	
	@Test
	public void testHomeAdress_MidSquareQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		HashTable table = new HashTable(10, "mid_square", "quadratic_probing");
		table.insert(testEntry1);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry1.getKey());
		assertTrue(testEntry1.getKey()+" is not at home adress.", adresses == null);
	}
	
	@Test
	public void testCollisionAdress_DivisionLinear() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		Entry testEntry2 = new Entry();
		testEntry2.setKey("X8IG4LDXS");
		testEntry2.setData("OK");
		
		HashTable table = new HashTable(10, "division", "linear_probing");
		table.insert(testEntry1);
		table.insert(testEntry2);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry2.getKey());
		assertTrue(testEntry2.getKey() + " is at home adress.", adresses != null);
		assertTrue(testEntry2.getKey() + " should have home adress 2.", adresses.length == 1 && adresses[0] == 2);
	}
	
	@Test
	public void testCollisionAdress_FoldingLinear() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		Entry testEntry2 = new Entry();
		testEntry2.setKey("Z7IG5LDXS");
		testEntry2.setData("OK");
		
		HashTable table = new HashTable(10, "folding", "linear_probing");
		table.insert(testEntry1);
		table.insert(testEntry2);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry2.getKey());
		assertTrue(testEntry2.getKey() + " is at home adress.", adresses != null);
		assertTrue(testEntry2.getKey() + " should have home adress 5.", adresses.length == 1 && adresses[0] == 5);
	}
	
	@Test
	public void testCollisionAdress_MidSquareLinear() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		Entry testEntry2 = new Entry();
		testEntry2.setKey("F5HF8MECA");
		testEntry2.setData("OK");
		
		HashTable table = new HashTable(10, "mid_square", "linear_probing");
		table.insert(testEntry1);
		table.insert(testEntry2);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry2.getKey());
		assertTrue(testEntry2.getKey() + " is at home adress.", adresses != null);
		assertTrue(testEntry2.getKey() + " should have home adress 0.", adresses.length == 1 && adresses[0] == 0);
	}
	
	@Test
	public void testCollisionAdress_DivisionQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		Entry testEntry2 = new Entry();
		testEntry2.setKey("X8IG4LDXS");
		testEntry2.setData("OK");
		
		HashTable table = new HashTable(10, "division", "quadratic_probing");
		table.insert(testEntry1);
		table.insert(testEntry2);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry2.getKey());
		assertTrue(testEntry2.getKey() + " is at home adress.", adresses != null);
		assertTrue(testEntry2.getKey() + " should have home adress 2.", adresses.length == 1 && adresses[0] == 2);
	}
	
	@Test
	public void testCollisionAdress_FoldingQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		Entry testEntry2 = new Entry();
		testEntry2.setKey("Z7IG5LDXS");
		testEntry2.setData("OK");
		
		HashTable table = new HashTable(10, "folding", "quadratic_probing");
		table.insert(testEntry1);
		table.insert(testEntry2);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry2.getKey());
		assertTrue(testEntry2.getKey() + " is at home adress.", adresses != null);
		assertTrue(testEntry2.getKey() + " should have home adress 5.", adresses.length == 1 && adresses[0] == 5);
	}
	
	@Test
	public void testCollisionAdress_MidSquareQuadratic() {
		Entry testEntry1 = new Entry();
		testEntry1.setKey("Z8IG4LDXS");
		testEntry1.setData("OK");
		
		Entry testEntry2 = new Entry();
		testEntry2.setKey("F5HF8MECA");
		testEntry2.setData("OK");
		
		HashTable table = new HashTable(10, "mid_square", "quadratic_probing");
		table.insert(testEntry1);
		table.insert(testEntry2);
		Vector<String> tableVector = table.getHashTable();
		int[] adresses = getAdresses(tableVector, testEntry2.getKey());
		assertTrue(testEntry2.getKey() + " is at home adress.", adresses != null);
		assertTrue(testEntry2.getKey() + " should have home adress 0.", adresses.length == 1 && adresses[0] == 0);
	}
	
	private int[] getAdresses(Vector<String> table, String key) {
		
		for (String line : table) {
			if(line.matches(".*"+key+".*")) {
				String[] lastPart = line.split("\\x7C");
				if(lastPart.length == 3) {
					String allNumbersAndEnd = lastPart[line.split("\\x7C").length - 1].substring(0, lastPart[line.split("\\x7C").length - 1].indexOf("}"));
					String[] allNumbers = allNumbersAndEnd.split(",");
					int[] numbers = new int[allNumbers.length];
					for (int i = 0; i < allNumbers.length; i++) {
						numbers[i] = Integer.valueOf(allNumbers[i].trim());
					}
					return numbers;
				}
			}
		}
		
		return null;
	}
	
	private void printVector(Vector<String> dot) {
		try {
			FileWriter fw = new FileWriter("test.txt");			
			BufferedWriter bw = new BufferedWriter(fw);
			
			for (String string : dot) {
				bw.write(string + System.getProperty("line.separator"));
			}
			
			bw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?