Initial commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Gradle build configuration for specific lab module / exercise
|
||||
*/
|
||||
// enabled plugins
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
// Project/Module information
|
||||
description = 'Lab06 Stepik'
|
||||
group = 'ch.zhaw.prog2'
|
||||
version = '2022.1'
|
||||
|
||||
// Dependency configuration
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Junit 5 dependencies
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.+'
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.+'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.+'
|
||||
// Mockito dependencies
|
||||
testImplementation 'org.mockito:mockito-core:4.3.+'
|
||||
}
|
||||
|
||||
// Test task configuration
|
||||
test {
|
||||
// Use JUnit platform for unit tests
|
||||
useJUnitPlatform()
|
||||
// Output results of individual tests
|
||||
testLogging {
|
||||
events "PASSED", "SKIPPED", "FAILED"
|
||||
}
|
||||
}
|
||||
|
||||
// Java plugin configuration
|
||||
java {
|
||||
// By default the Java version of the gradle process is used as source/target version.
|
||||
// This can be overridden, to ensure a specific version. Enable only if required.
|
||||
sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
|
||||
targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
|
||||
|
||||
// Java compiler specific options
|
||||
compileJava {
|
||||
// source files should be UTF-8 encoded
|
||||
options.encoding = 'UTF-8'
|
||||
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ch.zhaw.prog2.functional.stepik;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
public class ComposingPredicate {
|
||||
|
||||
/**
|
||||
* Write a solution which is using streams.
|
||||
*
|
||||
* @see #disjunctAllNoStream(List)
|
||||
*/
|
||||
public static IntPredicate disjunctAll(List<IntPredicate> predicates) {
|
||||
throw new UnsupportedOperationException(); // TODO: remove this line and implement your solution
|
||||
}
|
||||
|
||||
/**
|
||||
* Classical implementation provided by lecturer to help you solve this exercise.
|
||||
* <p>
|
||||
* This solution works, but you have to search a solution using streams which will lead you
|
||||
* to a solution with less lines of code.
|
||||
*/
|
||||
public static IntPredicate disjunctAllNoStream(List<IntPredicate> predicates) {
|
||||
IntPredicate disjunct = x -> false;
|
||||
for (IntPredicate currentPredicate : predicates) {
|
||||
disjunct = disjunct.or(currentPredicate);
|
||||
}
|
||||
return disjunct;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package ch.zhaw.prog2.functional.stepik;
|
||||
|
||||
import ch.zhaw.prog2.functional.stepik.ComposingPredicate;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Do not modify this test class
|
||||
*/
|
||||
class ComposingPredicateTest {
|
||||
private static final IntPredicate isEven = x -> x % 2 == 0;
|
||||
private static final IntPredicate isDividableBy3 = x -> x % 3 == 0;
|
||||
private static final List<IntPredicate> predicateList = List.of(isEven, isDividableBy3);
|
||||
private List<Integer> expected;
|
||||
private IntStream testIntegers;
|
||||
|
||||
/*
|
||||
* This tests your solution
|
||||
*/
|
||||
@Test
|
||||
@Disabled("This exercise is not mandatory. Enable it, if you solve this exercise.")
|
||||
void disjunctAll() {
|
||||
assertDoesNotThrow(
|
||||
() -> ComposingPredicate.disjunctAll(List.of(x -> true)),
|
||||
"You have to implement ComposingPredicate.disjunctAll"
|
||||
);
|
||||
IntPredicate alwaysTrue = ComposingPredicate.disjunctAll(List.of(x -> true));
|
||||
assertTrue(alwaysTrue.test(1), "Test with one predicate only");
|
||||
|
||||
IntPredicate dividableBy2Or3 = ComposingPredicate.disjunctAll(predicateList);
|
||||
assertArrayEquals(expected.toArray(), testIntegers.filter(dividableBy2Or3).boxed().toArray());
|
||||
}
|
||||
|
||||
/*
|
||||
* This tests the given classical solution without streams
|
||||
*/
|
||||
@Test
|
||||
void disjunctAllNoStream() {
|
||||
IntPredicate alwaysTrue = ComposingPredicate.disjunctAllNoStream(List.of(x -> true));
|
||||
assertTrue(alwaysTrue.test(1), "Test with one predicate only");
|
||||
|
||||
IntPredicate dividableBy2Or3 = ComposingPredicate.disjunctAllNoStream(predicateList);
|
||||
assertArrayEquals(expected.toArray(), testIntegers.filter(dividableBy2Or3).boxed().toArray());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testIntegers = IntStream.range(1, 10);
|
||||
expected = List.of(2, 3, 4, 6, 8, 9);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user