SplayTree高分测试用例

测试用例结果展示

覆盖率

 变异得分

SplayTree高分测试用例_第1张图片

测试注意点

  1. 从SplayTree测起,然后再测SubSplayTree,因为前者调用后者。
  2. SplaySubTree的remove方法大部分内容需要通过反射才能测到。
  3. value和index在SplayTree当中都不是唯一的。一个index可能对应多个value。

不足之处

  1. 没考虑到异常怎么接住。
  2. 对SplayTree这个数据结构的理解还很浅显。

测试文件MyTest.java

package net.mooctest;

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;

public class MyTest {
	
	private Integer valArr[];
	private Integer remArr[];
	private Integer conArr[];
	private SplayTree splayTree;
	private int howmanynumbers;
	private int removeCnt;
	private int containsCnt;
	
	@Before
	public void initializeValArr() {
		
		this.howmanynumbers = 100;
		this.removeCnt = 0;
		this.containsCnt = 0;
		
		valArr = new Integer[howmanynumbers];  
		remArr = new Integer[howmanynumbers/7+1];  
		conArr = new Integer[howmanynumbers/9+1];
		
		for(int i=0;i();
		for(int i=0;i();
		for(int i=0;i();
		for(int i=0;i();
		
		for(int i=0;i splaySubTree = new SplaySubTree(null);
		assertNull(splaySubTree.find(1));
		int primeArr[] = {
			2, 41, 5, 7, 67, 23, 11,  17, 19
		};
		for(int i=0;i splaySubTree = new SplaySubTree(0);
		
		int primeArr[] = {
			2, 41, 5, 7, 23, 11, 67, 17, 19
		};
		
		System.out.println(primeArr.length);
		
		for(int i=0;i 的 Class 对象  
        Class splaySubTreeClass = splaySubTree.getClass();  
  
        // 获取私有属性 count 的 Field 对象  
        Field countField = splaySubTreeClass.getDeclaredField("count");  
  
        // 设置私有属性 count 的可访问性  
        countField.setAccessible(true); 
		
		splaySubTree = splaySubTree.remove(7);
		// 访问私有属性 count 的值  
        int countValue = (int) countField.get(splaySubTree);  
//        System.out.println("countValue: " + countValue);
        assertEquals(0, countValue);
//		System.out.println(splaySubTree.toString());
		splaySubTree = splaySubTree.remove(7);
		countValue = (int) countField.get(splaySubTree);  
//        System.out.println("countValue: " + countValue); 
        assertEquals(-1, countValue);

	}
	
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test007() {
			SplaySubTree splaySubTree = new SplaySubTree(1);
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test008() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree splaySubTree = new SplaySubTree(1);
			// 获取 SplaySubTree 的 Class 对象  
	        Class splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field leftField = splaySubTreeClass.getDeclaredField("left");  
	        // 设置私有属性 count 的可访问性  
	        leftField.setAccessible(true);         
			SplaySubTree splaySubTree2 = new SplaySubTree(2);
			
			leftField.set(splaySubTree,splaySubTree2);
			
			
			
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test009() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree splaySubTree = new SplaySubTree(1);
			// 获取 SplaySubTree 的 Class 对象  
	        Class splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field rightField = splaySubTreeClass.getDeclaredField("right");  
	        // 设置私有属性 count 的可访问性  
	        rightField.setAccessible(true);         
			SplaySubTree splaySubTree2 = new SplaySubTree(2);
			
			rightField.set(splaySubTree,splaySubTree2);
		
			splaySubTree.remove(1);
		}
				
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test0010() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree splaySubTree = new SplaySubTree(1);
			// 获取 SplaySubTree 的 Class 对象  
	        Class splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field leftField = splaySubTreeClass.getDeclaredField("left");
	        Field parentField = splaySubTreeClass.getDeclaredField("parent");  
	        // 设置私有属性 count 的可访问性  
	        leftField.setAccessible(true);   
	        parentField.setAccessible(true);   
	        
			SplaySubTree splaySubTree2 = new SplaySubTree(2);
			leftField.set(splaySubTree,splaySubTree2);
			SplaySubTree splaySubTree3 = new SplaySubTree(3);
			parentField.set(splaySubTree,splaySubTree3);
		
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test0011() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree splaySubTree = new SplaySubTree(1);
			// 获取 SplaySubTree 的 Class 对象  
	        Class splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field rightField = splaySubTreeClass.getDeclaredField("right");  
	        Field parentField = splaySubTreeClass.getDeclaredField("parent"); 
	        // 设置私有属性 count 的可访问性  
	        rightField.setAccessible(true);   
	        parentField.setAccessible(true); 
	        
			SplaySubTree splaySubTree2 = new SplaySubTree(2);
			rightField.set(splaySubTree,splaySubTree2);
			SplaySubTree splaySubTree3 = new SplaySubTree(3);
			parentField.set(splaySubTree,splaySubTree3);
		
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test0012() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree splaySubTree = new SplaySubTree(1);
			// 获取 SplaySubTree 的 Class 对象  
	        Class splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field rightField = splaySubTreeClass.getDeclaredField("right");  
	        Field leftField = splaySubTreeClass.getDeclaredField("left"); 
	        // 设置私有属性 count 的可访问性  
	        rightField.setAccessible(true);   
	        leftField.setAccessible(true); 
	        
			SplaySubTree splaySubTree2 = new SplaySubTree(2);
			rightField.set(splaySubTree,splaySubTree2);
			SplaySubTree splaySubTree3 = new SplaySubTree(3);
			leftField.set(splaySubTree,splaySubTree3);

			String expectedStr = " data=1 left=3 right=2 sz=1 cnt=1\n" + 
					" data=3 left= null right null sz=1 cnt=1\n" + 
					" data=2 left= null right null sz=1 cnt=1\n";
			
			assertEquals(expectedStr, splaySubTree.toString());
		}

}

被测文件(1/2)SplayTree.java

package net.mooctest;

import java.util.Arrays;

public class SplayTree > {
	SplaySubTree root;
	
	public SplayTree(){
		root = new SplaySubTree(null);
	}
	
	/**
	 * @param index - of the node to search for.
	 * @return  - null if index<=0 or index>=size otherwise SubTree at index. 
	 */
	public T get(long index) {
		SplaySubTree cT = root.get(index);
		if(cT==null)return null;
		cT.splay();
		root = cT;
		return cT.getData();
	}
	
	/**
	 * @return - the number of nodes in the tree.
	 */
	public long size() { return root.size();}

	/**
	 * @param node - to search for.
	 * @return - the index of node. All nodes are ordered according to the compareTo(T) method.
	 *         
	 */
	public long indexOf(T node) {
		long index = root.indexOf(node);
		get(index);
		return index;
	}
	
	/**
	 * @param node - is added to the tree.
	 *             If node is null tree is unchanged.
	 */
	public void add(T node) {
		root = root.add(node);
	}
	
	/**
	 * @param node - is removed from the tree.
	 *             If node is null tree is unchanged.
	 */
	public void remove(T node) {
		root = root.remove(node);
	}
	
	/**
	 * @param node
	 * @return
	 */
	public boolean contains(T node) {
		SplaySubTree temp = root.find(node);
		if(temp!=null){
			temp.splay();
			root = temp;
		}
		return temp != null;
	}
	
	@Override
	public String toString(){
		return root.toString();
	}

	public static void main(String[] args) {
		SplayTree test = new SplayTree();
		int howmanynumbers = 10000;
		for (int i = 0; i < howmanynumbers; i++) {
			int val = (int)(Math.random()*100);
			test.add(val);
		}
		

		System.out.println(test);
	}
}

被测文件(2/2)SubSplayTree.java

package net.mooctest;

public class SplaySubTree> {

	private T data;
	private SplaySubTree left, right, parent;
	private long size; // number of nodes in tree
	private int count;

	/**
	 * @param node
	 *            - If node==null then size will be 0 otherwise node will be in the
	 *            tree and size will be 1
	 */
	public SplaySubTree(T node) {
		data = node;
		if (node != null) {
			size = count = 1;
		}
	}
	
	

	public String toString() {
		String lft = "";
		String rght = "";
		String myData = " data=" + data;
		if (left != null) {
			myData += " left=" + left.data;
			lft = left.toString();
		} else {
			myData += " left= null";
		}
		if (right != null) {
			myData += " right=" + right.data;
			rght = right.toString();
		} else {
			myData += " right null";
		}
		myData += " sz="+size + " cnt="+count;
		return myData + "\n" + lft + rght;
	}

	public T getData() {
		return data;
	}

	/**
	 * @param index
	 *            - of the node to search for.
	 * @return - null if index<=0 or index>=size otherwise SubTree at index.
	 */
	public SplaySubTree get(long index) {
		if (index > size || index < 0)
			return null;
		long cS = 1;
		SplaySubTree cT = this;
		if (cT.left != null)
			cS += cT.left.size;
		while (cS != index) {
			if (cS > index) {
				cS--;
				cT = cT.left;
				if (cT != null && cT.right != null)
					cS -= cT.right.size;
			} else {
				cS++;
				cT = cT.right;
				if (cT != null && cT.left != null)
					cS += cT.left.size;
			}
		}
		return cT;
	}

	/**
	 * @return - the number of nodes in the tree.
	 */
	public long size() {
		return size;
	}

	/**
	 * @param node
	 *            - to search for.
	 * @return - the index of node. All nodes are ordered according to the
	 *         compareTo(T) method.
	 * 
	 */
	public long indexOf(T node) {
		if (node == null)
			return -1;
		long cI = 1;
		SplaySubTree cT = this;
		if (cT.left != null)
			cI += cT.left.size;
		while (!cT.data.equals(node)) {
			if (cT.data.compareTo(node) > 0) {
				cI--;
				cT = cT.left;
				if (cT != null && cT.right != null)
					cI -= cT.right.size;
			} else {
				cI++;
				cT = cT.right;
				if (cT != null && cT.left != null)
					cI += cT.left.size;
			}
			if (cT == null)
				return -1;
		}

		return cI;
	}

	/**
	 * @param node
	 *            - is added to the tree. If node is null tree is unchanged.
	 * @return - New root of the tree.
	 */
	public SplaySubTree add(T node) {
		if (node == null)
			return this;
		if (this.data == null)
			return new SplaySubTree(node);
		SplaySubTree current = this;
		SplaySubTree child = null;

		if (this.data.compareTo(node) < 0)
			child = this.right;
		else if(this.data.compareTo(node)>0)
			child = this.left;
		while (child != null && current.data.compareTo(node)!=0) {
			current = child;
			if (current.data.compareTo(node) < 0)
				child = current.right;
			else if(current.data.compareTo(node)>0)
				child = current.left;
		}
		
		SplaySubTree newNode = new SplaySubTree(node);
		if (current.data.compareTo(node) < 0) {
			current.right = newNode;
		} else if(current.data.compareTo(node)>0){
			current.left = newNode;
		}else {
			current.size++;
			current.count++;
			newNode = current;
			current = newNode.parent;
		}
		newNode.parent = current;
		if (newNode.splay())
			return newNode;
		return this;
	}

	/**
	 * @param node
	 *            - is removed from the tree. If node is null tree is unchanged.
	 * @return - New root of the tree.
	 */
	public SplaySubTree remove(T node) {
		if (node == null)
			return this;
		SplaySubTree x = find(node);
		if (x == null)
			return this;
		if(x.data.equals(node)) {
			x.count--;
			x.size--;
			if(size>0) {
				x.splay();
				return x;
			}
		}
		// To delete a node x:
		// if x has no children remove it.

		if (x.left == null && x.right == null) {
			if (x.parent != null) {
				if (x.parent.left == x) {
					parent.left = null;
				} else
					parent.right = null;
			} else
				return new SplaySubTree(null);

		}
		// if x has one child remove x, and put the child in place of x
		if (x.left == null) {
			if (x.parent != null) {
				if (x.parent.left == x) {
					parent.left = x.right;
					x.right.parent = parent;
					x = x.right;
				} else {
					parent.right = x.right;
					x.right.parent = parent;
					x = x.right;
				}
			} else {
				x.right.parent = null;
				return x.right;
			}
		} else if (x.right == null) {
			if (x.parent != null) {
				if (x.parent.left == x) {
					parent.left = x.left;
					x.left.parent = parent;
					x = x.left;
				} else {
					parent.right = x.left;
					x.left.parent = parent;
					x = x.left;
				}
			} else {
				x.left.parent = null;
				return x.left;
			}
		} else {
			// if x has two children, swap its value with that of
			// the rightmost node of its left sub tree
			SplaySubTree rmc = x.left;
			while (rmc.right != null)
				rmc = rmc.right;
			x.data = rmc.data;
			// Then remove that node instead.
			rmc.left.parent = rmc.parent;
			if (rmc.parent == x) {
				x.left = rmc.left;
			} else {
				rmc.parent.right = rmc.left;
			}
			x = rmc;
		}

		// After deletion, splay the parent of the removed node to the top of
		// the tree.
		x.splay();
		return x;
	}

	/**
	 * @param other
	 * @return
	 */
	public SplaySubTree join(SplaySubTree other) {
		return null;

	}

	/**
	 * @param node
	 * @return
	 */
	public SplaySubTree split(T node) {
		return null;
	}

	/**
	 * @param node
	 * @return
	 */
	public SplaySubTree find(T node) {
		SplaySubTree current = this;
		if (this.data == null)
			return null;
		while (current != null) {
			if (node.equals(current.data))
				return current;
			if (node.compareTo(current.data) < 0)
				current = current.left;
			else
				current = current.right;
		}
		return current;
	}

	/**
	 * Assuming this node is an interior or leaf node of a larger tree this method
	 * moves this node up to the root balancing the tree in the process
	 */
	public boolean splay() {
		
		while (this.parent != null) {
			SplaySubTree p = this.parent;
			SplaySubTree g = p.parent;
			if (g == null && this == p.left) {
				zig();
			} else if (g == null && this == p.right) {
				zag();
			} else if (p.left == this && g.left == p) {
				zigzig();
			} else if (p.right == this && g.right == p) {
				zagzag();
			} else if (p.right == this && g.left == p) {
				zigzag();
			} else {
				zagzig();
			}
		}
		return true;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zig() {
		SplaySubTree b = this.right;
		SplaySubTree p = this.parent;

		this.right = p;
		p.parent = this;
		p.left = b;
		if (b != null)
			b.parent = p;
		this.parent = null;
		p.size = p.count;
		if (p.right != null)
			p.size += p.right.size;
		if (b != null)
			p.size += b.size;
		this.size = p.size + this.count;
		if (this.left != null)
			this.size += this.left.size;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zag() {
		SplaySubTree b = this.left;
		SplaySubTree p = this.parent;

		this.left = p;
		p.parent = this;
		p.right = b;
		if (b != null)
			b.parent = p;
		this.parent = null;
		p.size = p.count;
		if (b != null)
			p.size += b.size;
		if (p.left != null)
			p.size += p.left.size;
		this.size = p.size + this.count;
		if (this.right != null)
			this.size += this.right.size;
	}

	/**
	 * This is a helper method used by zigzig, zagzag, zigzag, zagzig This "fixes"
	 * the great grandparent
	 */
	private void fixGG(SplaySubTree g) {
		SplaySubTree gg = g.parent;
		if (gg != null) {
			if (g == gg.left)
				gg.left = this;
			if (g == gg.right)
				gg.right = this;
		}
		this.parent = gg;
		// might need to update size
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zigzig() {
		SplaySubTree g = parent.parent;
		SplaySubTree b = this.right;
		SplaySubTree p = this.parent;
		SplaySubTree c = p.right;
		fixGG(g);

		if (b != null)
			b.parent = p;
		p.left = b;
		if (c != null)
			c.parent = g;
		g.left = c;
		g.parent = p;
		p.right = g;
		p.parent = this;
		this.right = p;

		g.size = g.count;
		if (c != null)
			g.size += c.size;
		if (g.right != null)
			g.size += g.right.size;
		p.size = p.count;
		if (g != null)
			p.size += g.size;
		if (b != null)
			p.size += b.size;
		this.size = p.size + this.count;
		if (this.left != null)
			this.size += this.left.size;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zagzag() {
		SplaySubTree g = parent.parent;
		SplaySubTree b = this.left;
		SplaySubTree p = this.parent;
		SplaySubTree c = p.left;

		fixGG(g);
		if (b != null)
			b.parent = p;
		// above line throws java.lang.NullPointerException

		p.right = b;
		if (c != null)
			c.parent = g;
		g.right = c;
		g.parent = p;
		p.left = g;
		p.parent = this;
		this.left = p;
		g.size = g.count;
		if (g.left != null)
			g.size += g.left.size;
		if (c != null)
			g.size += c.size;
		p.size = g.size + p.count;
		if (b != null)
			p.size += b.size;
		this.size = p.size + this.count;
		if (this.right != null)
			this.size += this.right.size;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zigzag() {
		SplaySubTree g = parent.parent;
		SplaySubTree b = this.left;
		SplaySubTree p = this.parent;
		SplaySubTree c = this.right;

		fixGG(g);
		if (b != null)
			b.parent = p;
		p.right = b;
		if (c != null)
			c.parent = g;
		g.left = c;
		p.parent = this;
		this.left = p;
		g.parent = this;
		this.right = g;
		g.size = g.count;
		if (g.right != null)
			g.size += g.right.size;
		if (c != null)
			g.size += c.size;
		p.size = p.count;
		if (p.left != null)
			p.size += p.left.size;
		if (b != null)
			p.size += b.size;
		this.size = g.size + p.size + this.count;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zagzig() {

		SplaySubTree g = parent.parent;
		SplaySubTree b = this.right;
		SplaySubTree p = this.parent;
		SplaySubTree c = this.left;
		fixGG(g);
		if (b != null)
			b.parent = p;
		p.left = b;
		if (c != null)
			c.parent = g;
		g.right = c;
		p.parent = this;
		this.right = p;
		g.parent = this;
		this.left = g;
		g.size = g.count;
		if (g.left != null)
			g.size += g.left.size;
		if (c != null)
			g.size += c.size;
		p.size = p.count;
		if (p.right != null)
			p.size += p.right.size;
		if (b != null)
			p.size += b.size;
		this.size = g.size + p.size + this.count;
	}

}

你可能感兴趣的:(软件测试技术,测试用例)