using System;
namespace main
{
class test_main
{
public void test_int()
{
ut.assert(0, 0);
ut.SelfTestAssert("");
ut.assert(1, 0);
ut.SelfTestAssert(@"(12) : test_main.test_int: FAILED: actual: 1 expected: 0");
}
public void test_long()
{
ut.assert(0L, 0L);
ut.SelfTestAssert("");
ut.assert(1L, 0L);
ut.SelfTestAssert("(19) : test_main.test_long: FAILED: actual: 1 expected: 0");
}
public void test_string()
{
ut.assert("s1", "s1");
ut.SelfTestAssert("");
ut.assert("s1", "s2");
ut.SelfTestAssert("(27) : test_main.test_string: FAILED: actual: s1 expected: s2");
}
public void test_nullstrings()
{
ut.assert((string) null, (string) null);
ut.SelfTestAssert("");
ut.assert("", (string) null);
ut.SelfTestAssert("(34) : test_main.test_nullstrings: FAILED: actual: '' expected: <null>");
ut.assert((string) null, "");
ut.SelfTestAssert("(36) : test_main.test_nullstrings: FAILED: actual: <null> expected: ''");
ut.assert("", "");
ut.SelfTestAssert("");
}
public void test_bool()
{
ut.assert(true, true);
ut.SelfTestAssert("");
ut.assert(false, true);
ut.SelfTestAssert("(45) : test_main.test_bool: FAILED: actual: False expected: True");
ut.assert(true, false);
ut.SelfTestAssert("(47) : test_main.test_bool: FAILED: actual: True expected: False");
ut.assert(false, false);
ut.SelfTestAssert("");
}
class myexception : System.Exception
{
public myexception(string msg) : base(msg)
{
}
} ;
public void test_excp_test()
{
ut.assert(0, 0);
int x = 0;
x = x / x;
ut.assert(1, 1);
}
public void test_excp_test2()
{
ut.assert(0, 0);
throw new Exception("system.Exception");
ut.assert(1, 1);
}
public void test_excp_test3()
{
ut.assert(0, 0);
throw new myexception("myexception");
ut.assert(1, 1);
}
public void test_stringformatting()
{
ut.assert((16).ToString("d3"), "016");
ut.assert((6).ToString("d2"), "06");
}
[STAThread]
static void Main(string[] args)
{
//normal example
ut.Run();
// //print the testcase names
// //ut.Run(0, true);
//
// //print the testcase names and sort the test cases in alphabetical order
// //ut.Run(-1, true);
//
// //print the testcase names and randomize the order of the test cases
// //ut.Run(-2, true);
//
// //print the testcase names and randomize the order of the test cases using 500 as a seed
// //ut.Run(500, true);
// return 0;
}
}
}
|
using System;
using System.Reflection;
using System.Diagnostics;
public class ut
{
private static int mNumTestCases;
private static int mNumAsserts;
private static int mNumFailures;
private static int mNumExceptions;
private static string mCurrentCaseName;
private static string mCurrentOutputLine;
//main routine: determines and runs all methods prefixed with "test" in classes prefixed with "test"
public static void Run()
{
//initialize
mNumAsserts = 0;
mNumFailures = 0;
mNumTestCases = 0;
mNumExceptions = 0;
//get all classes in the current assembly
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
//does the class start with the test prefix?
if (!type.Name.StartsWith("test")) continue;
//it does. Create an instance of the class
object instance = Activator.CreateInstance(type);
//get all methods in the class
foreach (MethodInfo mi in type.GetMethods())
{
//check the signature: no parameters? void return type?
if (mi.GetParameters().Length != 0) continue;
if (!mi.ReturnType.Name.Equals("Void")) continue;
//found a valid method, invoke it
mNumTestCases++;
//keep track the current class.method name for reporting
mCurrentCaseName = type.Name + "." + mi.Name;
try
{
mi.Invoke(instance, null);
}
catch(Exception ex)
{
//catch and report any exceptions
mNumExceptions++;
PrintException(new StackFrame( 1, true ), ex.GetBaseException().Message);
}
}
}
PrintStatistics();
}
//these just put up a facade for converting to strings
public static void assert(int actual, int expected)
{
assertbase(new StackFrame( 1, true ), actual.ToString(), expected.ToString());
}
public static void assert(long actual, long expected)
{
assertbase(new StackFrame( 1, true ), actual.ToString(), expected.ToString());
}
public static void assert(bool actual, bool expected)
{
assertbase(new StackFrame( 1, true ), actual.ToString(), expected.ToString());
}
public static void assert(string actual, string expected)
{
assertbase(new StackFrame( 1, true ), actual, expected);
}
//this does the actual work
private static void assertbase(StackFrame sf, string actual, string expected)
{
mCurrentOutputLine = "";
mNumAsserts++;
if (actual == null && expected == null) return;
bool err = true;
if (actual == null && expected != null)
actual = "<null>";
else if (actual != null && expected == null)
expected = "<null>";
else if (actual.CompareTo(expected) == 0)
err = false;
if (err)
{
mNumFailures++;
PrintAssert(sf, actual, expected);
}
}
//used to test the tester
public static void SelfTestAssert(string expected)
{
if (mCurrentOutputLine.CompareTo(expected) == 0) return;
PrintLine("SELF TEST FAILED: ");
PrintLine(" actual : " + mCurrentOutputLine);
PrintLine(" expected: " + expected);
}
private static void PrintAssert(StackFrame sf, string actual, string expected)
{
if (actual.CompareTo("") == 0)
actual = "''";
if (expected.CompareTo("") == 0)
expected = "''";
PrintFailure(sf, "actual: " + actual + " expected: " + expected);
}
private static void PrintException(StackFrame sf, string msg)
{
PrintFailure(sf, "threw exception: " + msg);
}
private static void PrintFailure(StackFrame sf, string line)
{
mCurrentOutputLine = "(" + (sf.GetFileLineNumber() - 1) + ") : " + mCurrentCaseName + ": FAILED: " + line;
PrintLine(sf.GetFileName() + mCurrentOutputLine);
}
private static void PrintStatistics()
{
PrintLine("Num Test Cases: " + mNumTestCases);
PrintLine("Num Asserts : " + mNumAsserts);
PrintLine("Num Failures : " + mNumFailures);
PrintLine("Num Exceptions: " + mNumExceptions);
}
//Use this method and only this method to print a line
// it writes it to the console and to the Debug window. That way F8 works correctly in the MSVS IDE
private static void PrintLine(string line)
{
Console.Out.WriteLine(line);
Debug.WriteLine(line);
}
}
|