Commit | Line | Data |
---|---|---|
4bd7f2db | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 Ericsson |
4bd7f2db AM |
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 | ||
f357bcd4 | 12 | package org.eclipse.tracecompass.ctf.core.tests.types; |
866e5b51 FC |
13 | |
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
16 | ||
a4fa4e36 | 17 | import java.nio.ByteBuffer; |
866e5b51 FC |
18 | import java.nio.ByteOrder; |
19 | ||
a4fa4e36 | 20 | import org.eclipse.jdt.annotation.NonNull; |
680f9173 | 21 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
22 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
23 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; | |
24 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; | |
25 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
26 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
866e5b51 FC |
27 | import org.junit.Before; |
28 | import org.junit.Test; | |
29 | ||
30 | /** | |
31 | * The class <code>IntegerDefinitionTest</code> contains tests for the class | |
32 | * <code>{@link IntegerDefinition}</code>. | |
284fdee8 | 33 | * |
866e5b51 FC |
34 | * @author ematkho |
35 | * @version $Revision: 1.0 $ | |
36 | */ | |
37 | public class IntegerDefinitionTest { | |
38 | ||
39 | private IntegerDefinition fixture; | |
a4fa4e36 MK |
40 | @NonNull private static final String NAME = "testInt"; |
41 | @NonNull private static final String clockName = "clock"; | |
4a9c1f07 | 42 | |
866e5b51 | 43 | /** |
a34d8eee | 44 | * Perform pre-test initialization. |
a4fa4e36 | 45 | * |
680f9173 | 46 | * @throws CTFException |
a4fa4e36 | 47 | * won't happen |
866e5b51 FC |
48 | */ |
49 | @Before | |
680f9173 | 50 | public void setUp() throws CTFException { |
a4fa4e36 MK |
51 | IntegerDeclaration id = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); |
52 | ByteBuffer byb = ByteBuffer.allocate(128); | |
53 | byb.mark(); | |
54 | byb.putInt(1); | |
55 | byb.reset(); | |
56 | BitBuffer bb = new BitBuffer(byb); | |
57 | fixture = id.createDefinition(null, NAME, bb); | |
866e5b51 FC |
58 | } |
59 | ||
866e5b51 FC |
60 | /** |
61 | * Run the IntegerDefinition(IntegerDeclaration,DefinitionScope,String) | |
62 | * constructor test. | |
63 | */ | |
64 | @Test | |
65 | public void testIntegerDefinition() { | |
a4fa4e36 MK |
66 | IntegerDeclaration declaration = IntegerDeclaration.createDeclaration(1, false, 1, |
67 | ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); | |
866e5b51 | 68 | IDefinitionScope definitionScope = null; |
4a9c1f07 | 69 | String fieldName = ""; |
866e5b51 FC |
70 | |
71 | IntegerDefinition result = new IntegerDefinition(declaration, | |
a4fa4e36 | 72 | definitionScope, fieldName, 1); |
866e5b51 FC |
73 | assertNotNull(result); |
74 | } | |
75 | ||
76 | /** | |
77 | * Run the IntegerDeclaration getDeclaration() method test. | |
78 | */ | |
79 | @Test | |
80 | public void testGetDeclaration() { | |
866e5b51 FC |
81 | IntegerDeclaration result = fixture.getDeclaration(); |
82 | assertNotNull(result); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Run the long getValue() method test. | |
87 | */ | |
88 | @Test | |
89 | public void testGetValue() { | |
866e5b51 | 90 | long result = fixture.getValue(); |
a4fa4e36 | 91 | assertEquals(0L, result); |
866e5b51 FC |
92 | } |
93 | ||
94 | /** | |
95 | * Run the String toString() method test. | |
96 | */ | |
97 | @Test | |
98 | public void testToString() { | |
866e5b51 | 99 | String result = fixture.toString(); |
a4fa4e36 | 100 | assertEquals("0", result); |
866e5b51 | 101 | } |
3b11ddc7 JCK |
102 | |
103 | /** | |
104 | * Run the IntegerDefinition formatNumber(Long, int, boolean) method test | |
105 | * for unsigned values. | |
106 | */ | |
107 | @Test | |
108 | public void testFormatNumber_unsignedLong() { | |
109 | ||
110 | long unsignedLongValue = -64; | |
111 | String result = IntegerDefinition.formatNumber(unsignedLongValue, 10, false); | |
112 | // -64 + 2^64 = 18446744073709551552 | |
113 | assertEquals("18446744073709551552", result); | |
114 | ||
115 | unsignedLongValue = -131940199973272L; | |
116 | result = IntegerDefinition.formatNumber(unsignedLongValue, 10, false); | |
117 | // -131940199973272l + 2^64 = 18446612133509578344 | |
118 | assertEquals("18446612133509578344", result); | |
119 | ||
120 | unsignedLongValue = 123456789L; | |
121 | result = IntegerDefinition.formatNumber(unsignedLongValue, 10, false); | |
122 | assertEquals("123456789", result); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Run the IntegerDefinition formatNumber(Long, int, boolean) method test | |
127 | * for signed values. | |
128 | */ | |
129 | @Test | |
130 | public void testFormatNumber_signedLong() { | |
131 | long signedValue = -64L; | |
132 | String result = IntegerDefinition.formatNumber(signedValue, 10, true); | |
133 | assertEquals("-64", result); | |
134 | ||
135 | signedValue = -131940199973272L; | |
136 | result = IntegerDefinition.formatNumber(signedValue, 10, true); | |
137 | assertEquals("-131940199973272", result); | |
138 | ||
139 | signedValue = 123456789L; | |
140 | result = IntegerDefinition.formatNumber(signedValue, 10, true); | |
141 | assertEquals("123456789", result); | |
142 | } | |
284fdee8 | 143 | } |