ss: Switch the statesystem to the datastore HT
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / backend / historytree / SSIntervalStringReadWriteTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.statesystem.core.tests.backend.historytree;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
14
15 import java.io.File;
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;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import java.util.stream.IntStream;
27
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;
38
39 import com.google.common.collect.ImmutableList;
40 import com.google.common.collect.ImmutableSet;
41 import com.google.common.collect.Sets;
42
43 /**
44 * Test the reading/writing logic in {@link StateSystemInterval}, particularly regarding
45 * the string length limitations.
46 *
47 * @author Alexandre Montplaisir
48 */
49 @RunWith(Parameterized.class)
50 @NonNullByDefault({})
51 public class SSIntervalStringReadWriteTest {
52
53 private static final Charset CHARSET = Charset.forName("UTF-8");
54
55 private int fNbChars;
56 private int fCharLength;
57 private char fChar;
58
59 /**
60 * Parameter generator.
61 *
62 * Generates a combination of all possible string lengths and all possible
63 * character lengths.
64 *
65 * @return The test parameters
66 */
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(
70 ImmutableSet.of(
71 0,
72 10,
73 Byte.MAX_VALUE - 1,
74 (int) Byte.MAX_VALUE,
75 Byte.MAX_VALUE + 1,
76
77 Short.MAX_VALUE / 2 - 1,
78 Short.MAX_VALUE / 2,
79 Short.MAX_VALUE / 2 + 1,
80
81 Short.MAX_VALUE / 3 - 1,
82 Short.MAX_VALUE / 3,
83 Short.MAX_VALUE / 3 + 1,
84
85 Short.MAX_VALUE - 1,
86 (int) Short.MAX_VALUE,
87 Short.MAX_VALUE + 1),
88
89 ImmutableSet.of(1, 2, 3)
90 ));
91
92 return set.stream()
93 .map(List::toArray)
94 .collect(Collectors.toList());
95 }
96
97 /**
98 * Test constructor, take the generated parameters as parameters.
99 *
100 * @param nbChars
101 * The number of characters in the test string.
102 * @param charLength
103 * The length (in bytes) of the UTF-8-encoded form of the
104 * character being used.
105 */
106 public SSIntervalStringReadWriteTest(Integer nbChars, Integer charLength) {
107 fNbChars = nbChars.intValue();
108 fCharLength = charLength.intValue();
109 switch (charLength) {
110 case 1:
111 fChar = 'a';
112 break;
113 case 2:
114 fChar = 'é';
115 break;
116 case 3:
117 fChar = '长'; // "chang" means long / length in Chinese!
118 break;
119 default:
120 throw new IllegalArgumentException();
121 }
122 }
123
124 /**
125 * Test method
126 *
127 * @throws IOException
128 * Fails the test
129 */
130 @Test
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);
137
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 */
141 try {
142 new StateSystemInterval(0, 10, 1, value);
143 } catch (IllegalArgumentException e) {
144 return;
145 }
146 fail();
147 }
148 StateSystemInterval interval = new StateSystemInterval(0, 10, 1, value);
149 writeAndReadInterval(interval);
150 }
151
152 private static void writeAndReadInterval(StateSystemInterval interval) throws IOException {
153 int sizeOnDisk = interval.getSizeOnDisk();
154
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();) {
159
160 ByteBuffer bb = ByteBuffer.allocate(sizeOnDisk);
161 bb.order(ByteOrder.LITTLE_ENDIAN);
162 bb.clear();
163
164 ISafeByteBufferWriter sbbw = SafeByteBufferFactory.wrapWriter(bb, sizeOnDisk);
165 interval.writeSegment(sbbw);
166
167 bb.flip();
168 int written = fc.write(bb);
169 assertEquals(sizeOnDisk, written);
170 }
171
172 /* Read the interval from the file */
173 StateSystemInterval readInterval;
174 try (FileInputStream fis = new FileInputStream(tempFile);
175 FileChannel fc = fis.getChannel();) {
176
177 ByteBuffer bb = ByteBuffer.allocate(sizeOnDisk);
178 bb.order(ByteOrder.LITTLE_ENDIAN);
179 bb.clear();
180
181 int read = fc.read(bb);
182 assertEquals(sizeOnDisk, read);
183 bb.flip();
184
185 ISafeByteBufferReader sbbr = SafeByteBufferFactory.wrapReader(bb, sizeOnDisk);
186 readInterval = StateSystemInterval.DESERIALISER.readInterval(sbbr);
187 }
188
189 assertEquals(interval, readInterval);
190 }
191 }
This page took 0.073211 seconds and 5 git commands to generate.