Skip to content

Commit

Permalink
Merge pull request #243 from apache/minor_fixes_to_main
Browse files Browse the repository at this point in the history
Release Process: fixed some SpotBugs "dead stores" in test code.
  • Loading branch information
leerho authored Dec 10, 2024
2 parents 3350ab9 + 4a71d87 commit f5a6e1c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ datasketches-memory*/.gitignore
# Netbeans project files
nb-configuration.xml

# VSCode project files
**/.vscode/

# Additional tools
.clover/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.datasketches.memory.MurmurHash3.*;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.lang.foreign.Arena;
Expand Down Expand Up @@ -251,7 +252,8 @@ public void checkEmptyOrNullExceptions() {
long[] arr = null;
hash(arr, 1L);
fail();
} catch (final IllegalArgumentException e) { }
}
catch (final IllegalArgumentException e) { }
try {
int[] arr = null; hash(arr, 1L); fail();
} catch (final IllegalArgumentException e) { }
Expand All @@ -261,20 +263,20 @@ public void checkEmptyOrNullExceptions() {
try {
byte[] arr = null; hash(arr, 1L); fail();
} catch (final IllegalArgumentException e) { }

long[] out = new long[2];
try {
long[] out = new long[2];
String in = null; hash(in, 1L, out); fail();
} catch (final IllegalArgumentException e) { }
try {
long[] out = new long[2];
Memory mem = Memory.wrap(new byte[0]);
out = hash(mem, 0L, 4L, 1L, out);
hash(mem, 0L, 4L, 1L, out);
} catch (final IllegalArgumentException e) { }
try (Arena arena = Arena.ofConfined()) {
Memory mem = WritableMemory.allocateDirect(8, 1, ByteOrder.nativeOrder(), memReqSvr, arena);
long[] out = new long[2];
out = hash(mem, 0L, 4L, 1L, out);
} catch (Exception ee) {}
}
assertTrue((out[0] != 0) && (out[1] != 0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,71 +403,53 @@ public void checkDegenerateRegionReturn() {
public void checkAsWritableMemoryRO() {
ByteBuffer bb = ByteBuffer.allocate(64);
WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
WritableMemory wmem = wbuf.asWritableMemory(); //OK
assertNotNull(wmem);

try {
Buffer buf = Buffer.wrap(bb.asReadOnlyBuffer());
wbuf = (WritableBuffer) buf;
wmem = wbuf.asWritableMemory();
wbuf.asWritableMemory();
Assert.fail();
} catch (IllegalArgumentException expected) {
// expected
}
catch (IllegalArgumentException expected) { }
}

@Test
public void checkWritableDuplicateRO() {
ByteBuffer bb = ByteBuffer.allocate(64);
WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
@SuppressWarnings("unused")
WritableBuffer wdup = wbuf.writableDuplicate();

try {
Buffer buf = Buffer.wrap(bb);
wbuf = (WritableBuffer) buf;
@SuppressWarnings("unused")
WritableBuffer wdup2 = wbuf.writableDuplicate();
wbuf.writableDuplicate();
Assert.fail();
} catch (IllegalArgumentException expected) {
// ignore
}
} catch (IllegalArgumentException expected) { }
}

@Test
public void checkWritableRegionRO() {
ByteBuffer bb = ByteBuffer.allocate(64);
WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
@SuppressWarnings("unused")
WritableBuffer wreg = wbuf.writableRegion();

try {
Buffer buf = Buffer.wrap(bb);
wbuf = (WritableBuffer) buf;
@SuppressWarnings("unused")
WritableBuffer wreg2 = wbuf.writableRegion();
wbuf.writableRegion();
Assert.fail();
} catch (IllegalArgumentException expected) {
// ignore
}
} catch (IllegalArgumentException expected) { }
}

@Test
public void checkWritableRegionWithParamsRO() {
ByteBuffer bb = ByteBuffer.allocate(64);
WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
@SuppressWarnings("unused")
WritableBuffer wreg = wbuf.writableRegion(0, 1, wbuf.getTypeByteOrder());

try {
Buffer buf = Buffer.wrap(bb);
wbuf = (WritableBuffer) buf;
@SuppressWarnings("unused")
WritableBuffer wreg2 = wbuf.writableRegion(0, 1, wbuf.getTypeByteOrder());
wbuf.writableRegion(0, 1, wbuf.getTypeByteOrder());
Assert.fail();
} catch (IllegalArgumentException expected) {
// ignore
}
} catch (IllegalArgumentException expected) { }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,19 @@ public void checkWrapWithBO() {
}

@Test
@SuppressWarnings("unused")
public void checkOwnerClientCase() {
WritableMemory owner = WritableMemory.allocate(64);
Memory client1 = owner; //Client1 cannot write (no API)
owner.putInt(0, 1); //But owner can write
((WritableMemory)client1).putInt(0, 2); //Client1 can write, but with explicit effort.
Memory client2 = owner.region(0, owner.getCapacity()); //client2 cannot write (no API)
owner.putInt(0, 3); //But Owner should be able to write
owner.putInt(0, 1); //owner contains 1
Memory client1 = owner; //owner can cast to a read-only alias, but underneath it is a writable MemorySegment
assertEquals(client1.getInt(0), 1); //client contains 1
//client1.putInt(0, 2) //no API to do this
((WritableMemory)client1).putInt(0, 2); //can write to client1, but with explicit cast.
assertEquals(client1.getInt(0), 2); //client1 now contains 2
assertEquals(owner.getInt(0), 2); //so now owner contains 2 also
Memory client2 = owner.region(0, owner.getCapacity()); //create a readOnly region (MemorySegment) from owner
try {
((WritableMemory) client2).putInt(0, 3); //now you cannot make client2 writable
} catch (UnsupportedOperationException e) { }
}

@Test
Expand Down

0 comments on commit f5a6e1c

Please sign in to comment.