- Main Page
- Classes
- Files

- File List

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

    00001 ﻿namespace Tests
    00002 {
    00003     using System;
    00004     using System.Text;
    00005     using System.Threading;
    00006     using Microsoft.VisualStudio.TestTools.UnitTesting;
    00007     using NewGamePhysics.Mathematics;
    00008
    00012     [TestClass]
    00013     public class UnitTestUnbiaser
    00014     {
    00015         const int NumBits = 1024;
    00016         const int InputBiasPercent = 25;
    00017
    00018         public UnitTestUnbiaser()
    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         [TestMethod]
    00041         public void BitUnbiaserVonNeuman()
    00042         {
    00043             BitUnbiaser unbiaser = new BitUnbiaser(BitUnbiasAlgorithm.VonNeuman);
    00044
    00045             string input = RandomBitStream(NumBits, InputBiasPercent);
    00046             string output = unbiaser.Process(input);
    00047
    00048             Console.WriteLine("vonNeuman Test");
    00049             Console.WriteLine("Input  = {0}", input);
    00050             Console.WriteLine("Input Bias  = {0}%", CalculateBiasPercent(input));
    00051
    00052             Console.WriteLine("Output = {0}", output);
    00053             Console.WriteLine("Output Bias  = {0}%", CalculateBiasPercent(output));
    00054
    00055             Console.WriteLine("vonNeuman Yield = {0}%", 100.0 * (double)output.Length / (double)input.Length);
    00056             Console.WriteLine("");
    00057         }
    00058
    00059         [TestMethod]
    00060         public void BitUnbiaserMLS()
    00061         {
    00062             BitUnbiaser unbiaser = new BitUnbiaser(BitUnbiasAlgorithm.MLS);
    00063
    00064             string input = RandomBitStream(NumBits, InputBiasPercent);
    00065             string output = unbiaser.Process(input);
    00066
    00067             Console.WriteLine("MLS Test");
    00068             Console.WriteLine("Input  = {0}", input);
    00069             Console.WriteLine("Input Bias  = {0}%", CalculateBiasPercent(input));
    00070
    00071             Console.WriteLine("Output = {0}", output);
    00072             Console.WriteLine("Output Bias  = {0}%", CalculateBiasPercent(output));
    00073
    00074             Console.WriteLine("MLS Yield = {0}%", 100.0 * (double)output.Length / (double)input.Length);
    00075             Console.WriteLine("");
    00076         }
    00077
    00078         [TestMethod]
    00079         public void BitUnbiaserAMLS()
    00080         {
    00081
    00082             BitUnbiaser unbiaser = new BitUnbiaser(BitUnbiasAlgorithm.AMLS);
    00083
    00084             string input = RandomBitStream(NumBits, InputBiasPercent);
    00085             string output = unbiaser.Process(input);
    00086
    00087             Console.WriteLine("AMLS Test");
    00088             Console.WriteLine("Input  = {0}", input);
    00089             Console.WriteLine("Input Bias  = {0}%", CalculateBiasPercent(input));
    00090
    00091             Console.WriteLine("Output = {0}", output);
    00092             Console.WriteLine("Output Bias  = {0}%", CalculateBiasPercent(output));
    00093
    00094             Console.WriteLine("AMLS Yield = {0}%", 100.0 * (double)output.Length / (double)input.Length);
    00095             Console.WriteLine("");
    00096         }
    00097
    00098         [TestMethod]
    00099         public void ValueUnbiaserPairwise()
    00100         {
    00101
    00102             ValueUnbiaser unbiaser = new ValueUnbiaser(ValueUnbiasAlgorithm.Pairwise);
    00103
    00104             int dataSize = 40;
    00105             double[] data = new double[dataSize];
    00106
    00107             Random rand = new Random();
    00108             Console.WriteLine("PairWise Test");
    00109             for (int i = 0; i< dataSize; i++)
    00110             {
    00111                 data[i] = rand.NextDouble() + rand.NextDouble();
    00112                 Console.WriteLine(i + " = " + data[i]);
    00113             }
    00114
    00115             string output = unbiaser.Extract(data);
    00116
    00117             Console.WriteLine("Output = {0}", output);
    00118             Console.WriteLine("Yield = {0}%", 100.0 * (double)output.Length / (double)data.Length);
    00119             Console.WriteLine("");
    00120         }
    00121
    00122         private double CalculateBiasPercent(string bitStream)
    00123         {
    00124             int length = bitStream.Length;
    00125             char[] bitStreamC = bitStream.ToCharArray();
    00126             int count = 0;
    00127             for (int i = 0; i < length; i++)
    00128             {
    00129                 if (bitStreamC[i] == '1')
    00130                 {
    00131                     count++;
    00132                 }
    00133             }
    00134
    00135             return 100.0 * (double)count / (double)length;
    00136         }
    00137
    00138         private string RandomBitStream(int size, int bias)
    00139         {
    00140             Thread.Sleep(100);
    00141             Random rand = new Random();
    00142             StringBuilder sb = new StringBuilder(size);
    00143             for (int i = 0; i < size; i++)
    00144             {
    00145                 sb.Append( (rand.Next() % 100 < bias) ? '1' : '0');
    00146             }
    00147
    00148             return sb.ToString();
    00149         }
    00150     }
    00151 }
    00152
    00153
    00154