01: // ========================================================================
02: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
03: // ------------------------------------------------------------------------
04: // Licensed under the Apache License, Version 2.0 (the "License");
05: // you may not use this file except in compliance with the License.
06: // You may obtain a copy of the License at
07: // http://www.apache.org/licenses/LICENSE-2.0
08: // Unless required by applicable law or agreed to in writing, software
09: // distributed under the License is distributed on an "AS IS" BASIS,
10: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11: // See the License for the specific language governing permissions and
12: // limitations under the License.
13: // ========================================================================
14:
15: package org.mortbay.io;
16:
17: import java.io.ByteArrayInputStream;
18: import java.io.ByteArrayOutputStream;
19:
20: import junit.framework.TestSuite;
21:
22: import org.mortbay.util.IO;
23:
24: /* ------------------------------------------------------------ */
25: /** Util meta Tests.
26: * @author Greg Wilkins (gregw)
27: */
28: public class IOTest extends junit.framework.TestCase {
29: public IOTest(String name) {
30: super (name);
31: }
32:
33: public static junit.framework.Test suite() {
34: TestSuite suite = new TestSuite(IOTest.class);
35: return suite;
36: }
37:
38: /* ------------------------------------------------------------ */
39: /** main.
40: */
41: public static void main(String[] args) {
42: junit.textui.TestRunner.run(suite());
43: }
44:
45: /* ------------------------------------------------------------ */
46: public void testIO() throws InterruptedException {
47: // Only a little test
48: ByteArrayInputStream in = new ByteArrayInputStream(
49: "The quick brown fox jumped over the lazy dog"
50: .getBytes());
51: ByteArrayOutputStream out = new ByteArrayOutputStream();
52:
53: IO.copyThread(in, out);
54: Thread.sleep(1500);
55: System.err.println(out);
56:
57: assertEquals("copyThread", out.toString(),
58: "The quick brown fox jumped over the lazy dog");
59: }
60:
61: /* ------------------------------------------------------------ */
62: public void testStringSpeed() {
63: String s = "012345678901234567890000000000000000000000000";
64: char[] ca = new char[s.length()];
65: int loops = 1000000;
66:
67: long start = System.currentTimeMillis();
68: long result = 0;
69: for (int loop = 0; loop < loops; loop++) {
70: for (int c = s.length(); c-- > 0;)
71: result += s.charAt(c);
72: }
73: long end = System.currentTimeMillis();
74: System.err.println("charAt " + (end - start) + " " + result);
75:
76: start = System.currentTimeMillis();
77: result = 0;
78: for (int loop = 0; loop < loops; loop++) {
79: s.getChars(0, s.length(), ca, 0);
80: for (int c = s.length(); c-- > 0;)
81: result += ca[c];
82: }
83: end = System.currentTimeMillis();
84: System.err.println("getChars " + (end - start) + " " + result);
85:
86: }
87: }
|