KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > TestBitFormat


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import junit.framework.*;
36
37 public class TestBitFormat extends TestCase {
38     public void testDefault() {
39         BitFormat format = new BitFormat();
40
41         assertEquals("0", "00000000 00000000 00000000 00000000", format.format(0));
42         assertEquals("1", "00000000 00000000 00000000 00000001", format.format(1));
43         assertEquals("-1", "11111111 11111111 11111111 11111111", format.format(-1));
44         assertEquals("20", "00000000 00000000 00000000 00010100", format.format(20));
45         assertEquals("Byte.MAX_VALUE", "00000000 00000000 00000000 01111111", format.format(Byte.MAX_VALUE));
46         assertEquals("Byte.MIN_VALUE", "11111111 11111111 11111111 10000000", format.format(Byte.MIN_VALUE));
47         assertEquals("Short.MAX_VALUE", "00000000 00000000 01111111 11111111", format.format(Short.MAX_VALUE));
48         assertEquals("Short.MIN_VALUE", "11111111 11111111 10000000 00000000", format.format(Short.MIN_VALUE));
49         assertEquals("Integer.MAX_VALUE", "01111111 11111111 11111111 11111111", format.format(Integer.MAX_VALUE));
50         assertEquals("Integer.MIN_VALUE", "10000000 00000000 00000000 00000000", format.format(Integer.MIN_VALUE));
51     }
52
53     public void testLength4() {
54         BitFormat format = new BitFormat(4);
55
56         assertEquals("0", "0000", format.format(0));
57         assertEquals("1", "0001", format.format(1));
58         assertEquals("-1", "1111", format.format(-1));
59         assertEquals("20", "0100", format.format(20));
60         assertEquals("Byte.MAX_VALUE", "1111", format.format(Byte.MAX_VALUE));
61         assertEquals("Byte.MIN_VALUE", "0000", format.format(Byte.MIN_VALUE));
62         assertEquals("Short.MAX_VALUE", "1111", format.format(Short.MAX_VALUE));
63         assertEquals("Short.MIN_VALUE", "0000", format.format(Short.MIN_VALUE));
64         assertEquals("Integer.MAX_VALUE", "1111", format.format(Integer.MAX_VALUE));
65         assertEquals("Integer.MIN_VALUE", "0000", format.format(Integer.MIN_VALUE));
66     }
67
68     public void testLength16() {
69         BitFormat format = new BitFormat(16);
70
71         assertEquals("0", "00000000 00000000", format.format(0));
72         assertEquals("1", "00000000 00000001", format.format(1));
73         assertEquals("-1", "11111111 11111111", format.format(-1));
74         assertEquals("20", "00000000 00010100", format.format(20));
75         assertEquals("Byte.MAX_VALUE", "00000000 01111111", format.format(Byte.MAX_VALUE));
76         assertEquals("Byte.MIN_VALUE", "11111111 10000000", format.format(Byte.MIN_VALUE));
77         assertEquals("Short.MAX_VALUE", "01111111 11111111", format.format(Short.MAX_VALUE));
78         assertEquals("Short.MIN_VALUE", "10000000 00000000", format.format(Short.MIN_VALUE));
79         assertEquals("Integer.MAX_VALUE", "11111111 11111111", format.format(Integer.MAX_VALUE));
80         assertEquals("Integer.MIN_VALUE", "00000000 00000000", format.format(Integer.MIN_VALUE));
81     }
82
83     public void testGroups8() {
84         BitFormat format = new BitFormat(BitFormat.DEFAULT_MAX_LENGTH, 4);
85
86         assertEquals("0", "0000 0000 0000 0000 0000 0000 0000 0000", format.format(0));
87         assertEquals("1", "0000 0000 0000 0000 0000 0000 0000 0001", format.format(1));
88         assertEquals("-1", "1111 1111 1111 1111 1111 1111 1111 1111", format.format(-1));
89         assertEquals("20", "0000 0000 0000 0000 0000 0000 0001 0100", format.format(20));
90         assertEquals("Byte.MAX_VALUE", "0000 0000 0000 0000 0000 0000 0111 1111", format.format(Byte.MAX_VALUE));
91         assertEquals("Byte.MIN_VALUE", "1111 1111 1111 1111 1111 1111 1000 0000", format.format(Byte.MIN_VALUE));
92         assertEquals("Short.MAX_VALUE", "0000 0000 0000 0000 0111 1111 1111 1111", format.format(Short.MAX_VALUE));
93         assertEquals("Short.MIN_VALUE", "1111 1111 1111 1111 1000 0000 0000 0000", format.format(Short.MIN_VALUE));
94         assertEquals("Integer.MAX_VALUE", "0111 1111 1111 1111 1111 1111 1111 1111", format.format(Integer.MAX_VALUE));
95         assertEquals("Integer.MIN_VALUE", "1000 0000 0000 0000 0000 0000 0000 0000", format.format(Integer.MIN_VALUE));
96     }
97
98     public void testSeparator() {
99         BitFormat format = new BitFormat(BitFormat.DEFAULT_MAX_LENGTH, BitFormat.DEFAULT_GROUP_SIZE, '-');
100
101         assertEquals("0", "00000000-00000000-00000000-00000000", format.format(0));
102         assertEquals("1", "00000000-00000000-00000000-00000001", format.format(1));
103         assertEquals("-1", "11111111-11111111-11111111-11111111", format.format(-1));
104         assertEquals("20", "00000000-00000000-00000000-00010100", format.format(20));
105         assertEquals("Byte.MAX_VALUE", "00000000-00000000-00000000-01111111", format.format(Byte.MAX_VALUE));
106         assertEquals("Byte.MIN_VALUE", "11111111-11111111-11111111-10000000", format.format(Byte.MIN_VALUE));
107         assertEquals("Short.MAX_VALUE", "00000000-00000000-01111111-11111111", format.format(Short.MAX_VALUE));
108         assertEquals("Short.MIN_VALUE", "11111111-11111111-10000000-00000000", format.format(Short.MIN_VALUE));
109         assertEquals("Integer.MAX_VALUE", "01111111-11111111-11111111-11111111", format.format(Integer.MAX_VALUE));
110         assertEquals("Integer.MIN_VALUE", "10000000-00000000-00000000-00000000", format.format(Integer.MIN_VALUE));
111     }
112 }
113
Popular Tags