- Main Page
- Classes
- Files

- File List

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

    00001 ﻿namespace Tests
    00002 {
    00003     using System;
    00004     using System.Text;
    00005     using System.Threading;
    00006     using Microsoft.VisualStudio.TestTools.UnitTesting;
    00007     using System.Net.NetworkInformation;
    00008     using NewGamePhysics.Networking;
    00009
    00013     [TestClass]
    00014     public class UnitTestPlayTrulyRandom
    00015     {
    00016         static PlayTrulyRandom ptr = null;
    00017
    00018         public UnitTestPlayTrulyRandom()
    00019         {
    00020         }
    00021
    00022         private TestContext testContextInstance;
    00023
    00028         public TestContext TestContext
    00029         {
    00030             get
    00031             {
    00032                 return testContextInstance;
    00033             }
    00034             set
    00035             {
    00036                 testContextInstance = value;
    00037             }
    00038         }
    00039
    00040         [ClassInitialize()]
    00041         public static void PlayTrulyRandomInit(TestContext testContext)
    00042         {
    00043             ptr = null;
    00044             string agentName = "test";
    00045             string userName = Guid.NewGuid().ToString();
    00046             try
    00047             {
    00048                 ptr = new PlayTrulyRandom(agentName, userName, 8);
    00049                 Console.WriteLine("PlayTrulyRandom('{0}','{1}')", agentName, userName);
    00050                 Assert.IsNotNull(ptr, "ptr");
    00051
    00052                 // Test read to check if we have connectivity
    00053                 int retrievedBits;
    00054                 int randomNumber = ptr.Next(0, 1, out retrievedBits);
    00055             }
    00056             catch (Exception e)
    00057             {
    00058                 if (e.Message.Contains("Could not retrieve"))
    00059                 {
    00060                     // assume no internet connectivity
    00061                     ptr = null;
    00062                     return;
    00063                 }
    00064                 else
    00065                 {
    00066                     throw;
    00067                 }
    00068             }
    00069         }
    00070
    00071         [TestMethod]
    00072         public void PlayTrulyRandomNextValidRange()
    00073         {
    00074             const int NumTests = 20;
    00075             const int NumRepeats = 5;
    00076             int[] min = new int[NumTests];
    00077             int[] max = new int[NumTests];
    00078
    00079             // skip test if we are not connected
    00080             if (ptr == null)
    00081             {
    00082                 return;
    00083             }
    00084
    00085             // Some specific test values
    00086             int t = 0;
    00087             min[t] =  0;
    00088             max[t] =  0;
    00089             t++;
    00090             min[t] = 0;
    00091             max[t] = 1;
    00092             t++;
    00093             min[t] = 0;
    00094             max[t] = 2;
    00095             t++;
    00096             min[t] = -1;
    00097             max[t] = 0;
    00098             t++;
    00099             min[t] = -1;
    00100             max[t] = 1;
    00101             t++;
    00102             min[t] = -2;
    00103             max[t] = -2;
    00104             t++;
    00105             min[t] = -2;
    00106             max[t] = -1;
    00107             t++;
    00108             min[t] = 2;
    00109             max[t] = 2;
    00110             t++;
    00111             min[t] = -2;
    00112             max[t] = 2;
    00113             t++;
    00114             min[t] = int.MinValue;
    00115             max[t] = int.MaxValue;
    00116             t++;
    00117
    00118             // Generate additional bounds at random
    00119             // in the byte range
    00120             Random rnd = new Random();
    00121             for (int i = t; i < NumTests; i++)
    00122             {
    00123                 min[i] = rnd.Next(0, byte.MaxValue);
    00124                 max[i] = rnd.Next(min[i], min[i] + byte.MaxValue);
    00125             }
    00126
    00127             // Generate random numbers and test them
    00128             for (int i = 0; i < NumTests; i++)
    00129             {
    00130                 for (int j = 0; j < NumRepeats; j++)
    00131                 {
    00132                     int retrievedBits;
    00133                     int randomNumber = ptr.Next(min[i], max[i], out retrievedBits);
    00134                     Console.WriteLine("Test_{3}.{4}: Next({0},{1}) = {2}", min[i], max[i], randomNumber, i+1, j+1);
    00135                     Assert.IsTrue(randomNumber >= min[i]);
    00136                     Assert.IsTrue(randomNumber <= max[i]);
    00137                 }
    00138
    00139                 Console.WriteLine("");
    00140             }
    00141         }
    00142     }
    00143 }
    00144
    00145