Skip to content

Commit

Permalink
Merge pull request #50 from Gabriel-Trintinalia/small-tests-improvements
Browse files Browse the repository at this point in the history
Make ext and mod test classes extend abstract class
  • Loading branch information
Gabriel-Trintinalia authored Apr 26, 2023
2 parents de17a66 + 76b2a39 commit d671a73
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 70 deletions.
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ allprojects {
useJUnitPlatform()
}

task unitTests(type: Test) {
useJUnitPlatform {
excludeTags("CorsetTest")
}
}

task corsetTests(type: Test) {
useJUnitPlatform {
includeTags("CorsetTest")
}
}

javadoc {
options.addStringOption('Xdoclint:all', '-quiet')
options.addStringOption('Xwerror', '-html5')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static UInt256 multiplyRange(Bytes[] range1, Bytes[] range2) {
UInt256 sum = UInt256.ZERO;
for (int i = 0; i < range1.length; i++) {
UInt256 prod =
UInt256.fromBytes(range1[i]).multiply(UInt256.fromBytes(range2[range2.length - i - 1]));
UInt256.fromBytes(range1[i]).multiply(UInt256.fromBytes(range2[range2.length - i - 1]));
sum = sum.add(prod);
}
return sum;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.alu.mul;

import static net.consensys.linea.zktracer.module.Util.boolToByte;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import java.util.stream.Stream;

import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

@TestInstance(Lifecycle.PER_CLASS)
@Tag("CorsetTest")
public abstract class AbstractModuleTracerCorsetTest extends AbstractBaseModuleTracerTest {
static final Random rand = new Random();
private static final int TEST_REPETITIONS = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.module.ModuleTracer;
import net.consensys.linea.zktracer.module.alu.add.AddTracer;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.bytestheta.BaseBytes;
import net.consensys.linea.zktracer.module.alu.add.Adder;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.module.ModuleTracer;
import net.consensys.linea.zktracer.module.alu.ext.ExtTracer;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.jupiter.api.Assertions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.module.ModuleTracer;
import net.consensys.linea.zktracer.module.alu.mod.ModTracer;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.alu.mul;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -43,6 +44,7 @@
import org.slf4j.LoggerFactory;

@ExtendWith(MockitoExtension.class)
@Tag("CorsetTest")
class ShfTracerTest {
private static final Logger LOG = LoggerFactory.getLogger(ShfTracerTest.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,87 +14,32 @@
*/
package net.consensys.linea.zktracer.module.wcp;

import static net.consensys.linea.zktracer.OpCode.SGT;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;
import static net.consensys.linea.zktracer.OpCode.GT;

import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.operation.Operation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.ZkTraceBuilder;
import net.consensys.linea.zktracer.ZkTracer;
import net.consensys.linea.zktracer.corset.CorsetValidator;
import net.consensys.linea.zktracer.module.wcp.WcpTracer;
import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.module.ModuleTracer;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class WcpTracerTest {
private ZkTracer zkTracer;
private ZkTraceBuilder zkTraceBuilder;
@Mock MessageFrame mockFrame;
@Mock Operation mockOperation;

private static final Random rand = new Random();
private static final int TEST_REPETITIONS = 4;

@BeforeEach
void setUp() {
zkTraceBuilder = new ZkTraceBuilder();
zkTracer = new ZkTracer(zkTraceBuilder, List.of(new WcpTracer()));
when(mockFrame.getCurrentOperation()).thenReturn(mockOperation);
}
class WcpTracerTest extends AbstractModuleTracerCorsetTest {

@ParameterizedTest()
@MethodSource("provideRandomArguments")
void testRandomWcp(OpCode opCode, final Bytes32 arg1, Bytes32 arg2) {
when(mockOperation.getOpcode()).thenReturn((int) opCode.value);
when(mockFrame.getStackItem(0)).thenReturn(arg1);
when(mockFrame.getStackItem(1)).thenReturn(arg2);
zkTracer.tracePreExecution(mockFrame);
assertThat(CorsetValidator.isValid(zkTraceBuilder.build().toJson())).isTrue();
@Override
protected ModuleTracer getModuleTracer() {
return new WcpTracer();
}

@Test
public void testNonRandomWcp() {
@Override
protected Stream<Arguments> provideNonRandomArguments() {
Bytes32 arg1 =
Bytes32.fromHexString("0xdcd5cf52e4daec5389587d0d0e996e6ce2d0546b63d3ea0a0dc48ad984d180a9");
Bytes32 arg2 =
Bytes32.fromHexString("0x0479484af4a59464a48818b3980174687661bafb13d06f49537995fa6c02159e");
traceOperation(SGT, arg1, arg2);
}

public static Stream<Arguments> provideRandomArguments() {
final List<Arguments> arguments = new ArrayList<>();
for (OpCode opCode : new WcpTracer().supportedOpCodes()) {
for (int i = 0; i <= TEST_REPETITIONS; i++) {
Bytes32[] payload = new Bytes32[2];
payload[0] = Bytes32.random(rand);
payload[1] = Bytes32.random(rand);
arguments.add(Arguments.of(opCode, payload[0], payload[1]));
}
}
return arguments.stream();
}

private void traceOperation(OpCode opcode, Bytes32 arg1, Bytes32 arg2) {
when(mockOperation.getOpcode()).thenReturn((int) opcode.value);
when(mockFrame.getStackItem(0)).thenReturn(arg1);
when(mockFrame.getStackItem(1)).thenReturn(arg2);
zkTracer.tracePreExecution(mockFrame);
assertThat(CorsetValidator.isValid(zkTraceBuilder.build().toJson())).isTrue();
return Stream.of(Arguments.of(GT, List.of(arg1, arg2)));
}
}

0 comments on commit d671a73

Please sign in to comment.