Commit fe8c404d authored by Dr. Martin Goik's avatar Dr. Martin Goik

Junit 3 --> Junit 4, supplementary tests

parent 45bf96a9
......@@ -62,7 +62,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -9,6 +9,6 @@ public class Driver {
* @param args unused
*/
public static void main(String[] args) {
System.out.println("GCD(12, 18) = " + Math.getGcd(12, 18));
System.out.println("GCD(49, -21) = " + Math.getGcd(49, -21));
}
}
......@@ -18,19 +18,31 @@ public class Math {
* @return The GCD of a and b
*/
public static long getGcd(long a, long b) {
// Following http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html
if (a < b) { // Swap the values of a and b
long tmp = a;
a = b;
b = tmp;
}
while (0 != b) {
long r = a % b;
a = b;
b = r;
if (a == 0 && b == 0) {
return 1;
} else {
a = java.lang.Math.abs(a); // More efficient than if (a < 0) {a = -a;}
b = java.lang.Math.abs(b);
if (a == 0) { // b != 0, see condition before
return b;
} else if (b == 0) {
return a;
} else {
// Now we actually implement Euclid's algorithm by following
// http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html
if (a < b) { // Swap the values of a and b if required
final long tmp = a; // Save a's value temporarily
a = b;
b = tmp;
}
while (0 != b) {
long r = a % b;
a = b;
b = r;
}
return a;
}
}
return a;
}
}
package de.hdm_stuttgart.mi.sd1.fraction;
import de.hdm_stuttgart.mi.sd1.gcd.Math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for Gcd.
*/
public class FractionTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing GCD computation");
testsuite.addTestSuite(FractionTest.class);
return testsuite; }
/**
*
*/
public void testApp() {
assertEquals(Math.getGcd(18, 12), 6);
assertEquals(Math.getGcd(56, 21), 7);
}
}
package de.hdm_stuttgart.mi.sd1.fraction;
import static org.junit.Assert.*;
import org.junit.Test;
import de.hdm_stuttgart.mi.sd1.gcd.Math;
public class GcdTest {
@Test
public void testTwoPositiveIntegers() {
assertEquals(6, Math.getGcd(18, 12));
}
@Test
public void testReverseOrder() {
assertEquals(6, Math.getGcd(12, 18));
}
@Test
public void testBothZero() {
assertEquals(1, Math.getGcd(0, 0));
}
@Test
public void testNonzeroZero() {
assertEquals(1, Math.getGcd(0, 0));
}
@Test
public void testZeroNonzero() {
assertEquals(7, Math.getGcd(0, 7));
}
@Test
public void testNegativePositive() {
assertEquals(9, Math.getGcd(-36, 27));
}
@Test
public void testPositiveNegative() {
assertEquals(7, Math.getGcd(49, -21));
}
}
......@@ -65,7 +65,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -65,7 +65,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -62,7 +62,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.mi.sd1.fraction;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
/**
* Unit test for simple App.
*/
public class FractionTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing Fractions (sum + product)");
testsuite.addTestSuite(FractionTest.class);
return testsuite; }
import org.junit.Test;
public class FractionTest {
/**
* Sums and products of fractions
*/
public void testApp() {
@Test
public void testAdditionMultiply() {
// Input
final Fraction
twoThird = new Fraction(2, 3), // Construct a fraction object (2/3)
......
......@@ -68,7 +68,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.mi.sd1.fraction;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
/**
* Unit test for simple App.
*/
public class FractionTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing Fractions (sum + product)");
testsuite.addTestSuite(FractionTest.class);
return testsuite; }
import org.junit.Test;
public class FractionTest {
/**
* Sums and products of fractions
*/
public void testApp() {
@Test
public void testAddMultiplyReduce() {
// Just constructor
final Fraction twoThirds = new Fraction(4, 6);
assertEquals(2, twoThirds.getNumerator());
assertEquals(3, twoThirds.getDenominator());
// Input
final Fraction
twoThird = new Fraction(2, 3), // Construct a fraction object (2/3)
threeSeven = new Fraction(3, 7); // Construct a fraction object (3/7)
twoThird = new Fraction(5, 6), // Construct a fraction object (2/3)
threeSeven = new Fraction(2, 9); // Construct a fraction object (3/7)
// Computed results
final Fraction
sum = twoThird.add(threeSeven), // (2/3) + (3/7)
sum = twoThird.add(threeSeven), // (5/6) + (2/9)
product = twoThird.mult(threeSeven); // (2/3) * (3/7)
assertTrue(sum.getNumerator() == 23 && sum.getDenominator() == 21);
assertTrue(product.getNumerator() == 2 && product.getDenominator() == 7);
assertTrue(sum.getNumerator() == 19 && sum.getDenominator() == 18);
assertTrue(product.getNumerator() == 5 && product.getDenominator() == 27);
final Fraction sum2 = new Fraction(5, 18).add(new Fraction(7, 12));
assertTrue(sum2.getNumerator() == 31 && sum2.getDenominator() == 36);
final Fraction product2 = new Fraction(5, 18).mult(new Fraction(12, 7));
assertTrue(product2.getNumerator() == 10 && product2.getDenominator() == 21);
}
}
......@@ -63,7 +63,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.mi.sd1.interest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AccountTest extends TestCase {
public class AccountTest {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing accounts");
testsuite.addTestSuite(AccountTest.class);
return testsuite;
}
/**
* Testing account balance and interest calculatios
* Testing account balance and interest calculations
*/
@Test
public void testApp() {
Account account = new Account(20.); // Create a new instance of class Account
......
......@@ -63,7 +63,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.mi.sd1.interest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AccountTest extends TestCase {
public class AccountTest {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing accounts");
testsuite.addTestSuite(AccountTest.class);
return testsuite;
}
/**
* Testing account balance and interest calculatios
* Testing account balance and interest calculations
*/
public void testApp() {
@Test
public void testBalanceInterest() {
Account account = new Account(20.); // Create a new instance of class Account
......
......@@ -50,7 +50,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.de.sd1.loop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
* Empty!
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
public class AppTest {
@Test
public void testApp() {
assertTrue(true);
}
}
......@@ -49,7 +49,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.de.sd1.loop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
public class AppTest {
/**
* Empty
*/
@Test
public void testApp() {
assertTrue(true);
}
}
......@@ -62,7 +62,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.de.sd1.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Testing exponential implementation
*
*/
public class ExpTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing exponential");
testsuite.addTestSuite(ExpTest.class);
return testsuite; }
/**
* Sums and products of fractions
*/
public void testApp() {
}
}
package de.hdm_stuttgart.de.sd1.math;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Testing exponential implementation
*
*/
public class MaxAbsTest{
/**
* Absolute value.
*/
@Test
public void testAbsoluteValue() {
assertTrue(nearlyEqualCheck(Math.abs(4.1), 4.1, 1E-14));
assertTrue(nearlyEqualCheck(Math.abs(-3.1), 3.1, 1E-14));
assertTrue(nearlyEqualCheck(Math.abs(0.), 0., 1E-14));
}
/**
* Max value.
*/
@Test
public void testMaxValue() {
assertTrue(nearlyEqualCheck(Math.max(4.1, 5.2), 5.2, 1E-14));
assertTrue(nearlyEqualCheck(Math.max(-4.1, 3.2), 3.2, 1E-14));
assertTrue(nearlyEqualCheck(Math.max(3.2, -4.1), 3.2, 1E-14));
assertTrue(nearlyEqualCheck(Math.max(-5.7, -4.1), -4.1, 1E-14));
assertTrue(nearlyEqualCheck(Math.max(-4.1, -5.7), -4.1, 1E-14));
}
/**
* Test, whether a given value value is within the
* interval [reference - epsilon, reference + epsilon]
*
* This is helpful to assure two values are identical within
* a given tolerance limit being impose by e.g. limited
* computational precision.
*
* @param value The value to be tested
* @param reference The center of the interval
* @param epsilon Twice the inverval's length
* @return
*/
static boolean nearlyEqualCheck(double value, double reference, double epsilon) {
return reference - epsilon < value && value < reference + epsilon;
}
}
......@@ -62,7 +62,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -15,7 +15,7 @@ public class Math {
/**
*
* @param seriesLimit The last term's index of a power series to be included,
* {@link }}.
*
*/
public static void setSeriesLimit(int seriesLimit) {
Math.seriesLimit = seriesLimit;
......
package de.hdm_stuttgart.de.sd1.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Testing exponential implementation
*
*/
public class ExpTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing exponential");
testsuite.addTestSuite(ExpTest.class);
return testsuite; }
public class ExpTest {
/**
* Sums and products of fractions
*/
@Test
public void testApp() {
Math.setSeriesLimit(8);
assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy!
assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-5); // Mind limited computational accuracy!
}
}
......@@ -62,7 +62,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......
package de.hdm_stuttgart.de.sd1.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Testing exponential implementation
*
*/
public class ExpTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing exponential");
testsuite.addTestSuite(ExpTest.class);
return testsuite; }
public class ExpTest {
/**
* Sums and products of fractions
*/
@Test
public void testApp() {
Math.setSeriesLimit(8);
assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy!
......
......@@ -62,7 +62,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
......