LeafGroups { public static List<Group> enumerateFrom(Group root) { List<Group> leaves = new ArrayList<>(); visitNode(root, leaves); return leaves; }  static List<Group> enumerateFrom(Group root);  }
@Test public void rootGroupCountedAsLeafWhenNoChildren() { Group g = new Group(0, "donkeykong"); List<Group> leaves = LeafGroups.enumerateFrom(g); assertThat(leaves.size(), is(1)); assertThat(leaves.get(0).getName(), is("donkeykong")); }
@Test public void singleLeafIsEnumerated() throws Exception { Group g = new Group(0, "donkeykong", dummyDistribution()); Group child = new Group(1, "mario"); g.addSubGroup(child); List<Group> leaves = LeafGroups.enumerateFrom(g); assertThat(leaves.size(), is(1)); assertThat(leaves.get(0).getName(), is("mario")); }
@Test public void singleLeafIsEnumeratedInNestedCase() throws Exception { Group g = new Group(0, "donkeykong", dummyDistribution()); Group child = new Group(1, "mario", dummyDistribution()); child.addSubGroup(new Group(2, "toad")); g.addSubGroup(child); List<Group> leaves = LeafGroups.enumerateFrom(g); assertThat(leaves.size(), is(1)); assertThat(leaves.get(0).getName(), is("toad")); }
@Test public void multipleLeafGroupsAreEnumerated() throws Exception { Group g = new Group(0, "donkeykong", dummyDistribution()); Group child = new Group(1, "mario", dummyDistribution()); child.addSubGroup(new Group(2, "toad")); child.addSubGroup(new Group(3, "yoshi")); g.addSubGroup(child); g.addSubGroup(new Group(4, "luigi")); List<Group> leaves = LeafGroups.enumerateFrom(g); leaves.sort((a, b) -> Integer.compare(a.getIndex(), b.getIndex())); assertThat(leaves.size(), is(3)); assertThat(leaves.get(0).getName(), is("toad")); assertThat(leaves.get(1).getName(), is("yoshi")); assertThat(leaves.get(2).getName(), is("luigi")); }