Java源码示例:com.cedarsoftware.util.DeepEquals
示例1
@Test
public void testUpdateAfterSetLeafNodeWorks() throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
final Semaphore semaphore = new Semaphore(0);
final Map<String, Object> expected = new MapBuilder().put("a", 1L).put("b", 2L).build();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (DeepEquals.deepEquals(snapshot.getValue(), expected)) {
semaphore.release();
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
ref.setValueAsync(42);
ref.updateChildrenAsync(expected);
TestHelpers.waitFor(semaphore);
}
示例2
public void add(final Query query, final Object expected) {
count++;
ValueEventListener listener =
query.addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Object result = snapshot.getValue();
// Hack to handle race condition in initial data
if (DeepEquals.deepEquals(expected, result)) {
// We may pass through intermediate states, but we should end up with
// the correct
// state
semaphore.release(1);
}
}
@Override
public void onCancelled(DatabaseError error) {
fail("Listen cancelled");
}
});
expectations.add(new QueryAndListener(query, listener));
}
示例3
@AfterClass
public static void compare() {
System.out.println("USING SAMPLE #: " + SAMPLE_WEEK);
assertEquals(napiSamples.size(), simpleSamples.size());
if(doPrintout) {
System.out.println("\n--------------------------------");
for(Iterator<SampleWeek> it = simpleSamples.iterator(), it2 = napiSamples.iterator();it.hasNext() && it2.hasNext();) {
SampleWeek sw1 = it.next();
SampleWeek sw2 = it2.next();
System.out.println("Seq#: " + sw1.seqNum + " - " + sw2.seqNum);
System.out.println("Encoder: " + Arrays.toString(sw1.encoderOut) + " - " + Arrays.toString(sw2.encoderOut));
System.out.println("SP: " + Arrays.toString(sw1.spOut) + " - " + Arrays.toString(sw2.spOut));
System.out.println("TM (in): " + Arrays.toString(sw1.tmIn) + " - " + Arrays.toString(sw2.tmIn));
System.out.println("TM (pred. cols): " + Arrays.toString(sw1.tmPred) + " - " + Arrays.toString(sw2.tmPred));
System.out.println("TM (Active Cells): " + Arrays.toString(sw1.activeCells) + " - " + Arrays.toString(sw2.activeCells));
System.out.println("TM (Predictive Cells): " + Arrays.toString(sw1.predictiveCells) + " - " + Arrays.toString(sw2.predictiveCells));
System.out.println("Anomaly Score: " + sw1.score + " - " + sw2.score);
System.out.println("");
}
}
assertTrue(DeepEquals.deepEquals(simpleSamples, napiSamples));
}
示例4
@Test
public void testThreadedPublisher_TemporalMemoryNetwork() {
Network network = createAndRunTestTemporalMemoryNetwork();
Layer<?> l = network.lookup("r1").lookup("1");
Connections cn = l.getConnections();
SerialConfig config = new SerialConfig("testThreadedPublisher_TemporalMemoryNetwork", SerialConfig.SERIAL_TEST_DIR);
PersistenceAPI api = Persistence.get(config);
byte[] bytes = api.write(cn);
Connections serializedConnections = api.read(bytes);
Network network2 = createAndRunTestTemporalMemoryNetwork();
Layer<?> l2 = network2.lookup("r1").lookup("1");
Connections newCons = l2.getConnections();
boolean b = DeepEquals.deepEquals(newCons, serializedConnections);
deepCompare(newCons, serializedConnections);
assertTrue(b);
}
示例5
@Test
public void testThreadedPublisher_SpatialPoolerNetwork() {
Network network = createAndRunTestSpatialPoolerNetwork(0, 6);
Layer<?> l = network.lookup("r1").lookup("1");
Connections cn = l.getConnections();
SerialConfig config = new SerialConfig("testThreadedPublisher_SpatialPoolerNetwork", SerialConfig.SERIAL_TEST_DIR);
PersistenceAPI api = Persistence.get(config);
byte[] bytes = api.write(cn);
//Serialize above Connections for comparison with same run but unserialized below...
Connections serializedConnections = api.read(bytes);
Network network2 = createAndRunTestSpatialPoolerNetwork(0, 6);
Layer<?> l2 = network2.lookup("r1").lookup("1");
Connections newCons = l2.getConnections();
//Compare the two Connections (both serialized and regular runs) - should be equal
boolean b = DeepEquals.deepEquals(newCons, serializedConnections);
deepCompare(newCons, serializedConnections);
assertTrue(b);
}
示例6
@Test
public void testHTMSensor_DaysOfWeek() {
Object[] n = { "some name", ResourceLocator.path("days-of-week.csv") };
HTMSensor<File> sensor = (HTMSensor<File>)Sensor.create(
FileSensor::create, SensorParams.create(Keys::path, n));
Parameters p = getParameters();
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
sensor.initEncoder(p);
SerialConfig config = new SerialConfig("testHTMSensor_DaysOfWeek", SerialConfig.SERIAL_TEST_DIR);
PersistenceAPI api = Persistence.get(config);
byte[] bytes = api.write(sensor);
HTMSensor<File> serializedSensor = api.read(bytes);
boolean b = DeepEquals.deepEquals(serializedSensor, sensor);
deepCompare(serializedSensor, sensor);
assertTrue(b);
}
示例7
@Test
public void testHTMSensor_HotGym() {
Object[] n = { "some name", ResourceLocator.path("rec-center-hourly-small.csv") };
HTMSensor<File> sensor = (HTMSensor<File>)Sensor.create(
FileSensor::create, SensorParams.create(Keys::path, n));
sensor.initEncoder(getTestEncoderParams());
SerialConfig config = new SerialConfig("testHTMSensor_HotGym");
PersistenceAPI api = Persistence.get(config);
byte[] bytes = api.write(sensor);
assertNotNull(bytes);
assertTrue(bytes.length > 0);
HTMSensor<File> serializedSensor = api.read(bytes);
boolean b = DeepEquals.deepEquals(serializedSensor, sensor);
deepCompare(serializedSensor, sensor);
assertTrue(b);
}
示例8
@Test
public void testSerializeObservableSensor() {
PublisherSupplier supplier = PublisherSupplier.builder()
.addHeader("dayOfWeek")
.addHeader("darr")
.addHeader("B").build();
ObservableSensor<String[]> oSensor = new ObservableSensor<>(SensorParams.create(Keys::obs, new Object[] {"name", supplier}));
SerialConfig config = new SerialConfig("testSerializeObservableSensor", SerialConfig.SERIAL_TEST_DIR);
PersistenceAPI api = Persistence.get(config);
byte[] bytes = api.write(oSensor);
ObservableSensor<String[]> serializedOSensor = api.read(bytes);
boolean b = DeepEquals.deepEquals(serializedOSensor, oSensor);
deepCompare(serializedOSensor, oSensor);
assertTrue(b);
}
示例9
private void assertToken(String token, String expectedToken, Map<String, Object> expectedAuth)
throws IOException {
assertTrue(token.startsWith("gauth|"));
String jsonString = token.substring(6);
Map<String, Object> map = JsonMapper.parseJson(jsonString);
assertEquals(expectedToken, map.get("token"));
Map<String, Object> auth = (Map)map.get("auth");
DeepEquals.deepEquals(expectedAuth, auth);
}
示例10
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DatabaseConfig that = (DatabaseConfig) o;
return tmpfsEnabled == that.tmpfsEnabled &&
Objects.equals(dockerImage, that.dockerImage) &&
Objects.equals(tmpfsOptions, that.tmpfsOptions) &&
Objects.equals(initdbProperties, that.initdbProperties) &&
Objects.equals(configProperties, that.configProperties) &&
DeepEquals.deepEquals(customizers, that.customizers);
}
示例11
/**
* Usage of {@link DeepEquals} in order to ensure the same hashcode
* for the same equal content regardless of cycles.
*/
@Override
public int hashCode() {
Random rnd = (Random)paramMap.remove(KEY.RANDOM);
int hc = DeepEquals.deepHashCode(paramMap);
paramMap.put(KEY.RANDOM, rnd);
return hc;
}
示例12
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((backingList == null) ? 0 : DeepEquals.deepHashCode(backingList));
result = prime * result + capacity;
result = prime * result + currentSize;
return result;
}
示例13
private void deepCompare(Object obj1, Object obj2) {
try {
assertTrue(DeepEquals.deepEquals(obj1, obj2));
System.out.println("expected(" + obj1.getClass().getSimpleName() + "): " + obj1 + " actual: (" + obj1.getClass().getSimpleName() + "): " + obj2);
}catch(AssertionError ae) {
System.out.println("expected(" + obj1.getClass().getSimpleName() + "): " + obj1 + " but was: (" + obj1.getClass().getSimpleName() + "): " + obj2);
}
}
示例14
@Test
public void testSegmentPositionForSortKey() {
Parameters retVal = Parameters.getTemporalDefaultParameters();
retVal.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
retVal.set(KEY.CELLS_PER_COLUMN, 4);
Connections connections = new Connections();
retVal.apply(connections);
TemporalMemory.init(connections);
Cell cell10 = connections.getCell(10);
DistalDendrite segment0 = connections.createSegment(cell10);
Cell cell9 = connections.getCell(9);
DistalDendrite segment1 = connections.createSegment(cell9);
Cell cell11 = connections.getCell(11);
DistalDendrite segment2 = connections.createSegment(cell11);
DistalDendrite segment3 = connections.createSegment(cell11);
DistalDendrite segment4 = connections.createSegment(cell11);
List<DistalDendrite> expected = Arrays.asList(segment1, segment0, segment2, segment3, segment4);
List<DistalDendrite> segments = Arrays.asList(segment3, segment2, segment0, segment4, segment1);
assertFalse(DeepEquals.deepEquals(expected, segments));
Collections.sort(segments, connections.segmentPositionSortKey);
assertTrue(DeepEquals.deepEquals(expected, segments));
}
示例15
@Test
public void testCopy() {
Parameters retVal = Parameters.getTemporalDefaultParameters();
retVal.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
retVal.set(KEY.CELLS_PER_COLUMN, 4);
Connections connections = new Connections();
retVal.apply(connections);
TemporalMemory.init(connections);
assertTrue(DeepEquals.deepEquals(connections, connections.copy()));
}
示例16
/**
* Creates a synapse over the synapses per segment limit, and verifies
* that the lowest permanence synapse is removed to make room for the new
* synapse.
*/
@Test
public void testSynapseReuse() {
Parameters retVal = Parameters.getTemporalDefaultParameters();
retVal.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
retVal.set(KEY.CELLS_PER_COLUMN, 4);
retVal.set(KEY.MAX_SYNAPSES_PER_SEGMENT, 2);
Connections connections = new Connections();
retVal.apply(connections);
TemporalMemory.init(connections);
Cell cell10 = connections.getCell(10);
DistalDendrite segment1 = connections.createSegment(cell10);
Synapse synapse1 = connections.createSynapse(segment1, connections.getCell(50), 0.34);
Synapse synapse2 = connections.createSynapse(segment1, connections.getCell(51), 0.48);
assertTrue(DeepEquals.deepEquals(
Arrays.asList(synapse1, synapse2), connections.getSynapses(segment1)));
// Add an additional synapse to force it over the limit of num synapses
// per segment.
connections.createSynapse(segment1, connections.getCell(52), .52);
// Ensure lower permanence synapse was removed.
Set<Cell> expected = IntStream.range(51, 53).mapToObj(i -> connections.getCell(i)).collect(Collectors.toSet());
assertTrue(DeepEquals.deepEquals(expected,
connections.getSynapses(segment1)
.stream()
.map(s -> s.getPresynapticCell())
.collect(Collectors.toSet())));
}
示例17
public boolean cmp(Value o) {
return DeepEquals.deepEquals(this, o);
}
示例18
public boolean cmp(Value o) {
return DeepEquals.deepEquals(this, o);
}
示例19
@Override
public int hashCode() {
int result = Objects.hash(dockerImage, tmpfsOptions, tmpfsEnabled, initdbProperties, configProperties);
result = 31 * result + DeepEquals.deepHashCode(customizers);
return result;
}
示例20
/**
* This implementation skips over any native comparisons (i.e. "==")
* because their hashcodes will not be equal.
*/
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Parameters other = (Parameters)obj;
if(paramMap == null) {
if(other.paramMap != null)
return false;
} else {
Class<?>[] classArray = new Class[] { Object.class };
try {
for(KEY key : paramMap.keySet()) {
if(paramMap.get(key) == null || other.paramMap.get(key) == null) continue;
Class<?> thisValueClass = paramMap.get(key).getClass();
Class<?> otherValueClass = other.paramMap.get(key).getClass();
boolean isSpecial = isSpecial(key, thisValueClass);
if(!isSpecial && (thisValueClass.getMethod("equals", classArray).getDeclaringClass() != thisValueClass ||
otherValueClass.getMethod("equals", classArray).getDeclaringClass() != otherValueClass)) {
continue;
}else if(isSpecial) {
if(int[].class.isAssignableFrom(thisValueClass)) {
if(!Arrays.equals((int[])paramMap.get(key), (int[])other.paramMap.get(key))) return false;
}else if(key == KEY.FIELD_ENCODING_MAP) {
if(!DeepEquals.deepEquals(paramMap.get(key), other.paramMap.get(key))) {
return false;
}
}
}else if(!other.paramMap.containsKey(key) || !paramMap.get(key).equals(other.paramMap.get(key))) {
return false;
}
}
}catch(Exception e) { return false; }
}
return true;
}
示例21
@Test
public void testCreateSegmentReuse() {
Parameters retVal = Parameters.getTemporalDefaultParameters();
retVal.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
retVal.set(KEY.CELLS_PER_COLUMN, 4);
retVal.set(KEY.MAX_SEGMENTS_PER_CELL, 2);
Connections connections = new Connections();
retVal.apply(connections);
TemporalMemory.init(connections);
Cell cell42 = connections.getCell(42);
Cell cell1 = connections.getCell(1);
Cell cell2 = connections.getCell(2);
DistalDendrite segment1 = connections.createSegment(cell42);
connections.createSynapse(segment1, cell1, 0.5);
connections.createSynapse(segment1, cell2, 0.5);
// Let some time pass
connections.startNewIteration();
connections.startNewIteration();
connections.startNewIteration();
// Create a segment with 3 synapses.
Cell cell3 = connections.getCell(3);
DistalDendrite segment2 = connections.createSegment(cell42);
connections.createSynapse(segment2, cell1, 0.5);
connections.createSynapse(segment2, cell2, 0.5);
connections.createSynapse(segment2, cell3, 0.5);
connections.startNewIteration();
// Give the first segment some activity.
connections.recordSegmentActivity(segment1);
// Create a new segment with 1 synapse.
DistalDendrite segment3 = connections.createSegment(cell42);
connections.createSynapse(segment3, cell1, 0.5);
List<DistalDendrite> segments = connections.getSegments(cell42);
assertEquals(2, segments.size());
// Verify first segment is there with same synapses.
Set<Cell> expected = IntStream.range(1, 3).mapToObj(i -> connections.getCell(i)).collect(Collectors.toSet());
assertTrue(DeepEquals.deepEquals(expected,
connections.getSynapses(segments.get(0))
.stream()
.map(s -> s.getPresynapticCell())
.collect(Collectors.toSet())));
// Verify second segment has been replaced.
expected = IntStream.range(1, 2).mapToObj(i -> connections.getCell(i)).collect(Collectors.toSet());
System.out.println("expected = " + expected);
System.out.println("actual = " + connections.getSynapses(segments.get(1))
.stream()
.map(s -> s.getPresynapticCell())
.collect(Collectors.toSet()));
assertTrue(DeepEquals.deepEquals(expected,
connections.getSynapses(segments.get(1))
.stream()
.map(s -> s.getPresynapticCell())
.collect(Collectors.toSet())));
// Verify the flatIdxs were properly reused.
assertTrue(segment1.getIndex() < 2);
assertTrue(segment3.getIndex() < 2);
assertEquals(segment1, connections.segmentForFlatIdx(segment1.getIndex()));
assertEquals(segment3, connections.segmentForFlatIdx(segment3.getIndex()));
}
示例22
@Test
public void testWriteRead() {
// Create two classifiers, so one can be serialized and tested against the other
SDRClassifier c1 = new SDRClassifier(new TIntArrayList(new int[] { 1 }), 0.1, 0.1, 0);
SDRClassifier c2 = new SDRClassifier(new TIntArrayList(new int[] { 1 }), 0.1, 0.1, 0);
// Create input vectors A, B, and C (int[] of active bit indices from below)
int[] inputA = new int[] { 1, 5, 9 };
int[] inputB = new int[] { 2, 4, 6 };
int[] inputC = new int[] { 3, 5, 7 };
// Create classification Map
Map<String, Object> classification = new HashMap<>();
// Have both classifiers process input A
classification.put("bucketIdx", 0);
classification.put("actValue", "A");
Classification<String> result1 = c1.compute(0, classification, inputA, true, true);
Classification<String> result2 = c2.compute(0, classification, inputA, true, true);
// Compare results, should be equal
assertArrayEquals(result1.getStats(1), result2.getStats(1), 0.0);
assertArrayEquals(result1.getActualValues(), result2.getActualValues());
// Serialize classifier #2
SerialConfig config = new SerialConfig("testSerializeSDRClassifier", SerialConfig.SERIAL_TEST_DIR);
PersistenceAPI api = Persistence.get(config);
byte[] data = api.write(c2);
// Deserialize classifier #2 into new reference.
SDRClassifier reifiedC2 = api.read(data);
// Make sure it isn't null...
assertNotNull(reifiedC2);
// Make sure pre- and post-serialization classifiers are identical
assertTrue(DeepEquals.deepEquals(c2, reifiedC2));
// Have the non-serialized classifier and the deserialized classifier
// process input B.
classification.put("bucketIdx", 1);
classification.put("actValue", "B");
result1 = c1.compute(0, classification, inputB, true, true);
result2 = reifiedC2.compute(0, classification, inputB, true, true);
// Compare the results. Make sure (de)serialization hasn't
// messed up classifier #2.
assertArrayEquals(result1.getStats(1), result2.getStats(1), 0.0);
assertArrayEquals(result1.getActualValues(), result2.getActualValues());
// Process input C - just to be safe...
classification.put("bucketIdx", 2);
classification.put("actValue", "C");
result1 = c1.compute(0, classification, inputC, true, true);
result2 = reifiedC2.compute(0, classification, inputC, true, true);
// Compare results
assertArrayEquals(result1.getStats(1), result2.getStats(1), 0.0);
assertArrayEquals(result1.getActualValues(), result2.getActualValues());
}
示例23
public boolean cmp(Value o) {
return DeepEquals.deepEquals(this, o);
}
示例24
@Test
public void testRlpDecode() {
int pos = 0;
String emptyString;
byte[] decodedData;
Object[] decodedList;
emptyString = (String)RLP.decode(Hex.decode(result01), pos).getDecoded();
assertEquals("", emptyString);
emptyString = (String)RLP.decode(Hex.decode(result02), pos).getDecoded();
assertEquals(test02, emptyString);
decodedData = (byte[]) RLP.decode(Hex.decode(result03), pos).getDecoded();
assertEquals(test03, bytesToAscii(decodedData));
decodedData = (byte[]) RLP.decode(Hex.decode(result04), pos).getDecoded();
assertEquals(test04, bytesToAscii(decodedData));
decodedData = (byte[]) RLP.decode(Hex.decode(result05), pos).getDecoded();
assertEquals(test05, bytesToAscii(decodedData));
decodedList = (Object[]) RLP.decode(Hex.decode(result06), pos).getDecoded();
assertEquals(test06[0], bytesToAscii((byte[]) decodedList[0]));
assertEquals(test06[1], bytesToAscii((byte[]) decodedList[1]));
decodedList = (Object[]) RLP.decode(Hex.decode(result07), pos).getDecoded();
assertEquals(test07[0], bytesToAscii((byte[]) decodedList[0]));
assertEquals(test07[1], bytesToAscii((byte[]) decodedList[1]));
assertEquals(test07[2], bytesToAscii((byte[]) decodedList[2]));
// 1
decodedData = (byte[]) RLP.decode(Hex.decode(result08), pos).getDecoded();
assertEquals(test08, byteArrayToInt(decodedData));
// 10
decodedData = (byte[]) RLP.decode(Hex.decode(result09), pos).getDecoded();
assertEquals(test09, byteArrayToInt(decodedData));
// 100
decodedData = (byte[]) RLP.decode(Hex.decode(result10), pos).getDecoded();
assertEquals(test10, byteArrayToInt(decodedData));
// 1000
decodedData = (byte[]) RLP.decode(Hex.decode(result11), pos).getDecoded();
assertEquals(test11, byteArrayToInt(decodedData));
decodedData = (byte[]) RLP.decode(Hex.decode(result12), pos).getDecoded();
assertTrue(test12.compareTo(new BigInteger(1, decodedData)) == 0);
decodedData = (byte[]) RLP.decode(Hex.decode(result13), pos).getDecoded();
assertTrue(test13.compareTo(new BigInteger(1, decodedData)) == 0);
// Need to test with different expected value, because decoding doesn't recognize types
Object testObject1 = RLP.decode(Hex.decode(result14), pos).getDecoded();
assertTrue(DeepEquals.deepEquals(expected14, testObject1));
Object testObject2 = RLP.decode(Hex.decode(result15), pos).getDecoded();
assertTrue(DeepEquals.deepEquals(test15, testObject2));
// Need to test with different expected value, because decoding doesn't recognize types
Object testObject3 = RLP.decode(Hex.decode(result16), pos).getDecoded();
assertTrue(DeepEquals.deepEquals(expected16, testObject3));
}