⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashtabletestcase.java

📁 自己上学编的Hashtable的java源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -