import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.Arrays; import java.util.Random; public class SolutionTest { public static int speedLimit(int speed, int[] signals) { int penalty = 0; for (int i = 0; i < signals.length; i++){ if (speed > signals[i]){ if (speed - signals[i] >= 30){ penalty += 500; } else if (speed - signals[i] >= 20 && speed - signals[i] < 30){ penalty += 250; } else if (speed - signals[i] >= 10 && speed - signals[i] < 20){ penalty += 100; } } } return penalty; } @Test public void randomTestCases() { final int SPEED_LIMIT = 220; final int[] SIGNALS_LIMIT = new int [] {80, 90, 100, 110, 120}; for (int i = 0; i < 100; i++) { int speed = randIntSpeed(SPEED_LIMIT); int[]signals = new int [new Random().nextInt(20)]; for (int j = 0; j < signals.length ; j++) signals[j] =randIntSignals(SIGNALS_LIMIT); int result =speedLimit(speed, signals); assertEquals("Testing for: " + speed + Arrays.toString(signals), result, Kata.speedLimit(speed, signals)); } } private int randIntSpeed(int speedLimit) {//cualquier velocidad debajo del limite. return (int)(Math.random() * speedLimit) + 1; } private int randIntSignals(int[] signalsLimit) {//cualquier señal int random = (int)(Math.random() * 4)+1; return signalsLimit[random]; } }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- import java.util.Arrays;
- import java.util.Random;
// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {@Testvoid testSomething() {// assertEquals("expected", "actual");- public class SolutionTest {
- public static int speedLimit(int speed, int[] signals) {
- int penalty = 0;
- for (int i = 0; i < signals.length; i++){
- if (speed > signals[i]){
- if (speed - signals[i] >= 30){
- penalty += 500;
- } else if (speed - signals[i] >= 20 && speed - signals[i] < 30){
- penalty += 250;
- } else if (speed - signals[i] >= 10 && speed - signals[i] < 20){
- penalty += 100;
- }
- }
- }
- return penalty;
- }
- @Test
- public void randomTestCases() {
- final int SPEED_LIMIT = 220;
- final int[] SIGNALS_LIMIT = new int [] {80, 90, 100, 110, 120};
- for (int i = 0; i < 100; i++) {
- int speed = randIntSpeed(SPEED_LIMIT);
- int[]signals = new int [new Random().nextInt(20)];
- for (int j = 0; j < signals.length ; j++) signals[j] =randIntSignals(SIGNALS_LIMIT);
- int result =speedLimit(speed, signals);
- assertEquals("Testing for: " + speed + Arrays.toString(signals),
- result, Kata.speedLimit(speed, signals));
- }
- }
- private int randIntSpeed(int speedLimit) {//cualquier velocidad debajo del limite.
- return (int)(Math.random() * speedLimit) + 1;
- }
}- private int randIntSignals(int[] signalsLimit) {//cualquier señal
- int random = (int)(Math.random() * 4)+1;
- return signalsLimit[random];
- }
- }