Skip to content

Commit

Permalink
Merge 33bbcd4 into 3ae6bc1
Browse files Browse the repository at this point in the history
  • Loading branch information
leerho committed Sep 16, 2019
2 parents 3ae6bc1 + 33bbcd4 commit 4105e03
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 123 deletions.
57 changes: 57 additions & 0 deletions src/main/java/org/apache/datasketches/Util.java
Expand Up @@ -26,6 +26,14 @@
import static java.lang.Math.round;
import static org.apache.datasketches.hash.MurmurHash3.hash;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* Common utility functions.
*
Expand Down Expand Up @@ -685,4 +693,53 @@ public static boolean isLessThanUnsigned(final long n1, final long n2) {
return (n1 < n2) ^ ((n1 < 0) != (n2 < 0));
}

//Resources

/**
* Gets the absolute path of the given resource file's shortName.
*
* <p>Note that the ClassLoader.getResource(shortName) returns a URL,
* which can have special characters, e.g., "%20" for spaces. This method
* obtains the URL, converts it to a URI, then does a uri.getPath(), which
* decodes any special characters in the URI path. This is required to make
* obtaining resources operating-system independent.</p>
*
* @param shortFileName the last name in the pathname's name sequence.
* @return the absolute path of the given resource file's shortName.
*/
public static String getResourcePath(final String shortFileName) {
try {
final URL url = Util.class.getClassLoader().getResource(shortFileName);
final URI uri = url.toURI();
final String path = uri.getPath(); //decodes any special characters
return path;
} catch (final NullPointerException | URISyntaxException e) {
throw new SketchesArgumentException("Cannot find resource: " + shortFileName + LS + e);
}
}

/**
* Gets the file defined by the given resource file's shortFileName.
* @param shortFileName the last name in the pathname's name sequence.
* @return the file defined by the given resource file's shortFileName.
*/
public static File getResourceFile(final String shortFileName) {
return new File(getResourcePath(shortFileName));
}

/**
* Returns a byte array of the contents of the file defined by the given resource file's
* shortFileName.
* @param shortFileName the last name in the pathname's name sequence.
* @return a byte array of the contents of the file defined by the given resource file's
* shortFileName.
*/
public static byte[] getResourceBytes(final String shortFileName) {
try {
return Files.readAllBytes(Paths.get(getResourcePath(shortFileName)));
} catch (final IOException e) {
throw new SketchesArgumentException("Cannot read resource: " + shortFileName + LS + e);
}
}

}
70 changes: 44 additions & 26 deletions src/test/java/org/apache/datasketches/UtilTest.java
Expand Up @@ -19,6 +19,7 @@

package org.apache.datasketches;

import static java.lang.Math.pow;
import static org.apache.datasketches.Util.bytesToInt;
import static org.apache.datasketches.Util.bytesToLong;
import static org.apache.datasketches.Util.bytesToString;
Expand All @@ -31,6 +32,8 @@
import static org.apache.datasketches.Util.evenlyLgSpaced;
import static org.apache.datasketches.Util.floorPowerOf2;
import static org.apache.datasketches.Util.floorPowerOfBdouble;
import static org.apache.datasketches.Util.getResourceBytes;
import static org.apache.datasketches.Util.getResourceFile;
import static org.apache.datasketches.Util.intToBytes;
import static org.apache.datasketches.Util.isLessThanUnsigned;
import static org.apache.datasketches.Util.isMultipleOf8AndGT0;
Expand All @@ -42,9 +45,11 @@
import static org.apache.datasketches.Util.pwrLawNextDouble;
import static org.apache.datasketches.Util.simpleIntLog2;
import static org.apache.datasketches.Util.zeroPad;
import static java.lang.Math.pow;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.File;

import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -53,6 +58,7 @@
*/
@SuppressWarnings("javadoc")
public class UtilTest {
private static final String LS = System.getProperty("line.separator");

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkPowerOf2() {
Expand Down Expand Up @@ -170,13 +176,13 @@ public void checkZeroPad() {
long v = 123456789;
String vHex = Long.toHexString(v);
String out = zeroPad(vHex, 16);
println(out);
println("Pad 16, prepend 0: " + out);
}

@Test
public void checkCharacterPad() {
String s = "Sleeping ... ";
String out = characterPad(s, 20, 'z', true);
String s = "Pad 30, postpend z:";
String out = characterPad(s, 30, 'z', true);
println(out);
}

Expand Down Expand Up @@ -321,39 +327,51 @@ public void checkSimpleIntLog2() {
} catch (SketchesArgumentException e) {}
}

//Resources

@Test
public void printlnTest() {
println("PRINTING: "+this.getClass().getName());
print(" Long MAX & MIN: "); print(Long.MAX_VALUE); print(", "); println(Long.MIN_VALUE);
print(" Doubles: "); print(1.2345); print(", "); println(5.4321);
public void resourcefileExists() {
final String shortFileName = "cpc-empty.bin";
final File file = getResourceFile(shortFileName);
assertTrue(file.exists());
}

/**
* @param s value to print
*/
static void println(String s) {
print(s + '\t');
@Test(expectedExceptions = SketchesArgumentException.class)
public void resourceFileNotFound() {
final String shortFileName = "cpc-empty.bin";
getResourceFile(shortFileName + "123");
}

/**
* @param d value to print
*/
static void println(double d) {
print(Double.toString(d) + '\t');
@Test
public void resourceBytesCorrect() {
final String shortFileName = "cpc-empty.bin";
final byte[] bytes = getResourceBytes(shortFileName);
assertTrue(bytes.length == 8);
}

/**
* @param d value to print
*/
static void print(double d) {
print(Double.toString(d));
@Test(expectedExceptions = SketchesArgumentException.class)
public void resourceBytesFileNotFound() {
final String shortFileName = "cpc-empty.bin";
getResourceBytes(shortFileName + "123");
}

@Test
public void printlnTest() {
println("PRINTING: "+this.getClass().getName());
}

static void println(final Object o) {
if (o == null) { print(LS); }
else { print(o.toString() + LS); }
}

/**
* @param s value to print
* @param o value to print
*/
static void print(String s) {
//System.out.print(s); //disable here
static void print(final Object o) {
if (o != null) {
//System.out.print(o.toString()); //disable here
}
}

}
25 changes: 12 additions & 13 deletions src/test/java/org/apache/datasketches/cpc/CpcCBinariesTest.java
Expand Up @@ -19,16 +19,16 @@

package org.apache.datasketches.cpc;

import static org.apache.datasketches.Util.getResourceFile;
import static org.testng.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import org.testng.annotations.Test;

import org.apache.datasketches.memory.MapHandle;
import org.apache.datasketches.memory.Memory;
import org.testng.annotations.Test;

/**
* Checks sketch images obtained from C++.
Expand All @@ -42,7 +42,7 @@ public class CpcCBinariesTest {
@Test
public void checkEmptyBin() {
String fileName = "cpc-empty.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory wmem = mh.get();
println(PreambleUtil.toString(wmem, true));
Expand All @@ -56,7 +56,7 @@ public void checkEmptyBin() {
@Test
public void checkSparseBin() {
String fileName = "cpc-sparse.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
println("CPP GENERATED SKETCH FROM BINARY FILE LgK=11, U0 to U99");
Expand All @@ -82,7 +82,7 @@ public void checkSparseBin() {
@Test
public void checkHybridBin() {
String fileName = "cpc-hybrid.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
println("CPP GENERATED SKETCH FROM BINARY FILE LgK=11, U0 to U199");
Expand All @@ -108,7 +108,7 @@ public void checkHybridBin() {
@Test
public void checkPinnedBin() {
String fileName = "cpc-pinned.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
println("CPP GENERATED SKETCH FROM BINARY FILE LgK=11, U0 to U1999");
Expand All @@ -134,7 +134,7 @@ public void checkPinnedBin() {
@Test
public void checkSlidingBin() {
String fileName = "cpc-sliding.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
println("CPP GENERATED SKETCH FROM BINARY FILE LgK=11, U0 to U19999");
Expand Down Expand Up @@ -162,7 +162,7 @@ public void checkSlidingBin() {
@Test
public void checkEmptyImages() {
String fileName = "cpc-empty.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
int cap = (int) mem.getCapacity();
Expand All @@ -182,7 +182,7 @@ public void checkEmptyImages() {
@Test
public void checkSparseImages() {
String fileName = "cpc-sparse.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
int cap = (int) mem.getCapacity();
Expand All @@ -203,7 +203,7 @@ public void checkSparseImages() {
@Test
public void checkHybridImages() {
String fileName = "cpc-hybrid.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
int cap = (int) mem.getCapacity();
Expand All @@ -224,7 +224,7 @@ public void checkHybridImages() {
@Test
public void checkPinnedImages() {
String fileName = "cpc-pinned.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
int cap = (int) mem.getCapacity();
Expand All @@ -245,7 +245,7 @@ public void checkPinnedImages() {
@Test
public void checkSlidingImages() {
String fileName = "cpc-sliding.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
int cap = (int) mem.getCapacity();
Expand Down Expand Up @@ -304,5 +304,4 @@ static void println(String s) {
//ps.println(s); //disable here
}


}
Expand Up @@ -19,6 +19,7 @@

package org.apache.datasketches.cpc;

import static org.apache.datasketches.Util.getResourceFile;
import static org.testng.Assert.assertTrue;

import java.io.File;
Expand All @@ -27,13 +28,12 @@
import java.nio.ByteOrder;
import java.nio.file.Files;

import org.testng.annotations.Test;

import org.apache.datasketches.SketchesArgumentException;
import org.apache.datasketches.memory.MapHandle;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.WritableMapHandle;
import org.apache.datasketches.memory.WritableMemory;
import org.apache.datasketches.SketchesArgumentException;
import org.testng.annotations.Test;

/**
* @author Lee Rhodes
Expand All @@ -47,13 +47,12 @@ public class SpecialCBinariesTest {
@SuppressWarnings("unused")
public void checkCpc10mBin() {
String fileName = "cpc-10m.bin";
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
File file = getResourceFile(fileName);
try (MapHandle mh = Memory.map(file)) {
Memory mem = mh.get();
try {
CpcSketch sk = CpcSketch.heapify(mem);
} catch (SketchesArgumentException e) {} // Image was truncated by 4 bytes

} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Expand Up @@ -19,17 +19,16 @@

package org.apache.datasketches.kll;

import static org.apache.datasketches.Util.getResourceBytes;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.SketchesArgumentException;
import org.apache.datasketches.tuple.TestUtil;
import org.apache.datasketches.memory.Memory;
import org.testng.annotations.Test;

@SuppressWarnings("javadoc")
public class KllFloatsSketchTest {
Expand Down Expand Up @@ -353,7 +352,7 @@ public void serializeDeserializeOneItem() {

@Test
public void deserializeOneItemV1() throws Exception {
byte[] bytes = TestUtil.readBytesFromFile(getClass().getClassLoader().getResource("kll_sketch_float_one_item_v1.bin").getFile());
byte[] bytes = getResourceBytes("kll_sketch_float_one_item_v1.bin");
KllFloatsSketch sketch = KllFloatsSketch.heapify(Memory.wrap(bytes));
assertFalse(sketch.isEmpty());
assertFalse(sketch.isEstimationMode());
Expand Down

0 comments on commit 4105e03

Please sign in to comment.