| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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 | |
| 14 | package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; |
| 15 | |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | |
| 18 | import java.nio.ByteOrder; |
| 19 | |
| 20 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
| 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.EnumDeclaration; |
| 25 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
| 26 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; |
| 27 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
| 28 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; |
| 29 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
| 30 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
| 31 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 32 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; |
| 33 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventField; |
| 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 | * |
| 41 | * @author ematkho |
| 42 | * @version 1.0 |
| 43 | */ |
| 44 | public class CtfTmfEventFieldTest { |
| 45 | |
| 46 | private static final String ROOT = "root"; |
| 47 | private static final String SEQ = "seq"; |
| 48 | private static final String ARRAY = "array"; |
| 49 | private static final String STR = "str"; |
| 50 | private static final String FLOAT = "float"; |
| 51 | private static final String LEN = "len"; |
| 52 | private static final String INT = "int"; |
| 53 | private static final String NAME = "test"; |
| 54 | private static final String STRUCT = "struct"; |
| 55 | private static final String VARIANT = "variant"; |
| 56 | private static final String ENUM = "enum"; |
| 57 | |
| 58 | private StructDefinition fixture; |
| 59 | |
| 60 | /** |
| 61 | * Perform pre-test initialization. |
| 62 | */ |
| 63 | @Before |
| 64 | public void setUp() { |
| 65 | StructDeclaration sDec = new StructDeclaration(1l); |
| 66 | StringDeclaration strDec = new StringDeclaration(); |
| 67 | IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8, |
| 68 | ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); |
| 69 | FloatDeclaration flDec = new FloatDeclaration(8, 24, |
| 70 | ByteOrder.BIG_ENDIAN, 8); |
| 71 | ArrayDeclaration arrDec = new ArrayDeclaration(2, intDec); |
| 72 | SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec); |
| 73 | StructDeclaration structDec = new StructDeclaration(32); |
| 74 | EnumDeclaration enumDec = new EnumDeclaration(intDec); |
| 75 | VariantDeclaration varDec = new VariantDeclaration(); |
| 76 | sDec.addField(INT, intDec); |
| 77 | sDec.addField(LEN, intDec); |
| 78 | sDec.addField(FLOAT, flDec); |
| 79 | sDec.addField(STR, strDec); |
| 80 | sDec.addField(ARRAY, arrDec); |
| 81 | sDec.addField(SEQ, seqDec); |
| 82 | structDec.addField(STR,strDec); |
| 83 | structDec.addField(INT, intDec); |
| 84 | sDec.addField(STRUCT, structDec); |
| 85 | enumDec.add(0, 1, LEN); |
| 86 | enumDec.add(2, 3, FLOAT); |
| 87 | sDec.addField(ENUM, enumDec); |
| 88 | varDec.addField(LEN, intDec); |
| 89 | varDec.addField(FLOAT, flDec); |
| 90 | varDec.setTag(ENUM); |
| 91 | sDec.addField(VARIANT, varDec); |
| 92 | fixture = sDec.createDefinition(fixture, ROOT); |
| 93 | int capacity = 2048; |
| 94 | java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(capacity); |
| 95 | for (int i = 0; i < capacity; i++) { |
| 96 | bb.put((byte) 2); |
| 97 | } |
| 98 | bb.position(20); |
| 99 | bb.put((byte) 0); |
| 100 | bb.position(40); |
| 101 | bb.put((byte) 0); |
| 102 | bb.position(60); |
| 103 | bb.put((byte) 0); |
| 104 | bb.position(0); |
| 105 | fixture.read(new BitBuffer(bb)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 110 | */ |
| 111 | @Test |
| 112 | public void testParseField_float() { |
| 113 | FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); |
| 114 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME); |
| 115 | assertEquals("test=9.551467814359616E-38", result.toString()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testParseField_array() { |
| 123 | Definition fieldDef = fixture.lookupArray(ARRAY); |
| 124 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 125 | assertEquals("test=[02, 02]", result.toString()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testParseField_int() { |
| 133 | Definition fieldDef = fixture.lookupDefinition(INT); |
| 134 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 135 | assertEquals("test=02", result.toString()); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testParseField_sequence() { |
| 143 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
| 144 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 145 | assertEquals("test=[02, 02]", result.toString()); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testParseField_sequence_value() { |
| 153 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
| 154 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 155 | assertEquals("[2, 2]", result.getValue().toString()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testParseField_string() { |
| 163 | Definition fieldDef = fixture.lookupDefinition(STR); |
| 164 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 165 | assertEquals("test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2", result.toString()); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testParseField_struct() { |
| 173 | Definition fieldDef = fixture.lookupDefinition(STRUCT); |
| 174 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 175 | assertEquals("test=[str=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2, int=02]", result.toString()); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 180 | */ |
| 181 | @Test |
| 182 | public void testParseField_enum() { |
| 183 | Definition fieldDef = fixture.lookupDefinition(ENUM); |
| 184 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 185 | assertEquals("test=float", result.toString()); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testParseField_variant() { |
| 193 | Definition fieldDef = fixture.lookupDefinition(VARIANT); |
| 194 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 195 | assertEquals("test=float=9.551467814359616E-38", result.toString()); |
| 196 | } |
| 197 | } |