- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/Tests/UnitTestScanf.cs

    00001 ﻿namespace Tests
    00002 {
    00003     using System;
    00004     using Microsoft.VisualStudio.TestTools.UnitTesting;
    00005     using NewGamePhysics.Utilities;
    00006
    00010     [TestClass]
    00011     public class UnitTestScanf
    00012     {
    00013         private TestContext testContextInstance;
    00014
    00015         public UnitTestScanf()
    00016         {
    00017         }
    00018
    00023         public TestContext TestContext
    00024         {
    00025             get
    00026             {
    00027                 return testContextInstance;
    00028             }
    00029             set
    00030             {
    00031                 testContextInstance = value;
    00032             }
    00033         }
    00034
    00035         [TestMethod]
    00036         public void ScanfNullResult()
    00037         {
    00038             object[] result;
    00039             result = ScanWrapper("1", "");
    00040             Assert.IsTrue(result == null || result.Length ==  0);
    00041             result = ScanWrapper("1", " ");
    00042             Assert.IsTrue(result == null || result.Length == 0);
    00043             result = ScanWrapper("1", "i");
    00044             Assert.IsTrue(result == null || result.Length == 0);
    00045             result = ScanWrapper("1", "%");
    00046             Assert.IsTrue(result == null || result.Length == 0);
    00047             result = ScanWrapper("1", "%%");
    00048             Assert.IsTrue(result == null || result.Length == 0);
    00049         }
    00050
    00051         [TestMethod]
    00052         public void ScanfSkip()
    00053         {
    00054             object[] result;
    00055             result = ScanWrapper("1 2", "%*i %i");
    00056             Assert.IsNotNull(result);
    00057             Assert.AreEqual(2, result.Length);
    00058             Assert.AreEqual(null, result[0]);
    00059             Assert.AreEqual(2, (int)result[1]);
    00060             result = ScanWrapper("1 2", "%i %*i");
    00061             Assert.IsNotNull(result);
    00062             Assert.AreEqual(2, result.Length);
    00063             Assert.AreEqual(1, (int)result[0]);
    00064             Assert.AreEqual(null, result[1]);
    00065             result = ScanWrapper("1 2", "%*i %*i");
    00066             Assert.IsNotNull(result);
    00067             Assert.AreEqual(2, result.Length);
    00068             Assert.AreEqual(null, result[0]);
    00069             Assert.AreEqual(null, result[1]);
    00070         }
    00071
    00072         [TestMethod]
    00073         public void Scanfd()
    00074         {
    00075             object[] result;
    00076             result = ScanWrapper("0", "%d");
    00077             Assert.IsNotNull(result);
    00078             Assert.AreEqual(1, result.Length);
    00079             Assert.AreEqual(0, (int)result[0]);
    00080             result = ScanWrapper("1", "%d");
    00081             Assert.IsNotNull(result);
    00082             Assert.AreEqual(1, result.Length);
    00083             Assert.AreEqual(1, (int)result[0]);
    00084             result = ScanWrapper("-1", "%d");
    00085             Assert.IsNotNull(result);
    00086             Assert.AreEqual(1, result.Length);
    00087             Assert.AreEqual(-1, (int)result[0]);
    00088             result = ScanWrapper("123", "%d");
    00089             Assert.IsNotNull(result);
    00090             Assert.AreEqual(1, result.Length);
    00091             Assert.AreEqual(123, (int)result[0]);
    00092         }
    00093
    00094         [TestMethod]
    00095         public void Scanfi()
    00096         {
    00097             object[] result;
    00098             result = ScanWrapper("0", "%i");
    00099             Assert.IsNotNull(result);
    00100             Assert.AreEqual(1, result.Length);
    00101             Assert.AreEqual(0, (int)result[0]);
    00102             result = ScanWrapper("1", "%i");
    00103             Assert.IsNotNull(result);
    00104             Assert.AreEqual(1, result.Length);
    00105             Assert.AreEqual(1, (int)result[0]);
    00106             result = ScanWrapper("-1", "%i");
    00107             Assert.IsNotNull(result);
    00108             Assert.AreEqual(1, result.Length);
    00109             Assert.AreEqual(-1, (int)result[0]);
    00110             result = ScanWrapper("123", "%i");
    00111             Assert.IsNotNull(result);
    00112             Assert.AreEqual(1, result.Length);
    00113             Assert.AreEqual(123, (int)result[0]);
    00114         }
    00115
    00116         [TestMethod]
    00117         public void Scanfli()
    00118         {
    00119             object[] result;
    00120             result = ScanWrapper("0", "%li");
    00121             Assert.IsNotNull(result);
    00122             Assert.AreEqual(1, result.Length);
    00123             Assert.AreEqual(0, (long)result[0]);
    00124             result = ScanWrapper("1", "%li");
    00125             Assert.IsNotNull(result);
    00126             Assert.AreEqual(1, result.Length);
    00127             Assert.AreEqual(1, (long)result[0]);
    00128             result = ScanWrapper("-1", "%li");
    00129             Assert.IsNotNull(result);
    00130             Assert.AreEqual(1, result.Length);
    00131             Assert.AreEqual(-1, (long)result[0]);
    00132             result = ScanWrapper("123", "%li");
    00133             Assert.IsNotNull(result);
    00134             Assert.AreEqual(1, result.Length);
    00135             Assert.AreEqual(123, (long)result[0]);
    00136         }
    00137
    00138         [TestMethod]
    00139         public void Scanfhi()
    00140         {
    00141             object[] result;
    00142             result = ScanWrapper("0", "%hi");
    00143             Assert.IsNotNull(result);
    00144             Assert.AreEqual(1, result.Length);
    00145             Assert.AreEqual(0, (short)result[0]);
    00146             result = ScanWrapper("1", "%hi");
    00147             Assert.IsNotNull(result);
    00148             Assert.AreEqual(1, result.Length);
    00149             Assert.AreEqual(1, (short)result[0]);
    00150             result = ScanWrapper("-1", "%hi");
    00151             Assert.IsNotNull(result);
    00152             Assert.AreEqual(1, result.Length);
    00153             Assert.AreEqual(-1, (short)result[0]);
    00154             result = ScanWrapper("123", "%hi");
    00155             Assert.IsNotNull(result);
    00156             Assert.AreEqual(1, result.Length);
    00157             Assert.AreEqual(123, (short)result[0]);
    00158         }
    00159
    00160         [TestMethod]
    00161         public void Scanfo()
    00162         {
    00163             object[] result;
    00164             result = ScanWrapper("0", "%o");
    00165             Assert.IsNotNull(result);
    00166             Assert.AreEqual(1, result.Length);
    00167             Assert.AreEqual((uint)0, (uint)result[0]);
    00168             result = ScanWrapper("1", "%o");
    00169             Assert.IsNotNull(result);
    00170             Assert.AreEqual(1, result.Length);
    00171             Assert.AreEqual((uint)1, (uint)result[0]);
    00172             result = ScanWrapper("10", "%o");
    00173             Assert.IsNotNull(result);
    00174             Assert.AreEqual(1, result.Length);
    00175             Assert.AreEqual((uint)8, (uint)result[0]);
    00176         }
    00177
    00178         [TestMethod]
    00179         public void Scanfu()
    00180         {
    00181             object[] result;
    00182             result = ScanWrapper("1", "%u");
    00183             Assert.IsNotNull(result);
    00184             Assert.AreEqual(1, result.Length);
    00185             Assert.AreEqual((uint)1, (uint)result[0]);
    00186             result = ScanWrapper("0", "%u");
    00187             Assert.IsNotNull(result);
    00188             Assert.AreEqual(1, result.Length);
    00189             Assert.AreEqual((uint)0, (uint)result[0]);
    00190             result = ScanWrapper("-1", "%u");
    00191             Assert.IsNotNull(result);
    00192             Assert.AreEqual(1, result.Length);
    00193             Assert.AreEqual(null, result[0]);
    00194             result = ScanWrapper("123", "%u");
    00195             Assert.IsNotNull(result);
    00196             Assert.AreEqual(1, result.Length);
    00197             Assert.AreEqual((uint)123, (uint)result[0]);
    00198         }
    00199
    00200         [TestMethod]
    00201         public void Scanflu()
    00202         {
    00203             object[] result;
    00204             result = ScanWrapper("1", "%lu");
    00205             Assert.IsNotNull(result);
    00206             Assert.AreEqual(1, result.Length);
    00207             Assert.AreEqual((ulong)1, (ulong)result[0]);
    00208             result = ScanWrapper("0", "%lu");
    00209             Assert.IsNotNull(result);
    00210             Assert.AreEqual(1, result.Length);
    00211             Assert.AreEqual((ulong)0, (ulong)result[0]);
    00212             result = ScanWrapper("-1", "%lu");
    00213             Assert.IsNotNull(result);
    00214             Assert.AreEqual(1, result.Length);
    00215             Assert.AreEqual(null, result[0]);
    00216             result = ScanWrapper("123", "%lu");
    00217             Assert.IsNotNull(result);
    00218             Assert.AreEqual(1, result.Length);
    00219             Assert.AreEqual((ulong)123, (ulong)result[0]);
    00220         }
    00221
    00222         [TestMethod]
    00223         public void Scanfhu()
    00224         {
    00225             object[] result;
    00226             result = ScanWrapper("1", "%hu");
    00227             Assert.IsNotNull(result);
    00228             Assert.AreEqual(1, result.Length);
    00229             Assert.AreEqual(1, (ushort)result[0]);
    00230             result = ScanWrapper("0", "%hu");
    00231             Assert.IsNotNull(result);
    00232             Assert.AreEqual(1, result.Length);
    00233             Assert.AreEqual(0, (ushort)result[0]);
    00234             result = ScanWrapper("-1", "%hu");
    00235             Assert.IsNotNull(result);
    00236             Assert.AreEqual(1, result.Length);
    00237             Assert.AreEqual(null, result[0]);
    00238             result = ScanWrapper("123", "%hu");
    00239             Assert.IsNotNull(result);
    00240             Assert.AreEqual(1, result.Length);
    00241             Assert.AreEqual(123, (ushort)result[0]);
    00242         }
    00243
    00244         [TestMethod]
    00245         public void Scanfx()
    00246         {
    00247             object[] result;
    00248             result = ScanWrapper("0", "%x");
    00249             Assert.IsNotNull(result);
    00250             Assert.AreEqual(1, result.Length);
    00251             Assert.AreEqual((uint)0, (uint)result[0]);
    00252             result = ScanWrapper("1", "%x");
    00253             Assert.IsNotNull(result);
    00254             Assert.AreEqual(1, result.Length);
    00255             Assert.AreEqual((uint)1, (uint)result[0]);
    00256             result = ScanWrapper("f", "%x");
    00257             Assert.IsNotNull(result);
    00258             Assert.AreEqual(1, result.Length);
    00259             Assert.AreEqual((uint)15, (uint)result[0]);
    00260             result = ScanWrapper("10", "%x");
    00261             Assert.IsNotNull(result);
    00262             Assert.AreEqual(1, result.Length);
    00263             Assert.AreEqual((uint)16, (uint)result[0]);
    00264         }
    00265
    00266         [TestMethod]
    00267         public void Scanff()
    00268         {
    00269             object[] result;
    00270             result = ScanWrapper("0", "%f");
    00271             Assert.IsNotNull(result);
    00272             Assert.AreEqual(1, result.Length);
    00273             Assert.AreEqual(0.0f, (float)result[0]);
    00274             result = ScanWrapper("1", "%f");
    00275             Assert.IsNotNull(result);
    00276             Assert.AreEqual(1, result.Length);
    00277             Assert.AreEqual(1.0f, (float)result[0]);
    00278             result = ScanWrapper("1.0", "%f");
    00279             Assert.IsNotNull(result);
    00280             Assert.AreEqual(1, result.Length);
    00281             Assert.AreEqual(1.0f, (float)result[0]);
    00282             result = ScanWrapper("-1", "%f");
    00283             Assert.IsNotNull(result);
    00284             Assert.AreEqual(1, result.Length);
    00285             Assert.AreEqual(-1.0f, (float)result[0]);
    00286             result = ScanWrapper("-1.0", "%f");
    00287             Assert.IsNotNull(result);
    00288             Assert.AreEqual(1, result.Length);
    00289             Assert.AreEqual(-1.0f, (float)result[0]);
    00290             result = ScanWrapper("1e2", "%f");
    00291             Assert.IsNotNull(result);
    00292             Assert.AreEqual(1, result.Length);
    00293             Assert.AreEqual(100.0f, (float)result[0]);
    00294             result = ScanWrapper("1e-2", "%f");
    00295             Assert.IsNotNull(result);
    00296             Assert.AreEqual(1, result.Length);
    00297             Assert.AreEqual(0.01f, (float)result[0]);
    00298             result = ScanWrapper("-1.0e2", "%f");
    00299             Assert.IsNotNull(result);
    00300             Assert.AreEqual(1, result.Length);
    00301             Assert.AreEqual(-100.0f, (float)result[0]);
    00302             result = ScanWrapper("-1.0e-2", "%f");
    00303             Assert.IsNotNull(result);
    00304             Assert.AreEqual(1, result.Length);
    00305             Assert.AreEqual(-0.01f, (float)result[0]);
    00306         }
    00307
    00308         [TestMethod]
    00309         public void Scanflf()
    00310         {
    00311             object[] result;
    00312             result = ScanWrapper("0", "%lf");
    00313             Assert.IsNotNull(result);
    00314             Assert.AreEqual(1, result.Length);
    00315             Assert.AreEqual(0.0, (double)result[0]);
    00316             result = ScanWrapper("1", "%lf");
    00317             Assert.IsNotNull(result);
    00318             Assert.AreEqual(1, result.Length);
    00319             Assert.AreEqual(1.0, (double)result[0]);
    00320             result = ScanWrapper("1.0", "%lf");
    00321             Assert.IsNotNull(result);
    00322             Assert.AreEqual(1, result.Length);
    00323             Assert.AreEqual(1.0, (double)result[0]);
    00324             result = ScanWrapper("-1", "%lf");
    00325             Assert.IsNotNull(result);
    00326             Assert.AreEqual(1, result.Length);
    00327             Assert.AreEqual(-1.0, (double)result[0]);
    00328             result = ScanWrapper("-1.0", "%lf");
    00329             Assert.IsNotNull(result);
    00330             Assert.AreEqual(1, result.Length);
    00331             Assert.AreEqual(-1.0, (double)result[0]);
    00332             result = ScanWrapper("1e2", "%lf");
    00333             Assert.IsNotNull(result);
    00334             Assert.AreEqual(1, result.Length);
    00335             Assert.AreEqual(100.0, (double)result[0]);
    00336             result = ScanWrapper("1e-2", "%lf");
    00337             Assert.IsNotNull(result);
    00338             Assert.AreEqual(1, result.Length);
    00339             Assert.AreEqual(0.01, (double)result[0]);
    00340             result = ScanWrapper("-1.0e2", "%lf");
    00341             Assert.IsNotNull(result);
    00342             Assert.AreEqual(1, result.Length);
    00343             Assert.AreEqual(-100.0, (double)result[0]);
    00344             result = ScanWrapper("-1.0e-2", "%lf");
    00345             Assert.IsNotNull(result);
    00346             Assert.AreEqual(1, result.Length);
    00347             Assert.AreEqual(-0.01, (double)result[0]);
    00348         }
    00349
    00350         [TestMethod]
    00351         public void Scanfs()
    00352         {
    00353             object[] result;
    00354             result = ScanWrapper("Hello", "%s");
    00355             Assert.IsNotNull(result);
    00356             Assert.AreEqual(1, result.Length);
    00357             Assert.AreEqual("Hello", (string)result[0]);
    00358             result = ScanWrapper("Hello World", "%s");
    00359             Assert.IsNotNull(result);
    00360             Assert.AreEqual(1, result.Length);
    00361             Assert.AreEqual("Hello", (string)result[0]);
    00362             result = ScanWrapper("Hello World", "%s %s");
    00363             Assert.IsNotNull(result);
    00364             Assert.AreEqual(2, result.Length);
    00365             Assert.AreEqual("Hello", (string)result[0]);
    00366             Assert.AreEqual("World", (string)result[1]);
    00367         }
    00368
    00369         [TestMethod]
    00370         public void Scanfc()
    00371         {
    00372             object[] result;
    00373             result = ScanWrapper("Hello World", "%c");
    00374             Assert.IsNotNull(result);
    00375             Assert.AreEqual(1, result.Length);
    00376             Assert.AreEqual('H', (char)result[0]);
    00377             result = ScanWrapper("Hello World", "%c%c");
    00378             Assert.IsNotNull(result);
    00379             Assert.AreEqual(2, result.Length);
    00380             Assert.AreEqual('H', (char)result[0]);
    00381             Assert.AreEqual('e', (char)result[1]);
    00382             result = ScanWrapper("Hello World", "%ce%c");
    00383             Assert.IsNotNull(result);
    00384             Assert.AreEqual(2, result.Length);
    00385             Assert.AreEqual('H', (char)result[0]);
    00386             Assert.AreEqual('l', (char)result[1]);
    00387         }
    00388
    00396         private object[] ScanWrapper(string text, string format)
    00397         {
    00398             Scanf scanf = new Scanf();
    00399             object[] results;
    00400             System.Console.WriteLine("scanf.Scan('{0}','{1}')", text, format);
    00401             results = scanf.Scan(text, format);
    00402             if (null == results)
    00403             {
    00404                 System.Console.WriteLine("null");
    00405             }
    00406             else
    00407             {
    00408                 int i = 0;
    00409                 foreach (object result in results)
    00410                 {
    00411                     i++;
    00412                     if (null == result)
    00413                     {
    00414                         System.Console.WriteLine("{0}: null", i);
    00415                     }
    00416                     else
    00417                     {
    00418                         System.Console.WriteLine("{0}: type={1} value={2}", i, result.GetType(), result.ToString());
    00419                     }
    00420                 }
    00421             }
    00422
    00423             return results;
    00424         }
    00425     }
    00426 }