Skip to content

robot-name: collision detection not convincing #61

Closed

Description

@edescourtis

differentRobotsHaveDifferentNames and resetName are not proper tests for detecting collisions. They need many more iterations to be convincing (~1/670000).

Here is my modified test:

import org.junit.Test;

import java.util.stream.IntStream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;

public class RobotTest {

    private static final String EXPECTED_ROBOT_NAME_PATTERN = "\\w{2}\\d{3}";
    private final Robot robot1 = new Robot();
    private final Robot robot2 = new Robot();

    @Test
    public void hasName() {
        assertIsValidName(robot1.getName());
    }

    @Test
    public void differentRobotsHaveDifferentNames() {
        IntStream.range(0, 300000).forEach(x -> {
            assertThat(robot1.getName(), not(equalTo(new Robot().getName())));
        });
    }

    @Test
    public void resetName() {
        IntStream.range(0, 300000).forEach(x -> {
            final String name = robot2.getName();
            robot2.reset();
            final String name2 = robot2.getName();
            assertThat(name, not(equalTo(name2)));
            assertIsValidName(name2);
        });
    }

    private static void assertIsValidName(String name) {
        assertThat(name.matches(EXPECTED_ROBOT_NAME_PATTERN), is(true));
    }
}

Activity

  1. changed the title [-]robot-name[/-] [+]robot-name collisions detection not convincing[/+] on Aug 26, 2015
  2. changed the title [-]robot-name collisions detection not convincing[/-] [+]robot-name collision detection not convincing[/+] on Aug 27, 2015
  3. jtigger commented on Sep 18, 2015

    @jtigger
    Contributor

    Yeah, I had a similar reaction to this exercise.

    I was thinking of doing something definitive to cover.

    What to you think of something like this?

       public static final Long NUM_VALID_NAMES = 26l * 26l * 10l * 10l * 10l;
       @Test
       public void aNameIsNeverRepeated() {
          long numberOfDistinctGeneratedNames = LongStream.range(0, NUM_VALID_NAMES)
                                                   .mapToObj(i -> new Robot().getName())
                                                   .distinct()
                                                   .count();
          assertThat(numberOfDistinctGeneratedNames, equalTo(NUM_VALID_NAMES));
       }

    Would need to extend the API to be able to signal to Robot that it can "reset".

  4. edescourtis commented on Jan 12, 2016

    @edescourtis
    Author

    @jtigger Sounds like an improvement.

  5. self-assigned this
    on Nov 6, 2016
  6. matthewmorgan commented on Jan 3, 2017

    @matthewmorgan
    Contributor

    @exercism/java I've been playing around with this, and can't find any solution that both involves randomness and doesn't asymptotically creep toward finishing in any reasonable amount of time.

    I am wondering if, by imposing the requirement of randomness and a range of 670000 names, we haven't set ourselves up for failure.

    Currently the solution involves generating a name, checking if it's been used, and if it has, try randomly regenerating again. Any ideas on a better approach that preserves both the size of the set and the randomness?

  7. NobbZ commented on Jan 3, 2017

    @NobbZ
    Member
  8. matthewmorgan commented on Jan 3, 2017

    @matthewmorgan
    Contributor

    @NobbZ maybe removing the selected name from the list, to avoid needing to check for uniqueness? Then we would always know the name was valid and unique. I think that will work.

  9. stkent commented on Jan 3, 2017

    @stkent
    Contributor

    General (black box) tests for randomness are either very intense or always prone to failure some (controllable) percentage of the time. Neither are suitable here, unfortunately. I don't think we'll be able to exhaustively check all possible values, so the practical solution may be to come up with a set of approximate tests that yield 'reasonable' certainty. This could be accompanied by comments or notes in the hints describing why testing randomness is so hard.

    Another thought: perhaps we could update the robot interface to accept a generating function. Then we could insert our own generating function during the tests and verify in a smaller case?

  10. NobbZ commented on Jan 3, 2017

    @NobbZ
    Member

    maybe removing the selected name from the list

    This was implied. Sorry for not beeing explicit.

  11. changed the title [-]robot-name collision detection not convincing[/-] [+]robot-name: collision detection not convincing[/+] on Mar 8, 2017
  12. jtigger commented on Mar 8, 2017

    @jtigger
    Contributor

    @matthewmorgan we got yer name on this one... do you have time to tackle this? Or should we release it into the backlog (i.e. unassign it)?

  13. matthewmorgan commented on Mar 8, 2017

    @matthewmorgan
    Contributor

    @jtigger I have a solution for this, but it is pretty long running. The best comprehensive solution I could come up with was:

    1. generate all possible names and store them in a List
    2. to serve a name, generate a random index with an upper bound set by the current list size
    3. remove that name from the List
    4. ...repeat

    I recommend we either test a subset or pursue @stkent's idea of supplying a generator.

    If we can get a consensus on this I can implement it. @exercism/java ? Any feelings about this?

  14. jtigger commented on Apr 7, 2017

    @jtigger
    Contributor

    I had a similar solution. It has an interesting design pressure on the solution itself since relying on pure randomness generates a long tail of run-times (prompting an interesting conversation about resource trade-offs of memory for time).

    @matthewmorgan this conversation started some time before your plate got quite full. I'm gonna pull the assignment... if you have a desire squeeze in hitting this, call dibs with a PR.

  15. stkent commented on Jun 12, 2017

    @stkent
    Contributor

    I have a feeling that the specification of this exercise disallows convincing tests. There's no canonical test data. If anyone is interested in picking this up in the future I'd recommend starting by reviewing the tests in other tracks to see if this conundrum has already been worked around someplace else.

  16. stkent commented on Aug 16, 2017

    @stkent
    Contributor

    Closing until there's interest in actively working on this again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions