Commit | Line | Data |
---|---|---|
95bf10e7 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial generation with CodePro tools | |
11 | * Alexandre Montplaisir - Clean up, consolidate redundant tests | |
12 | *******************************************************************************/ | |
13 | ||
81c8e6f7 MK |
14 | package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; |
15 | ||
16 | import static org.junit.Assert.assertEquals; | |
17 | import static org.junit.Assert.assertNotNull; | |
18 | ||
19 | import java.nio.ByteOrder; | |
20 | ||
21 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; | |
22 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
23 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
24 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; | |
25 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; | |
26 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
27 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
28 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; | |
29 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
30 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
31 | import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; | |
32 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventField; | |
33 | import org.junit.After; | |
34 | import org.junit.Before; | |
35 | import org.junit.Test; | |
36 | ||
37 | /** | |
38 | * The class <code>CtfTmfEventFieldTest</code> contains tests for the class | |
39 | * <code>{@link CtfTmfEventField}</code>. | |
40 | * | |
81c8e6f7 | 41 | * @author ematkho |
95bf10e7 | 42 | * @version 1.0 |
81c8e6f7 MK |
43 | */ |
44 | public class CtfTmfEventFieldTest { | |
45 | ||
46 | private static final String ROOT = "root"; //$NON-NLS-1$ | |
47 | private static final String SEQ = "seq"; //$NON-NLS-1$ | |
48 | private static final String ARRAY = "array"; //$NON-NLS-1$ | |
49 | private static final String STR = "str"; //$NON-NLS-1$ | |
50 | private static final String FLOAT = "float"; //$NON-NLS-1$ | |
51 | private static final String LEN = "len"; //$NON-NLS-1$ | |
52 | private static final String INT = "int"; //$NON-NLS-1$ | |
53 | private static final String NAME = "test"; //$NON-NLS-1$ | |
54 | ||
95bf10e7 AM |
55 | private StructDefinition fixture; |
56 | ||
81c8e6f7 | 57 | /** |
95bf10e7 | 58 | * Launch the test. |
81c8e6f7 | 59 | * |
95bf10e7 AM |
60 | * @param args |
61 | * the command line arguments | |
62 | */ | |
63 | public static void main(String[] args) { | |
64 | new org.junit.runner.JUnitCore().run(CtfTmfEventFieldTest.class); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Perform pre-test initialization. | |
69 | */ | |
70 | @Before | |
71 | public void setUp() { | |
72 | StructDeclaration sDec = new StructDeclaration(1l); | |
73 | StringDeclaration strDec = new StringDeclaration(); | |
74 | IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8, | |
75 | ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); | |
76 | FloatDeclaration flDec = new FloatDeclaration(8, 24, | |
77 | ByteOrder.BIG_ENDIAN, 8); | |
78 | ArrayDeclaration arrDec = new ArrayDeclaration(2, intDec); | |
79 | SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec); | |
80 | sDec.addField(INT, intDec); | |
81 | sDec.addField(LEN, intDec); | |
82 | sDec.addField(FLOAT, flDec); | |
83 | sDec.addField(STR, strDec); | |
84 | sDec.addField(ARRAY, arrDec); | |
85 | sDec.addField(SEQ, seqDec); | |
86 | fixture = sDec.createDefinition(fixture, ROOT); | |
87 | int capacity = 1024; | |
88 | java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(capacity); | |
89 | for (int i = 0; i < capacity; i++) { | |
90 | bb.put((byte) 2); | |
91 | } | |
92 | bb.position(20); | |
93 | bb.put((byte) 0); | |
94 | bb.position(0); | |
95 | fixture.read(new BitBuffer(bb)); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Perform post-test clean-up. | |
100 | */ | |
101 | @After | |
102 | public void tearDown() { | |
103 | // Add additional tear down code here | |
104 | } | |
105 | ||
106 | /** | |
107 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
108 | */ |
109 | @Test | |
95bf10e7 AM |
110 | public void testParseField_float() { |
111 | FloatDefinition fieldDef; | |
112 | fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); | |
81c8e6f7 | 113 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_"+NAME); //$NON-NLS-1$ |
95bf10e7 | 114 | String result2 = CtfTmfEventField.copyFrom(result).toString(); |
81c8e6f7 MK |
115 | assertEquals( result2, "test=9.551467814359616E-38"); //$NON-NLS-1$ |
116 | } | |
117 | ||
118 | /** | |
119 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
120 | */ |
121 | @Test | |
95bf10e7 AM |
122 | public void testParseField_array() { |
123 | CtfTmfEventField result; | |
124 | result = CtfTmfEventField.parseField(fixture.lookupArray(ARRAY), NAME); | |
125 | String result2 = CtfTmfEventField.copyFrom(result).toString(); | |
81c8e6f7 MK |
126 | assertEquals( result2, "test={ 2, 2}"); //$NON-NLS-1$ |
127 | } | |
128 | ||
129 | /** | |
130 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
131 | */ |
132 | @Test | |
95bf10e7 | 133 | public void testParseField_int() { |
81c8e6f7 MK |
134 | Definition fieldDef = fixture.lookupDefinition(INT); |
135 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
136 | String result2 =CtfTmfEventField.copyFrom(result).toString(); | |
6c327eec | 137 | assertEquals( result2, "test=02"); //$NON-NLS-1$ |
81c8e6f7 MK |
138 | } |
139 | ||
140 | /** | |
141 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
142 | */ |
143 | @Test | |
95bf10e7 | 144 | public void testParseField_sequence() { |
81c8e6f7 MK |
145 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
146 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
147 | String result2 =CtfTmfEventField.copyFrom(result).toString(); | |
148 | assertEquals( result2, "test={ 2, 2}"); //$NON-NLS-1$ | |
149 | } | |
150 | ||
151 | /** | |
152 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
153 | */ |
154 | @Test | |
95bf10e7 | 155 | public void testParseField_string() { |
81c8e6f7 MK |
156 | Definition fieldDef = fixture.lookupDefinition(STR); |
157 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
158 | String result2 =CtfTmfEventField.copyFrom(result).toString(); | |
159 | assertEquals( result2, "test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2"); //$NON-NLS-1$ | |
160 | } | |
161 | ||
95bf10e7 AM |
162 | /** |
163 | * Test the clone() method. | |
164 | */ | |
81c8e6f7 MK |
165 | @Test |
166 | public void testClone() { | |
167 | Definition fieldDef = fixture.lookupDefinition(STR); | |
168 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
169 | assertNotNull(result.clone()); | |
170 | } | |
95bf10e7 | 171 | } |