001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.functor; 018 019import static org.junit.Assert.assertEquals; 020import static org.junit.Assert.assertNotNull; 021import static org.junit.Assert.assertSame; 022import static org.junit.Assert.assertTrue; 023 024import java.util.ArrayList; 025import java.util.Collection; 026import java.util.HashSet; 027import java.util.Iterator; 028import java.util.List; 029import java.util.Set; 030 031import org.apache.commons.functor.core.Identity; 032import org.apache.commons.functor.core.composite.Not; 033import org.apache.commons.functor.generator.FilteredGenerator; 034import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter; 035import org.apache.commons.functor.generator.loop.LoopGenerator; 036import org.apache.commons.functor.generator.loop.TransformedGenerator; 037import org.apache.commons.functor.range.IntegerRange; 038import org.junit.After; 039import org.junit.Before; 040import org.junit.Test; 041 042/** 043 * @version $Revision: 1541658 $ $Date: 2013-11-13 19:54:05 +0100 (Mi, 13 Nov 2013) $ 044 */ 045public class TestAlgorithms { 046 047 // Lifecycle 048 // ------------------------------------------------------------------------ 049 050 @Before 051 public void setUp() throws Exception { 052 list = new ArrayList<Integer>(); 053 evens = new ArrayList<Integer>(); 054 doubled = new ArrayList<Integer>(); 055 listWithDuplicates = new ArrayList<Integer>(); 056 sum = 0; 057 for (int i = 0; i < 10; i++) { 058 list.add(Integer.valueOf(i)); 059 doubled.add(Integer.valueOf(i * 2)); 060 listWithDuplicates.add(Integer.valueOf(i)); 061 listWithDuplicates.add(Integer.valueOf(i)); 062 sum += i; 063 if (i % 2 == 0) { 064 evens.add(Integer.valueOf(i)); 065 } 066 } 067 } 068 069 @After 070 public void tearDown() throws Exception { 071 list = null; 072 evens = null; 073 listWithDuplicates = null; 074 sum = 0; 075 } 076 077 // Tests 078 // ------------------------------------------------------------------------ 079 080 @Test 081 public void testRun() { 082 Summer summer = new Summer(); 083 IteratorToGeneratorAdapter.adapt(list.iterator()).run(summer); 084 assertEquals(sum, summer.sum); 085 } 086 087 @Test 088 public void testSelect1() { 089 Collection<Integer> result = 090 new FilteredGenerator<Integer>(IteratorToGeneratorAdapter.adapt(list.iterator()), isEven).toCollection(); 091 assertNotNull(result); 092 assertEquals(evens, result); 093 } 094 095 @Test 096 public void testSelect2() { 097 List<Integer> result = new ArrayList<Integer>(); 098 assertSame(result, 099 new FilteredGenerator<Integer>(IteratorToGeneratorAdapter.adapt(list.iterator()), isEven).to(result)); 100 assertEquals(evens, result); 101 } 102 103 @Test 104 public void testReject1() { 105 Collection<Integer> result = 106 new FilteredGenerator<Integer>(IteratorToGeneratorAdapter.adapt(list.iterator()), new Not<Integer>(isOdd)) 107 .toCollection(); 108 assertNotNull(result); 109 assertEquals(evens, result); 110 } 111 112 @Test 113 public void testReject2() { 114 List<Object> result = new ArrayList<Object>(); 115 assertSame(result, new FilteredGenerator<Integer>(IteratorToGeneratorAdapter.adapt(list.iterator()), 116 new Not<Integer>(isOdd)).to(result)); 117 assertEquals(evens, result); 118 } 119 120 @Test 121 public void testApplyToGenerator() { 122 LoopGenerator<Integer> gen = IteratorToGeneratorAdapter.adapt(new IntegerRange(1, 5)); 123 Summer summer = new Summer(); 124 125 new TransformedGenerator<Integer, Integer>(gen, new Doubler()).run(summer); 126 127 assertEquals(2 * (1 + 2 + 3 + 4), summer.sum); 128 } 129 130 @Test 131 public void testApply() { 132 Collection<Integer> result = 133 new TransformedGenerator<Integer, Integer>(IteratorToGeneratorAdapter.adapt(list.iterator()), new Doubler()) 134 .toCollection(); 135 assertNotNull(result); 136 assertEquals(doubled, result); 137 } 138 139 @Test 140 public void testApply2() { 141 Set<Integer> set = new HashSet<Integer>(); 142 assertSame(set, new TransformedGenerator<Integer, Integer>(IteratorToGeneratorAdapter.adapt(list.iterator()), 143 Identity.<Integer> instance()).to(set)); 144 assertEquals(list.size(), set.size()); 145 for (Iterator<Integer> iter = list.iterator(); iter.hasNext();) { 146 assertTrue(set.contains(iter.next())); 147 } 148 } 149 150 @Test 151 public void testApply3() { 152 Set<Object> set = new HashSet<Object>(); 153 assertSame(set, 154 new TransformedGenerator<Object, Object>(IteratorToGeneratorAdapter.adapt(listWithDuplicates.iterator()), 155 Identity.instance()).to(set)); 156 assertTrue(listWithDuplicates.size() > set.size()); 157 for (Iterator<Integer> iter = listWithDuplicates.iterator(); iter.hasNext();) { 158 assertTrue(set.contains(iter.next())); 159 } 160 } 161 162 // Attributes 163 // ------------------------------------------------------------------------ 164 private List<Integer> list = null; 165 private List<Integer> doubled = null; 166 private List<Integer> evens = null; 167 private List<Integer> listWithDuplicates = null; 168 private int sum = 0; 169 private Predicate<Integer> isEven = new Predicate<Integer>() { 170 public boolean test(Integer obj) { 171 return obj.intValue() % 2 == 0; 172 } 173 }; 174 private Predicate<Integer> isOdd = new Predicate<Integer>() { 175 public boolean test(Integer obj) { 176 return obj.intValue() % 2 != 0; 177 } 178 }; 179 180 // Classes 181 // ------------------------------------------------------------------------ 182 183 static class Counter implements NullaryProcedure { 184 public void run() { 185 count++; 186 } 187 188 public int count = 0; 189 } 190 191 static class Summer implements Procedure<Integer> { 192 public void run(Integer that) { 193 sum += that.intValue(); 194 } 195 196 public int sum = 0; 197 } 198 199 static class Doubler implements Function<Integer, Integer> { 200 public Integer evaluate(Integer obj) { 201 return Integer.valueOf(2 * obj.intValue()); 202 } 203 } 204}