001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import junit.framework.*;
036:
037: public class TestBitFormat extends TestCase {
038: public void testDefault() {
039: BitFormat format = new BitFormat();
040:
041: assertEquals("0", "00000000 00000000 00000000 00000000", format
042: .format(0));
043: assertEquals("1", "00000000 00000000 00000000 00000001", format
044: .format(1));
045: assertEquals("-1", "11111111 11111111 11111111 11111111",
046: format.format(-1));
047: assertEquals("20", "00000000 00000000 00000000 00010100",
048: format.format(20));
049: assertEquals("Byte.MAX_VALUE",
050: "00000000 00000000 00000000 01111111", format
051: .format(Byte.MAX_VALUE));
052: assertEquals("Byte.MIN_VALUE",
053: "11111111 11111111 11111111 10000000", format
054: .format(Byte.MIN_VALUE));
055: assertEquals("Short.MAX_VALUE",
056: "00000000 00000000 01111111 11111111", format
057: .format(Short.MAX_VALUE));
058: assertEquals("Short.MIN_VALUE",
059: "11111111 11111111 10000000 00000000", format
060: .format(Short.MIN_VALUE));
061: assertEquals("Integer.MAX_VALUE",
062: "01111111 11111111 11111111 11111111", format
063: .format(Integer.MAX_VALUE));
064: assertEquals("Integer.MIN_VALUE",
065: "10000000 00000000 00000000 00000000", format
066: .format(Integer.MIN_VALUE));
067: }
068:
069: public void testLength4() {
070: BitFormat format = new BitFormat(4);
071:
072: assertEquals("0", "0000", format.format(0));
073: assertEquals("1", "0001", format.format(1));
074: assertEquals("-1", "1111", format.format(-1));
075: assertEquals("20", "0100", format.format(20));
076: assertEquals("Byte.MAX_VALUE", "1111", format
077: .format(Byte.MAX_VALUE));
078: assertEquals("Byte.MIN_VALUE", "0000", format
079: .format(Byte.MIN_VALUE));
080: assertEquals("Short.MAX_VALUE", "1111", format
081: .format(Short.MAX_VALUE));
082: assertEquals("Short.MIN_VALUE", "0000", format
083: .format(Short.MIN_VALUE));
084: assertEquals("Integer.MAX_VALUE", "1111", format
085: .format(Integer.MAX_VALUE));
086: assertEquals("Integer.MIN_VALUE", "0000", format
087: .format(Integer.MIN_VALUE));
088: }
089:
090: public void testLength16() {
091: BitFormat format = new BitFormat(16);
092:
093: assertEquals("0", "00000000 00000000", format.format(0));
094: assertEquals("1", "00000000 00000001", format.format(1));
095: assertEquals("-1", "11111111 11111111", format.format(-1));
096: assertEquals("20", "00000000 00010100", format.format(20));
097: assertEquals("Byte.MAX_VALUE", "00000000 01111111", format
098: .format(Byte.MAX_VALUE));
099: assertEquals("Byte.MIN_VALUE", "11111111 10000000", format
100: .format(Byte.MIN_VALUE));
101: assertEquals("Short.MAX_VALUE", "01111111 11111111", format
102: .format(Short.MAX_VALUE));
103: assertEquals("Short.MIN_VALUE", "10000000 00000000", format
104: .format(Short.MIN_VALUE));
105: assertEquals("Integer.MAX_VALUE", "11111111 11111111", format
106: .format(Integer.MAX_VALUE));
107: assertEquals("Integer.MIN_VALUE", "00000000 00000000", format
108: .format(Integer.MIN_VALUE));
109: }
110:
111: public void testGroups8() {
112: BitFormat format = new BitFormat(BitFormat.DEFAULT_MAX_LENGTH,
113: 4);
114:
115: assertEquals("0", "0000 0000 0000 0000 0000 0000 0000 0000",
116: format.format(0));
117: assertEquals("1", "0000 0000 0000 0000 0000 0000 0000 0001",
118: format.format(1));
119: assertEquals("-1", "1111 1111 1111 1111 1111 1111 1111 1111",
120: format.format(-1));
121: assertEquals("20", "0000 0000 0000 0000 0000 0000 0001 0100",
122: format.format(20));
123: assertEquals("Byte.MAX_VALUE",
124: "0000 0000 0000 0000 0000 0000 0111 1111", format
125: .format(Byte.MAX_VALUE));
126: assertEquals("Byte.MIN_VALUE",
127: "1111 1111 1111 1111 1111 1111 1000 0000", format
128: .format(Byte.MIN_VALUE));
129: assertEquals("Short.MAX_VALUE",
130: "0000 0000 0000 0000 0111 1111 1111 1111", format
131: .format(Short.MAX_VALUE));
132: assertEquals("Short.MIN_VALUE",
133: "1111 1111 1111 1111 1000 0000 0000 0000", format
134: .format(Short.MIN_VALUE));
135: assertEquals("Integer.MAX_VALUE",
136: "0111 1111 1111 1111 1111 1111 1111 1111", format
137: .format(Integer.MAX_VALUE));
138: assertEquals("Integer.MIN_VALUE",
139: "1000 0000 0000 0000 0000 0000 0000 0000", format
140: .format(Integer.MIN_VALUE));
141: }
142:
143: public void testSeparator() {
144: BitFormat format = new BitFormat(BitFormat.DEFAULT_MAX_LENGTH,
145: BitFormat.DEFAULT_GROUP_SIZE, '-');
146:
147: assertEquals("0", "00000000-00000000-00000000-00000000", format
148: .format(0));
149: assertEquals("1", "00000000-00000000-00000000-00000001", format
150: .format(1));
151: assertEquals("-1", "11111111-11111111-11111111-11111111",
152: format.format(-1));
153: assertEquals("20", "00000000-00000000-00000000-00010100",
154: format.format(20));
155: assertEquals("Byte.MAX_VALUE",
156: "00000000-00000000-00000000-01111111", format
157: .format(Byte.MAX_VALUE));
158: assertEquals("Byte.MIN_VALUE",
159: "11111111-11111111-11111111-10000000", format
160: .format(Byte.MIN_VALUE));
161: assertEquals("Short.MAX_VALUE",
162: "00000000-00000000-01111111-11111111", format
163: .format(Short.MAX_VALUE));
164: assertEquals("Short.MIN_VALUE",
165: "11111111-11111111-10000000-00000000", format
166: .format(Short.MIN_VALUE));
167: assertEquals("Integer.MAX_VALUE",
168: "01111111-11111111-11111111-11111111", format
169: .format(Integer.MAX_VALUE));
170: assertEquals("Integer.MIN_VALUE",
171: "10000000-00000000-00000000-00000000", format
172: .format(Integer.MIN_VALUE));
173: }
174: }
|