Skip to content

Commit bae01e0

Browse files
committed
test fix
1 parent be6396a commit bae01e0

File tree

2 files changed

+89
-40
lines changed

2 files changed

+89
-40
lines changed

src/main/java/io/tiledb/java/api/Attribute.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,23 @@ public void setFillValue(NativeArray value, BigInteger size) throws TileDBError
296296
* @throws TileDBError
297297
*/
298298
public void setFillValue(Object value) throws TileDBError {
299-
int size;
299+
int byteSize;
300300
NativeArray array;
301301

302302
if (value.getClass().isArray()) {
303-
size = Array.getLength(value);
303+
byteSize = Array.getLength(value) * this.type.getNativeSize();
304304
array = new NativeArray(ctx, value, this.type.javaClass());
305305
} else {
306-
if (value instanceof String) size = ((String) value).length();
307-
else size = this.type.getNativeSize();
308-
array = new NativeArray(ctx, size, this.type.javaClass());
306+
if (value instanceof String) byteSize = ((String) value).length();
307+
else byteSize = this.type.getNativeSize();
308+
array = new NativeArray(ctx, byteSize, this.type.javaClass());
309309
array.setItem(0, value);
310310
}
311311

312312
try {
313313
ctx.handleError(
314314
tiledb.tiledb_attribute_set_fill_value(
315-
ctx.getCtxp(), attributep, array.toVoidPointer(), BigInteger.valueOf((long) size)));
315+
ctx.getCtxp(), attributep, array.toVoidPointer(), BigInteger.valueOf(byteSize)));
316316
} catch (TileDBError err) {
317317
throw err;
318318
}
@@ -329,22 +329,25 @@ public void setFillValue(Object value) throws TileDBError {
329329
* @return A pair with the fill value and its size
330330
* @throws TileDBError
331331
*/
332-
public Pair<Object, Long> getFillValue() throws TileDBError {
332+
public Pair<Object, Integer> getFillValue() throws TileDBError {
333333

334334
try (NativeArray value = new NativeArray(ctx, this.type.getNativeSize(), this.type)) {
335335
SWIGTYPE_p_unsigned_long_long size = tiledb.new_ullp();
336336
SWIGTYPE_p_p_void v = tiledb.new_voidpArray(1);
337337

338338
ctx.handleError(tiledb.tiledb_attribute_get_fill_value(ctx.getCtxp(), attributep, v, size));
339-
int extractedSize = tiledb.ullp_value(size).intValue();
339+
int byteSize = tiledb.ullp_value(size).intValue();
340+
int numElements = byteSize / this.type.getNativeSize();
340341

341342
Object fillValue;
342-
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, extractedSize)) {
343-
if (this.isVar()) fillValue = fillValueArray.toJavaArray(extractedSize);
343+
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, numElements)) {
344+
if (this.isVar() || this.getCellValNum() > 1)
345+
fillValue = fillValueArray.toJavaArray(numElements);
344346
else fillValue = fillValueArray.getItem(0);
345347
}
346348

347-
return new Pair(fillValue, tiledb.ullp_value(size));
349+
return new Pair(fillValue, byteSize);
350+
348351
} catch (TileDBError err) {
349352
throw err;
350353
}
@@ -388,16 +391,16 @@ public void setFillValueNullable(NativeArray value, BigInteger size, boolean val
388391
* @throws TileDBError
389392
*/
390393
public void setFillValueNullable(Object value, boolean valid) throws TileDBError {
391-
int size;
394+
int byteSize;
392395
NativeArray array;
393396

394397
if (value.getClass().isArray()) {
395-
size = Array.getLength(value);
398+
byteSize = Array.getLength(value) * this.type.getNativeSize();
396399
array = new NativeArray(ctx, value, this.type.javaClass());
397400
} else {
398-
if (value instanceof String) size = ((String) value).length();
399-
else size = this.type.getNativeSize();
400-
array = new NativeArray(ctx, size, this.type.javaClass());
401+
if (value instanceof String) byteSize = ((String) value).length();
402+
else byteSize = this.type.getNativeSize();
403+
array = new NativeArray(ctx, byteSize, this.type.javaClass());
401404
array.setItem(0, value);
402405
}
403406

@@ -407,7 +410,7 @@ public void setFillValueNullable(Object value, boolean valid) throws TileDBError
407410
ctx.getCtxp(),
408411
attributep,
409412
array.toVoidPointer(),
410-
BigInteger.valueOf(size),
413+
BigInteger.valueOf(byteSize),
411414
valid ? (short) 1 : (short) 0));
412415
} catch (TileDBError err) {
413416
throw err;
@@ -426,7 +429,7 @@ public void setFillValueNullable(Object value, boolean valid) throws TileDBError
426429
* Pair(5, Pair(4, true))
427430
* @throws TileDBError
428431
*/
429-
public Pair<Object, Pair<Long, Boolean>> getFillValueNullable() throws TileDBError {
432+
public Pair<Object, Pair<Integer, Boolean>> getFillValueNullable() throws TileDBError {
430433

431434
try (NativeArray value = new NativeArray(ctx, this.type.getNativeSize(), this.type)) {
432435
NativeArray validArr = new NativeArray(ctx, 1, Datatype.TILEDB_UINT8);
@@ -438,17 +441,19 @@ public Pair<Object, Pair<Long, Boolean>> getFillValueNullable() throws TileDBErr
438441
tiledb.tiledb_attribute_get_fill_value_nullable(
439442
ctx.getCtxp(), attributep, v, size, valid));
440443

441-
int extractedSize = tiledb.ullp_value(size).intValue();
444+
int byteSize = tiledb.ullp_value(size).intValue();
445+
int numElements = byteSize / this.type.getNativeSize();
442446

443447
Object fillValue;
444-
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, extractedSize)) {
445-
if (this.isVar()) fillValue = fillValueArray.toJavaArray(extractedSize);
448+
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, numElements)) {
449+
if (this.isVar() || this.getCellValNum() > 1)
450+
fillValue = fillValueArray.toJavaArray(numElements);
446451
else fillValue = fillValueArray.getItem(0);
447452
}
448453

449454
boolean validBoolean = validArr.getUint8_tArray().getitem(0) == 0 ? false : true;
450455

451-
return new Pair(fillValue, new Pair(tiledb.ullp_value(size), validBoolean));
456+
return new Pair(fillValue, new Pair(byteSize, validBoolean));
452457
} catch (TileDBError err) {
453458
throw err;
454459
}

src/test/java/io/tiledb/java/api/AttributeTest.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import static io.tiledb.java.api.Constants.TILEDB_VAR_NUM;
2828

29-
import java.math.BigInteger;
3029
import org.junit.Assert;
3130
import org.junit.Test;
3231

@@ -69,8 +68,8 @@ public void testAttributeSetFillValue() throws Exception {
6968
a.setFillValue(5);
7069

7170
Assert.assertEquals(5, a.getFillValue().getFirst());
72-
Assert.assertEquals(
73-
BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond());
71+
72+
Assert.assertEquals(a.getType().getNativeSize(), (int) (long) a.getFillValue().getSecond());
7473
}
7574

7675
try (Context ctx = new Context();
@@ -79,8 +78,7 @@ public void testAttributeSetFillValue() throws Exception {
7978
a.setFillValue(5L);
8079

8180
Assert.assertEquals(5L, a.getFillValue().getFirst());
82-
Assert.assertEquals(
83-
BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond());
81+
Assert.assertEquals(a.getType().getNativeSize(), (int) (long) a.getFillValue().getSecond());
8482
}
8583

8684
try (Context ctx = new Context();
@@ -90,7 +88,7 @@ public void testAttributeSetFillValue() throws Exception {
9088

9189
Assert.assertEquals((byte) 'c', a.getFillValue().getFirst());
9290
Assert.assertEquals(
93-
BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond());
91+
(int) (long) a.getType().getNativeSize(), (int) (long) a.getFillValue().getSecond());
9492
}
9593

9694
try (Context ctx = new Context();
@@ -99,8 +97,7 @@ public void testAttributeSetFillValue() throws Exception {
9997
a.setFillValue((byte) 'c');
10098

10199
Assert.assertEquals((byte) 'c', a.getFillValue().getFirst());
102-
Assert.assertEquals(
103-
BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond());
100+
Assert.assertEquals(a.getType().getNativeSize(), (int) (long) a.getFillValue().getSecond());
104101
}
105102

106103
try (Context ctx = new Context();
@@ -111,18 +108,39 @@ public void testAttributeSetFillValue() throws Exception {
111108
a.setFillValue(str);
112109

113110
Assert.assertEquals(str, new String((byte[]) a.getFillValue().getFirst()));
114-
Assert.assertEquals(BigInteger.valueOf(str.length()), a.getFillValue().getSecond());
111+
Assert.assertEquals(str.length(), (int) (long) a.getFillValue().getSecond());
115112
}
116113

117114
try (Context ctx = new Context();
118115
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_INT32)) {
116+
119117
a.setCellValNum(2);
120118

121119
int[] arr = new int[] {1, 2};
122120
a.setFillValue(arr);
123121

124122
Assert.assertArrayEquals(arr, (int[]) a.getFillValue().getFirst());
125-
Assert.assertEquals(BigInteger.valueOf(arr.length), a.getFillValue().getSecond());
123+
Assert.assertEquals(
124+
arr.length * a.getType().getNativeSize(), (int) (long) a.getFillValue().getSecond());
125+
}
126+
}
127+
128+
@Test
129+
public void testAttributeSetFillValueVarSize() throws Exception {
130+
try (Context ctx = new Context();
131+
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_INT32)) {
132+
133+
a.setCellValNum(2);
134+
135+
int[] arr = new int[] {1, 2};
136+
137+
a.setFillValue(arr);
138+
Assert.assertArrayEquals(arr, (int[]) a.getFillValue().getFirst());
139+
140+
a.setCellVar();
141+
142+
a.setFillValue(arr);
143+
Assert.assertArrayEquals(arr, (int[]) a.getFillValue().getFirst());
126144
}
127145
}
128146

@@ -137,8 +155,8 @@ public void testAttributeSetFillValueNullable() throws Exception {
137155

138156
Assert.assertEquals(5, a.getFillValueNullable().getFirst());
139157
Assert.assertEquals(
140-
BigInteger.valueOf(a.getType().getNativeSize()),
141-
a.getFillValueNullable().getSecond().getFirst());
158+
a.getType().getNativeSize(),
159+
(int) (long) a.getFillValueNullable().getSecond().getFirst());
142160

143161
Assert.assertEquals(true, a.getFillValueNullable().getSecond().getSecond());
144162
}
@@ -152,8 +170,8 @@ public void testAttributeSetFillValueNullable() throws Exception {
152170

153171
Assert.assertEquals(5L, a.getFillValueNullable().getFirst());
154172
Assert.assertEquals(
155-
BigInteger.valueOf(a.getType().getNativeSize()),
156-
a.getFillValueNullable().getSecond().getFirst());
173+
a.getType().getNativeSize(),
174+
(int) (long) a.getFillValueNullable().getSecond().getFirst());
157175

158176
Assert.assertEquals(false, a.getFillValueNullable().getSecond().getSecond());
159177
}
@@ -167,8 +185,8 @@ public void testAttributeSetFillValueNullable() throws Exception {
167185

168186
Assert.assertEquals((byte) 'c', a.getFillValueNullable().getFirst());
169187
Assert.assertEquals(
170-
BigInteger.valueOf(a.getType().getNativeSize()),
171-
a.getFillValueNullable().getSecond().getFirst());
188+
a.getType().getNativeSize(),
189+
(int) (long) a.getFillValueNullable().getSecond().getFirst());
172190

173191
Assert.assertEquals(false, a.getFillValueNullable().getSecond().getSecond());
174192
}
@@ -183,20 +201,46 @@ public void testAttributeSetFillValueNullable() throws Exception {
183201

184202
Assert.assertEquals(str, new String((byte[]) a.getFillValueNullable().getFirst()));
185203
Assert.assertEquals(
186-
BigInteger.valueOf(str.length()), a.getFillValueNullable().getSecond().getFirst());
204+
str.length(), (int) (long) a.getFillValueNullable().getSecond().getFirst());
187205
}
188206

189207
try (Context ctx = new Context();
190208
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_INT32)) {
191209
a.setNullable(true);
210+
192211
a.setCellValNum(2);
193212

194213
int[] arr = new int[] {1, 2};
195214
a.setFillValueNullable(arr, true);
196215

197216
Assert.assertArrayEquals(arr, (int[]) a.getFillValueNullable().getFirst());
198217
Assert.assertEquals(
199-
BigInteger.valueOf(arr.length), a.getFillValueNullable().getSecond().getFirst());
218+
arr.length * a.getType().getNativeSize(),
219+
(long) (int) a.getFillValueNullable().getSecond().getFirst());
220+
221+
a.setCellVar();
222+
223+
a.setFillValueNullable(arr, true);
224+
225+
Assert.assertArrayEquals(arr, (int[]) a.getFillValueNullable().getFirst());
226+
Assert.assertEquals(
227+
arr.length * a.getType().getNativeSize(),
228+
(long) (int) a.getFillValueNullable().getSecond().getFirst());
229+
}
230+
}
231+
232+
@Test
233+
public void testAttributeSetFillValueNullableVarSize() throws Exception {
234+
try (Context ctx = new Context();
235+
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_INT32)) {
236+
a.setNullable(true);
237+
238+
a.setCellValNum(2);
239+
240+
int[] arr = new int[] {1, 2};
241+
a.setFillValueNullable(arr, true);
242+
243+
Assert.assertArrayEquals(arr, (int[]) a.getFillValueNullable().getFirst());
200244
}
201245
}
202246

0 commit comments

Comments
 (0)