/**
*
*/
package org.grainednoise.deferred;
import static org.junit.Assert.*;
import org.grainednoise.deferred.core.Tuple;
import org.grainednoise.deferred.core.Tuple.Tuple0;
import org.grainednoise.deferred.core.Tuple.Tuple1;
import org.grainednoise.deferred.core.Tuple.Tuple2;
import org.grainednoise.deferred.core.Tuple.Tuple3;
import org.junit.Test;
/**
* @author grainednoise
*
*/
public class TestTuple {
/**
* Test method for {@link org.grainednoise.deferred.core.Tuple#hashCode()}.
*/
@Test
public void testHashCode() {
Tuple0 tuple0 = Tuple.of();
Tuple1<Object> tuple1 = Tuple.of(null);
Tuple2<Object, Object> tuple2 = Tuple.of(null, null);
assertNotSame(tuple0, tuple1);
assertNotSame(tuple0, tuple2);
assertNotSame(tuple1, tuple2);
assertNotSame(tuple0.hashCode(), tuple1.hashCode());
assertNotSame(tuple0.hashCode(), tuple2.hashCode());
assertNotSame(tuple1.hashCode(), tuple2.hashCode());
}
/**
* Test method for {@link org.grainednoise.deferred.core.Tuple#of()}.
*/
@Test
public void testTuple0() {
Tuple0 tuple0 = Tuple.of();
assertTrue(tuple0 instanceof Tuple);
assertNotNull(tuple0);
assertEquals(0, tuple0.procuctArity());
assertIndexError(tuple0, 0);
assertIndexError(tuple0, -1);
assertIndexError(tuple0, 1);
}
/**
* Test method for {@link org.grainednoise.deferred.core.Tuple#of(java.lang.Object)}.
*/
@Test
public void testTuple1() {
Tuple1<Integer> tuple1 = Tuple.of(42);
assertTrue(tuple1 instanceof Tuple);
assertNotNull(tuple1);
assertEquals(Integer.valueOf(42), tuple1.get1());
assertEquals(Integer.valueOf(42), tuple1.productElement(0));
assertEquals(1, tuple1.procuctArity());
assertIndexError(tuple1, -1);
assertIndexError(tuple1, 1);
}
/**
* Test method for {@link org.grainednoise.deferred.core.Tuple#of(java.lang.Object, java.lang.Object)}.
*/
@Test
public void testTuple2() {
Tuple2<Integer, String> tuple2 = Tuple.of(42, "forty-two");
assertTrue(tuple2 instanceof Tuple);
assertNotNull(tuple2);
assertEquals(Integer.valueOf(42), tuple2.get1());
assertEquals(Integer.valueOf(42), tuple2.productElement(0));
assertEquals("forty-two", tuple2.get2());
assertEquals("forty-two", tuple2.productElement(1));
assertEquals(2, tuple2.procuctArity());
assertIndexError(tuple2, -1);
assertIndexError(tuple2, 2);
}
/**
* Test method for {@link org.grainednoise.deferred.core.Tuple#of(java.lang.Object, java.lang.Object, java.lang.Object)}.
*/
@Test
public void testTuple3() {
Tuple3<Integer, String, Character> tuple3 = Tuple.of(42, "forty-two", '?');
assertTrue(tuple3 instanceof Tuple);
assertNotNull(tuple3);
assertEquals(Integer.valueOf(42), tuple3.get1());
assertEquals(Integer.valueOf(42), tuple3.productElement(0));
assertEquals("forty-two", tuple3.get2());
assertEquals("forty-two", tuple3.productElement(1));
assertEquals(Character.valueOf('?'), tuple3.get3());
assertEquals(Character.valueOf('?'), tuple3.productElement(2));
assertEquals(3, tuple3.procuctArity());
assertIndexError(tuple3, -1);
assertIndexError(tuple3, 3);
}
private static void assertIndexError(Tuple tuple, int index) {
try {
tuple.productElement(index);
fail("Expected element on index " + index + " not to exist");
}
catch (IndexOutOfBoundsException e) {
return;
}
}
}