Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/19/10 12:55:18 (14 years ago)
Author:
abeham
Message:

updated the fake random number generator to throw an exception when the requested random variable doesn't fit the bounds
updated the OrderCrossoverTest to test for thrown exceptions #889

Location:
trunk/sources/HeuristicLab.Permutation/3.3/Tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Permutation/3.3/Tests/OrderCrossoverTest.cs

    r2836 r2840  
    124124      // based on the previous with changed breakpoints
    125125      random.Reset();
    126       random.IntNumbers = new int[] { 4, 4 };
    127       expected = new Permutation(new int[] { 3, 4, 0, 9, 7, 8, 2, 1, 6, 5 });
     126      random.IntNumbers = new int[] { 6, 9 };
     127      expected = new Permutation(new int[] { 3, 4, 8, 2, 7, 1, 6, 0, 5, 9 });
    128128      Assert.IsTrue(expected.Validate());
    129129      actual = target.Cross(random, parents);
     
    132132      // another one based on the previous with changed breakpoints
    133133      random.Reset();
    134       random.IntNumbers = new int[] { 9, 9 };
    135       expected = new Permutation(new int[] { 5, 3, 4, 0, 8, 2, 7, 1, 6, 9 });
    136       Assert.IsTrue(expected.Validate());
    137       actual = target.Cross(random, parents);
    138       Assert.IsTrue(actual.Validate());
    139       Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
     134      random.IntNumbers = new int[] { 0, 9 };
     135      expected = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
     136      Assert.IsTrue(expected.Validate());
     137      actual = target.Cross(random, parents);
     138      Assert.IsTrue(actual.Validate());
     139      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
     140      // perform a test with more than two parents
     141      random.Reset();
     142      bool exceptionFired = false;
     143      try {
     144        target.Cross(random, new ItemArray<Permutation>(new Permutation[] { new Permutation(4), new Permutation(4), new Permutation(4)}));
     145      } catch (System.InvalidOperationException) {
     146        exceptionFired = true;
     147      }
     148      Assert.IsTrue(exceptionFired);
     149      // perform a test when two permutations are of unequal length
     150      random.Reset();
     151      exceptionFired = false;
     152      try {
     153        target.Cross(random, new ItemArray<Permutation>(new Permutation[] { new Permutation(8), new Permutation(6) }));
     154      } catch (System.ArgumentException) {
     155        exceptionFired = true;
     156      }
     157      Assert.IsTrue(exceptionFired);
    140158    }
    141159
     
    196214      // based on the previous with changed breakpoints
    197215      random.Reset();
    198       random.IntNumbers = new int[] { 4, 4 };
    199       expected = new Permutation(new int[] { 3, 4, 0, 9, 7, 8, 2, 1, 6, 5 });
     216      random.IntNumbers = new int[] { 6, 9 };
     217      expected = new Permutation(new int[] { 3, 4, 8, 2, 7, 1, 6, 0, 5, 9 });
    200218      Assert.IsTrue(expected.Validate());
    201219      actual = OrderCrossover.Apply(random, parent1, parent2);
     
    204222      // another one based on the previous with changed breakpoints
    205223      random.Reset();
    206       random.IntNumbers = new int[] { 9, 9 };
    207       expected = new Permutation(new int[] { 5, 3, 4, 0, 8, 2, 7, 1, 6, 9 });
    208       Assert.IsTrue(expected.Validate());
    209       actual = OrderCrossover.Apply(random, parent1, parent2);
    210       Assert.IsTrue(actual.Validate());
    211       Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
     224      random.IntNumbers = new int[] { 0, 9 };
     225      expected = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
     226      Assert.IsTrue(expected.Validate());
     227      actual = OrderCrossover.Apply(random, parent1, parent2);
     228      Assert.IsTrue(actual.Validate());
     229      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
     230      // perform a test when the two permutations are of unequal length
     231      random.Reset();
     232      bool exceptionFired = false;
     233      try {
     234        OrderCrossover.Apply(random, new Permutation(8), new Permutation(6));
     235      } catch (System.ArgumentException) {
     236        exceptionFired = true;
     237      }
     238      Assert.IsTrue(exceptionFired);
    212239    }
    213240
  • trunk/sources/HeuristicLab.Permutation/3.3/Tests/Random.cs

    r2836 r2840  
    6262    public int Next(int maxVal) {
    6363      if (nextIntIndex >= intNumbers.Length) throw new InvalidOperationException("Random: No more integer random numbers available");
     64      if (IntNumbers[nextIntIndex] >= maxVal) throw new InvalidOperationException("Random: Next integer random number (" + IntNumbers[nextIntIndex] + ") is >= " + maxVal);
    6465      return intNumbers[nextIntIndex++];
    6566    }
     
    6768    public int Next(int minVal, int maxVal) {
    6869      if (nextIntIndex >= intNumbers.Length) throw new InvalidOperationException("Random: No more integer random numbers available");
     70      if (IntNumbers[nextIntIndex] < minVal || IntNumbers[nextIntIndex] >= maxVal) throw new InvalidOperationException("Random: Next integer random number (" + IntNumbers[nextIntIndex] + ") is not in the range [" + minVal + ";" + maxVal + ")");
    6971      return intNumbers[nextIntIndex++];
    7072    }
     
    7274    public double NextDouble() {
    7375      if (nextDoubleIndex >= doubleNumbers.Length) throw new InvalidOperationException("Random: No more double random numbers available");
     76      if (doubleNumbers[nextDoubleIndex] < 0.0 || doubleNumbers[nextDoubleIndex] >= 1.0) throw new InvalidOperationException("Random: Next double ranomd number (" + DoubleNumbers[nextDoubleIndex] + ") is not in the range [0;1)");
    7477      return doubleNumbers[nextDoubleIndex++];
    7578    }
Note: See TracChangeset for help on using the changeset viewer.