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 return builder.toString(); 90 } 91 testPreOrder()92 public void testPreOrder() { 93 assertThat(iterationOrder(ADAPTER.preOrderTraversal(h))).isEqualTo("hdabcegf"); 94 } 95 testPostOrder()96 public void testPostOrder() { 97 assertThat(iterationOrder(ADAPTER.postOrderTraversal(h))).isEqualTo("abcdefgh"); 98 } 99 testBreadthOrder()100 public void testBreadthOrder() { 101 assertThat(iterationOrder(ADAPTER.breadthFirstTraversal(h))).isEqualTo("hdegabcf"); 102 } 103 testUsing()104 public void testUsing() { 105 assertThat(iterationOrder(ADAPTER_USING_USING.preOrderTraversal(h))).isEqualTo("hdabcegf"); 106 } 107 108 @GwtIncompatible // NullPointerTester testNulls()109 public void testNulls() { 110 NullPointerTester tester = new NullPointerTester(); 111 tester.testAllPublicInstanceMethods(ADAPTER); 112 } 113 } 114