package com.zj.c01;
public class MathTool {
public static int gcd(int num1, int num2) {
int r = 0;
while (num2 != 0) {
r = num1 % num2;
num1 = num2;
num2 = r;
}
return num1;
}
}
|
package com.zj.c01;
import junit.framework.TestCase;
public class MathToolTest extends TestCase {
public MathToolTest(String name) {
super(name);
}
public void testGcd() {
assertEquals(5, MathTool.gcd(10,
5));
}
}
|
package com.zj.c01;
public class NumberTool {
public static int getMax(int[] arr) {
int max = Integer.MIN_VALUE;
if (arr.length == 0)
throw new RuntimeException("Empty list");
for (int index = 0; index < arr.length; index++) {
if (arr[index] > max)
max = arr[index];
}
return max;
}
public static int getMin(int[] arr) {
int min = Integer.MAX_VALUE;
if (arr.length == 0)
throw new RuntimeException("Empty list");
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min)
min = arr[i];
}
return min;
}
}
|
package com.zj.c01;
import junit.framework.TestCase;
public class NumberToolTest extends TestCase {
public NumberToolTest(String name)
{
super(name);
}
public void testSimple() {
assertEquals(9, NumberTool.getMax(new int[] { 7, 8, 9 }));
}
public void testOrder() {
assertEquals(9, NumberTool.getMax(new int[] { 9, 8, 7 }));
assertEquals(9, NumberTool.getMax(new int[] { 7, 9, 8 }));
assertEquals(9, NumberTool.getMax(new int[] { 8, 7, 9 }));
}
public void testDups() {
assertEquals(9, NumberTool.getMax(new int[] { 9, 7, 9, 8
}));
}
public void testOne() {
assertEquals(1, NumberTool.getMax(new int[] { 1 }));
}
public void testNegitave() {
assertEquals(-7, NumberTool.getMax(new int[] { -7, -8, -9
}));
}
public void testEmpty() {
try {
NumberTool.getMax(new int[] {});
fail("Should have thrown an exception");
} catch (RuntimeException e) {
assertTrue(true);
}
}
}
|
package com.zj.c01;
import junit.framework.TestCase;
public class NumberToolTest2 extends TestCase {
private int[] arr;
public NumberToolTest2(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
arr = new int[] { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
};
}
protected void tearDown() throws Exception
{
super.tearDown();
arr = null;
}
public void testMax() {
assertEquals(5, NumberTool.getMax(arr));
}
public void testMin() {
assertEquals(-5, NumberTool.getMin(arr));
}
}
|
static public void run(Class testClass) {
run(new TestSuite(testClass));
}
|
package com.zj.c02;
import com.zj.c01.NumberToolTest;
import junit.framework.Test;
import junit.framework.TestSuite;
public class PartTest {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new NumberToolTest("testSimple"));
suite.addTest(new NumberToolTest("testNegitave"));
return suite;
}
}
|
package com.zj.c02;
import com.zj.c01.MathToolTest;
import com.zj.c01.NumberToolTest;
import junit.framework.Test;
import junit.framework.TestSuite;
public class CompositeTest {
public static Test suite() {
TestSuite suite = new TestSuite("Running all tests.");
suite.addTestSuite(MathToolTest.class);
suite.addTestSuite(NumberToolTest.class);
return suite;
}
}
|
package com.zj.c02;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
import com.zj.c01.MathToolTest;
import com.zj.c01.NumberToolTest;
public class WrapperCompositeTest {
public static Test suite() {
TestSuite suite = new TestSuite("Running all tests with env.");
suite.addTestSuite(MathToolTest.class);
suite.addTestSuite(NumberToolTest.class);
TestSetup wrapper = new TestSetup(suite)
{
protected void setUp() {
doSetUp();
}
protected void tearDown() {
doTearDown();
}
};
return wrapper;
}
public static void doSetUp() {
// initialization codes
}
public static void doTearDown() {
// release codes
}
}
|