1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
.backend
.historytree
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.fail
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.FileOutputStream
;
18 import java
.io
.IOException
;
19 import java
.nio
.ByteBuffer
;
20 import java
.nio
.ByteOrder
;
21 import java
.nio
.channels
.FileChannel
;
22 import java
.nio
.charset
.Charset
;
23 import java
.util
.List
;
25 import java
.util
.stream
.Collectors
;
26 import java
.util
.stream
.IntStream
;
28 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
29 import org
.eclipse
.tracecompass
.internal
.provisional
.datastore
.core
.serialization
.ISafeByteBufferReader
;
30 import org
.eclipse
.tracecompass
.internal
.provisional
.datastore
.core
.serialization
.ISafeByteBufferWriter
;
31 import org
.eclipse
.tracecompass
.internal
.provisional
.datastore
.core
.serialization
.SafeByteBufferFactory
;
32 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.StateSystemInterval
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
34 import org
.junit
.Test
;
35 import org
.junit
.runner
.RunWith
;
36 import org
.junit
.runners
.Parameterized
;
37 import org
.junit
.runners
.Parameterized
.Parameters
;
39 import com
.google
.common
.collect
.ImmutableList
;
40 import com
.google
.common
.collect
.ImmutableSet
;
41 import com
.google
.common
.collect
.Sets
;
44 * Test the reading/writing logic in {@link StateSystemInterval}, particularly regarding
45 * the string length limitations.
47 * @author Alexandre Montplaisir
49 @RunWith(Parameterized
.class)
51 public class SSIntervalStringReadWriteTest
{
53 private static final Charset CHARSET
= Charset
.forName("UTF-8");
56 private int fCharLength
;
60 * Parameter generator.
62 * Generates a combination of all possible string lengths and all possible
65 * @return The test parameters
67 @Parameters(name
= "nb of chars: {0}, char length: {1}")
68 public static Iterable
<Object
[]> getTestParams() {
69 Set
<List
<Integer
>> set
= Sets
.cartesianProduct(ImmutableList
.of(
77 Short
.MAX_VALUE
/ 2 - 1,
79 Short
.MAX_VALUE
/ 2 + 1,
81 Short
.MAX_VALUE
/ 3 - 1,
83 Short
.MAX_VALUE
/ 3 + 1,
86 (int) Short
.MAX_VALUE
,
89 ImmutableSet
.of(1, 2, 3)
94 .collect(Collectors
.toList());
98 * Test constructor, take the generated parameters as parameters.
101 * The number of characters in the test string.
103 * The length (in bytes) of the UTF-8-encoded form of the
104 * character being used.
106 public SSIntervalStringReadWriteTest(Integer nbChars
, Integer charLength
) {
107 fNbChars
= nbChars
.intValue();
108 fCharLength
= charLength
.intValue();
109 switch (charLength
) {
117 fChar
= '长'; // "chang" means long / length in Chinese!
120 throw new IllegalArgumentException();
127 * @throws IOException
131 public void testStringWithChars() throws IOException
{
132 StringBuilder sb
= new StringBuilder();
133 IntStream
.range(0, fNbChars
).forEach(i
-> sb
.append(fChar
));
134 String str
= sb
.toString();
135 assertEquals(fNbChars
, str
.length());
136 assertEquals(fNbChars
* fCharLength
, str
.getBytes(CHARSET
).length
);
138 TmfStateValue value
= TmfStateValue
.newValueString(str
);
139 if (fNbChars
* fCharLength
> Short
.MAX_VALUE
) {
140 /* For sizes that are known to be too long, expect an exception */
142 new StateSystemInterval(0, 10, 1, value
);
143 } catch (IllegalArgumentException e
) {
148 StateSystemInterval interval
= new StateSystemInterval(0, 10, 1, value
);
149 writeAndReadInterval(interval
);
152 private static void writeAndReadInterval(StateSystemInterval interval
) throws IOException
{
153 int sizeOnDisk
= interval
.getSizeOnDisk();
155 /* Write the interval to a file */
156 File tempFile
= File
.createTempFile("test-interval", ".interval");
157 try (FileOutputStream fos
= new FileOutputStream(tempFile
, false);
158 FileChannel fc
= fos
.getChannel();) {
160 ByteBuffer bb
= ByteBuffer
.allocate(sizeOnDisk
);
161 bb
.order(ByteOrder
.LITTLE_ENDIAN
);
164 ISafeByteBufferWriter sbbw
= SafeByteBufferFactory
.wrapWriter(bb
, sizeOnDisk
);
165 interval
.writeSegment(sbbw
);
168 int written
= fc
.write(bb
);
169 assertEquals(sizeOnDisk
, written
);
172 /* Read the interval from the file */
173 StateSystemInterval readInterval
;
174 try (FileInputStream fis
= new FileInputStream(tempFile
);
175 FileChannel fc
= fis
.getChannel();) {
177 ByteBuffer bb
= ByteBuffer
.allocate(sizeOnDisk
);
178 bb
.order(ByteOrder
.LITTLE_ENDIAN
);
181 int read
= fc
.read(bb
);
182 assertEquals(sizeOnDisk
, read
);
185 ISafeByteBufferReader sbbr
= SafeByteBufferFactory
.wrapReader(bb
, sizeOnDisk
);
186 readInterval
= StateSystemInterval
.DESERIALISER
.readInterval(sbbr
);
189 assertEquals(interval
, readInterval
);