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
.statesystem
.core
.backend
.historytree
.HTInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
31 import org
.junit
.Test
;
32 import org
.junit
.runner
.RunWith
;
33 import org
.junit
.runners
.Parameterized
;
34 import org
.junit
.runners
.Parameterized
.Parameters
;
36 import com
.google
.common
.collect
.ImmutableList
;
37 import com
.google
.common
.collect
.ImmutableSet
;
38 import com
.google
.common
.collect
.Sets
;
41 * Test the reading/writing logic in {@link HTInterval}, particularly regarding
42 * the string length limitations.
44 * @author Alexandre Montplaisir
46 @RunWith(Parameterized
.class)
48 public class HTIntervalStringReadWriteTest
{
50 private static final Charset CHARSET
= Charset
.forName("UTF-8");
53 private int fCharLength
;
57 * Parameter generator.
59 * Generates a combination of all possible string lenghts and all possible
62 * @return The test parameters
64 @Parameters(name
= "nb of chars: {0}, char length: {1}")
65 public static Iterable
<Object
[]> getTestParams() {
66 Set
<List
<Integer
>> set
= Sets
.cartesianProduct(ImmutableList
.of(
74 Short
.MAX_VALUE
/ 2 - 1,
76 Short
.MAX_VALUE
/ 2 + 1,
78 Short
.MAX_VALUE
/ 3 - 1,
80 Short
.MAX_VALUE
/ 3 + 1,
83 (int) Short
.MAX_VALUE
,
86 ImmutableSet
.of(1, 2, 3)
91 .collect(Collectors
.toList());
95 * Test constructor, take the generated parameters as parameters.
98 * The number of characters in the test string.
100 * The length (in bytes) of the UTF-8-encoded form of the
101 * character being used.
103 public HTIntervalStringReadWriteTest(Integer nbChars
, Integer charLength
) {
104 fNbChars
= nbChars
.intValue();
105 fCharLength
= charLength
.intValue();
106 switch (charLength
) {
114 fChar
= '长'; // "chang" means long / length in Chinese!
117 throw new IllegalArgumentException();
124 * @throws IOException
128 public void testStringWithChars() throws IOException
{
129 StringBuilder sb
= new StringBuilder();
130 IntStream
.range(0, fNbChars
).forEach(i
-> sb
.append(fChar
));
131 String str
= sb
.toString();
132 assertEquals(fNbChars
, str
.length());
133 assertEquals(fNbChars
* fCharLength
, str
.getBytes(CHARSET
).length
);
135 TmfStateValue value
= TmfStateValue
.newValueString(str
);
136 if (fNbChars
* fCharLength
> Short
.MAX_VALUE
) {
137 /* For sizes that are known to be too long, expect an exception */
139 new HTInterval(0, 10, 1, value
);
140 } catch (IllegalArgumentException e
) {
145 HTInterval interval
= new HTInterval(0, 10, 1, value
);
146 writeAndReadInterval(interval
);
149 private static void writeAndReadInterval(HTInterval interval
) throws IOException
{
150 int sizeOnDisk
= interval
.getSizeOnDisk();
152 /* Write the interval to a file */
153 File tempFile
= File
.createTempFile("test-interval", ".interval");
154 try (FileOutputStream fos
= new FileOutputStream(tempFile
, false);
155 FileChannel fc
= fos
.getChannel();) {
157 ByteBuffer bb
= ByteBuffer
.allocate(sizeOnDisk
);
158 bb
.order(ByteOrder
.LITTLE_ENDIAN
);
161 interval
.writeInterval(bb
);
163 int written
= fc
.write(bb
);
164 assertEquals(sizeOnDisk
, written
);
167 /* Read the interval from the file */
168 HTInterval readInterval
;
169 try (FileInputStream fis
= new FileInputStream(tempFile
);
170 FileChannel fc
= fis
.getChannel();) {
172 ByteBuffer bb
= ByteBuffer
.allocate(sizeOnDisk
);
173 bb
.order(ByteOrder
.LITTLE_ENDIAN
);
176 int read
= fc
.read(bb
);
177 assertEquals(sizeOnDisk
, read
);
179 readInterval
= HTInterval
.readFrom(bb
);
182 assertEquals(interval
, readInterval
);