001: /*
002: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.shared.uuid.test;
008:
009: import com.hp.hpl.jena.shared.uuid.Bits;
010:
011: import junit.framework.TestCase;
012:
013: public class TestBits extends TestCase {
014: public void testMask1() {
015: long v = Bits.mask(0, 1);
016: check(0x1L, v);
017: }
018:
019: public void testMask2() {
020: long v = Bits.mask(0, 2);
021: check(0x3L, v);
022: }
023:
024: public void testMask3() {
025: long v = Bits.mask(1, 2);
026: check(0x2L, v);
027: }
028:
029: public void testMask4() {
030: long v = Bits.mask(0, 64);
031: check(-1L, v);
032: }
033:
034: public void testMask5() {
035: long v = Bits.mask(16, 48);
036: check(0x0000FFFFFFFF0000L, v);
037: }
038:
039: public void testMask6() {
040: long v = Bits.mask(16, 64);
041: check(0xFFFFFFFFFFFF0000L, v);
042: }
043:
044: public void testMask7() {
045: long v = Bits.mask(0, 0);
046: check(0L, v);
047: }
048:
049: public void testMaskZero1() {
050: long v = Bits.maskZero(0, 1);
051: check(~0x1L, v);
052: }
053:
054: public void testMaskZero2() {
055: long v = Bits.maskZero(0, 2);
056: check(~0x3L, v);
057: }
058:
059: public void testMaskZero3() {
060: long v = Bits.maskZero(1, 2);
061: check(0xFFFFFFFFFFFFFFFDL, v);
062: }
063:
064: public void testMaskZero4() {
065: long v = Bits.maskZero(0, 64);
066: check(0, v);
067: }
068:
069: public void testMaskZero5() {
070: long v = Bits.maskZero(16, 48);
071: check(0xFFFF00000000FFFFL, v);
072: }
073:
074: public void testMaskZero6() {
075: long v = Bits.maskZero(16, 64);
076: check(0xFFFFL, v);
077: }
078:
079: public void testMaskZero7() {
080: long v = Bits.maskZero(0, 0);
081: check(-1L, v);
082: }
083:
084: public void testClear1() {
085: long v = 0xF0F0;
086: v = Bits.clear(v, 4, 8);
087: String s = Long.toHexString(v);
088: check(0xF000L, v);
089: }
090:
091: public void testClear2() {
092: long v = 0x8000000000000000L;
093: v = Bits.clear(v, 63, 64);
094: String s = Long.toHexString(v);
095: check(0x0L, v);
096: }
097:
098: public void testClear3() {
099: long v = 0xC000000000000000L;
100: v = Bits.clear(v, 63, 64);
101: String s = Long.toHexString(v);
102: check(0x4000000000000000L, v);
103: }
104:
105: public void testClear4() {
106: long v = -1;
107: v = Bits.clear(v, 63, 64);
108: String s = Long.toHexString(v);
109: check(0x7FFFFFFFFFFFFFFFL, v);
110: }
111:
112: public void testClear5() {
113: long v = -1;
114: v = Bits.clear(v, 32, 64);
115: String s = Long.toHexString(v);
116: check(0x00000000FFFFFFFFL, v);
117: }
118:
119: public void testClear6() {
120: long v = -1;
121: v = Bits.clear(v, 0, 32);
122: String s = Long.toHexString(v);
123: check(0xFFFFFFFF00000000L, v);
124: }
125:
126: public void testClear7() {
127: long v = -1L;
128: v = Bits.clear(v, 0, 0);
129: String s = Long.toHexString(v);
130: check(-1L, v);
131: }
132:
133: public void testSet1() {
134: long v = 0x0;
135: v = Bits.set(v, 0, 1);
136: check(1, v);
137: }
138:
139: public void testSet2() {
140: long v = 0x1;
141: v = Bits.set(v, 0, 1);
142: check(1, v);
143: }
144:
145: public void testSet3() {
146: long v = 0xF0;
147: v = Bits.set(v, 0, 1);
148: check(0xF1, v);
149: }
150:
151: public void testSet4() {
152: long v = 0xF0F0F0F0F0F0F0F0L;
153: v = Bits.set(v, 0, 8);
154: check(0xF0F0F0F0F0F0F0FFL, v);
155: }
156:
157: public void testSet5() {
158: long v = 0;
159: v = Bits.set(v, 16, 48);
160: check(0x0000FFFFFFFF0000L, v);
161: }
162:
163: public void testSet6() {
164: long v = 0;
165: v = Bits.set(v, 63, 64);
166: check(0x8000000000000000L, v);
167: }
168:
169: public void testSet7() {
170: long v = 0;
171: v = Bits.set(v, 62, 64);
172: check(0xC000000000000000L, v);
173: }
174:
175: public void testSet8() {
176: long v = 0;
177: v = Bits.set(v, 0, 64);
178: check(-1L, v);
179: }
180:
181: public void testSet9() {
182: long v = 0;
183: v = Bits.set(v, 10, 10);
184: check(0, v);
185: }
186:
187: public void testSetBit1() {
188: long v = 0;
189: v = Bits.set(v, 0);
190: check(1, v);
191: }
192:
193: public void testSetBit2() {
194: long v = 0;
195: v = Bits.set(v, 1);
196: check(2, v);
197: }
198:
199: public void testSetBit3() {
200: long v = 1;
201: v = Bits.set(v, 0);
202: check(1, v);
203: }
204:
205: public void testSetBit4() {
206: long v = -1;
207: v = Bits.set(v, 0);
208: check(-1, v);
209: }
210:
211: public void testSetBit5() {
212: long v = 0;
213: v = Bits.set(v, 62);
214: check(0x4000000000000000L, v);
215: }
216:
217: public void testSetBit6() {
218: long v = 0;
219: v = Bits.set(v, 63);
220: check(0x8000000000000000L, v);
221: }
222:
223: public void testBitTest1() {
224: long v = 0;
225: assertTrue(Bits.test(v, false, 0));
226: }
227:
228: public void testBitTest2() {
229: long v = 1;
230: assertTrue(Bits.test(v, true, 0));
231: }
232:
233: public void testBitTest3() {
234: long v = -1;
235: assertTrue(Bits.test(v, true, 63));
236: }
237:
238: public void testBitTest4() {
239: long v = 0x7FFFFFFFFFFFFFFFL;
240: assertTrue(Bits.test(v, false, 63));
241: }
242:
243: public void testBitsTest1() {
244: long v = 0xFEDCBA9876543210L;
245: assertTrue(Bits.test(v, 0x0, 0, 4));
246: }
247:
248: public void testBitsTest2() {
249: long v = 0xFEDCBA9876543210L;
250: assertTrue(Bits.test(v, 0x10, 0, 8));
251: }
252:
253: public void testBitsTest3() {
254: long v = 0xFEDCBA9876543210L;
255: assertTrue(Bits.test(v, v, 0, 64));
256: }
257:
258: public void testBitsTest4() {
259: long v = 0xFEDCBA9876543210L;
260: assertFalse(Bits.test(v, 0, 0, 64));
261: }
262:
263: public void testBitsTest5() {
264: long v = 0xFEDCBA9876543210L;
265: assertTrue(Bits.test(v, 0x0000BA9876540000L, 16, 48));
266: }
267:
268: public void testAccess1() {
269: long v = -1;
270: v = Bits.access(v, 4, 8);
271: check(0xF0L, v);
272: }
273:
274: public void testAccess2() {
275: long v = 0xFEDCBA9876543210L;
276: v = Bits.access(v, 0, 8);
277: check(0x10L, v);
278: }
279:
280: public void testAccess3() {
281: long v = 0xFEDCBA9876543210L;
282: v = Bits.access(v, 0, 64);
283: check(0xFEDCBA9876543210L, v);
284: }
285:
286: public void testAccess4() {
287: long v = 0xFEDCBA9876543210L;
288: v = Bits.access(v, 62, 64);
289: check(0xC000000000000000L, v);
290: }
291:
292: public void testAccess5() {
293: long v = 0xFEDCBA9876543210L;
294: v = Bits.access(v, 0, 2);
295: check(0L, v);
296: }
297:
298: public void testPack1() {
299: long v = 0;
300: v = Bits.pack(v, 0xFL, 0, 4);
301: check(0xFL, v);
302: }
303:
304: public void testPack2() {
305: long v = 0xF0;
306: v = Bits.pack(v, 0x2, 0, 4);
307: check(0xF2L, v);
308: }
309:
310: public void testPack3() {
311: long v = -1;
312: v = Bits.pack(v, 0x2, 0, 8);
313: check(0xFFFFFFFFFFFFFF02L, v);
314: }
315:
316: public void testPack4() {
317: long v = 0xFFFFFFFF00000000L;
318: v = Bits.pack(v, 0x2, 16, 32);
319: check(0xFFFFFFFF00020000L, v);
320: }
321:
322: public void testPack5() {
323: long v = 0xFFFFFFFF00000000L;
324: v = Bits.pack(v, 0xFFFF, 16, 32);
325: check(0xFFFFFFFFFFFF0000L, v);
326: }
327:
328: public void testUnpack1() {
329: long v = 0xABCDABCDABCDABCDL;
330: v = Bits.unpack(v, 0, 4);
331: check(0xDL, v);
332: }
333:
334: public void testUnpack2() {
335: long v = 0xABCDABCDABCDABCDL;
336: v = Bits.unpack(v, 63, 64);
337: check(1L, v);
338: }
339:
340: public void testUnpack3() {
341: long v = 0xABCDABCDABCDABCDL;
342: v = Bits.unpack(v, 56, 64);
343: check(0xABL, v);
344: }
345:
346: public void testUnpack4() {
347: long v = 0xABCD12345678ABCDL;
348: v = Bits.unpack(v, 32, 48);
349: check(0x1234L, v);
350: }
351:
352: public void testUnpackStr1() {
353: String s = "ABCD";
354: long v = Bits.unpack(s, 0, 4);
355: check(0xABCDL, v);
356: }
357:
358: public void testUnpackStr2() {
359: String s = "ABCD";
360: long v = Bits.unpack(s, 2, 4);
361: check(0xCDL, v);
362: }
363:
364: public void testUnpackStr3() {
365: String s = "ABCD";
366: long v = Bits.unpack(s, 0, 2);
367: check(0xABL, v);
368: }
369:
370: private void check(long expected, long actual) {
371: check(null, expected, actual);
372: }
373:
374: private void check(String msg, long expected, long actual) {
375: if (expected == actual)
376: return;
377: String s = "Expected: " + Long.toHexString(expected)
378: + " : Got: " + Long.toHexString(actual);
379: if (msg != null)
380: s = msg + ": " + s;
381: assertFalse(s, true);
382: }
383: }
384:
385: /*
386: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
387: * All rights reserved.
388: *
389: * Redistribution and use in source and binary forms, with or without
390: * modification, are permitted provided that the following conditions
391: * are met:
392: * 1. Redistributions of source code must retain the above copyright
393: * notice, this list of conditions and the following disclaimer.
394: * 2. Redistributions in binary form must reproduce the above copyright
395: * notice, this list of conditions and the following disclaimer in the
396: * documentation and/or other materials provided with the distribution.
397: * 3. The name of the author may not be used to endorse or promote products
398: * derived from this software without specific prior written permission.
399: *
400: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
401: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
402: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
403: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
404: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
405: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
406: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
407: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
408: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
409: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
410: */
|