initial setup

This commit is contained in:
Jaanus Poeial
2016-09-22 12:39:36 +03:00
commit f3009d068e
27 changed files with 3879 additions and 0 deletions

261
test/Aout.java Normal file
View File

@@ -0,0 +1,261 @@
/** Helper methods.
* Arrays converted to array expressions (toString(...))
* and prettyprinting (toPrettyString(...)).
* @author Jaanus
*/
public class Aout {
/** Just testing. */
public static void main (String[] args) {
System.out.println (toString (new String[]
{null, "", "0", "a\nb", " ", " ", "\t", "möäüÕga", "\"", "\\", "\'"}));
} // main
/** Conversion for int[][] . */
public static String toString (int[][] m) {
if (m == null) return "(int[][])null";
StringBuffer sb = new StringBuffer();
sb.append ("new int[][]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
if (m[i] == null)
sb.append ("null");
else {
sb.append ("{");
for (int j=0; j < m[i].length; j++) {
if (j > 0) sb.append (", ");
sb.append (String.valueOf (m[i][j]));
} // for j
sb.append ("}");
}
} // for i
sb.append ("}");
return sb.toString();
} // toString int[][]
/** Conversion for double[][] . */
public static String toString (double[][] m) {
if (m == null) return "(double[][])null";
StringBuffer sb = new StringBuffer();
sb.append ("new double[][]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
if (m[i] == null)
sb.append ("null");
else {
sb.append ("{");
for (int j=0; j < m[i].length; j++) {
if (j > 0) sb.append (", ");
sb.append (String.valueOf (m[i][j]));
} // for j
sb.append ("}");
}
} // for i
sb.append ("}");
return sb.toString();
} // toString double[][]
/** Conversion for int[] . */
public static String toString (int[] m) {
if (m == null) return "(int[])null";
StringBuffer sb = new StringBuffer();
sb.append ("new int[]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
sb.append (String.valueOf (m[i]));
} // for i
sb.append ("}");
return sb.toString();
} // toString int[]
/** Conversion for double[] . */
public static String toString (double[] m) {
if (m == null) return "(double[])null";
StringBuffer sb = new StringBuffer();
sb.append ("new double[]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
sb.append (String.valueOf (m[i]));
} // for i
sb.append ("}");
return sb.toString();
} // toString double[]
/** Conversion for int . */
public static String toString (int n) {
return String.valueOf (n);
} // toString int
/** Conversion for double . */
public static String toString (double d) {
return String.valueOf (d);
} // toString double
/** Conversion for String . */
public static String toString (String s) {
if (s == null)
return "null";
StringBuffer tmp = new StringBuffer();
for (int k=0; k < s.length(); k++) {
char c = s.charAt (k);
switch (c) {
case '\n': { tmp.append ("\\n"); break; }
case '\t': { tmp.append ("\\t"); break; }
case '\b': { tmp.append ("\\b"); break; }
case '\f': { tmp.append ("\\f"); break; }
case '\r': { tmp.append ("\\r"); break; }
case '\\': { tmp.append ("\\\\"); break; }
case '\'': { tmp.append ("\\\'"); break; }
case '\"': { tmp.append ("\\\""); break; }
// TODO!!! add more escapes if needed
default: tmp.append (c);
} // switch
} // for k
return "\"" + tmp.toString() + "\"";
} // toString String
/** Conversion for String[] . */
public static String toString (String[] m) {
if (m == null)
return "(String[])null";
StringBuffer sb = new StringBuffer();
sb.append ("new String[]{");
for (int i=0; i < m.length; i++) {
if (i > 0)
sb.append (", ");
sb.append (toString (m[i]));
} // for i
sb.append ("}");
return sb.toString();
} // toString String[]
/** Double number as string with the given length.
* @param d argument
* @param len length
* @return d as string
*/
public static String fString (double d, int len) {
if (len<1)
return "";
// pad on ruum punkti ja v6imaliku miinusm2rgi jaoks
int pad = 1 + ((d<0)?1:0);
// loga on t2isosa numbrikohtade arv
int loga = (int)Math.max (0., Math.log10 (Math.abs (d))) + 1;
// kk on punkti j2rel olevate kohtade arv
int kk = (int)Math.max (len-pad-loga, 0);
String fs = "%" + String.valueOf (len) + "." +
String.valueOf (kk) + "f";
String res = "";
try {
res = String.format ((java.util.Locale)null, fs, d);
} catch (IllegalArgumentException e) {
res = String.valueOf (d);
} // try
return res;
} // fString
/** Prettyprint for double[][] .
* @param m array to print
* @param fs format string for element
* @return m array as multiline string
*/
public static String toPrettyString (double[][] m, String fs) {
String nl = System.getProperty ("line.separator");
if (m == null)
return "nullpointer instead of this matrix" + nl;
// throw new NullPointerException ("(double[][])null"); // alternative
if (m.length == 0)
return "this matrix is empty" + nl;
StringBuffer sb = new StringBuffer(nl);
for (int i=0; i < m.length; i++) {
if (m[i] == null)
sb.append ("nullpointer instead of this row" + nl);
else {
if (m[i].length == 0)
sb.append ("this row is empty");
else {
for (int j=0; j < m[i].length; j++) {
String elem = "";
if (fs == null || fs.length() < 1) {
// TODO!!! keera siit, kui tahad pilti muuta
elem = fString (m[i][j], 6) + "\t";
} else {
try {
elem = String.format ((java.util.Locale)null,
fs, m[i][j]) + " "; // remove space if needed
} catch (IllegalArgumentException e) {
elem = fString (m[i][j], 6) + "\t";
} // try
}
sb.append (elem);
} // for j
} // nonempty row
sb.append (nl);
} // non-null row
} // for i
return sb.toString();
} // toPrettyString double[][]
/** Version of double[][] prettyprint without format string. */
public static String toPrettyString (double[][] m) {
return toPrettyString (m, null);
} // toPrettyString double[][]
/** Prettyprint for int[][] .
* @param m array to print
* @param fs format string for element
* @return m array as a multiline string
*/
public static String toPrettyString (int[][] m, String fs) {
String nl = System.getProperty ("line.separator");
if (m == null)
return "nullpointer instead of this matrix" + nl;
// throw new NullPointerException ("(double[][])null"); // alternative
if (m.length == 0)
return "this matrix is empty" + nl;
StringBuffer sb = new StringBuffer(nl);
for (int i=0; i < m.length; i++) {
if (m[i] == null)
sb.append ("nullpointer instead of this row" + nl);
else {
if (m[i].length == 0)
sb.append ("this row is empty");
else {
for (int j=0; j < m[i].length; j++) {
String elem = "";
if (fs == null || fs.length() < 1)
fs = "%5d"; // TODO!!! keera siit, kui vaja
try {
elem = String.format ((java.util.Locale)null,
fs, m[i][j]) + " "; // remove space if needed
} catch (IllegalArgumentException e) {
elem = String.valueOf (m[i][j]) + "\t";
} // try
sb.append (elem);
} // for j
} // nonempty row
sb.append (nl);
} // non-null row
} // for i
return sb.toString();
} // toPrettyString int[][]
/** Version of int[][] prettyprint without format string. */
public static String toPrettyString (int[][] m) {
return toPrettyString (m, null);
} // toPrettyString int[][]
/** Prettyprint for String[] . */
public static String toPrettyString (String[] m) {
String nl = System.getProperty ("line.separator");
if (m == null) return "(String[])null";
StringBuffer sb = new StringBuffer();
for (int i=0; i < m.length; i++) {
sb.append (toString (m[i]) + nl);
} // for i
return sb.toString();
} // toPrettyString String[]
} // Aout

335
test/LfractionTest.java Normal file
View File

@@ -0,0 +1,335 @@
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
/** Testklass.
* @author Jaanus
*/
public class LfractionTest {
@Test (timeout=1000)
public void testPlus() {
Lfraction f1 = new Lfraction (2, 5);
Lfraction f2 = new Lfraction (4, 15);
Lfraction sum = f1.plus(f2);
assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
new Lfraction (2, 3), sum);
Lfraction sm2 = sum.plus (sum);
assertEquals ("Wrong sum.plus(sum): <2/3> + <2/3>",
new Lfraction (4, 3), sm2);
assertEquals ("Do not change the arguments of the sum",
new Lfraction (2, 3), sum);
f1 = new Lfraction (-1, 250);
f2 = new Lfraction (-1, 375);
sum = f1.plus(f2);
assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
new Lfraction (-1, 150), sum);
f1 = new Lfraction (1, 221);
f2 = new Lfraction (1, 323);
sum = f1.plus (f2);
assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
new Lfraction (32, 4199), sum);
f1 = new Lfraction (1, 39203);
f2 = new Lfraction (1, 41989);
sum = f1.plus (f2);
assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
new Lfraction (408, 8271833), sum);
f1 = new Lfraction (-2, 5);
f2 = new Lfraction (2, 5);
sum = f1.plus(f2);
assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
new Lfraction (0, 1), sum);
}
@Test (timeout=1000)
public void testTimes() {
Lfraction f1 = new Lfraction (2, 5);
Lfraction f2 = new Lfraction (4, 15);
Lfraction prd = f1.times (f2);
assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
new Lfraction (8, 75), prd);
f1 = new Lfraction (-3, 5);
f2 = new Lfraction (-5, 7);
prd = f1.times (f2);
assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
new Lfraction (3, 7), prd);
Lfraction pr2 = prd.times (prd);
assertEquals ("Wrong prd.times(prd): <3/7> * <3/7>",
new Lfraction (9, 49), pr2);
assertEquals ("Do not change the arguments of the product",
new Lfraction (3, 7), prd);
f1 = new Lfraction (0, 1);
f2 = new Lfraction (2, 3);
prd = f1.times (f2);
assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
new Lfraction (0, 1), prd);
f1 = new Lfraction (3, 2);
f2 = new Lfraction (2, 3);
prd = f1.times (f2);
assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
new Lfraction (1, 1), prd);
f1 = new Lfraction (3, 5);
f2 = new Lfraction (5, 7);
prd = f1.times (f2);
assertTrue ("Result must be reduced: 3/5 * 5/7 -> 3/7",
prd.getDenominator()==7);
}
@Test (expected=RuntimeException.class)
public void testCreateZeroDenominator() {
Lfraction f = new Lfraction (1, 0);
}
@Test (expected=RuntimeException.class)
public void testZeroInverse() {
Lfraction f = new Lfraction (0, 1);
f.inverse();
}
@Test (timeout=1000)
public void testClone() {
Lfraction f1 = new Lfraction (2, 5);
Lfraction f2 = null;
try {
f2 = (Lfraction)f1.clone();
} catch (CloneNotSupportedException e) {};
assertNotSame ("clone must differ from original", f2, f1);
assertEquals ("clone must be equal to original", f1, f2);
f1 = f2.plus(f1);
assertEquals ("clone must be independent from original",
new Lfraction (2, 5), f2);
}
@Test (timeout=1000)
public void testToString() {
String s = new Lfraction (1, 4).toString();
assertTrue (s + " must represent quarter",
(s.indexOf('1') < s.indexOf('4')) && (s.indexOf('1') >= 0));
s = new Lfraction (-1, 5).toString();
assertTrue (s + " does not contain minus", s.indexOf('-') >= 0);
}
@Test (expected=RuntimeException.class)
public void testDivideByZero() {
Lfraction f1 = new Lfraction (1, 5);
Lfraction f2 = new Lfraction (0, 1);
Lfraction q = f1.divideBy (f2);
}
@Test (timeout=1000)
public void testMinus() {
Lfraction f1 = new Lfraction (2, 5);
Lfraction f2 = new Lfraction (4, 15);
Lfraction dif = f1.minus (f2);
assertEquals ("Wrong difference: <" + f1 + "> - <" + f2 + ">",
new Lfraction (2, 15), dif);
Lfraction df2 = dif.minus (dif);
assertEquals ("Wrong difference: <2/15> - <2/15>",
new Lfraction (0, 1), df2);
assertEquals ("Do not change the arguments of the difference",
new Lfraction (2, 15), dif);
f1 = new Lfraction (-2, 5);
f2 = new Lfraction (-4, 15);
dif = f1.minus (f2);
assertEquals ("Wrong difference: <" + f1 + "> - <" + f2 + ">",
new Lfraction (-2, 15), dif);
}
@Test (timeout=1000)
public void testDivideBy() {
Lfraction f1 = new Lfraction (-2, 7);
Lfraction f2 = new Lfraction (-1, 14);
Lfraction f = f1.divideBy (f2);
assertEquals ("Wrong quotient: <" + f1 + "> / <" + f2 + ">",
new Lfraction (4, 1), f);
f = f2.divideBy (f1);
assertEquals ("Wrong quotient: <" + f1 + "> / <" + f2 + ">",
new Lfraction (1, 4), f);
Lfraction f3 = f.divideBy (f);
assertEquals (f.toString() + " divided by itself",
new Lfraction (1, 1), f3);
assertEquals ("Do not change the arguments of the quotient",
new Lfraction (1, 4), f);
}
@Test (timeout=1000)
public void testOpposite() {
Lfraction f1 = new Lfraction (1, 6);
Lfraction f2 = f1.opposite();
assertEquals ("Wrong opposite", new Lfraction (-1, 6), f2);
assertEquals ("Do not change the argument of opposite",
new Lfraction (1, 6), f1);
f1 = new Lfraction (-4, 75);
f2 = f1.opposite();
assertEquals ("Wrong opposite", new Lfraction (4, 75), f2);
f1 = new Lfraction (0, 1);
f2 = f1.opposite();
assertEquals ("zero must be neutral to opposite", f1, f2);
}
@Test (timeout=1000)
public void testInverse() {
Lfraction f1 = new Lfraction (2, 3);
Lfraction f2 = f1.inverse();
assertEquals ("Wrong inverse ", new Lfraction (3, 2), f2);
assertEquals ("Do not change the argument of inverse",
new Lfraction (2, 3), f1);
f1 = new Lfraction (-4, 75);
f2 = f1.inverse();
assertEquals ("Wrong inverse", new Lfraction (-75, 4), f2);
assertTrue ("Denominator must always be positive",
f2.getDenominator() > 0);
f1 = new Lfraction (1, 1);
f2 = f1.inverse();
assertEquals ("1 must be neutral to inverse", f1, f2);
}
@Test (timeout=1000)
public void testGetters() {
Lfraction f1 = new Lfraction (2, 3);
long num = f1.getNumerator();
assertEquals ("wrong numerator ", 2, num);
f1 = new Lfraction (-4, 75);
num = f1.getNumerator();
assertEquals ("Wrong numerator", -4, num);
f1 = new Lfraction (0, 7);
num = f1.getNumerator();
assertEquals ("Wrong numerator", 0, num);
f1 = new Lfraction (2, 3);
long den = f1.getDenominator();
assertEquals ("wrong denominator ", 3, den);
f1 = new Lfraction (-4, 75);
den = f1.getDenominator();
assertEquals ("Wrong denominator", 75, den);
}
@Test (timeout=1000)
public void testIntegerPart() {
Lfraction f1 = new Lfraction (2, 3);
long i = f1.integerPart();
assertEquals ("wrong integer part ", 0, i);
f1 = new Lfraction (3, 2);
i = f1.integerPart();
assertEquals ("wrong integer part ", 1, i);
f1 = new Lfraction (32, 3);
i = f1.integerPart();
assertEquals ("wrong integer part ", 10, i);
f1 = new Lfraction (33, 3);
i = f1.integerPart();
assertEquals ("wrong integer part ", 11, i);
f1 = new Lfraction (-33, 3);
i = f1.integerPart();
assertEquals ("wrong integer part ", -11, i);
}
@Test (timeout=1000)
public void testLfractionPart() {
Lfraction f1 = new Lfraction (2, 3);
Lfraction i = f1.fractionPart();
assertEquals ("wrong fraction part ", new Lfraction (2, 3), i);
f1 = new Lfraction (3, 2);
i = f1.fractionPart();
assertEquals ("wrong fraction part ", new Lfraction (1, 2), i);
f1 = new Lfraction (32, 3);
i = f1.fractionPart();
assertEquals ("wrong fraction part ", new Lfraction (2, 3), i);
f1 = new Lfraction (33, 3);
i = f1.fractionPart();
assertEquals ("wrong fraction part ", new Lfraction (0, 1), i);
f1 = new Lfraction (-33, 3);
i = f1.fractionPart();
assertEquals ("wrong fraction part ", new Lfraction (0, 1), i);
f1 = new Lfraction (-5, 4);
i = f1.fractionPart();
assertTrue ("wrong fraction part " + i.toString()
+ " for " + f1.toString(),
i.equals (new Lfraction (-1, 4)) || i.equals (new Lfraction (3, 4)));
}
@Test (timeout=1000)
public void testEquals() {
Lfraction f1 = new Lfraction (2, 5);
Lfraction f2 = new Lfraction (4, 10);
assertTrue ("2/5 must be equal to 4/10", f1.equals (f2));
assertFalse ("2/5 is not 3/5", f1.equals (new Lfraction (3, 5)));
f1 = new Lfraction (12345678901234567L, 1L);
f2 = new Lfraction (12345678901234568L, 1L);
assertFalse ("12345678901234567/1 is not 12345678901234568/1",
f1.equals (f2));
}
@Test (timeout=1000)
public void testCompareTo() {
Lfraction f1 = new Lfraction (2, 5);
Lfraction f2 = new Lfraction (4, 7);
assertTrue ("2/5 must be less than 4/7", f1.compareTo (f2) < 0);
assertTrue ("2/5 must be equal to 4/10",
f1.compareTo (new Lfraction (4, 10)) == 0);
assertTrue ("4/7 must be greater than 2/5", f2.compareTo (f1) > 0);
f1 = new Lfraction (-2, 5);
f2 = new Lfraction (-4, 7);
assertTrue ("-2/5 must be greater than -4/7", f1.compareTo (f2) > 0);
assertTrue ("-2/5 must be equal to -4/10",
f1.compareTo (new Lfraction (-4, 10)) == 0);
assertTrue ("-4/7 must be less than -2/5", f2.compareTo (f1) < 0);
f1 = new Lfraction (12345678901234567L, 1L);
f2 = new Lfraction (12345678901234568L, 1L);
assertFalse ("12345678901234567/1 is not 12345678901234568/1",
f1.compareTo (f2) == 0);
}
@Test (timeout=1000)
public void testToLfraction() {
Lfraction f1 = Lfraction.toLfraction (Math.PI, 7);
Lfraction f2 = new Lfraction (22, 7);
assertTrue ("Math.PI must be nearly 22/7", f1.equals (f2));
f1 = Lfraction.toLfraction (-10., 2);
f2 = new Lfraction (-20, 2);
assertTrue ("-10. must be -20/2", f1.equals (f2));
}
@Test (timeout=1000)
public void testToDouble() {
Lfraction f = new Lfraction (2, 5);
assertEquals ("2/5 must be nearly 0.4", 0.4, f.toDouble(), 0.00001);
f = new Lfraction (-17, 100);
assertEquals ("-17/100 must be nearly -0.17", -0.17,
f.toDouble(), 0.00001);
}
@Test (timeout=1000)
public void testValueOf() {
Lfraction f = new Lfraction (2, 5);
assertEquals ("valueOf must read back what toString outputs. ",
f, Lfraction.valueOf (f.toString()));
f = new Lfraction (-17, 100);
assertEquals ("valueOf must read back what toString outputs. ",
f, Lfraction.valueOf (f.toString()));
}
@Test (timeout=1000)
public void testHashCode() {
Lfraction q1 = new Lfraction (1L, 2L);
int h1 = q1.hashCode();
Lfraction q2 = new Lfraction (1L, 2L);
int h2 = q2.hashCode();
Lfraction q3 = null;
try {
q3 = (Lfraction)q1.clone();
} catch (CloneNotSupportedException e) {};
int h3 = q3.hashCode();
assertTrue ("hashCode has to be same for equal objects", h1 == h2);
assertTrue ("hashCode has to be same for clone objects", h1 == h3);
assertTrue ("hashCode has to be same for the same object",
h1 == q1.hashCode());
q2 = new Lfraction (0L, 2L);
h2 = q2.hashCode();
q3 = new Lfraction (1L, 3L);
h3 = q3.hashCode();
assertFalse ("hashCode does not depend on numerator", h1 == h2);
assertFalse ("hashCode does not depend on denominator", h1 == h3);
}
}

BIN
test/hamcrest-core-1.3.jar Normal file

Binary file not shown.

BIN
test/junit-4.12.jar Normal file

Binary file not shown.