00001 using System;
00002 using System.Diagnostics;
00003 using System.Reflection;
00004
00005 namespace FFT.Test {
00006 using FFT;
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 public abstract class UnitTest {
00021
00022
00023
00024
00025 public abstract void Test();
00026
00027
00028
00029
00030
00031
00032
00033 public override string ToString()
00034 {
00035 return this.GetType().Name.Replace("Test",null);
00036 }
00037
00038
00039
00040
00041 protected static int FailedCount;
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 public static void AssertEq<T>(T result, T goal, string msg) where T : IEquatable<T> {
00055 Console.Write( " " + msg );
00056 if ( result.Equals(goal)) {
00057 Console.Write("...OK\n");
00058 } else {
00059 FailedCount++;
00060 Console.Write("...FAILED!\n");
00061 Console.Write(" Expected: {0}\n",goal);
00062 Console.Write(" Have: {0}\n",result);
00063 }
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 public static void AssertEq<T>(T[] result, T[] goal, string msg)
00073 where T : IEquatable<T>
00074 {
00075 Console.Write(" " + msg);
00076
00077 if (result.Length != goal.Length) {
00078 FailedCount++;
00079 Console.Write("...FAILED!\n");
00080 Console.Write(" Expected length: {0}\n",goal.Length);
00081 Console.Write(" Have length: {0}\n",result.Length);
00082 return ;
00083 }
00084
00085 bool equal = true;
00086 int i = 0;
00087 while (equal && i < result.Length) {
00088 equal = result[i].Equals(goal[i]);
00089 i++;
00090 }
00091
00092 if (equal) {
00093 Console.Write("...OK\n");
00094 } else {
00095 FailedCount++;
00096 Console.Write("...FAILED!\n");
00097 Console.Write(" Position: {0}\n",--i);
00098 Console.Write(" Expected: {0}\n",goal[i]);
00099 Console.Write(" Have: {0}\n",result[i]);
00100 }
00101 }
00102
00103
00104
00105
00106 public static double Precision = 1E-10;
00107
00108
00109
00110
00111
00112
00113
00114 public static void AssertNearEq(Complex[] result, Complex[] goal, string msg)
00115 {
00116 Console.Write(" " + msg);
00117
00118 if (result.Length != goal.Length) {
00119 FailedCount++;
00120 Console.Write("...FAILED!\n");
00121 Console.Write(" Expected length: {0}\n",goal.Length);
00122 Console.Write(" Have length: {0}\n",result.Length);
00123
00124
00125 return;
00126 }
00127
00128 bool equal = true;
00129 int i = 0;
00130 while (equal && i < result.Length) {
00131 equal = (result[i]-goal[i]).Modulus < Precision;
00132 i++;
00133 }
00134
00135 if (equal) {
00136 Console.Write("...OK\n");
00137 } else {
00138 FailedCount++;
00139 Console.Write("...FAILED!\n");
00140 Console.Write(" Position: {0}\n",--i);
00141 Console.Write(" Expected: {0}\n",goal[i]);
00142 Console.Write(" Have: {0}\n",result[i]);
00143
00144
00145 }
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 public static void RunTests()
00155 {
00156 Assembly assembly = Assembly.GetCallingAssembly();
00157
00158 FailedCount = 0;
00159 foreach (Type t in assembly.GetTypes()) {
00160 if (t.IsSubclassOf(Type.GetType("FFT.Test.UnitTest"))) {
00161 ConstructorInfo c = t.GetConstructor(new Type[0]);
00162 UnitTest unit = (UnitTest) c.Invoke(new object[0]);
00163
00164 Console.Write("-----------------------------------------------\n" +
00165 " Test suite: {0}\n" +
00166 "-----------------------------------------------\n",
00167 unit.ToString());
00168 unit.Test();
00169 }
00170 }
00171
00172 if (FailedCount != 0) {
00173 Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"+
00174 " TOTAL FAILED COUNT: {0} \n" +
00175 "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",
00176 FailedCount);
00177 }
00178 }
00179
00180
00181 }
00182 }