1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations 12 * under the License. 13 */ 14 15 package com.google.common.collect; 16 17 import static com.google.common.truth.Truth.assertThat; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.base.Function; 22 import com.google.common.testing.NullPointerTester; 23 import java.util.Arrays; 24 import java.util.List; 25 import junit.framework.TestCase; 26 27 /** 28 * Tests for {@code TreeTraverser}. 29 * 30 * @author Louis Wasserman 31 */ 32 @GwtCompatible(emulated = true) 33 public class TreeTraverserTest extends TestCase { 34 private static class Node { 35 final char value; 36 Node(char value)37 Node(char value) { 38 this.value = value; 39 } 40 } 41 42 private static final class Tree extends Node { 43 final List<Tree> children; 44 Tree(char value, Tree... children)45 public Tree(char value, Tree... children) { 46 super(value); 47 this.children = Arrays.asList(children); 48 } 49 } 50 51 private static final TreeTraverser<Tree> ADAPTER = 52 new TreeTraverser<Tree>() { 53 @Override 54 public Iterable<Tree> children(Tree node) { 55 return node.children; 56 } 57 }; 58 59 private static final TreeTraverser<Tree> ADAPTER_USING_USING = 60 TreeTraverser.using( 61 new Function<Tree, Iterable<Tree>>() { 62 @Override 63 public Iterable<Tree> apply(Tree node) { 64 return node.children; 65 } 66 }); 67 68 // h 69 // / | \ 70 // / e \ 71 // d g 72 // /|\ | 73 // / | \ f 74 // a b c 75 static final Tree a = new Tree('a'); 76 static final Tree b = new Tree('b'); 77 static final Tree c = new Tree('c'); 78 static final Tree d = new Tree('d', a, b, c); 79 static final Tree e = new Tree('e'); 80 static final Tree f = new Tree('f'); 81 static final Tree g = new Tree('g', f); 82 static final Tree h = new Tree('h', d, e, g); 83 iterationOrder(Iterable<? extends Node> iterable)84 static String iterationOrder(Iterable<? extends Node> iterable) { 85 StringBuilder builder = new StringBuilder(); 86 for (Node t : iterable) { 87 builder.append(t.value); 88 } 89 StringBuilder forEachBuilder = new StringBuilder(); 90 iterable.forEach(t -> forEachBuilder.append(t.value)); 91 assertTrue( 92 "Iterator content was " + builder + " but forEach content was " + forEachBuilder, 93 builder.toString().contentEquals(forEachBuilder)); 94 return builder.toString(); 95 } 96 testPreOrder()97 public void testPreOrder() { 98 assertThat(iterationOrder(ADAPTER.preOrderTraversal(h))).isEqualTo("hdabcegf"); 99 } 100 testPostOrder()101 public void testPostOrder() { 102 assertThat(iterationOrder(ADAPTER.postOrderTraversal(h))).isEqualTo("abcdefgh"); 103 } 104 testBreadthOrder()105 public void testBreadthOrder() { 106 assertThat(iterationOrder(ADAPTER.breadthFirstTraversal(h))).isEqualTo("hdegabcf"); 107 } 108 testUsing()109 public void testUsing() { 110 assertThat(iterationOrder(ADAPTER_USING_USING.preOrderTraversal(h))).isEqualTo("hdabcegf"); 111 } 112 113 @GwtIncompatible // NullPointerTester testNulls()114 public void testNulls() { 115 NullPointerTester tester = new NullPointerTester(); 116 tester.testAllPublicInstanceMethods(ADAPTER); 117 } 118 } 119