# Tests.UnitTestSpline Class Reference

Unit tests for CubicSpline class. More...

List of all members.

## Public Member Functions
void | SplineExceptionOnNoPointsAdded ()
| Test exception when trying to interpolate with no points added.
void | SplineExceptionOnOnlyOnePointAdded ()
| Test exception when trying to interpolate with only one point added.
void | SplineExceptionInvalidDimension ()
| Test exception when trying to create spline with wrong dimension.
void | SplineExceptionAddingWithDimensionMismatch ()
| Test exception when trying to add points with the wrong dimension.
void | SplineExceptionOnOnlyTwoPointsAdded ()
| Test exception when trying to interpolate with only two points added.
void | SplineExceptionOnOnlyThreePointsAdded ()
| Test exception when trying to interpolate with only three points added.
void | SplineInterpolateFourPointLine ()
| Interpolate between 4 points forming a straight line.
void | SplineRejectDuplicatePointsWhenAdding ()
| Validate that adding duplicate x coordinates will reject the data.
void | SplineCanClearDataArrayThenAddAndInterpolate ()
| Validate that the data array can be cleared.
void | SplineInterpolateFourPointShape ()
| Interpolate between 4 points forming Z shape.
void | SplineInterpolateFourPointShape2D ()
| Interpolate between 4 points forming Z shape in two dimensions.
void | SplineInterpolatePastEdgeOfFourPointShape ()
| Interpolate left and right of the 4 points forming a Z shape.
void | SplineInterpolateSineWave ()
| Interpolate points along a sine wave approximated with 12 points.
## Properties
TestContext | TestContext`[get, set]`
| Gets or sets the test context which provides information about and functionality for the current test run. /summary>.

## Detailed Description

Unit tests for CubicSpline class.

Definition at line 12 of file UnitTestSpline.cs.

## Member Function Documentation

void Tests.UnitTestSpline.SplineCanClearDataArrayThenAddAndInterpolate | ( |  | ) |

Validate that the data array can be cleared.

Definition at line 192 of file UnitTestSpline.cs.

    00193         {
    00194             double[,] data = {{0.0, 0.0},
    00195                            {1.0, 1.0},
    00196                            {2.0, 2.0},
    00197                            {3.0, 3.0},
    00198                           };
    00199
    00200             CubicSpline spline = new CubicSpline();
    00201
    00202             // several tries
    00203             for (int t = 0; t < 3; t++)
    00204             {
    00205                 for (int i = 0; i < 4; i++)
    00206                 {
    00207                     bool addResult = spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00208                     Assert.IsTrue(addResult);
    00209                 }
    00210
    00211                 Assert.AreEqual(4, spline.Count);
    00212                 Assert.IsTrue(spline.CanInterpolate);
    00213
    00214                 // test interpolator
    00215                 Random random = new Random();
    00216                 for (int j = 0; j < 10; j++)
    00217                 {
    00218                     double x = random.NextDouble();
    00219                     double[] y = spline.Interpolate(x);
    00220                     Assert.AreEqual(x, y[0]);
    00221                 }
    00222
    00223                 // test clear
    00224                 spline.Clear();
    00225                 Assert.AreEqual(0, spline.Count);
    00226                 Assert.IsFalse(spline.CanInterpolate);
    00227             }
    00228         }

void Tests.UnitTestSpline.SplineExceptionAddingWithDimensionMismatch | ( |  | ) |

Test exception when trying to add points with the wrong dimension.

Definition at line 80 of file UnitTestSpline.cs.

    00081         {
    00082             CubicSpline spline = new CubicSpline(2);
    00083             Assert.AreEqual(2, spline.Dimensions);
    00084             spline.AddDataPoint(1.0, new double[] { 1.0 });
    00085         }

void Tests.UnitTestSpline.SplineExceptionInvalidDimension | ( |  | ) |

Test exception when trying to create spline with wrong dimension.

Definition at line 70 of file UnitTestSpline.cs.

    00071         {
    00072             CubicSpline spline = new CubicSpline(0);
    00073         }

void Tests.UnitTestSpline.SplineExceptionOnNoPointsAdded | ( |  | ) |

Test exception when trying to interpolate with no points added.

Definition at line 44 of file UnitTestSpline.cs.

    00045         {
    00046             CubicSpline spline = new CubicSpline();
    00047             Assert.IsFalse(spline.CanInterpolate);
    00048             double[] y = spline.Interpolate(1.5);
    00049         }

void Tests.UnitTestSpline.SplineExceptionOnOnlyOnePointAdded | ( |  | ) |

Test exception when trying to interpolate with only one point added.

Definition at line 56 of file UnitTestSpline.cs.

    00057         {
    00058             CubicSpline spline = new CubicSpline();
    00059             spline.AddDataPoint(1.0, new double[] { 1.0 });
    00060             Assert.AreEqual(1, spline.Count);
    00061             Assert.IsFalse(spline.CanInterpolate);
    00062             double[] y = spline.Interpolate(1.5);
    00063         }

void Tests.UnitTestSpline.SplineExceptionOnOnlyThreePointsAdded | ( |  | ) |

Test exception when trying to interpolate with only three points added.

Definition at line 113 of file UnitTestSpline.cs.

    00114         {
    00115             double[,] data = {{1.0, 1.0},
    00116                               {2.0, 2.0},
    00117                               {3.0, 1.0}};
    00118
    00119             CubicSpline spline = new CubicSpline();
    00120             for (int i = 0; i < 3; i++)
    00121             {
    00122                 spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00123             }
    00124
    00125             Assert.AreEqual(3, spline.Count);
    00126             Assert.IsFalse(spline.CanInterpolate);
    00127             double[] y = spline.Interpolate(1.5);
    00128         }

void Tests.UnitTestSpline.SplineExceptionOnOnlyTwoPointsAdded | ( |  | ) |

Test exception when trying to interpolate with only two points added.

Definition at line 92 of file UnitTestSpline.cs.

    00093         {
    00094             double[,] data = {{1.0, 1.0},
    00095                               {2.0, 2.0}};
    00096
    00097             CubicSpline spline = new CubicSpline();
    00098             for (int i = 0; i < 2; i++)
    00099             {
    00100                 spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00101             }
    00102
    00103             Assert.AreEqual(2, spline.Count);
    00104             Assert.IsFalse(spline.CanInterpolate);
    00105             double[] y = spline.Interpolate(1.5);
    00106         }

void Tests.UnitTestSpline.SplineInterpolateFourPointLine | ( |  | ) |

Interpolate between 4 points forming a straight line.

Definition at line 134 of file UnitTestSpline.cs.

    00135         {
    00136             double[,] data = {{0.0, 0.0},
    00137                               {1.0, 1.0},
    00138                               {2.0, 2.0},
    00139                               {3.0, 3.0}};
    00140
    00141             CubicSpline spline = new CubicSpline();
    00142             for (int i = 0; i < 4; i++)
    00143             {
    00144                 spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00145             }
    00146
    00147             Assert.AreEqual(4, spline.Count);
    00148             Assert.IsTrue(spline.CanInterpolate);
    00149
    00150             for (int i = 0; i <= 30; i++)
    00151             {
    00152                 double x = 0.1 * (double)i;
    00153                 double[] y = spline.Interpolate(x);
    00154                 Assert.AreEqual(x, y[0]);
    00155             }
    00156         }

void Tests.UnitTestSpline.SplineInterpolateFourPointShape | ( |  | ) |

Interpolate between 4 points forming Z shape.

Definition at line 234 of file UnitTestSpline.cs.

    00235         {
    00236             double[,] data = {{1.0, 1.0},
    00237                               {2.0, 2.0},
    00238                               {3.0, 1.0},
    00239                               {4.0, 3.0}};
    00240
    00241             CubicSpline spline = new CubicSpline();
    00242             for (int i = 0; i < 4; i++)
    00243             {
    00244                 spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00245             }
    00246
    00247             Assert.AreEqual(4, spline.Count);
    00248             Assert.IsTrue(spline.CanInterpolate);
    00249
    00250             double[] testinput = { 1.5, 2.5, 3.5, 3.9 };
    00251             double[] testoutput = { 1.6, 1.6, 2.0, 2.85 };
    00252
    00253             for (int i=0; i<testinput.Length; i++)
    00254             {
    00255                 double[] y = spline.Interpolate(testinput[i]);
    00256                 Assert.AreEqual(testoutput[i], y[0], 0.1);
    00257             }
    00258         }

void Tests.UnitTestSpline.SplineInterpolateFourPointShape2D | ( |  | ) |

Interpolate between 4 points forming Z shape in two dimensions.

Definition at line 264 of file UnitTestSpline.cs.

    00265         {
    00266             double[,] data = {{1.0, 1.0, 2.0},
    00267                               {2.0, 2.0, 4.0},
    00268                               {3.0, 1.0, 2.0},
    00269                               {4.0, 3.0, 6.0}};
    00270
    00271             CubicSpline spline = new CubicSpline(2);
    00272             Assert.AreEqual(2, spline.Dimensions);
    00273             for (int i = 0; i < 4; i++)
    00274             {
    00275                 spline.AddDataPoint(data[i, 0], new double[] { data[i, 1], data[i, 2] });
    00276             }
    00277
    00278             Assert.AreEqual(4, spline.Count);
    00279             Assert.IsTrue(spline.CanInterpolate);
    00280
    00281             double[] testinput = { 1.5, 2.5, 3.5, 3.9 };
    00282             double[] testoutput = { 1.6, 1.6, 2.0, 2.85 };
    00283
    00284             for (int i = 0; i < testinput.Length; i++)
    00285             {
    00286                 double[] y = spline.Interpolate(testinput[i]);
    00287                 Assert.AreEqual(testoutput[i], y[0], 0.1);
    00288                 Assert.AreEqual(2 * testoutput[i], y[1], 0.2);
    00289             }
    00290         }

void Tests.UnitTestSpline.SplineInterpolatePastEdgeOfFourPointShape | ( |  | ) |

Interpolate left and right of the 4 points forming a Z shape.

Definition at line 296 of file UnitTestSpline.cs.

    00297         {
    00298             double[,] data = {{1.0, 1.0},
    00299                               {2.0, 2.0},
    00300                               {3.0, 1.0},
    00301                               {4.0, 2.0}};
    00302
    00303             CubicSpline spline = new CubicSpline();
    00304             for (int i = 0; i < 4; i++)
    00305             {
    00306                 spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00307             }
    00308
    00309             Assert.AreEqual(4, spline.Count);
    00310             Assert.IsTrue(spline.CanInterpolate);
    00311
    00312             double[] testinput = { 0.8, 0.9, 4.1, 4.2 };
    00313             double[] testoutput = { 0.8, 0.9, 2.1, 2.2 };
    00314
    00315             for (int i = 0; i < testinput.Length; i++)
    00316             {
    00317                 double[] y = spline.Interpolate(testinput[i]);
    00318                 Assert.AreEqual(testoutput[i], y[0], 0.1);
    00319             }
    00320         }

void Tests.UnitTestSpline.SplineInterpolateSineWave | ( |  | ) |

Interpolate points along a sine wave approximated with 12 points.

Definition at line 326 of file UnitTestSpline.cs.

    00327         {
    00328
    00329             int N = 12;
    00330             double[] x = new double[N];
    00331             double[] y = new double[N];
    00332             double xx = Math.PI;
    00333             double step = 4 * Math.PI / (N - 1);
    00334
    00335             for (int i = 0; i < N; ++i, xx += step)
    00336             {
    00337                 double yy = Math.Sin(2 * xx) / xx;
    00338                 x[i] = xx;
    00339                 y[i] = yy;
    00340             }
    00341
    00342             CubicSpline spline = new CubicSpline();
    00343             for (int i = 0; i < N; i++)
    00344             {
    00345                 spline.AddDataPoint(x[i], new double[] { y[i] });
    00346             }
    00347
    00348             Assert.AreEqual(N, spline.Count);
    00349             Assert.IsTrue(spline.CanInterpolate);
    00350
    00351             N = 30;
    00352             xx = Math.PI;
    00353             step = 3 * Math.PI / (N - 1);
    00354             for (int i = 0; i < N; ++i, xx += step)
    00355             {
    00356                 double yExpected = Math.Sin(2 * xx) / xx;
    00357                 double[] yCalculated = spline.Interpolate(xx);
    00358                 Assert.AreEqual(yExpected, yCalculated[0], 0.1);
    00359             }
    00360         }

void Tests.UnitTestSpline.SplineRejectDuplicatePointsWhenAdding | ( |  | ) |

Validate that adding duplicate x coordinates will reject the data.

Definition at line 162 of file UnitTestSpline.cs.

    00163         {
    00164             double[,] data = {{0.0, 0.0},
    00165                            {1.0, 1.0},
    00166                            {2.0, 2.0},
    00167                            {3.0, 3.0},
    00168                           };
    00169
    00170             CubicSpline spline = new CubicSpline();
    00171             for (int i = 0; i < 4; i++)
    00172             {
    00173                 bool addResult = spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00174                 Assert.IsTrue(addResult);
    00175             }
    00176
    00177             // try to add again
    00178             for (int i = 0; i < 4; i++)
    00179             {
    00180                 bool addResult = spline.AddDataPoint(data[i, 0], new double[] { data[i, 1] });
    00181                 Assert.IsFalse(addResult);
    00182             }
    00183
    00184             Assert.AreEqual(4, spline.Count);
    00185             Assert.IsTrue(spline.CanInterpolate);
    00186         }

The documentation for this class was generated from the following file:

- C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/Tests/UnitTestSpline.cs