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 junit.framework.TestCase;
18:
19: /* ------------------------------------------------------------------------------- */
20: /**
21: *
22: */
23: public class BufferUtilTest extends TestCase {
24:
25: /**
26: * Constructor for BufferUtilTest.
27: * @param arg0
28: */
29: public BufferUtilTest(String arg0) {
30: super (arg0);
31: }
32:
33: public static void main(String[] args) {
34: junit.textui.TestRunner.run(BufferUtilTest.class);
35: }
36:
37: public void testToInt() throws Exception {
38: Buffer buf[] = { new ByteArrayBuffer("0"),
39: new ByteArrayBuffer(" 42 "),
40: new ByteArrayBuffer(" 43abc"),
41: new ByteArrayBuffer("-44"),
42: new ByteArrayBuffer(" - 45;"),
43: new ByteArrayBuffer("-2147483648"),
44: new ByteArrayBuffer("2147483647"), };
45:
46: int val[] = { 0, 42, 43, -44, -45, -2147483648, 2147483647 };
47:
48: for (int i = 0; i < buf.length; i++)
49: assertEquals("t" + i, val[i], BufferUtil.toInt(buf[i]));
50: }
51:
52: public void testPutInt() throws Exception {
53: int val[] = { 0, 42, 43, -44, -45, -2147483648, 2147483647 };
54:
55: String str[] = { "0", "42", "43", "-44", "-45", "-2147483648",
56: "2147483647" };
57:
58: Buffer buffer = new ByteArrayBuffer(12);
59:
60: for (int i = 0; i < val.length; i++) {
61: buffer.clear();
62: BufferUtil.putDecInt(buffer, val[i]);
63: assertEquals("t" + i, str[i], buffer.toString());
64: }
65: }
66:
67: public void testPutHexInt() throws Exception {
68: int val[] = { 0, 42, 43, -44, -45, -2147483648, 2147483647 };
69:
70: String str[] = { "0", "2A", "2B", "-2C", "-2D", "-80000000",
71: "7FFFFFFF" };
72:
73: Buffer buffer = new ByteArrayBuffer(12);
74:
75: for (int i = 0; i < val.length; i++) {
76: buffer.clear();
77: BufferUtil.putHexInt(buffer, val[i]);
78: assertEquals("t" + i, str[i], buffer.toString());
79: }
80: }
81: }
|