making TestCase generic
This commit is contained in:
parent
b55927b6ae
commit
739a34dde8
6 changed files with 61 additions and 22 deletions
|
@ -6,7 +6,8 @@ import org.capnproto.Text;
|
|||
import org.capnproto.benchmark.CarSalesSchema.*;
|
||||
|
||||
public class CarSales
|
||||
implements TestCase<ParkingLot.Builder, ParkingLot.Reader, TotalValue.Builder, TotalValue.Reader, Long> {
|
||||
extends TestCase<ParkingLot.Factory, ParkingLot.Builder, ParkingLot.Reader,
|
||||
TotalValue.Factory, TotalValue.Builder, TotalValue.Reader, Long> {
|
||||
|
||||
static final long carValue(Car.Reader car) {
|
||||
long result = 0;
|
||||
|
@ -89,7 +90,7 @@ public class CarSales
|
|||
}
|
||||
|
||||
|
||||
public Long setupRequest(Common.FastRand rng, ParkingLot.Builder request) {
|
||||
public final Long setupRequest(Common.FastRand rng, ParkingLot.Builder request) {
|
||||
long result = 0;
|
||||
StructList.Builder<Car.Builder> cars = request.initCars(rng.nextLessThan(200));
|
||||
for (int i = 0; i < cars.size(); ++i) {
|
||||
|
@ -101,7 +102,7 @@ public class CarSales
|
|||
}
|
||||
|
||||
|
||||
public void handleRequest(ParkingLot.Reader request, TotalValue.Builder response) {
|
||||
public final void handleRequest(ParkingLot.Reader request, TotalValue.Builder response) {
|
||||
long result = 0;
|
||||
StructList.Reader<Car.Reader> cars = request.getCars();
|
||||
for (int i =0; i < cars.size(); ++i) {
|
||||
|
@ -110,7 +111,7 @@ public class CarSales
|
|||
response.setAmount(result);
|
||||
}
|
||||
|
||||
public boolean checkResponse(TotalValue.Reader response, Long expected) {
|
||||
public final boolean checkResponse(TotalValue.Reader response, Long expected) {
|
||||
return response.getAmount() == expected;
|
||||
}
|
||||
|
||||
|
@ -118,20 +119,11 @@ public class CarSales
|
|||
public static void main(String[] args) {
|
||||
Common.FastRand rng = new Common.FastRand();
|
||||
|
||||
TestCase<ParkingLot.Builder, ParkingLot.Reader,
|
||||
TotalValue.Builder, TotalValue.Reader, Long> testCase = new CarSales();
|
||||
TestCase<ParkingLot.Factory, ParkingLot.Builder, ParkingLot.Reader,
|
||||
TotalValue.Factory, TotalValue.Builder, TotalValue.Reader, Long> testCase = new CarSales();
|
||||
|
||||
testCase.execute(ParkingLot.factory, TotalValue.factory);
|
||||
|
||||
for (int i = 0; i < 50000; ++i) {
|
||||
MessageBuilder requestMessage = new MessageBuilder();
|
||||
MessageBuilder responseMessage = new MessageBuilder();
|
||||
ParkingLot.Builder request = requestMessage.initRoot(ParkingLot.factory);
|
||||
long expected = testCase.setupRequest(rng, request);
|
||||
TotalValue.Builder response = responseMessage.initRoot(TotalValue.factory);
|
||||
testCase.handleRequest(request.asReader(), response);
|
||||
if (!testCase.checkResponse(response.asReader(), expected)) {
|
||||
System.out.println("mismatch!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,4 +42,6 @@ public class Common {
|
|||
return a % b;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -5,7 +5,10 @@ import org.capnproto.StructList;
|
|||
import org.capnproto.Text;
|
||||
import org.capnproto.benchmark.EvalSchema.*;
|
||||
|
||||
public class Eval {
|
||||
public class Eval
|
||||
extends TestCase<Expression.Factory, Expression.Builder, Expression.Reader,
|
||||
EvaluationResult.Factory, EvaluationResult.Builder, EvaluationResult.Reader, Integer> {
|
||||
|
||||
public static int makeExpression(Common.FastRand rng, Expression.Builder exp, int depth) {
|
||||
exp.setOp(Operation.values()[rng.nextLessThan(Operation.values().length)]);
|
||||
|
||||
|
@ -67,4 +70,16 @@ public class Eval {
|
|||
throw new Error("impossible");
|
||||
}
|
||||
}
|
||||
|
||||
public final Integer setupRequest(Common.FastRand rng, Expression.Builder request) {
|
||||
return makeExpression(rng, request, 0);
|
||||
}
|
||||
|
||||
public final void handleRequest(Expression.Reader request, EvaluationResult.Builder response) {
|
||||
response.setValue(evaluateExpression(request));
|
||||
}
|
||||
|
||||
public final boolean checkResponse(EvaluationResult.Reader response, Integer expected) {
|
||||
return response.getValue() == expected;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,32 @@ package org.capnproto.benchmark;
|
|||
|
||||
import org.capnproto.FromStructReader;
|
||||
import org.capnproto.FromStructBuilder;
|
||||
import org.capnproto.StructFactory;
|
||||
import org.capnproto.MessageBuilder;
|
||||
|
||||
public interface TestCase<RequestBuilder, RequestReader, ResponseBuilder, ResponseReader, Expectation> {
|
||||
public Expectation setupRequest(Common.FastRand rng, RequestBuilder request);
|
||||
public void handleRequest(RequestReader request, ResponseBuilder response);
|
||||
public boolean checkResponse(ResponseReader response, Expectation expected);
|
||||
public abstract class TestCase<RequestFactory extends StructFactory<RequestBuilder, RequestReader>,
|
||||
RequestBuilder, RequestReader,
|
||||
ResponseFactory extends StructFactory<ResponseBuilder, ResponseReader>,
|
||||
ResponseBuilder, ResponseReader, Expectation> {
|
||||
public abstract Expectation setupRequest(Common.FastRand rng, RequestBuilder request);
|
||||
public abstract void handleRequest(RequestReader request, ResponseBuilder response);
|
||||
public abstract boolean checkResponse(ResponseReader response, Expectation expected);
|
||||
|
||||
public void execute(RequestFactory requestFactory, ResponseFactory responseFactory) {
|
||||
Common.FastRand rng = new Common.FastRand();
|
||||
|
||||
for (int i = 0; i < 50000; ++i) {
|
||||
MessageBuilder requestMessage = new MessageBuilder();
|
||||
MessageBuilder responseMessage = new MessageBuilder();
|
||||
RequestBuilder request = requestMessage.initRoot(requestFactory);
|
||||
Expectation expected = this.setupRequest(rng, request);
|
||||
ResponseBuilder response = responseMessage.initRoot(responseFactory);
|
||||
this.handleRequest(requestFactory.asReader(request), response);
|
||||
if (!this.checkResponse(responseFactory.asReader(response), expected)) {
|
||||
System.out.println("mismatch!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1275,6 +1275,10 @@ private:
|
|||
spaces(indent), " public final org.capnproto.StructSize structSize() {\n",
|
||||
spaces(indent), " return ", fullName, ".STRUCT_SIZE;\n",
|
||||
spaces(indent), " }\n",
|
||||
spaces(indent), " public final Reader asReader(Builder builder) {\n",
|
||||
spaces(indent), " return new Reader(builder._builder.asReader());\n",
|
||||
spaces(indent), " }\n",
|
||||
|
||||
spaces(indent), " }\n",
|
||||
spaces(indent), " public static final Factory factory = new Factory();\n",
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
package org.capnproto;
|
||||
|
||||
public interface StructFactory<Builder, Reader> extends FromStructBuilder<Builder>, FromStructReader<Reader>{
|
||||
|
||||
public Reader asReader(Builder builder);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue