using System;
using System.Collections;
using System.Diagnostics;
namespace ut
{
public class utx
{
private static Asserter mAsserter = null;
private static Reporter mReporter = null;
//Internal use only!
internal static void Set(Reporter r, Asserter a)
{
mReporter = r;
mAsserter = a;
}
//used only for testing utcs!
public static void assertNextAssertFails()
{
mReporter.SetNextAssertFails();
}
//bools
public static void assert(bool b)
{
SetLocation();
mAsserter.assert(null, b, true);
}
public static void assertnot(bool b)
{
SetLocation();
mAsserter.assertnot(null, b, true);
}
public static void assert(string msg, bool b)
{
SetLocation();
mAsserter.assert(msg, b, true);
}
public static void assertnot(string msg, bool b)
{
SetLocation();
mAsserter.assertnot(msg, b, true);
}
//int
public static void assert(string msg, int actual, int expected)
{
SetLocation();
mAsserter.assert(msg, actual.ToString(), expected.ToString());
}
public static void assert(int actual, int expected)
{
SetLocation();
mAsserter.assert(null, actual.ToString(), expected.ToString());
}
public static void assertnot(string msg, int actual, int expected)
{
SetLocation();
mAsserter.assertnot(msg, actual.ToString(), expected.ToString());
}
public static void assertnot(int actual, int expected)
{
SetLocation();
mAsserter.assertnot(null, actual.ToString(), expected.ToString());
}
//char
public static void assert(string msg, char actual, char expected)
{
SetLocation();
mAsserter.assert(msg, actual.ToString(), expected.ToString());
}
public static void assert(char actual, char expected)
{
SetLocation();
mAsserter.assert(null, actual.ToString(), expected.ToString());
}
public static void assertnot(string msg, char actual, char expected)
{
SetLocation();
mAsserter.assertnot(msg, actual.ToString(), expected.ToString());
}
public static void assertnot(char actual, char expected)
{
SetLocation();
mAsserter.assertnot(null, actual.ToString(), expected.ToString());
}
//byte
public static void assert(string msg, byte actual, byte expected)
{
SetLocation();
mAsserter.assert(msg, actual.ToString(), expected.ToString());
}
public static void assert(byte actual, byte expected)
{
SetLocation();
mAsserter.assert(null, actual.ToString(), expected.ToString());
}
public static void assertnot(string msg, byte actual, byte expected)
{
SetLocation();
mAsserter.assertnot(msg, actual.ToString(), expected.ToString());
}
public static void assertnot(byte actual, byte expected)
{
SetLocation();
mAsserter.assertnot(null, actual.ToString(), expected.ToString());
}
//double
public static void assert(string msg, double actual, double expected, double epsilon)
{
SetLocation();
mAsserter.assert(msg, actual, expected, epsilon);
}
public static void assert(double actual, double expected, double epsilon)
{
SetLocation();
mAsserter.assert(null, actual, expected, epsilon);
}
public static void assertnot(string msg, double actual, double expected, double epsilon)
{
SetLocation();
mAsserter.assertnot(msg, actual, expected, epsilon);
}
public static void assertnot(double actual, double expected, double epsilon)
{
SetLocation();
mAsserter.assertnot(null, actual, expected, epsilon);
}
//string
public static void assert(string actual, string expected)
{
SetLocation();
mAsserter.assert(null, actual, expected);
}
public static void assert(string msg, string actual, string expected)
{
SetLocation();
mAsserter.assert(msg, (object) actual, (object) expected);
}
public static void assertnot(string actual, string expected)
{
SetLocation();
mAsserter.assertnot(null, actual, expected);
}
public static void assertnot(string msg, string actual, string expected)
{
SetLocation();
mAsserter.assertnot(msg, (object) actual, (object) expected);
}
//ICollection
public static void assert(ICollection actual, ICollection expected)
{
SetLocation();
mAsserter.assert(null, actual, expected);
}
public static void assert(string msg, ICollection actual, ICollection expected)
{
SetLocation();
mAsserter.assert(msg, actual, expected);
}
public static void assertnot(ICollection actual, ICollection expected)
{
SetLocation();
mAsserter.assertnot(null, actual, expected);
}
public static void assertnot(string msg, ICollection actual, ICollection expected)
{
SetLocation();
mAsserter.assertnot(msg, actual, expected);
}
//objects
public static void assert(object actual, object expected)
{
SetLocation();
mAsserter.assert(null, actual, expected);
}
public static void assert(string msg, object actual, object expected)
{
SetLocation();
mAsserter.assert(msg, actual, expected);
}
public static void assertnot(object actual, object expected)
{
SetLocation();
mAsserter.assertnot(null, actual, expected);
}
public static void assertnot(string msg, object actual, object expected)
{
SetLocation();
mAsserter.assertnot(null, actual, expected);
}
private static void SetLocation()
{
StackTrace st = new StackTrace(true);
StackFrame mSFrame = st.GetFrame(2);
mReporter.SetLocation(mSFrame.GetFileName(), mSFrame.GetFileLineNumber() - 1, 1);
}
}
}
|
using System;
using System.Collections;
namespace ut
{
internal class Asserter
{
private Reporter mReporter;
public Asserter(Reporter rep)
{
mReporter = rep;
}
//object
public void assert(string msg, object actual, object expected)
{
mReporter.IncAsserts();
if (ConditionIsTrue(actual, expected)) return;
mReporter.ReportFailedAssert(msg, actual, expected);
}
public void assertnot(string msg, object actual, object expected)
{
mReporter.IncAsserts();
if (ConditionIsFalse(actual, expected)) return;
mReporter.ReportFailedAssertNot(msg, actual, expected);
}
//double
public void assert(string msg, double actual, double expected, double epsilon)
{
mReporter.IncAsserts();
//if (NullConditionsHandled(actual, expected)) return true;
//todo: check for NAN
if (ConditionEquals(DoublesEqual(actual, expected, epsilon), actual, expected))
return;
mReporter.ReportFailedAssert(msg, actual.ToString("0.0##"), expected.ToString("0.0##"));
}
public void assertnot(string msg, double actual, double expected, double epsilon)
{
mReporter.IncAsserts();
//if (NotNullConditionsHandled(actual, expected)) return true;
//todo: check for NAN
if (ConditionEquals(!DoublesEqual(actual, expected, epsilon), actual, expected))
return;
mReporter.ReportFailedAssertNot(msg, actual.ToString("0.0##"), expected.ToString("0.0##"));
}
private bool DoublesEqual(double actual, double expected, double epsilon)
{
return actual - epsilon <= expected && expected <= actual + epsilon;
}
//ICollection
public void assert(string msg, ICollection actual, ICollection expected)
{
if (NullConditionsHandled(actual, expected)) return;
if (CountConditionsHandled(actual, expected)) return;
IEnumerator enumActual = actual.GetEnumerator();
IEnumerator enumExp = expected.GetEnumerator();
while(enumActual.MoveNext() && enumExp.MoveNext())
assert(mReporter.message(msg, "items do not match"), enumActual.Current, enumExp.Current);
}
public void assertnot(string msg, ICollection actual, ICollection expected)
{
if (NotNullConditionsHandled(actual, expected)) return;
if (NotCountConditionsHandled(actual, expected)) return;
IEnumerator enumActual = actual.GetEnumerator();
IEnumerator enumExp = expected.GetEnumerator();
int mismatches = 0;
while(enumActual.MoveNext() && enumExp.MoveNext())
{
if (!enumActual.Current.Equals(enumExp.Current)) mismatches++;
}
assertnot(mReporter.message(msg, "all items match"), mismatches, 0);
}
//private
private bool ConditionIsTrue(object actual, object expected)
{
if (NullConditionsHandled(actual, expected)) return true;
return ConditionEquals(expected.Equals(actual), actual, expected);
}
private bool ConditionIsFalse(object actual, object expected)
{
if (NotNullConditionsHandled(actual, expected)) return true;
return ConditionEquals(!expected.Equals(actual), actual, expected);
}
private bool ConditionEquals(bool condition, object actual, object expected)
{
if (!condition) return false;
mReporter.CheckMissingAssertFailure();
return true;
}
private bool NotNullConditionsHandled(object actual, object expected)
{
if (actual == null && expected == null)
{
mReporter.ReportBothObjectsAreNull();
return true;
}
if (actual == null ^ expected == null)
{
mReporter.CheckMissingAssertFailure();
return true;
}
return false;
}
private bool NullConditionsHandled(object actual, object expected)
{
if (actual == null && expected == null)
{
mReporter.CheckMissingAssertFailure();
return true;
}
if (actual == null ^ expected == null)
{
mReporter.ReportSingleNullObject(actual, expected);
return true;
}
return false;
}
private bool CountConditionsHandled(ICollection actual, ICollection expected)
{
if (actual.Count != expected.Count)
{
mReporter.ReportCountsDontMatch();
return true;
}
return false;
}
private bool NotCountConditionsHandled(ICollection actual, ICollection expected)
{
if (actual.Count != expected.Count)
{
mReporter.CheckMissingAssertFailure();
return true;
}
if (actual.Count == 0 && expected.Count == 0)
{
mReporter.ReportBothObjectsAreEmpty();
return true;
}
return false;
}
}
}
|
using System;
using System.IO;
using System.Collections;
using ut;
public class testutcs
{
public void testcsharpequalequal()
{
//verify the '==' works the way I think it does
object oA = "s";
object oB = "s";
utx.assert(oA == oB);
object o1 = "s";
object o2 = "s ".Trim();
utx.assert(o1 != o2);
utx.assert("objs as strings", (string) o1, (string) o2);
object obj1 = new object();
object obj2 = new object();
utx.assert(obj1 != obj2);
utx.assert(!obj1.Equals(obj2)); //compare is equivalent to 'obj1 != obj2'
utx.assert(obj1.ToString(), obj2.ToString()); //bogus! compares the types!
object t1 = new ArrayList();
object t2 = new object();
utx.assert(t1 != t2);
utx.assert(!t1.Equals(t2)); //compare is equivalent to 'obj1 != obj2'
//bogus! just compares the types!
utx.assert(t1.ToString(), t1.ToString());
}
public void test_sample_failure_message()
{
// utx.assert(1, 0);
// utx.assert("message here", 1, 0);
}
//bools
public void testliterals_bool()
{
utx.assert("bool", true);
utx.assert(true);
utx.assertNextAssertFails();
utx.assert(false);
utx.assertnot("!bool", false);
utx.assertnot(false);
utx.assertNextAssertFails();
utx.assertnot(true);
}
public void testvariables_bool()
{
bool b = true;
utx.assert("bool", b);
utx.assert(b);
utx.assertNextAssertFails();
utx.assert(!b);
utx.assertnot("bool", !b);
utx.assertnot(!b);
utx.assertNextAssertFails();
utx.assertnot(b);
}
//int
public void testliterals_int()
{
utx.assert("ints", 1, 1);
utx.assert(1, 1);
utx.assertNextAssertFails();
utx.assert(1, 2);
utx.assertnot("ints", 1, 2);
utx.assertnot(1, 2);
utx.assertNextAssertFails();
utx.assertnot(1, 1);
}
public void testvariables_int()
{
int i1 = 1;
int i2 = 1;
int i3 = 1;
int i4 = 2;
utx.assert((object) i1 != (object)i2);
utx.assert("ints", i1, i2);
utx.assert(i1, i2);
utx.assertNextAssertFails();
utx.assert(i3, i4);
utx.assert((object) i3 != (object)i4);
utx.assertnot("!ints", i3, i4);
utx.assertnot(i3, i4);
utx.assertNextAssertFails();
utx.assertnot(i1, i2);
}
//chars
public void testliterals_chars()
{
utx.assert("chars", 'a', 'a');
utx.assert('a', 'a');
utx.assertNextAssertFails();
utx.assert('a', 'b');
utx.assertnot("!chars", 'a', 'b');
utx.assertnot('a', 'b');
utx.assertNextAssertFails();
utx.assertnot('a', 'a');
}
public void testvariables_char()
{
char c1 = 'a';
char c2 = 'a';
char c3 = 'a';
char c4 = 'b';
utx.assert((object) c1 != (object)c2);
utx.assert("chars", c1, c2);
utx.assert(c1, c2);
utx.assertNextAssertFails();
utx.assert(c3, c4);
utx.assert((object) c3 != (object)c4);
utx.assertnot("!chars", c3, c4);
utx.assertnot(c3, c4);
utx.assertNextAssertFails();
utx.assertnot(c1, c2);
}
//bytes
public void testliterals_bytes()
{
utx.assert("bytes", (byte) 0x01, (byte) 0x01);
utx.assert((byte) 0x01, (byte) 0x01);
utx.assertNextAssertFails();
utx.assert((byte) 0x01, (byte) 0x02);
utx.assertnot("bytes", (byte) 0x01, (byte) 0x02);
utx.assertnot((byte) 0x01, (byte) 0x02);
utx.assertNextAssertFails();
utx.assertnot((byte) 0x01, (byte) 0x01);
}
public void testvariables_byte()
{
byte b1 = 0x01;
byte b2 = 0x01;
byte b3 = 0x01;
byte b4 = 0x02;
utx.assert((object) b1 != (object)b2);
utx.assert("bytes", b1, b2);
utx.assert(b1, b2);
utx.assertNextAssertFails();
utx.assert(b3, b4);
utx.assert((object) b3 != (object)b4);
utx.assertnot("!bytes", b3, b4);
utx.assertnot(b3, b4);
utx.assertNextAssertFails();
utx.assertnot(b1, b2);
}
//double
public void testliterals_doubles()
{
utx.assert("doubles", 1.0, 1.0, 0.0);
utx.assert(1.0, 1.0, 0.01);
utx.assertNextAssertFails();
utx.assert(1.0, 1.1, 0.01);
utx.assertnot("doubles", 1.0, 2.0, 0.1);
utx.assertnot(1.0, 2.0, 0.1);
utx.assertNextAssertFails();
utx.assertnot(1.0, 1.01, 0.01);
}
public void testliterals_doubles_tolerance()
{
utx.assert(1.0, 1.0001, 0.1);
utx.assert(1.0, 1.0001, 0.001);
utx.assert(1.0, 1.0001, 0.0001);
utx.assertNextAssertFails();
utx.assert(1.0, 1.0001, 0.00001);
}
public void testvariables_double()
{
double d1 = 1.0;
double d2 = 1.0;
double d3 = 1.0;
double d4 = 2.0;
utx.assert((object) d1 != (object)d2);
utx.assert("doubles", d1, d2);
utx.assert(d1, d2);
utx.assertNextAssertFails();
utx.assert(d3, d4);
utx.assert((object) d3 != (object)d4);
utx.assertnot("!doubles", d3, d4);
utx.assertnot(d3, d4);
utx.assertNextAssertFails();
utx.assertnot(d1, d2);
}
//strings
public void testliterals_strings()
{
utx.assert("strings", "s1", "s1");
utx.assert("s1", "s1");
utx.assertNextAssertFails();
utx.assert("s1", "s2");
utx.assertnot("!strings", "s1", "s2");
utx.assertnot("s1", "s2");
utx.assertNextAssertFails();
utx.assertnot("s1", "s1");
}
public void testvariables_string()
{
string s1 = "s";
string s2 = "s ".Trim(); //creates a new object
string s3 = "s";
string s4 = "x ".Trim(); //creates a new object
utx.assert((object) s1 != (object)s2);
utx.assert("strings", s1, s2);
utx.assert(s1, s2);
utx.assertNextAssertFails();
utx.assert(s3, s4);
utx.assert((object) s3 != (object)s4);
utx.assertnot("strings", s3, s4);
utx.assertnot(s3, s4);
utx.assertNextAssertFails();
utx.assertnot(s1, s2);
}
//objects
public void test_objects()
{
object s1 = "s";
object s2 = s1; //creates a new object
object s3 = "s";
object s4 = "x"; //creates a new object
utx.assert("objects", s1, s2);
utx.assert(s1, s2);
utx.assertNextAssertFails();
utx.assert(s3, s4);
utx.assertnot("objects", s3, s4);
utx.assertnot(s3, s4);
utx.assertNextAssertFails();
utx.assertnot(s1, s2);
}
public void test_object_nulls()
{
object x = null;
utx.assert(x, null);
utx.assert(null, x);
utx.assertNextAssertFails();
utx.assertnot(null, x);
utx.assertNextAssertFails();
utx.assertnot(x, null);
}
//lists
public void test_list()
{
ArrayList l1 = new ArrayList(new object[] {"a", "b"});
ArrayList l2 = new ArrayList(new object[] {"a", "b"});
utx.assert("list", l1, l2);
utx.assert(l1, l1);
utx.assert(l1, l2);
utx.assertNextAssertFails();
utx.assertnot(l1, l1);
utx.assertNextAssertFails();
utx.assertnot("list", l1, l1);
utx.assertNextAssertFails();
utx.assertnot(l1, l2);
utx.assertNextAssertFails();
utx.assertnot("list", l1, l2);
}
public void test_list_empty()
{
utx.assert("list2", new ArrayList(), new ArrayList());
utx.assert(new ArrayList(), new ArrayList());
utx.assertNextAssertFails();
utx.assertnot("list2", new ArrayList(), new ArrayList());
utx.assertNextAssertFails();
utx.assertnot(new ArrayList(), new ArrayList());
}
public void test_list_null()
{
utx.assert("list", (ICollection) null, (ICollection) null);
utx.assert((ICollection) null, (ICollection) null);
utx.assertNextAssertFails();
utx.assert("list", (ICollection) null, new ArrayList());
utx.assertNextAssertFails();
utx.assert((ICollection) null, new ArrayList());
utx.assertnot("list2", (ICollection) null, new ArrayList());
utx.assertnot((ICollection) null, new ArrayList());
utx.assertNextAssertFails();
utx.assertnot((ICollection) null, (ICollection) null);
utx.assertNextAssertFails();
utx.assertnot("list", (ICollection) null, (ICollection) null);
}
public void test_list_lengths()
{
ArrayList l1 = new ArrayList(new object[] {"a", "b", "c"});
ArrayList l2 = new ArrayList(new object[] {"a", "b"});
utx.assertnot("list", l1, l2);
utx.assertnot(l1, l2);
utx.assertNextAssertFails();
utx.assert(l1, l2);
utx.assertNextAssertFails();
utx.assert("list", l1, l2);
}
// public void test_excp()
// {
// int x = 0;
// x = 6 / x;
// }
public void test_junk()
{
ArrayList list1 = new ArrayList(new object[] {"a", "<missing>", "c"});
ArrayList list2 = new ArrayList(new object[] {"a", "b", "c"});
utx.assertnot(list1, list2);
}
}
|