source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/join/TestTupleWritable.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 5.8 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.mapred.join;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.DataInputStream;
23import java.io.DataOutputStream;
24
25import java.util.Arrays;
26import java.util.Random;
27
28import junit.framework.TestCase;
29
30import org.apache.hadoop.io.BooleanWritable;
31import org.apache.hadoop.io.BytesWritable;
32import org.apache.hadoop.io.FloatWritable;
33import org.apache.hadoop.io.IntWritable;
34import org.apache.hadoop.io.LongWritable;
35import org.apache.hadoop.io.Text;
36import org.apache.hadoop.io.Writable;
37
38public class TestTupleWritable extends TestCase {
39
40  private TupleWritable makeTuple(Writable[] writs) {
41    Writable[] sub1 = { writs[1], writs[2] };
42    Writable[] sub3 = { writs[4], writs[5] };
43    Writable[] sub2 = { writs[3], new TupleWritable(sub3), writs[6] };
44    Writable[] vals = { writs[0], new TupleWritable(sub1),
45                        new TupleWritable(sub2), writs[7], writs[8],
46                        writs[9] };
47    // [v0, [v1, v2], [v3, [v4, v5], v6], v7, v8, v9]
48    TupleWritable ret = new TupleWritable(vals);
49    for (int i = 0; i < 6; ++i) {
50      ret.setWritten(i);
51    }
52    ((TupleWritable)sub2[1]).setWritten(0);
53    ((TupleWritable)sub2[1]).setWritten(1);
54    ((TupleWritable)vals[1]).setWritten(0);
55    ((TupleWritable)vals[1]).setWritten(1);
56    for (int i = 0; i < 3; ++i) {
57      ((TupleWritable)vals[2]).setWritten(i);
58    }
59    return ret;
60  }
61
62  private int verifIter(Writable[] writs, TupleWritable t, int i) {
63    for (Writable w : t) {
64      if (w instanceof TupleWritable) {
65        i = verifIter(writs, ((TupleWritable)w), i);
66        continue;
67      }
68      assertTrue("Bad value", w.equals(writs[i++]));
69    }
70    return i;
71  }
72
73  public void testIterable() throws Exception {
74    Random r = new Random();
75    Writable[] writs = {
76      new BooleanWritable(r.nextBoolean()),
77      new FloatWritable(r.nextFloat()),
78      new FloatWritable(r.nextFloat()),
79      new IntWritable(r.nextInt()),
80      new LongWritable(r.nextLong()),
81      new BytesWritable("dingo".getBytes()),
82      new LongWritable(r.nextLong()),
83      new IntWritable(r.nextInt()),
84      new BytesWritable("yak".getBytes()),
85      new IntWritable(r.nextInt())
86    };
87    TupleWritable t = new TupleWritable(writs);
88    for (int i = 0; i < 6; ++i) {
89      t.setWritten(i);
90    }
91    verifIter(writs, t, 0);
92  }
93
94  public void testNestedIterable() throws Exception {
95    Random r = new Random();
96    Writable[] writs = {
97      new BooleanWritable(r.nextBoolean()),
98      new FloatWritable(r.nextFloat()),
99      new FloatWritable(r.nextFloat()),
100      new IntWritable(r.nextInt()),
101      new LongWritable(r.nextLong()),
102      new BytesWritable("dingo".getBytes()),
103      new LongWritable(r.nextLong()),
104      new IntWritable(r.nextInt()),
105      new BytesWritable("yak".getBytes()),
106      new IntWritable(r.nextInt())
107    };
108    TupleWritable sTuple = makeTuple(writs);
109    assertTrue("Bad count", writs.length == verifIter(writs, sTuple, 0));
110  }
111
112  public void testWritable() throws Exception {
113    Random r = new Random();
114    Writable[] writs = {
115      new BooleanWritable(r.nextBoolean()),
116      new FloatWritable(r.nextFloat()),
117      new FloatWritable(r.nextFloat()),
118      new IntWritable(r.nextInt()),
119      new LongWritable(r.nextLong()),
120      new BytesWritable("dingo".getBytes()),
121      new LongWritable(r.nextLong()),
122      new IntWritable(r.nextInt()),
123      new BytesWritable("yak".getBytes()),
124      new IntWritable(r.nextInt())
125    };
126    TupleWritable sTuple = makeTuple(writs);
127    ByteArrayOutputStream out = new ByteArrayOutputStream();
128    sTuple.write(new DataOutputStream(out));
129    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
130    TupleWritable dTuple = new TupleWritable();
131    dTuple.readFields(new DataInputStream(in));
132    assertTrue("Failed to write/read tuple", sTuple.equals(dTuple));
133  }
134
135  public void testWideTuple() throws Exception {
136    Text emptyText = new Text("Should be empty");
137    Writable[] values = new Writable[64];
138    Arrays.fill(values,emptyText);
139    values[42] = new Text("Number 42");
140                                     
141    TupleWritable tuple = new TupleWritable(values);
142    tuple.setWritten(42);
143   
144    for (int pos=0; pos<tuple.size();pos++) {
145      boolean has = tuple.has(pos);
146      if (pos == 42) {
147        assertTrue(has);
148      }
149      else {
150        assertFalse("Tuple position is incorrectly labelled as set: " + pos, has);
151      }
152    }
153  }
154 
155  public void testWideTuple2() throws Exception {
156    Text emptyText = new Text("Should be empty");
157    Writable[] values = new Writable[64];
158    Arrays.fill(values,emptyText);
159    values[9] = new Text("Number 9");
160                                     
161    TupleWritable tuple = new TupleWritable(values);
162    tuple.setWritten(9);
163   
164    for (int pos=0; pos<tuple.size();pos++) {
165      boolean has = tuple.has(pos);
166      if (pos == 9) {
167        assertTrue(has);
168      }
169      else {
170        assertFalse("Tuple position is incorrectly labelled as set: " + pos, has);
171      }
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.