source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/TestText.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: 8.1 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 */
18
19package org.apache.hadoop.io;
20
21import junit.framework.TestCase;
22
23import java.nio.ByteBuffer;
24import java.nio.charset.CharacterCodingException;
25import java.util.Random;
26
27/** Unit tests for LargeUTF8. */
28public class TestText extends TestCase {
29  private static final int NUM_ITERATIONS = 100;
30  public TestText(String name) { super(name); }
31
32  private static final Random RANDOM = new Random(1);
33
34  private static final int RAND_LEN = -1;
35 
36  // generate a valid java String
37  private static String getTestString(int len) throws Exception {
38    StringBuffer buffer = new StringBuffer();   
39    int length = (len==RAND_LEN) ? RANDOM.nextInt(1000) : len;
40    while (buffer.length()<length) {
41      int codePoint = RANDOM.nextInt(Character.MAX_CODE_POINT);
42      char tmpStr[] = new char[2];
43      if (Character.isDefined(codePoint)) {
44        //unpaired surrogate
45        if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT &&
46            !Character.isHighSurrogate((char)codePoint) &&
47            !Character.isLowSurrogate((char)codePoint)) {
48          Character.toChars(codePoint, tmpStr, 0);
49          buffer.append(tmpStr);
50        }
51      }
52    }
53    return buffer.toString();
54  }
55 
56  public static String getTestString() throws Exception {
57    return getTestString(RAND_LEN);
58  }
59 
60  public static String getLongString() throws Exception {
61    String str = getTestString();
62    int length = Short.MAX_VALUE+str.length();
63    StringBuffer buffer = new StringBuffer();
64    while(buffer.length()<length)
65      buffer.append(str);
66     
67    return buffer.toString();
68  }
69
70  public void testWritable() throws Exception {
71    for (int i = 0; i < NUM_ITERATIONS; i++) {
72      String str;
73      if (i == 0)
74        str = getLongString();
75      else
76        str = getTestString();
77      TestWritable.testWritable(new Text(str));
78    }
79  }
80
81
82  public void testCoding() throws Exception {
83    String before = "Bad \t encoding \t testcase";
84    Text text = new Text(before);
85    String after = text.toString();
86    assertTrue(before.equals(after));
87
88    for (int i = 0; i < NUM_ITERATIONS; i++) {
89      // generate a random string
90      if (i == 0)
91        before = getLongString();
92      else
93        before = getTestString();
94   
95      // test string to utf8
96      ByteBuffer bb = Text.encode(before);
97         
98      byte[] utf8Text = bb.array();
99      byte[] utf8Java = before.getBytes("UTF-8");
100      assertEquals(0, WritableComparator.compareBytes(
101                                                      utf8Text, 0, bb.limit(),
102                                                      utf8Java, 0, utf8Java.length));
103             
104      // test utf8 to string
105      after = Text.decode(utf8Java);
106      assertTrue(before.equals(after));
107    }
108  }
109 
110 
111  public void testIO() throws Exception {
112    DataOutputBuffer out = new DataOutputBuffer();
113    DataInputBuffer in = new DataInputBuffer();
114
115    for (int i = 0; i < NUM_ITERATIONS; i++) {
116      // generate a random string
117      String before;         
118      if (i == 0)
119        before = getLongString();
120      else
121        before = getTestString();
122       
123      // write it
124      out.reset();
125      Text.writeString(out, before);
126       
127      // test that it reads correctly
128      in.reset(out.getData(), out.getLength());
129      String after = Text.readString(in);
130      assertTrue(before.equals(after));
131       
132      // Test compatibility with Java's other decoder
133      int strLenSize = WritableUtils.getVIntSize(Text.utf8Length(before));
134      String after2 = new String(out.getData(), strLenSize, 
135                                 out.getLength()-strLenSize, "UTF-8");
136      assertTrue(before.equals(after2));
137    }
138  }
139
140  public void testCompare() throws Exception {
141    DataOutputBuffer out1 = new DataOutputBuffer();
142    DataOutputBuffer out2 = new DataOutputBuffer();
143    DataOutputBuffer out3 = new DataOutputBuffer();
144    Text.Comparator comparator = new Text.Comparator();
145    for (int i=0; i<NUM_ITERATIONS; i++) {
146      // reset output buffer
147      out1.reset();
148      out2.reset();
149      out3.reset();
150
151      // generate two random strings
152      String str1 = getTestString();
153      String str2 = getTestString();
154      if (i == 0) {
155        str1 = getLongString();
156        str2 = getLongString();
157      } else {
158        str1 = getTestString();
159        str2 = getTestString();
160      }
161         
162      // convert to texts
163      Text txt1 = new Text(str1);
164      Text txt2 = new Text(str2);
165      Text txt3 = new Text(str1);
166         
167      // serialize them
168      txt1.write(out1);
169      txt2.write(out2);
170      txt3.write(out3);
171         
172      // compare two strings by looking at their binary formats
173      int ret1 = comparator.compare(out1.getData(), 0, out1.getLength(),
174                                    out2.getData(), 0, out2.getLength());
175      // compare two strings
176      int ret2 = txt1.compareTo(txt2);
177         
178      assertEquals(ret1, ret2);
179         
180      // test equal
181      assertEquals(txt1.compareTo(txt3), 0);
182      assertEquals(comparator.compare(out1.getData(), 0, out3.getLength(),
183                                      out3.getData(), 0, out3.getLength()), 0);
184    }
185  }
186     
187  public void testFind() throws Exception {
188    Text text = new Text("abcd\u20acbdcd\u20ac");
189    assertTrue(text.find("abd")==-1);
190    assertTrue(text.find("ac")==-1);
191    assertTrue(text.find("\u20ac")==4);
192    assertTrue(text.find("\u20ac", 5)==11);
193  }
194
195  public void testFindAfterUpdatingContents() throws Exception {
196    Text text = new Text("abcd");
197    text.set("a".getBytes());
198    assertEquals(text.getLength(),1);
199    assertEquals(text.find("a"), 0);
200    assertEquals(text.find("b"), -1);
201  }
202
203  public void testValidate() throws Exception {
204    Text text = new Text("abcd\u20acbdcd\u20ac");
205    byte [] utf8 = text.getBytes();
206    int length = text.getLength();
207    Text.validateUTF8(utf8, 0, length);
208  }
209
210  public void testTextText() throws CharacterCodingException {
211    Text a=new Text("abc");
212    Text b=new Text("a");
213    b.set(a);
214    assertEquals("abc", b.toString());
215    a.append("xdefgxxx".getBytes(), 1, 4);
216    assertEquals("modified aliased string", "abc", b.toString());
217    assertEquals("appended string incorrectly", "abcdefg", a.toString());
218  }
219 
220  private class ConcurrentEncodeDecodeThread extends Thread {
221    public ConcurrentEncodeDecodeThread(String name) {
222      super(name);
223    }
224
225    public void run() {
226      String name = this.getName();
227      DataOutputBuffer out = new DataOutputBuffer();
228      DataInputBuffer in = new DataInputBuffer();
229      for (int i=0; i < 1000; ++i) {
230        try {
231          out.reset();
232          WritableUtils.writeString(out, name);
233         
234          in.reset(out.getData(), out.getLength());
235          String s = WritableUtils.readString(in);
236         
237          assertEquals(name, s);
238        } catch (Exception ioe) {
239          throw new RuntimeException(ioe);
240        }
241      }
242    }
243  }
244 
245  public void testConcurrentEncodeDecode() throws Exception{
246    Thread thread1 = new ConcurrentEncodeDecodeThread("apache");
247    Thread thread2 = new ConcurrentEncodeDecodeThread("hadoop");
248   
249    thread1.start();
250    thread2.start();
251   
252    thread2.join();
253    thread2.join();
254  }
255
256  public static void main(String[] args)  throws Exception
257  {
258    TestText test = new TestText("main");
259    test.testIO();
260    test.testCompare();
261    test.testCoding();
262    test.testWritable();
263    test.testFind();
264    test.testValidate();
265  }
266}
Note: See TracBrowser for help on using the repository browser.