| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Matthew Khouzam - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.tracecompass.ctf.core.tests.trace; |
| 13 | |
| 14 | import static org.junit.Assert.assertEquals; |
| 15 | import static org.junit.Assert.assertNotNull; |
| 16 | |
| 17 | import java.util.UUID; |
| 18 | |
| 19 | import org.eclipse.tracecompass.internal.ctf.core.trace.Utils; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | /** |
| 23 | * The class <code>UtilsTest</code> contains tests for the class |
| 24 | * {@link Utils}. |
| 25 | * |
| 26 | * @author ematkho |
| 27 | * @version $Revision: 1.0 $ |
| 28 | */ |
| 29 | public class UtilsTest { |
| 30 | |
| 31 | /** |
| 32 | * Run the UUID makeUUID(byte[]) method test. |
| 33 | */ |
| 34 | @Test |
| 35 | public void testMakeUUID() { |
| 36 | int byteSize = 32; |
| 37 | byte[] bytes = new byte[byteSize]; |
| 38 | for (int i = 0; i < byteSize; i++) { |
| 39 | bytes[i] = (byte) (i); |
| 40 | } |
| 41 | |
| 42 | UUID result = Utils.makeUUID(bytes); |
| 43 | assertNotNull(result); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Run the UUID makeUUID(byte[]) method test. |
| 48 | */ |
| 49 | @Test |
| 50 | public void testMakeUUID_2() { |
| 51 | byte[] bytes = new byte[] { (byte) 1, (byte) 1, (byte) 0, (byte) 0, |
| 52 | (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 1, (byte) 1, |
| 53 | (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; |
| 54 | |
| 55 | UUID result = Utils.makeUUID(bytes); |
| 56 | |
| 57 | assertNotNull(result); |
| 58 | assertEquals(72339069014638592L, result.getLeastSignificantBits()); |
| 59 | assertEquals(72339069014638592L, result.getMostSignificantBits()); |
| 60 | assertEquals("01010000-0000-0000-0101-000000000000", result.toString()); |
| 61 | assertEquals(0, result.variant()); |
| 62 | assertEquals(0, result.version()); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Run the UUID makeUUID(byte[]) method test. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testMakeUUID_3() { |
| 70 | byte[] bytes = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0, |
| 71 | (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, |
| 72 | (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; |
| 73 | |
| 74 | UUID result = Utils.makeUUID(bytes); |
| 75 | |
| 76 | assertNotNull(result); |
| 77 | assertEquals(0L, result.getLeastSignificantBits()); |
| 78 | assertEquals(0L, result.getMostSignificantBits()); |
| 79 | assertEquals("00000000-0000-0000-0000-000000000000", result.toString()); |
| 80 | assertEquals(0, result.variant()); |
| 81 | assertEquals(0, result.version()); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Run the int unsignedCompare(long,long) method test. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testUnsignedCompare() { |
| 89 | long a = 1L; |
| 90 | long b = 1L; |
| 91 | int result; |
| 92 | |
| 93 | result = Utils.unsignedCompare(a, b); |
| 94 | assertEquals(0, result); |
| 95 | |
| 96 | result = Utils.unsignedCompare(0L, 1L); |
| 97 | assertEquals(-1, result); |
| 98 | result = Utils.unsignedCompare(0xFFFFFFFFL, 0x100000000L); |
| 99 | assertEquals(-1, result); |
| 100 | result = Utils.unsignedCompare(-4L, -1L); |
| 101 | assertEquals(-1, result); |
| 102 | result = Utils.unsignedCompare(-0x80000000L, -1L); |
| 103 | assertEquals(-1, result); |
| 104 | result = Utils.unsignedCompare(0x7FFFFFFFFFFFFFFEL, 0x7FFFFFFFFFFFFFFFL); |
| 105 | assertEquals(-1, result); |
| 106 | |
| 107 | result = Utils.unsignedCompare(1L, 0L); |
| 108 | assertEquals(1, result); |
| 109 | result = Utils.unsignedCompare(0x100000000L, 0xFFFFFFFFL); |
| 110 | assertEquals(1, result); |
| 111 | result = Utils.unsignedCompare(-1L, -4L); |
| 112 | assertEquals(1, result); |
| 113 | result = Utils.unsignedCompare(-1L, -0x80000000L); |
| 114 | assertEquals(1, result); |
| 115 | result = Utils.unsignedCompare(0x7FFFFFFFFFFFFFFFL, 0x7FFFFFFFFFFFFFFEL); |
| 116 | assertEquals(1, result); |
| 117 | } |
| 118 | } |