Verification and Validation TESTING with Junit JUnit - LRI

int key; boolean color; // red=true, black=false. RBTree left;. RBTree right;. RBTree (int k) key=k; color=true; ... with the following OCL specification: package rbt.
221KB taille 5 téléchargements 213 vues
Verification and Validation Winter Trimester 2008 Prof. Burkhart Wolff Parc Orsay Universit´e 4, rue Jacques Monod Building H / Room 012

Prof. Christine Paulin Parc Orsay Universit´e 4, rue Jacques Monod Building N / Room 117

TESTING with Junit Date: 14.1.2009 The course website is www.lri.fr/~wolff/teach-material/2008-09/IFIPS-VnV/. Generally, exercises will be posted online Monday afternoon. They can be (but need not be) handed in at the start of the exercises sessions, or in any other way that the assistants allows for.

JUnit JUnit is a framework for doing unitary tests on Java programs. It is available from http: //www.junit.org/. Installing 1. The JUnit distribution files are available in directory /home/ensgnt/cpaulin/junit-4.5 referred to as $JUNIT HOME. 2. Add JUnit to the classpath: export CLASSPATH=$CLASSPATH:$JUNIT HOME/junit-4.5.jar:$JUNIT HOME/ 3. Test the installation by running the sample tests distributed with JUnit. java org.junit.runner.JUnitCore org.junit.tests.AllTests All the tests should pass with an ”OK” message. If not, verify your CLASSPATH. 4. The functions to be tested are available in the file /home/ensgnt/cpaulin/Tests.tgz to be copied in your working directory. Simple test To run JUnit, you have to create a java test file. 1. Create for instance a file TestRB.java in the same directory as your RBTree.java file with the following contents.

1

import org.junit.*; import static org.junit.Assert.*; public class TestRB private RBTree t1; // Will be executed before each test @Before public void setUp() t1 = new RBTree(5); @Test public void singlMem1() assertTrue(t1.isMember(5)); @Test public void singlMem2() assertEquals(false,t1.isMember(10));

2. Compile your file: javac TestRB.java 3. Run the tests by java org.junit.runner.JUnitCore TestRB Exercise 1 (Testing RedBlackTrees) The files exi/RBTree.class available in TP3.tgz implement a red-black tree data type. public class RBTree int key; boolean color; // red=true, black=false RBTree left; RBTree right; RBTree (int k) ...

key=k; color=true;

with the following OCL specification: package rbt context RBTree inv wff: left.oclIsDefined() and right.oclIsDefined() and key.oclIsDefined() and color.oclIsDefined() inv redinv: color implies (left = null or not left.color) and (right = null or not right.color) inv ordinv: ((left = null or left.max() < key) and (right = null or key < right.min())) inv balinv: (left.black_depth() = right.black_depth())

2

context RBTree::min():Integer pre: selfnull post: isMember(result) and Integer::allInstances()->forAll(m:Integer | self.isMember(m) implies resultforAll(m:Integer | self.isMember(m) implies mforAll(m:Integer | m = k or (self.isMember(m) = [email protected](m))) endpackage

1. Write test cases using JUnit in order to find which implementations contain bugs in insert or isMember code. • Use the test cases identified during the TD to generate a set of red-black trees on which the insert function will be tested. These definitions will be part of the @Before part of the JUnit file. • The RBTree.class file implements functions for testing the invariant: public boolean noRedRed() public boolean isOrd() public boolean isBalanced()

3

• The AllInstances construction in the post condition of the insert function cannot be directly executed. Find a way to solve this problem. Exercise 2 (Testing exceptional behavior) The code of red-black trees in ex12 contains an implementation of insertFail which is specified to raise a signal AlreadyThere whenever the element to be inserted is already in the tree. The OCL specification is the following: context RBTree::insertFail(k : Integer):OclVoid signal AlreadyThere pre: self null post: ([email protected](k) implies AlreadyThere.isSent()) and self.isMember(k) and Integer::allInstances()->forAll(m:Integer | m = k or (self.isMember(m) = [email protected](m))) Develop a test-case to check this behavior. In JUnit you have to prefix your test with @Test (expected=name_of_expected_exception.class) Then the test will pass if the body of the test raises the given exception.

4