MultiTest.java
/***************************************************************************
Copyright 2015 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.functor;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.NoSuchElementException;
public final class MultiTest {
private static void assertTrue(final boolean result) {
if(!result) { throw new AssertionError("Expected true"); }
}
public static void main(final String[] args) throws Exception {
testBlockThunk();
testChain();
testIdentity();
testIteratorApplicator();
testIteratorThunk();
testThunkBlock();
testThunkIterator();
}
public static void testBlockThunk() throws Exception {
final Block<CharSequence,Integer> cvt= new Block<CharSequence,Integer>() {
@Override public Integer eval(final CharSequence v) {
return Integer.valueOf(v.toString());
}
};
final Thunk<String> seq = new Thunk<String>() {
final String[] vals = new String[]{"0", "1", "2", "3"};
int pos = 0;
@Override public String eval() {
final String v = vals[pos];
pos = (pos + 1) % vals.length;
return v;
}
};
final Thunk<Integer> test = new BlockThunk<String,Integer>(cvt, seq);
assertTrue(Integer.valueOf(0).equals(test.eval()));
assertTrue(Integer.valueOf(1).equals(test.eval()));
assertTrue(Integer.valueOf(2).equals(test.eval()));
assertTrue(Integer.valueOf(3).equals(test.eval()));
assertTrue(Integer.valueOf(0).equals(test.eval()));
}
public static final Block<Long, Long> addOne = new Block<Long, Long>() {
@Override public Long eval(final Long v) {
return v.longValue() + 1;
}
};
public static void testChain() throws Exception {
final Block<Number,String> cvt= new Block<Number,String>() {
@Override public String eval(final Number v) {
return v.toString();
}
};
final Block<Long,String> c = new Chain<Long, Number, String>(cvt, addOne);
assertTrue("1".equals(c.eval(0l)));
assertTrue("2".equals(c.eval(1l)));
assertTrue("3".equals(c.eval(2l)));
}
public static void testIdentity() {
assertTrue(Identity.I.eval(null) == null);
assertTrue("A".equals(Identity.I.eval("A")));
final Identity<Integer> i = new Identity<>();
assertTrue(Integer.valueOf(1).equals(i.eval(1)));
assertTrue(Integer.valueOf(2).equals(i.eval(2)));
}
public static void testIteratorApplicator() {
final List<Long> values = Arrays.asList(0l,1l,2l,3l,4l,5l,6l,7l,8l,9l);
final Iterator<Long> it = new IteratorApplicator<Long,Long>(values, addOne);
for(int i=0;i<10;i++) {
assertTrue(it.hasNext());
assertTrue(Long.valueOf(i + 1l).equals(it.next()));
try {
it.remove();
throw new AssertionError("Remove is not supported.");
} catch (final UnsupportedOperationException uoe) {
// Expected
}
}
assertTrue(!it.hasNext());
try {
it.next();
throw new AssertionError("Tried to iterate past end of list.");
} catch (final NoSuchElementException nsee) {
// Expected.
}
}
public static void testIteratorThunk() throws Exception {
final List<Long> values = Arrays.asList(0l,1l,2l,3l,4l,5l,6l,7l,8l,9l);
final Thunk<Long> thunk = new IteratorThunk<>(values);
for(int i=0;i<10;i++) {
assertTrue(Long.valueOf(i).equals(thunk.eval()));
}
final Thunk<Long> thunk2 = new IteratorThunk<>(values.iterator());
for(int i=0;i<10;i++) {
assertTrue(Long.valueOf(i).equals(thunk2.eval()));
}
}
public static void testThunkBlock() throws Exception {
final Thunk<String> seq = new Thunk<String>() {
final String[] vals = new String[]{"0", "1", "2", "3"};
int pos = 0;
@Override public String eval() {
final String v = vals[pos];
pos = (pos + 1) % vals.length;
return v;
}
};
final Block<Object,String> b = new ThunkBlock<String,Object>(seq);
assertTrue("0".equals(b.eval(1)));
assertTrue("1".equals(b.eval(true)));
assertTrue("2".equals(b.eval(null)));
assertTrue("3".equals(b.eval("A")));
assertTrue("0".equals(b.eval(new Object())));
}
public static void testThunkIterator() throws Exception {
final Thunk<Integer> seq = new Thunk<Integer>() {
final Integer[] vals = new Integer[]{0,1,2,3};
int pos = 0;
@Override public Integer eval() {
final Integer v = vals[pos];
pos++;
return v;
}
};
final Iterator<Integer> it = new ThunkIterator<>(seq);
for(int i=0;i<4;i++) {
assertTrue(it.hasNext());
assertTrue(Integer.valueOf(i).equals(it.next()));
try {
it.remove();
throw new AssertionError("Remove is not supported.");
} catch (final UnsupportedOperationException uoe) {
// Expected
}
}
try {
it.next();
throw new AssertionError("This should iterate past the end of the array.");
} catch (final RuntimeException ex) {
// Expected.
}
}
}