| 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 | |
| 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.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.tmf.core.ctfadaptor.CtfTmfEventField; |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | |
| 35 | /** |
| 36 | * The class <code>CtfTmfEventFieldTest</code> contains tests for the class |
| 37 | * <code>{@link CtfTmfEventField}</code>. |
| 38 | * |
| 39 | * @author ematkho |
| 40 | * @version 1.0 |
| 41 | */ |
| 42 | @SuppressWarnings("nls") |
| 43 | public class CtfTmfEventFieldTest { |
| 44 | |
| 45 | private static final String ROOT = "root"; |
| 46 | private static final String SEQ = "seq"; |
| 47 | private static final String ARRAY = "array"; |
| 48 | private static final String STR = "str"; |
| 49 | private static final String FLOAT = "float"; |
| 50 | private static final String LEN = "len"; |
| 51 | private static final String INT = "int"; |
| 52 | private static final String NAME = "test"; |
| 53 | private static final String STRUCT = "struct"; |
| 54 | |
| 55 | private StructDefinition fixture; |
| 56 | |
| 57 | /** |
| 58 | * Perform pre-test initialization. |
| 59 | */ |
| 60 | @Before |
| 61 | public void setUp() { |
| 62 | StructDeclaration sDec = new StructDeclaration(1l); |
| 63 | StringDeclaration strDec = new StringDeclaration(); |
| 64 | IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8, |
| 65 | ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); |
| 66 | FloatDeclaration flDec = new FloatDeclaration(8, 24, |
| 67 | ByteOrder.BIG_ENDIAN, 8); |
| 68 | ArrayDeclaration arrDec = new ArrayDeclaration(2, intDec); |
| 69 | SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec); |
| 70 | StructDeclaration structDec = new StructDeclaration(32); |
| 71 | sDec.addField(INT, intDec); |
| 72 | sDec.addField(LEN, intDec); |
| 73 | sDec.addField(FLOAT, flDec); |
| 74 | sDec.addField(STR, strDec); |
| 75 | sDec.addField(ARRAY, arrDec); |
| 76 | sDec.addField(SEQ, seqDec); |
| 77 | structDec.addField(STR,strDec); |
| 78 | structDec.addField(INT, intDec); |
| 79 | sDec.addField(STRUCT, structDec); |
| 80 | fixture = sDec.createDefinition(fixture, ROOT); |
| 81 | int capacity = 2048; |
| 82 | java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(capacity); |
| 83 | for (int i = 0; i < capacity; i++) { |
| 84 | bb.put((byte) 2); |
| 85 | } |
| 86 | bb.position(20); |
| 87 | bb.put((byte) 0); |
| 88 | bb.position(40); |
| 89 | bb.put((byte) 0); |
| 90 | bb.position(0); |
| 91 | fixture.read(new BitBuffer(bb)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testParseField_float() { |
| 99 | FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); |
| 100 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME); |
| 101 | assertEquals("test=9.551467814359616E-38", result.toString()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testParseField_array() { |
| 109 | Definition fieldDef = fixture.lookupArray(ARRAY); |
| 110 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 111 | assertEquals("test=[2, 2]", result.toString()); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testParseField_int() { |
| 119 | Definition fieldDef = fixture.lookupDefinition(INT); |
| 120 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 121 | assertEquals("test=02", result.toString()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testParseField_sequence() { |
| 129 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
| 130 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 131 | assertEquals("test=[2, 2]", result.toString()); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testParseField_sequence_value() { |
| 139 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
| 140 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 141 | assertEquals("[2, 2]", result.getValue().toString()); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 146 | */ |
| 147 | @Test |
| 148 | public void testParseField_string() { |
| 149 | Definition fieldDef = fixture.lookupDefinition(STR); |
| 150 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 151 | assertEquals("test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2", result.toString()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Run the CtfTmfEventField parseField(Definition,String) method test. |
| 156 | */ |
| 157 | @Test |
| 158 | public void testParseField_struct() { |
| 159 | Definition fieldDef = fixture.lookupDefinition(STRUCT); |
| 160 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
| 161 | assertEquals("test=[str=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2, int=02]", result.toString()); |
| 162 | } |
| 163 | } |