3434
3535package net .imglib2 .view ;
3636
37+ import java .util .Arrays ;
38+ import java .util .List ;
3739import java .util .Random ;
3840
41+ import net .imglib2 .img .Img ;
42+ import net .imglib2 .util .ValuePair ;
3943import org .junit .Assert ;
4044import org .junit .Test ;
4145
4751import net .imglib2 .util .Intervals ;
4852import net .imglib2 .util .Pair ;
4953
54+ import static org .junit .Assert .assertArrayEquals ;
55+
5056/**
57+ * Tests {@link Views#concatenate(int, RandomAccessibleInterval[])}
5158 *
5259 * @author Philipp Hanslovsky
5360 *
5461 */
5562public class ConcatenateViewTest
5663{
5764
58- private final long [] dim = new long [] { 3 , 4 , 5 , 6 };
59-
60- private final long divider = 3 ;
61-
62- private final int axis = 3 ;
65+ @ Test
66+ public void testConcatenateSimple () {
67+ // setup
68+ Img <ByteType > a = ArrayImgs .bytes ( new byte []{ 1 , 2 , 3 , 4 }, 2 , 2 );
69+ Img <ByteType > b = ArrayImgs .bytes ( new byte []{ 7 , 8 }, 1 , 2 );
70+ Img <ByteType > expected = ArrayImgs .bytes ( new byte []{ 1 , 2 , 7 , 3 , 4 , 8 }, 3 , 2 );
71+ // process
72+ RandomAccessibleInterval < ByteType > result = Views .concatenate ( 0 , a , b );
73+ // test
74+ assertImageEquals ( expected , result );
75+ }
6376
6477 @ Test
6578 public void testConcatenate ()
79+ {
80+ testConcatenateImpl ( new long [] { 3 , 4 , 5 , 6 }, 3 , 3 );
81+ }
82+
83+ @ Test
84+ public void testConcatenateFirstAxis ()
85+ {
86+ testConcatenateImpl ( new long [] { 6 , 5 , 4 , 3 }, 0 , 3 );
87+ }
88+
89+ private static void testConcatenateImpl ( long [] dim , int axis , long divider )
90+ {
91+ // setup
92+ final Img < ByteType > img = createRandomImage ( dim );
93+ final List < RandomAccessibleInterval < ByteType > > parts = splitImage ( img , axis , divider );
94+ // process
95+ final RandomAccessibleInterval < ByteType > concatenated = Views .concatenate ( axis , parts );
96+ // test
97+ assertImageEquals ( img , concatenated );
98+ }
99+
100+ private static ArrayImg < ByteType , ByteArray > createRandomImage ( long [] dim )
66101 {
67102 final long numElements = Intervals .numElements ( dim );
68103 final Random rng = new Random ();
69104 final byte [] data = new byte [ ( int ) numElements ];
70105 rng .nextBytes ( data );
71- final ArrayImg < ByteType , ByteArray > img = ArrayImgs .bytes ( data , dim );
106+ return ArrayImgs .bytes ( data , dim );
107+ }
72108
109+ private static List < RandomAccessibleInterval < ByteType > > splitImage (
110+ RandomAccessibleInterval < ByteType > img ,
111+ int axis ,
112+ long divider )
113+ {
73114 final long [] min = Intervals .minAsLongArray ( img );
74115 final long [] max = Intervals .maxAsLongArray ( img );
75116 final long [] min1 = min .clone ();
@@ -83,10 +124,14 @@ public void testConcatenate()
83124 final IntervalView < ByteType > interval1 = Views .interval ( img , min1 , max1 );
84125 final IntervalView < ByteType > interval2 = Views .interval ( img , min2 , max2 );
85126
86- final RandomAccessibleInterval < ByteType > concatenated = Views .concatenate ( axis , interval1 , interval2 );
127+ return Arrays .asList ( interval1 , interval2 );
128+ }
87129
88- for ( final Pair < ByteType , ByteType > p : Views .flatIterable ( Views .interval ( Views .pair ( img , concatenated ), img ) ) )
130+ private static void assertImageEquals (
131+ RandomAccessibleInterval < ByteType > expected ,
132+ RandomAccessibleInterval < ByteType > actual )
133+ {
134+ for ( final Pair < ByteType , ByteType > p : Views .interval ( Views .pair ( expected , actual ), expected ) )
89135 Assert .assertEquals ( p .getA ().getInteger (), p .getB ().getInteger () );
90136 }
91-
92137}
0 commit comments