Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10646


Ignore:
Timestamp:
03/21/14 17:40:36 (11 years ago)
Author:
abeham
Message:

#2146: updated unit tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Random-3.3/RandomEnumerableSampleTest.cs

    r10465 r10646  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2929  public class RandomEnumerableSampleTest {
    3030    [TestMethod]
    31     [TestCategory("Problems.Random")]
     31    [TestCategory("General")]
    3232    [TestProperty("Time", "short")]
    3333    public void SampleProportionalWithoutRepetitionTest() {
     
    3838        var weights = Enumerable.Repeat(0.0, 100);
    3939        for (int i = 0; i < 1000; i++) {
    40           var sample =
    41             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
     40          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    4241          Assert.AreEqual(sample.Count(), 1);
    43           Assert.AreEqual(sample.Distinct().Count(), 1);
    4442        }
    4543      }
     
    5048        var weights = Enumerable.Repeat(0.0, 1);
    5149        for (int i = 0; i < 1000; i++) {
    52           var sample =
    53             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
     50          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    5451          Assert.AreEqual(sample.Count(), 1);
    55           Assert.AreEqual(sample.Distinct().Count(), 1);
    5652        }
    5753      }
     
    6157        var random = new MersenneTwister(31415);
    6258        var weights = new double[] { 1.0, 2.0 };
    63         for (int i = 0; i < 1000; i++) {
    64           var sample =
    65             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
     59        var zeroSelected = 0;
     60        for (int i = 0; i < 1000; i++) {
     61          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    6662          Assert.AreEqual(sample.Count(), 1);
    67           Assert.AreEqual(sample.Distinct().Count(), 1);
    68         }
     63          if (sample[0] == 0) zeroSelected++;
     64        }
     65        Assert.IsTrue(zeroSelected > 0 && zeroSelected < 1000);
    6966      }
    7067      {
     
    7471        var weights = new double[] { 1.0, 1000.0 };
    7572        for (int i = 0; i < 1000; i++) {
    76           var sample =
    77             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    78           Assert.AreEqual(sample.Count(), 1);
    79           Assert.AreEqual(sample.Distinct().Count(), 1);
     73          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
     74          Assert.AreEqual(sample.Count(), 2);
     75          Assert.AreEqual(sample.Distinct().Count(), 2);
    8076        }
    8177      }
     
    8581        var random = new MersenneTwister(31415);
    8682        var weights = Enumerable.Repeat(0.0, 1);
    87         var sample =
    88           RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
     83        var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
    8984        Assert.AreEqual(sample.Count(), 1);
    9085      }
     
    9691        var weights = Enumerable.Repeat(0.0, 100);
    9792        for (int i = 0; i < 1000; i++) {
    98           var sample =
    99             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
     93          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
    10094          Assert.AreEqual(sample.Count(), 10);
    10195          Assert.AreEqual(sample.Distinct().Count(), 10);
     
    109103        var weights = Enumerable.Repeat(0.0, 100);
    110104        for (int i = 0; i < 1000; i++) {
    111           var sample =
    112             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
     105          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
    113106          Assert.AreEqual(sample.Count(), 100);
    114107          Assert.AreEqual(sample.Distinct().Count(), 100);
     
    122115        var weights = Enumerable.Repeat(1.0, 10);
    123116        for (int i = 0; i < 1000; i++) {
    124 
    125           var sample =
    126             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
     117          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
    127118          Assert.AreEqual(sample.Count(), 10);
    128119          Assert.AreEqual(sample.Distinct().Count(), 10);
     
    132123      {
    133124        // select 10 of 10 uniformly (weights = 1)
     125        var items = Enumerable.Range(0, 10);
     126        var random = new MersenneTwister(31415);
     127        var weights = Enumerable.Repeat(1.0, 10);
     128        for (int i = 0; i < 1000; i++) {
     129          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, false).ToArray();
     130          Assert.AreEqual(sample.Count(), 10);
     131          Assert.AreEqual(sample.Distinct().Count(), 10);
     132        }
     133      }
     134
     135      {
     136        // select 10 of 10 uniformly (weights = 1)
     137        var items = Enumerable.Range(0, 10);
     138        var random = new MersenneTwister(31415);
     139        var weights = Enumerable.Repeat(1.0, 10);
     140        for (int i = 0; i < 1000; i++) {
     141          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, true).ToArray();
     142          Assert.AreEqual(sample.Count(), 10);
     143          Assert.AreEqual(sample.Distinct().Count(), 10);
     144        }
     145      }
     146
     147      {
     148        // select 5 of 10 uniformly (weights = 0..n)
     149        var items = Enumerable.Range(0, 10);
     150        var random = new MersenneTwister(31415);
     151        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     152        for (int i = 0; i < 1000; i++) {
     153          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, false, false).ToArray();
     154          Assert.AreEqual(sample.Count(), 5);
     155          Assert.AreEqual(sample.Distinct().Count(), 5);
     156        }
     157      }
     158
     159      {
     160        // select 5 of 10 uniformly (weights = 0..n)
     161        var items = Enumerable.Range(0, 10);
     162        var random = new MersenneTwister(31415);
     163        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     164        for (int i = 0; i < 1000; i++) {
     165          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, false).ToArray();
     166          Assert.AreEqual(sample.Count(), 5);
     167          Assert.AreEqual(sample.Distinct().Count(), 5);
     168        }
     169      }
     170
     171      {
     172        // select 5 of 10 uniformly (weights = 0..n)
     173        var items = Enumerable.Range(0, 10);
     174        var random = new MersenneTwister(31415);
     175        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     176        for (int i = 0; i < 1000; i++) {
     177          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, true).ToArray();
     178          Assert.AreEqual(sample.Count(), 5);
     179          Assert.AreEqual(sample.Distinct().Count(), 5);
     180        }
     181      }
     182
     183      {
     184        // select 10 of 100 uniformly (weights = 1)
    134185        // repeat 1000000 times and calculate statistics
    135186        var items = Enumerable.Range(0, 100);
Note: See TracChangeset for help on using the changeset viewer.