コード例 #1
0
  @Test
  public void optimalCandidateTest() {
    for (int historySize : new int[] {0, 1, 5, Integer.MAX_VALUE}) {
      for (int candidatesSize : new int[] {1, 10, 100}) {
        for (int length : new int[] {10000}) {
          for (boolean duplicates : new boolean[] {true, false}) {
            for (int parameters : new int[] {1, 2, 5}) {
              for (int choices : new int[] {1, 2, 5}) {
                try {
                  AdaptiveRandomAlgorithm<String> algorithm =
                      new AdaptiveRandomAlgorithm<String>(
                          historySize, candidatesSize, length, duplicates);
                  algorithm.initialize(
                      GeneratorTestUtils.prepareInput(parameters, choices), EMPTY_CONSTRAINTS);

                  List<List<String>> candidates = algorithm.getCandidates();
                  List<List<String>> history = algorithm.getCandidates();
                  List<String> optimalCandidate =
                      algorithm.getOptimalCandidate(candidates, history);

                  int optimalCandidateDistance = Integer.MAX_VALUE;
                  for (List<String> event : history) {
                    optimalCandidateDistance =
                        Math.min(
                            algorithm.distance(event, optimalCandidate), optimalCandidateDistance);
                  }
                  for (List<String> candidate : candidates) {
                    int candidateDistance = Integer.MAX_VALUE;
                    for (List<String> event : history) {
                      int eventDistance = algorithm.distance(candidate, event);
                      candidateDistance = Math.min(candidateDistance, eventDistance);
                    }
                    assertTrue(candidateDistance <= optimalCandidateDistance);
                  }
                } catch (GeneratorException e) {
                  fail("Unexpected GeneratorException: " + e.getMessage());
                }
              }
            }
          }
        }
      }
    }
  }
コード例 #2
0
  @Test
  public void distanceTest() {
    AdaptiveRandomAlgorithm<String> algorithm = new AdaptiveRandomAlgorithm<String>(0, 0, 0, false);

    List<String> v1 = new ArrayList<String>();
    List<String> v2 = new ArrayList<String>();

    for (int i = 0; i < 10; ++i) {
      String s = GeneratorTestUtils.randomString(10);
      v1.add(s);
      v2.add(s);
    }

    int distance = algorithm.distance(v1, v2);
    assertEquals(0, distance);
    for (int i = 0; i < 10; ++i) {
      v1.set(
          i,
          "some string that is unlikely to occure by random and is also longer than those generated");
      assertEquals(i + 1, algorithm.distance(v1, v2));
    }
  }