@Theory public void testMultiply(@ForAll(sampleSize = 10) @From(TableFactorGenerator.class) TableFactor factor1, @ForAll(sampleSize = 10) @From(TableFactorGenerator.class) TableFactor factor2) throws Exception { TableFactor result = factor1.multiply(factor2); for (int[] assignment : result) { double factor1Value = factor1.getAssignmentValue(subsetAssignment(assignment, result, factor1)); double factor2Value = factor2.getAssignmentValue(subsetAssignment(assignment, result, factor2)); assertEquals(factor1Value * factor2Value, result.getAssignmentValue(assignment), 1.0e-5); } // Check for no duplication for (int i = 0; i < result.neighborIndices.length; i++) { for (int j = 0; j < result.neighborIndices.length; j++) { if (i == j) continue; assertNotSame(result.neighborIndices[i], result.neighborIndices[j]); } } }
@Theory public void testResizeOnSetComponent(@ForAll(sampleSize = 50) @From(MapGenerator.class) Map<Integer,Integer> featureMap1, @ForAll(sampleSize = 50) @From(MapGenerator.class) Map<Integer,Integer> featureMap2) { ConcatVectorNamespace namespace = new ConcatVectorNamespace(); ConcatVector namespace1 = toNamespaceVector(namespace, featureMap1); ConcatVector namespace2 = toNamespaceVector(namespace, featureMap2); ConcatVector regular1 = toVector(featureMap1); ConcatVector regular2 = toVector(featureMap2); assertEquals(regular1.dotProduct(regular2), namespace1.dotProduct(namespace2), 1.0e-5); ConcatVector namespaceSum = namespace1.deepClone(); namespaceSum.addVectorInPlace(namespace2, 1.0); ConcatVector regularSum = regular1.deepClone(); regularSum.addVectorInPlace(regular2, 1.0); assertEquals(regular1.dotProduct(regularSum), namespace1.dotProduct(namespaceSum), 1.0e-5); assertEquals(regularSum.dotProduct(regular2), namespaceSum.dotProduct(namespace2), 1.0e-5); }
@Theory public void testProtoNamespace(@ForAll(sampleSize = 50) @From(NamespaceGenerator.class) ConcatVectorNamespace namespace) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); namespace.writeToStream(byteArrayOutputStream); byteArrayOutputStream.close(); byte[] bytes = byteArrayOutputStream.toByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); ConcatVectorNamespace recovered = ConcatVectorNamespace.readFromStream(byteArrayInputStream); assertEquals(namespace.featureToIndex, recovered.featureToIndex); assertEquals(namespace.sparseFeatureIndex, recovered.sparseFeatureIndex); assertEquals(namespace.reverseSparseFeatureIndex, recovered.reverseSparseFeatureIndex); }
@Theory public void testGetSparseIndex(@ForAll(sampleSize = 10) @InRange(minInt = 0, maxInt = 10000) int sparse1, @ForAll(sampleSize = 10) double sparse1Val, @ForAll(sampleSize = 10) @InRange(minInt = 0, maxInt = 10000) int sparse2, @ForAll(sampleSize = 10) double sparse2Val) throws Exception { ConcatVector v1 = new ConcatVector(2); ConcatVector v2 = new ConcatVector(2); v1.setSparseComponent(0, sparse1, sparse1Val); v1.setSparseComponent(1, sparse2, sparse1Val); v2.setSparseComponent(0, sparse2, sparse2Val); v2.setSparseComponent(1, sparse1, sparse2Val); assertEquals(sparse1, v1.getSparseIndex(0)); assertEquals(sparse2, v1.getSparseIndex(1)); assertEquals(sparse2, v2.getSparseIndex(0)); assertEquals(sparse1, v2.getSparseIndex(1)); }
@Theory public void testElementwiseSparseToSparse(@ForAll(sampleSize = 20) @InRange(minInt = 0, maxInt = 10) int sparseIndex1, @ForAll(sampleSize = 10) double val1, @ForAll(sampleSize = 20) @InRange(minInt = 0, maxInt = 10) int sparseIndex2, @ForAll(sampleSize = 10) double val2) { ConcatVector v1 = new ConcatVector(1); v1.setSparseComponent(0, sparseIndex1, val1); ConcatVector v2 = new ConcatVector(1); v2.setSparseComponent(0, sparseIndex2, val2); v1.elementwiseProductInPlace(v2); for (int i = 0; i < 10; i++) { double expected = 0.0f; if (i == sparseIndex1 && i == sparseIndex2) expected = val1 * val2; assertEquals(expected, v1.getValueAt(0, i), 5.0e-4); } }
@Theory public void testAppendSparseComponent(@ForAll(sampleSize = 10) @InRange(minInt = 0, maxInt = 10000) int sparse1, @ForAll(sampleSize = 10) double sparse1Val, @ForAll(sampleSize = 10) @InRange(minInt = 0, maxInt = 10000) int sparse2, @ForAll(sampleSize = 10) double sparse2Val) throws Exception { ConcatVector v1 = new ConcatVector(1); ConcatVector v2 = new ConcatVector(1); v1.setSparseComponent(0, sparse1, sparse1Val); v2.setSparseComponent(0, sparse2, sparse2Val); if (sparse1 == sparse2) { assertEquals(sparse1Val*sparse2Val, v1.dotProduct(v2), 5.0e-4); } else { assertEquals(0.0, v1.dotProduct(v2), 5.0e-4); } }
@Theory public void testConstructWithObservations(@ForAll(sampleSize = 50) @From(PartiallyObservedConstructorDataGenerator.class) PartiallyObservedConstructorData data, @ForAll(sampleSize = 2) @From(ConcatVectorGenerator.class) ConcatVector weights) throws Exception { int[] obsArray = new int[9]; for (int i = 0; i < obsArray.length; i++) obsArray[i] = -1; for (int i = 0; i < data.observations.length; i++) { obsArray[data.factor.neigborIndices[i]] = data.observations[i]; } TableFactor normalObservations = new TableFactor(weights, data.factor); for (int i = 0; i < obsArray.length; i++) { if (obsArray[i] != -1) { normalObservations = normalObservations.observe(i, obsArray[i]); } } TableFactor constructedObservations = new TableFactor(weights, data.factor, data.observations); assertArrayEquals(normalObservations.neighborIndices, constructedObservations.neighborIndices); for (int[] assn : normalObservations) { assertEquals(normalObservations.getAssignmentValue(assn), constructedObservations.getAssignmentValue(assn), 1.0e-9); } }
@Theory public void testObserve(@ForAll(sampleSize = 50) @From(TableFactorGenerator.class) TableFactor factor, @ForAll(sampleSize = 2) @InRange(minInt = 0, maxInt = 3) int observe, @ForAll(sampleSize = 2) @InRange(minInt = 0, maxInt = 1) int value) throws Exception { if (!Arrays.stream(factor.neighborIndices).boxed().collect(Collectors.toSet()).contains(observe)) return; if (factor.neighborIndices.length == 1) return; TableFactor observedOut = factor.observe(observe, value); int observeIndex = -1; for (int i = 0; i < factor.neighborIndices.length; i++) { if (factor.neighborIndices[i] == observe) observeIndex = i; } for (int[] assignment : factor) { if (assignment[observeIndex] == value) { assertEquals(factor.getAssignmentValue(assignment), observedOut.getAssignmentValue(subsetAssignment(assignment, factor, observedOut)), 1.0e-7); } } }
@Theory public void testLegalMoves(@ForAll(sampleSize = 10) @From(GameGenerator.class) Game game) throws Exception { Game.Event[] moves = game.getLegalMoves(); boolean containsHire = false; boolean containsWait = false; boolean containsObserve = false; boolean containsTurnIn = false; for (Game.Event e : moves) { if (e instanceof Game.HumanJobPosting) containsHire = true; if (e instanceof Game.Wait) containsWait = true; if (e instanceof Game.QueryLaunch) containsObserve = true; if (e instanceof Game.TurnIn) containsTurnIn = true; } assertTrue(containsHire); assertFalse(containsWait); assertFalse(containsObserve); assertTrue(containsTurnIn); }
/** * This is basically just to check that the JSON serialization works properly, and everything is indeed hooked up to * the HumanSourceServer. Workers accept jobs, then do them. * * @param numThreads * @param numJobs * @throws Exception */ @Theory public void testQueries(@ForAll(sampleSize = 2) @InRange(maxInt = 10, minInt = 2) int numThreads, @ForAll(sampleSize = 2) @InRange(maxInt = 100, minInt = 20) int numJobs) throws Exception { List<WebSocketMockBrowser> mockBrowsers = new ArrayList<>(); HumanSourceServerTest.busyTest(() -> { WebSocketMockBrowser mockWorkerWithBrowser = new WebSocketMockBrowser(); mockBrowsers.add(mockWorkerWithBrowser); new Thread(mockWorkerWithBrowser).start(); return mockWorkerWithBrowser; }, numThreads, numJobs); // Test that the payment system is working for (WebSocketMockBrowser mockBrowser : mockBrowsers) { Assert.assertEquals(HistoricalDatabase.numberOfTasksThisWorkUnit("" + mockBrowser.workerID), mockBrowser.totalQueriesReceived); } }
@Theory public void testCompileNormalizedModel(@ForAll(sampleSize = 1000) @From(GraphicalModelGenerator.class) GraphicalModel model, @ForAll(sampleSize = 10) @From(WeightsGenerator.class) ConcatVector weights) throws Exception { CliqueTree.MarginalResult unnormalizedSolve = new CliqueTree(model, weights).calculateMarginals(); GraphicalModel normalized = new CliqueTree(model, weights).compileNormalizedModel(); CliqueTree.MarginalResult normalizedSolve = new CliqueTree(normalized, weights).calculateMarginals(); assertEquals(unnormalizedSolve, normalizedSolve); double partitionWithWeights = normalizedSolve.partitionFunction; double partitionWithoutWeights = new CliqueTree(normalized, new ConcatVector(0)).calculateMarginals().partitionFunction; assertEquals(partitionWithoutWeights, partitionWithWeights, 1e-5); assertFalse(Double.isNaN(partitionWithoutWeights)); assertFalse(Double.isNaN(partitionWithWeights)); assertEquals(1.0, partitionWithWeights, 1e-5); assertEquals(1.0, partitionWithoutWeights, 1e-5); }
@Theory public void testProtoBatch(@ForAll(sampleSize = 50) @From(BatchGenerator.class) ModelBatch batch) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); batch.writeToStream(byteArrayOutputStream); byteArrayOutputStream.close(); byte[] bytes = byteArrayOutputStream.toByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); ModelBatch recovered = new ModelBatch(byteArrayInputStream); byteArrayInputStream.close(); assertEquals(batch.size(), recovered.size()); for (int i = 0; i < batch.size(); i++) { assertTrue(batch.get(i).valueEquals(recovered.get(i), 1.0e-5)); } }
@Theory public void testProtoBatch(@ForAll(sampleSize = 50) @From(ModelBatchTest.BatchGenerator.class) ModelBatch batch) throws IOException { ByteArrayInputStream emptyInputStream = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ModelLog log = new ModelLogDisk(emptyInputStream, byteArrayOutputStream); log.writeWithFactors = true; for (GraphicalModel model : batch) { log.add(model); } log.close(); byte[] bytes = byteArrayOutputStream.toByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); ModelLog recovered = new ModelLogDisk(byteArrayInputStream, byteArrayOutputStream); byteArrayInputStream.close(); assertEquals(batch.size(), recovered.size()); for (int i = 0; i < batch.size(); i++) { assertTrue(batch.get(i).valueEquals(recovered.get(i), 1.0e-5)); } }
@Theory public void testElementwiseDenseToSparse(@ForAll(sampleSize = 50) double[] dense1, @ForAll(sampleSize = 20) @InRange(minInt = 0, maxInt = 100) int sparseIndex, @ForAll(sampleSize = 10) double v) { assumeTrue(sparseIndex >= 0); assumeTrue(sparseIndex <= 100); ConcatVector v1 = new ConcatVector(1); v1.setDenseComponent(0, dense1); ConcatVector v2 = new ConcatVector(1); v2.setSparseComponent(0, sparseIndex, v); v2.elementwiseProductInPlace(v1); for (int i = 0; i < dense1.length; i++) { double expected = 0.0f; if (i == sparseIndex) expected = dense1[i] * v; assertEquals(expected, v2.getValueAt(0, i), 5.0e-4); } }
@Theory public <B> void returnsFailureIfBulkStringBuilderThrowsException(@ForAll byte[] bytes, @TestedOn(ints = {1, 2, 3, 5, 100}) int bufferSize) throws Exception { @SuppressWarnings("unchecked") BulkStringBuilderFactory<B, ?> bulkStringBuilderFactory = mock(BulkStringBuilderFactory.class); RuntimeException e1 = new RuntimeException(); RuntimeException e2 = new RuntimeException(); when(bulkStringBuilderFactory.append(any(), any(), any())).thenThrow(e1, e2); boolean oneChunk = bytes.length <= bufferSize; when(bulkStringBuilderFactory.appendLast(any(), any(), any())).thenThrow(oneChunk ? e1 : e2); ByteBuffer src = ByteBuffer.wrap(appendCRLF(bytes)); assertThat( parseReply(src, bufferSize, new BulkStringParser<>(bytes.length, bulkStringBuilderFactory), assertNoResult(), e -> e, charsetDecoder), equalTo(e1)); verifyZeroInteractions(charsetDecoder); verify(bulkStringBuilderFactory).create(bytes.length, charsetDecoder); if (oneChunk) { verify(bulkStringBuilderFactory).appendLast(any(), any(), eq(charsetDecoder)); } else { verify(bulkStringBuilderFactory).append(any(), any(), eq(charsetDecoder)); } verifyNoMoreInteractions(bulkStringBuilderFactory); }
@Theory public void testGetBestAssignment(@ForAll(sampleSize = 1000) @From(TableFactorGenerator.class) TableFactor factor) { int[] bestAssignment = factor.getBestAssignment(); for (int[] assn : factor) { assertTrue(factor.getAssignmentValue(assn) <= factor.getAssignmentValue(bestAssignment)); } }
@Theory public void parsesScanReply(@ForAll(sampleSize = 10) @InRange(minLong = 0) long cursor, @ForAll(sampleSize = 50) byte[][] elements, @TestedOn(ints = {3, 5, 10, 100, 1000}) int bufferSize) { ByteBuffer src = ByteBuffer.wrap(encodeScanReply(cursor, elements)); ScanResult<byte[][]> scanResult = parseReply(src, bufferSize, scanReply(array(byte[][]::new), new TestBulkStringBuilderFactory()), Function.identity(), assertNoFailure(), charsetDecoder); assertThat(scanResult.cursor, equalTo(cursor)); assertThat(scanResult.elements, equalTo(elements)); verifyZeroInteractions(charsetDecoder); }
@Theory public void canCallHasNextFreely(ArgumentSet as) { Iterator<?> iter = as.iterator(); while (iter.hasNext()) { assertTrue(iter.hasNext()); iter.next(); } assertTrue(!iter.hasNext()); }
@Theory public void testGetVariableSizes(@ForAll(sampleSize = 50) @From(GraphicalModelGenerator.class) GraphicalModel graphicalModel) throws IOException { int[] sizes = graphicalModel.getVariableSizes(); for (GraphicalModel.Factor f : graphicalModel.factors) { for (int i = 0; i < f.neigborIndices.length; i++) { assertEquals(f.getDimensions()[i], sizes[f.neigborIndices[i]]); } } }
@Theory public void testAddSparseToSparse(@ForAll(sampleSize = 20) @InRange(minInt = 0, maxInt = 10) int sparseIndex1, @ForAll(sampleSize = 10) double val1, @ForAll(sampleSize = 20) @InRange(minInt = 0, maxInt = 10) int sparseIndex2, @ForAll(sampleSize = 10) double val2) { ConcatVector v1 = new ConcatVector(1); v1.setSparseComponent(0, sparseIndex1, val1); ConcatVector v2 = new ConcatVector(1); v2.setSparseComponent(0, sparseIndex2, val2); double expected = v1.dotProduct(v2) + 0.7f*(v2.dotProduct(v2)); v1.addVectorInPlace(v2, 0.7f); assertEquals(expected, v1.dotProduct(v2), 5.0e-3); }
@Theory public void testRetrieveDense(@ForAll(sampleSize = 50) @From(DenseTestVectorGenerator.class) DenseTestVector d1) { int size = d1.vector.getNumberOfComponents(); assertEquals(d1.values.length, size); for (int i = 0; i < d1.values.length; i++) { if (!d1.vector.isComponentSparse(i)) { assertArrayEquals(d1.values[i], d1.vector.getDenseComponent(i), 1.0e-9); } } }
@Theory public void parsesBulkStringReply(@ForAll byte[] bytes, @TestedOn(ints = {1, 2, 3, 5, 100}) int bufferSize) { ByteBuffer src = ByteBuffer.wrap(encodeBulkString(bytes)); assertThat(parseReply(src, bufferSize, bulkStringReply(new TestBulkStringBuilderFactory()), Function.identity(), assertNoFailure(), charsetDecoder), equalTo(bytes)); verifyZeroInteractions(charsetDecoder); }
@Theory @Pairwise @WithConstraints("equals") public void equalsIsTransitive(Object x, Object y, Object z) { assertTrue(z.equals(x)); }
@Theory public <E> void failsToParseArrayReplyIfIntegerReplyIsFound(@ForAll long num, @TestedOn(ints = {1, 2, 3, 5, 100}) int bufferSize) { ByteBuffer src = ByteBuffer.wrap(encodeInteger(num)); @SuppressWarnings("unchecked") ArrayBuilderFactory<E, ?> arrayBuilderFactory = mock(ArrayBuilderFactory.class); @SuppressWarnings("unchecked") BulkStringBuilderFactory<?, E> bulkStringBuilderFactory = mock(BulkStringBuilderFactory.class); failsToParseReply(src, bufferSize, arrayReply(arrayBuilderFactory, bulkStringBuilderFactory), "Command returned integer reply while array reply was expected"); verifyZeroInteractions(bulkStringBuilderFactory); }
@Theory public void testElementwiseSparseToDense(@ForAll(sampleSize = 50) double[] dense1, @ForAll(sampleSize = 20) @InRange(minInt = 0, maxInt = 100) int sparseIndex, @ForAll(sampleSize = 10) double v) { ConcatVector v1 = new ConcatVector(1); v1.setDenseComponent(0, dense1); ConcatVector v2 = new ConcatVector(1); v2.setSparseComponent(0, sparseIndex, v); v1.elementwiseProductInPlace(v2); for (int i = 0; i < dense1.length; i++) { double expected = 0.0f; if (i == sparseIndex) expected = dense1[i] * v; assertEquals(expected, v1.getValueAt(0, i), 5.0e-4); } }
@Theory public void equalsIsRepeatable(Object x, Object y) { boolean alwaysTheSame = x.equals(y); for (int i = 0; i < 30; i++) assertThat(x.equals(y), is(alwaysTheSame)); }
@Theory public void parsesNullStringReply(@TestedOn(ints = {1, 2, 3, 5, 100}) int bufferSize) { ByteBuffer src = ByteBuffer.wrap(encodeNilBulkString()); assertThat(parseReply(src, bufferSize, simpleStringReply(), Function.identity(), assertNoFailure(), charsetDecoder), nullValue()); verifyZeroInteractions(charsetDecoder); }
/** * Global constraint on arguments of a test. */ @Theory public void yearDayRoundTrip(Year year, Month month, int monthDay) { // no assume needed because of global constraint int dayOfYear = year.atMonth(month).atDay(monthDay).getDayOfYear(); assertTrue(dayOfYear > 0); assertTrue(dayOfYear <= 366); assertEquals(month, year.atDay(dayOfYear).getMonth()); assertEquals(monthDay, year.atDay(dayOfYear).getDayOfMonth()); }
@Theory public void testCloneTable(@ForAll(sampleSize = 50) @From(FeatureFactorGenerator.class) ConcatVector[][][] factor3) throws IOException { ConcatVectorTable concatVectorTable = convertArrayToVectorTable(factor3); ConcatVectorTable cloned = concatVectorTable.cloneTable(); for (int i = 0; i < factor3.length; i++) { for (int j = 0; j < factor3[0].length; j++) { for (int k = 0; k < factor3[0][0].length; k++) { assertTrue(factor3[i][j][k].valueEquals(cloned.getAssignmentValue(new int[]{i, j, k}).get(), 1.0e-5)); } } } assertTrue(concatVectorTable.valueEquals(cloned, 1.0e-5)); }
@Theory public void testCalculateMap(@ForAll(sampleSize = 100) @From(GraphicalModelGenerator.class) GraphicalModel model, @ForAll(sampleSize = 10) @From(WeightsGenerator.class) ConcatVector weights) throws Exception { if (model.factors.size() == 0) return; CliqueTree inference = new CliqueTree(model, weights); // This is the basic check that inference works when you first construct the model checkMAPAgainstBruteForce(model, weights, inference); // Now we go through several random mutations to the model, and check that everything is still consistent Random r = new Random(); for (int i = 0; i < 10; i++) { randomlyMutateGraphicalModel(model, r); checkMAPAgainstBruteForce(model, weights, inference); } }
@Theory public void inOrder(boolean b1, double a, double b, double c, int i) { assertTrue(a <= b); assertTrue(b <= c); assertTrue(b1); }
@Theory public void testInnerProduct2(@ForAll(sampleSize = 10) @From(DenseTestVectorGenerator.class) DenseTestVector d1, @ForAll(sampleSize = 10) @From(DenseTestVectorGenerator.class) DenseTestVector d2, @ForAll(sampleSize = 10) @From(DenseTestVectorGenerator.class) DenseTestVector d3) throws Exception { // Test the invariant x^Tz + 0.7*y^Tz == (x+0.7*y)^Tz double d1d3 = d1.vector.dotProduct(d3.vector); assertEquals(d1.trueInnerProduct(d3), d1d3, 5.0e-4); double d2d3 = d2.vector.dotProduct(d3.vector); assertEquals(d2.trueInnerProduct(d3), d2d3, 5.0e-4); double expected = d1d3 + (0.7f*d2d3); assertEquals(d1.trueInnerProduct(d3) + (0.7*d2.trueInnerProduct(d3)), expected, 5.0e-4); }
@Theory public void parsesCharSequences(@ForAll String value, @TestedOn(ints = {4, 5, 6, 7, 10, 100, 1000}) int bufferSize, Charset charset) { ByteBuffer src = ByteBuffer.wrap(encodeBulkString(value.getBytes(charset))); CharSequence actual = parseReply(src, bufferSize, bulkStringReply(charSequence()), Function.identity(), assertNoFailure(), charset.newDecoder()); assertThat(actual, hasSameContentAs(value)); }
@Theory public void parsesLongs(@ForAll long value, @TestedOn(ints = {1, 2, 3, 5, 10, 100, 1000}) int bufferSize) { ByteBuffer src = ByteBuffer.wrap(encodeBulkString(Long.toString(value).getBytes(US_ASCII))); assertThat( parseReply(src, bufferSize, bulkStringReply(_long()), Function.identity(), assertNoFailure(), charsetDecoder), equalTo(value)); verifyZeroInteractions(charsetDecoder); }
@Theory public void testQueries(@ForAll(sampleSize = 3) @From(GameTest.GameGenerator.class) Game game) throws Exception { Random r = new Random(23); game.humansAvailableServerSide = 2; GamePlayerExhaustiveSearch bruteForce = new GamePlayerExhaustiveSearch(); System.err.println("Model: "+game.model.toString()); // This means that the game tree is too large, so we quit Game.Event firstMove = bruteForce.getNextMove(game, new UncertaintyUtility()); if (firstMove == null || firstMove instanceof Game.TurnIn) { System.err.println("Skip... "+firstMove); return; } GamePlayerRandom random = new GamePlayerRandom(r); double bruteForceUtil = 0; double randomUtil = 0; for (int i = 0; i < 10; i++) { game.resetEvents(); bruteForceUtil += GamePlayerTestBed.playSimulation(r, game, bruteForce, new UncertaintyUtility()).endUtility; game.resetEvents(); randomUtil += GamePlayerTestBed.playSimulation(r, game, random, new UncertaintyUtility()).endUtility; System.err.println("Brute force: " + bruteForceUtil); System.err.println("random: "+randomUtil); } // Over 10 runs, brute force will almost always do better than random, to the point where failure probability // is near 0 assertTrue(bruteForceUtil >= randomUtil); }
@Theory public void testGetClones(@ForAll(sampleSize = 10) @From(GameGenerator.class) Game game) throws Exception { Random r = new Random(42); GamePlayer gp = new GamePlayerRandom(r); while (!game.isTerminated()) { Game[] copies = game.getClones(5); for (int i = 0; i < copies.length; i++) { assertTrue(copies[i] != game); assertTrue(copies[i].stack.size() == game.stack.size()); for (int j = 0; j < copies[i].stack.size(); j++) { assertTrue(copies[i].stack.get(j) != game.stack.get(j)); if (!copies[i].stack.get(j).equals(game.stack.get(j))) { Game.Event e1 = copies[i].stack.get(j); Game.Event e2 = game.stack.get(j); boolean eq = e1.equals(e2); System.err.println("blah"); } assertTrue(copies[i].stack.get(j).equals(game.stack.get(j))); } assertTrue(copies[i].model != game.model); assertTrue(copies[i].model.valueEquals(game.model, 1.0e-7)); } Game.Event nextMove; if (game.isGameplayerTurn()) { nextMove = gp.getNextMove(game, null); } else { nextMove = game.sampleNextEvent(r); } nextMove.push(game); } }
@Theory public void testMultiquery(@ForAll(sampleSize = 10) @From(GameGenerator.class) Game game, @ForAll(sampleSize = 5) @InRange(minInt = 1, maxInt = 10) int numQueries) throws Exception { int initialNumVariable = numVariables(game.model); List<Integer> vars = new ArrayList<>(); vars.addAll(game.availableAnnotators.keySet()); Random r = new Random(); for (int i = 0; i < numQueries; i++) { //Recruit another human Game.HumanJobPosting jp = new Game.HumanJobPosting(); jp.push(game); Game.HumanArrival human = game.humanProvider.getArtificialHuman(game, jp); human.push(game); // Do the query int variable = vars.get(r.nextInt(vars.size())); Game.QueryLaunch ql = new Game.QueryLaunch(variable, human); ql.push(game); Game.QueryResponse qr = new Game.QueryResponse(ql, 0); qr.push(game); assertEquals(initialNumVariable + i + 1, numVariables(game.model)); } for (int i = 0; i < numQueries; i++) { // Pop the answer game.stack.peek().pop(game); // request game.stack.peek().pop(game); // humanArrival game.stack.peek().pop(game); // job posting game.stack.peek().pop(game); assertEquals(initialNumVariable + numQueries - i - 1, numVariables(game.model)); } }
@Theory public void testIsFinished(@ForAll(sampleSize = 10) @From(GameGenerator.class) Game game) throws Exception { Game.TurnIn t = new Game.TurnIn(); t.push(game); assertTrue(game.isTerminated()); t.pop(game); assertFalse(game.isTerminated()); }
@Theory public void testIsGameplayerTurn(@ForAll(sampleSize = 10) @From(GameGenerator.class) Game game) throws Exception { Game.Wait w = new Game.Wait(); assertTrue(game.isGameplayerTurn()); w.push(game); assertFalse(game.isGameplayerTurn()); w.pop(game); assertTrue(game.isGameplayerTurn()); }
@Theory public void parsesArrayReply(@ForAll(sampleSize = 40) byte[][] arrays, @TestedOn(ints = {1, 3, 5, 10, 100, 1000}) int bufferSize) { ByteBuffer src = ByteBuffer.wrap(encodeArray(arrays)); assertThat(parseReply(src, bufferSize, arrayReply(array(byte[][]::new), new TestBulkStringBuilderFactory()), Function.identity(), assertNoFailure(), charsetDecoder), equalTo(arrays)); verifyZeroInteractions(charsetDecoder); }