Skip to content
Permalink
Browse files

Unit test that actually tests a logical unit

  • Loading branch information...
joshbooks committed Mar 23, 2019
1 parent 3fbf7d8 commit aae74fd6d10eb71928fe626ab30f4d35bb87e9f6
@@ -1,5 +1,6 @@
package org.josh.JoshDB.FileTrie; package org.josh.JoshDB.FileTrie;


import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;


@@ -28,7 +29,7 @@
{ {
private volatile long[][] masterList; private volatile long[][] masterList;
// atomic updater for the master list, we use this instead of an // atomic updater for the master list, we use this instead of an
// AtomicReference, because we don't always need to access the masterList // AtomicReference because we don't always need to access the masterList
// atomically // atomically
private static final AtomicReferenceFieldUpdater private static final AtomicReferenceFieldUpdater
< <
@@ -52,7 +53,7 @@
// 001 -> 10, 11 // 001 -> 10, 11
// 010 -> 100, 101, 110, 111 // 010 -> 100, 101, 110, 111
// 111 -> 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 // 111 -> 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111

private static final int INT_BITS = Integer.BYTES * 8; private static final int INT_BITS = Integer.BYTES * 8;


/** /**
@@ -117,7 +118,7 @@ void ensureMasterListLongEnough(int requiredOffset)
int localMax = maxRequestedShallowLength.get(); int localMax = maxRequestedShallowLength.get();


boolean didSucceed = false; boolean didSucceed = false;
//set localMax to the deep Array Length we'll have after resizing // set localMax to the deep Array Length we'll have after resizing
while (localMax < requiredLength) while (localMax < requiredLength)
{ {
didSucceed = maxRequestedShallowLength.compareAndSet(localMax, requiredLength); didSucceed = maxRequestedShallowLength.compareAndSet(localMax, requiredLength);
@@ -145,7 +146,7 @@ void ensureMasterListLongEnough(int requiredOffset)
while (masterList.length < localMax); while (masterList.length < localMax);




//then add our chunks // then add our chunks
// to do that we want to atomically swap out the master // to do that we want to atomically swap out the master
// list in case any other requests have the same required // list in case any other requests have the same required
// length // length
@@ -164,7 +165,11 @@ void ensureMasterListLongEnough(int requiredOffset)
{ {
int sizeForArray = sizeOfSubArray(i); int sizeForArray = sizeOfSubArray(i);


replacement[i] = new long[sizeForArray]; long[] subArrayReplacement = new long[sizeForArray];
Arrays.fill(subArrayReplacement, -1);

//replacement[i] = new long[sizeForArray];
replacement[i] = subArrayReplacement;
} }
} }
while while
@@ -186,7 +191,14 @@ public long get(int index)
{ {
int highestBit = highestSetBit(index); int highestBit = highestSetBit(index);
int subIndex = index == 0 ? 0 : index ^ highestBit; int subIndex = index == 0 ? 0 : index ^ highestBit;
return masterList[highestBit][subIndex]; try
{
return masterList[highestBit][subIndex];
}
catch (ArrayIndexOutOfBoundsException e)
{
return -2;
}
} }


public void set(int index, long contents) public void set(int index, long contents)
@@ -73,8 +73,9 @@
private static native long appendToFile(long fd, byte[] bytes, long numBytes); private static native long appendToFile(long fd, byte[] bytes, long numBytes);


// could do this in native code but like nah // could do this in native code but like nah
private long appendToFileHelper(byte[] bytes) throws IOException long appendToFileHelper(byte[] bytes) throws IOException
{ {
assert bytes.length <= PIPE_BUF;
System.out.println("starting with fd " + fd.get()); System.out.println("starting with fd " + fd.get());
if (fd.get() == null || fd.get() <= 0) if (fd.get() == null || fd.get() <= 0)
{ {
@@ -85,13 +86,17 @@ private long appendToFileHelper(byte[] bytes) throws IOException
} }
} }


return long retVal =
appendToFile appendToFile
( (
fd.get(), fd.get(),
bytes, bytes,
bytes.length bytes.length
); );

assert retVal == bytes.length;

return retVal;
} }


// todo it seems like instantiation is pretty much just done to implement iterator // todo it seems like instantiation is pretty much just done to implement iterator
@@ -38,12 +38,12 @@ public void testRelativeSpeed()


AtomicResizingLongArray testThing = new AtomicResizingLongArray(); AtomicResizingLongArray testThing = new AtomicResizingLongArray();


for (int i = 0; i < 31337; i++) for (int i = 0; i < 0x1000000; i++)
{ {
testThing.set(i, (i * (Long.MAX_VALUE >> 4))); testThing.set(i, (i * (Long.MAX_VALUE >> 4)));
} }


for (int i = 0; i < 31337; i++) for (int i = 0; i < 0x1000000; i++)
{ {
if (testThing.get(i) != (i * (Long.MAX_VALUE >> 4))) if (testThing.get(i) != (i * (Long.MAX_VALUE >> 4)))
{ {

0 comments on commit aae74fd

Please sign in to comment.
You can’t perform that action at this time.