initial setup

This commit is contained in:
Jaanus Poeial
2016-09-16 11:24:20 +03:00
commit 5625a31752
8 changed files with 1240 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

227
test/LongStackTest.java Normal file
View File

@@ -0,0 +1,227 @@
import static org.junit.Assert.*;
import org.junit.Test;
/** Testklass.
* @author jaanus
*/
public class LongStackTest {
@Test (timeout=1000)
public void testNewStack() {
LongStack m = new LongStack();
assertTrue ("new stack must be empty;", m.stEmpty());
m.push (1);
m.pop();
assertTrue ("stack must be empty after one push and one pop; ", m.stEmpty());
}
@Test (timeout=1000)
public void testLIFO() {
LongStack m = new LongStack();
m.push (6);
m.push (-3);
long i1 = m.pop();
long i2 = m.pop();
assertTrue ("After two pushes and two pops stack must be empty;",
m.stEmpty());
assertTrue ("LIFO order must hold: 6 -3 returns -3 first;",
(i1 == -3) && (i2 == 6));
}
@Test (timeout=1000)
public void testOp() {
long tt = 0;
LongStack m = new LongStack();
m.push (5);
m.push (3);
m.op ("+");
tt = m.pop();
assertTrue ("5 + 3 must be 8; ", tt==8);
assertTrue ("push push op pop must not grow the stack; ", m.stEmpty());
m.push (2147483649L);
m.push (2147483648L);
m.op ("+");
tt = m.pop();
assertTrue ("2147483649 + 2147483648 must be 4294967297; ",
tt==4294967297L);
m.push (5);
m.push (3);
m.op ("-");
tt = m.pop();
assertTrue ("5 - 3 must be 2; ", tt==2);
assertTrue ("push push op pop must not grow the stack; ", m.stEmpty());
m.push (5);
m.push (3);
m.op ("*");
tt = m.pop();
assertTrue ("5 * 3 must be 15; ", tt==15);
assertTrue ("push push op pop must not grow the stack; ", m.stEmpty());
m.push (51);
m.push (3);
m.op ("/");
tt = m.pop();
assertTrue ("51 / 3 must be 17; ", tt==17);
assertTrue ("push push op pop must not grow the stack; ", m.stEmpty());
}
@Test (timeout=1000)
public void testTos() {
LongStack m = new LongStack();
m.push (2);
m.push (5);
long k = m.tos();
long k2 = m.pop();
assertEquals ("5 must be on top ", 5, k);
assertEquals ("tos must not change the top;", 5, k2);
long k3 = m.pop();
assertEquals ("tos must not change the stack;", 2, k3);
assertTrue ("tos must not pop;", m.stEmpty());
}
@Test (timeout=1000)
public void testEquals() {
LongStack m1 = new LongStack();
LongStack m2 = new LongStack();
assertTrue ("two empty stacks must be equal;", m1.equals(m2));
m1.push (1);
m2.push (1);
assertTrue ("1 in both stacks - stacks must be equal; ", m1.equals(m2));
m1.push (0);
assertFalse ("1 0 and just 1 must not be equal;", m1.equals(m2));
m2.push (3);
assertFalse ("1 0 and 1 3 must not be equal;", m1.equals(m2));
m1.pop();
m2.pop();
assertTrue ("1 in stacks with different history, stacks must be equal;",
m1.equals(m2));
m1.pop();
assertFalse ("first empty, second contains 1, must not be equal;",
m1.equals(m2));
}
@Test (expected=RuntimeException.class)
public void testPopEmpty() {
LongStack m = new LongStack();
m.pop();
}
@Test (expected=RuntimeException.class)
public void testOpUnderflow() {
LongStack m = new LongStack();
m.push (4);
m.op ("+");
}
@Test (timeout=1000)
public void testClone() {
LongStack m1 = new LongStack();
m1.push (5);
m1.push (4);
LongStack m2 = null;
try {
m2 = (LongStack)m1.clone();
} catch (CloneNotSupportedException e) {};
assertNotSame ("clone must differ from original;", m2, m1);
assertEquals ("clone must be equal to original;", m2, m1);
m1.pop();
m1.push (6);
assertFalse ("clone must be independent;", m1.equals(m2));
}
@Test (timeout=1000)
public void testToString() {
LongStack m = new LongStack();
assertNotNull ("empty stack must be ok;", m.toString());
m.push (-8);
m.push (7);
String s1 = m.toString().substring (0, 3);
m.push (2);
String s2 = m.toString().substring (0, 3);
assertEquals (
"top must be the last element; toString from bottom must start with -8 7 ",
s1, s2);
}
@Test (expected=RuntimeException.class)
public void testTosUnderflow() {
LongStack m = new LongStack();
m.tos();
}
@Test (timeout=1000)
public void testInterpret() {
String s = "1";
assertEquals ("expression: " + s, 1, LongStack.interpret (s));
s = "2 5 -";
assertEquals ("expression: " + s, -3, LongStack.interpret (s));
s = "35 10 -3 + /";
assertEquals ("expression: " + s, 5, LongStack.interpret (s));
}
@Test (expected=RuntimeException.class)
public void testInterpretStackbalance() {
String s = "35 10 -3 + / 2";
LongStack.interpret (s);
}
@Test (expected=RuntimeException.class)
public void testInterpretIllegalArg1() {
String s = "35 10 -3 + x 2";
LongStack.interpret (s);
}
@Test (expected=RuntimeException.class)
public void testInterpretIllegalArg2() {
String s = "35 y 10 -3 + - +";
LongStack.interpret (s);
}
@Test (expected=RuntimeException.class)
public void testInterpretUnderflow() {
String s = "35 10 + -";
LongStack.interpret (s);
}
@Test (expected=RuntimeException.class)
public void testInterpretNull() {
String s = null;
LongStack.interpret (s);
}
@Test (expected=RuntimeException.class)
public void testInterpretEmpty() {
String s = "";
LongStack.interpret (s);
}
@Test (expected=RuntimeException.class)
public void testInterpretOpfirst() {
String s = "- 3 2";
LongStack.interpret (s);
}
@Test (timeout=1000)
public void testInterpretLong() {
String s = "1 -10 4 8 3 - + * +";
assertEquals ("expression: " + Aout.toString (s), -89,
LongStack.interpret (s));
s = "156 154 152 - 3 + -";
assertEquals ("expression: " + Aout.toString (s), 151,
LongStack.interpret (s));
}
@Test (timeout=1000)
public void testInterpretTokenizer() {
String s = "1 2 +";
assertEquals ("expression: " + Aout.toString (s), 3,
LongStack.interpret (s));
s = " \t \t356 \t \t";
assertEquals ("expression: " + Aout.toString (s), 356,
LongStack.interpret (s));
s = "\t2 \t5 + \t";
assertEquals ("expression: " + Aout.toString (s), 7,
LongStack.interpret (s));
}
}

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.