feat: update structure
This commit is contained in:
181
cs2030s/labs/MockPE2/CS2030STest.java
Normal file
181
cs2030s/labs/MockPE2/CS2030STest.java
Normal file
@@ -0,0 +1,181 @@
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import javax.tools.DiagnosticCollector;
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
/**
|
||||
* A helper class to test CS2030S labs.
|
||||
*/
|
||||
class CS2030STest {
|
||||
|
||||
private static final String ANSI_RESET = "\u001B[0m";
|
||||
private static final String ANSI_RED = "\u001B[31m";
|
||||
private static final String ANSI_GREEN = "\u001B[32m";
|
||||
|
||||
/**
|
||||
* Test if two objects are equals.
|
||||
*
|
||||
* @param test A description of the test.
|
||||
* @param output The output from an expression.
|
||||
* @param expect The expected output from that expression.
|
||||
* @return this object.
|
||||
*/
|
||||
public CS2030STest expect(String test, Object output, Object expect) {
|
||||
System.out.print(test);
|
||||
if ((expect == null && output == null) || output.equals(expect)) {
|
||||
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
|
||||
} else {
|
||||
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
|
||||
System.out.println(" expected: " + expect);
|
||||
System.out.println(" got this: " + output);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a given supplier produces a given object.
|
||||
*
|
||||
* @param <T> The type of object the given task will produce.
|
||||
* @param test A description of the test.
|
||||
* @param task The task to run.
|
||||
* @param expect The expected output from that expression.
|
||||
* @return this object.
|
||||
*/
|
||||
public <T> CS2030STest expect(String test, Supplier<T> task, Object expect) {
|
||||
System.out.print(test);
|
||||
try {
|
||||
T output = CompletableFuture.supplyAsync(task).get(1, TimeUnit.SECONDS);
|
||||
if ((expect == null && output == null) || output.equals(expect)) {
|
||||
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
|
||||
} else {
|
||||
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
|
||||
System.out.println(" expected: " + expect);
|
||||
System.out.println(" got this: " + output);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
|
||||
System.out.println(" with exception: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a given producer returns a value. Wrapper around expect(..)
|
||||
* to simplify caller.
|
||||
*
|
||||
* @param <T> The type of object the given task will produce.
|
||||
* @param test A description of the test.
|
||||
* @param task The task to run.
|
||||
* @param expect The expected output from that expression.
|
||||
* @return this object.
|
||||
*/
|
||||
public <T> CS2030STest expectReturn(String test, Supplier<T> task, Object expect) {
|
||||
return this.expect(test + " returns " + expect, task, expect);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an expression throws an exception.
|
||||
*
|
||||
* @param test A description of the test.
|
||||
* @param task A lambda expression of the expression.
|
||||
* @param expectedE A exception instance of the same type as the expected one.
|
||||
* @return this object.
|
||||
*/
|
||||
public CS2030STest expectException(String test, Runnable task, Exception expectedE) {
|
||||
System.out.print(test + " throws " + expectedE.getClass().getSimpleName());
|
||||
boolean gotException = false;
|
||||
try {
|
||||
task.run();
|
||||
} catch (Exception e) {
|
||||
if (e.getClass().equals(expectedE.getClass())) {
|
||||
gotException = true;
|
||||
}
|
||||
}
|
||||
if (gotException) {
|
||||
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
|
||||
} else {
|
||||
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
|
||||
System.out.println(" did not catch expected exception " + expectedE.getClass());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CS2030STest expectException(String test, Runnable task, Exception expectedE, String msg) {
|
||||
System.out.print(test + " throws " + expectedE.getClass().getSimpleName());
|
||||
boolean gotException = false;
|
||||
boolean gotMessage = false;
|
||||
try {
|
||||
task.run();
|
||||
} catch (Exception e) {
|
||||
if (e.getClass().equals(expectedE.getClass())) {
|
||||
gotException = true;
|
||||
}
|
||||
if (e.getMessage().equals(msg)) {
|
||||
gotMessage = true;
|
||||
}
|
||||
}
|
||||
if (gotException && gotMessage) {
|
||||
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
|
||||
} else {
|
||||
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
|
||||
if (!gotException) {
|
||||
System.out.println(" did not catch expected exception " + expectedE.getClass());
|
||||
}
|
||||
if (!gotMessage) {
|
||||
System.out.println(" caught the right exception but with wrong message");
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an expression compiles with/without error.
|
||||
*
|
||||
* @param test A description of the test.
|
||||
* @param statement The java statement to compile
|
||||
* @param success Whether the statement is expected to compile or not
|
||||
* (true if yes; false otherwise)
|
||||
* @return this object.
|
||||
*/
|
||||
public CS2030STest expectCompile(String test, String statement, boolean success) {
|
||||
System.out.print(test);
|
||||
|
||||
class JavaSourceFromString extends SimpleJavaFileObject {
|
||||
final String code;
|
||||
|
||||
JavaSourceFromString(String code) {
|
||||
super(URI.create("string:///TempClass.java"), Kind.SOURCE);
|
||||
this.code = "class TempClass {void foo(){" + code + ";}}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
boolean noError = ToolProvider
|
||||
.getSystemJavaCompiler()
|
||||
// .getTask(null, null, new DiagnosticCollector<>(), null, null,
|
||||
.getTask(null, null, null, null, null,
|
||||
List.of(new JavaSourceFromString(statement)))
|
||||
.call();
|
||||
|
||||
if (noError != success) {
|
||||
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
|
||||
if (!success) {
|
||||
System.out.println(" expected compilation error but it compiles fine.");
|
||||
} else {
|
||||
System.out.println(" expected the statement to compile without errors but it does not.");
|
||||
}
|
||||
} else {
|
||||
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
12
cs2030s/labs/MockPE2/Main.java
Normal file
12
cs2030s/labs/MockPE2/Main.java
Normal file
@@ -0,0 +1,12 @@
|
||||
import java.util.stream.Stream;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Main {
|
||||
public static String reverse(String s) {
|
||||
return null;
|
||||
}
|
||||
public static Stream<String> palindrome(Stream<String> stream) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
BIN
cs2030s/labs/MockPE2/Question.pdf
Normal file
BIN
cs2030s/labs/MockPE2/Question.pdf
Normal file
Binary file not shown.
146
cs2030s/labs/MockPE2/Reader.java
Normal file
146
cs2030s/labs/MockPE2/Reader.java
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* @author A0000000X
|
||||
*/
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
abstract class Reader<T> {
|
||||
protected final List<T> inputs;
|
||||
protected Reader(List<T> inputs) {
|
||||
this.inputs = inputs;
|
||||
}
|
||||
protected Reader() {
|
||||
this.inputs = null;
|
||||
}
|
||||
|
||||
private static final Reader<?> EOF = new EOF();
|
||||
|
||||
public abstract T read();
|
||||
public abstract Reader<T> consume();
|
||||
public abstract boolean hasNext();
|
||||
public abstract <R> Reader<R> map(Immutator<? extends R, ? super T> f);
|
||||
public abstract Reader<T> flatMap(Immutator<? extends Reader<? extends T>, ? super T> f);
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> Reader<T> of(T... inputs) {
|
||||
List<T> currInputs = new ArrayList<T>();
|
||||
for (int i=0; i<inputs.length; i++) {
|
||||
currInputs.add(inputs[i]);
|
||||
}
|
||||
return new NonEmpty<>(currInputs);
|
||||
}
|
||||
|
||||
public static <T> Reader<T> of() {
|
||||
@SuppressWarnings("unchecked")
|
||||
Reader<T> res = (Reader<T>) EOF;
|
||||
return res;
|
||||
}
|
||||
|
||||
private static class NonEmpty<T> extends Reader<T> {
|
||||
protected NonEmpty(List<T> inputs) {
|
||||
super(inputs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read() {
|
||||
return this.inputs.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader<T> consume() {
|
||||
if (this.inputs.size() == 1) {
|
||||
return Reader.<T>of();
|
||||
}
|
||||
List<T> currInputs = new ArrayList<T>(this.inputs);
|
||||
currInputs.remove(0);
|
||||
return new NonEmpty<>(currInputs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Reader<R> map(Immutator<? extends R, ? super T> f) {
|
||||
List<R> currInputs = new ArrayList<R>();
|
||||
for (int i=0; i<this.inputs.size(); i++) {
|
||||
currInputs.add(f.invoke(this.inputs.get(i)));
|
||||
}
|
||||
return new NonEmpty<>(currInputs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader<T> flatMap(Immutator<? extends Reader<? extends T>, ? super T> f) {
|
||||
Reader<? extends T> tmp = f.invoke(this.inputs.get(0));
|
||||
List<T> currInputs = new ArrayList<T>();
|
||||
for (int i=0; i<tmp.inputs.size(); i++) {
|
||||
currInputs.add(tmp.inputs.get(i));
|
||||
}
|
||||
for (int i=1; i<this.inputs.size(); i++) {
|
||||
currInputs.add(this.inputs.get(i));
|
||||
}
|
||||
return new NonEmpty<>(currInputs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Reader";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof NonEmpty<?>)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class EOF extends Reader<Object> {
|
||||
@Override
|
||||
public Object read() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader<Object> consume() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Reader<R> map(Immutator<? extends R, Object> f) {
|
||||
return Reader.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader<Object> flatMap(Immutator<? extends Reader<? extends Object>, ? super Object> f) {
|
||||
return Reader.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EOF";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof EOF)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
cs2030s/labs/MockPE2/Test1.java
Normal file
37
cs2030s/labs/MockPE2/Test1.java
Normal file
@@ -0,0 +1,37 @@
|
||||
import java.util.List;
|
||||
import cs2030s.fp.Immutator;
|
||||
import cs2030s.fp.Reader;
|
||||
|
||||
/**
|
||||
* Test 1 for CS2030S Mock PE 2 (AY22/23 Sem 2). Tests
|
||||
* for Reader of(), read(), and hasNext() methods
|
||||
*
|
||||
* @author Adi Yoga S. Prabawa
|
||||
*/
|
||||
class Test1 {
|
||||
/**
|
||||
* Main method for Test1.
|
||||
*
|
||||
* @param args Ignored and unused command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
Reader<Integer> intInput = Reader.of(2, 0, 3, 0);
|
||||
i.expect("Reader<Integer> intInput = Reader.of(2, 0, 3, 0)", Reader.of(2, 0, 3, 0).toString(), "Reader");
|
||||
i.expect("intInput.read()", intInput.read(), 2);
|
||||
i.expect("intInput.read() to still read the same thing", intInput.read(), 2);
|
||||
i.expect("intInput.hasNext()", intInput.hasNext(), true);
|
||||
|
||||
Reader<Object> noInput = Reader.of();
|
||||
i.expect("Reader<Object> noInput = Reader.of()", Reader.of().toString(), "EOF");
|
||||
i.expectException("noInput.read()", () -> { noInput.read(); return; }, new java.util.NoSuchElementException());
|
||||
i.expect("intInput.hasNext()", noInput.hasNext(), false);
|
||||
|
||||
Reader<String> strInput = Reader.of("CS", "2030", "S");
|
||||
i.expect("Reader<String> strInput = Reader.of(\"CS\", \"2030\", \"S\")", Reader.of("CS", "2030", "S").toString(), "Reader");
|
||||
i.expect("strInput.read()", strInput.read(), "CS");
|
||||
i.expect("strInput.read() to still read the same thing", strInput.read(), "CS");
|
||||
i.expect("strInput.hasNext()", strInput.hasNext(), true);
|
||||
}
|
||||
}
|
||||
16
cs2030s/labs/MockPE2/Test1.jsh
Normal file
16
cs2030s/labs/MockPE2/Test1.jsh
Normal file
@@ -0,0 +1,16 @@
|
||||
import cs2030s.fp.Immutator
|
||||
import cs2030s.fp.Reader
|
||||
|
||||
Reader<Integer> intInput = Reader.of(2, 0, 3, 0);
|
||||
intInput.read();
|
||||
intInput.read(); // still the same
|
||||
intInput.hasNext();
|
||||
|
||||
Reader<Object> noInput = Reader.of();
|
||||
noInput.read();
|
||||
noInput.hasNext();
|
||||
|
||||
Reader<String> strInput = Reader.of("CS", "2030", "S");
|
||||
strInput.read();
|
||||
strInput.read(); // still the same
|
||||
strInput.hasNext();
|
||||
43
cs2030s/labs/MockPE2/Test2.java
Normal file
43
cs2030s/labs/MockPE2/Test2.java
Normal file
@@ -0,0 +1,43 @@
|
||||
import java.util.List;
|
||||
import cs2030s.fp.Immutator;
|
||||
import cs2030s.fp.Reader;
|
||||
|
||||
/**
|
||||
* Test 2 for CS2030S Mock PE 2 (AY22/23 Sem 2). Tests
|
||||
* for Reader consume() methods
|
||||
*
|
||||
* @author Adi Yoga S. Prabawa
|
||||
*/
|
||||
class Test2 {
|
||||
/**
|
||||
* Main method for Test2.
|
||||
*
|
||||
* @param args Ignored and unused command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
Reader<Integer> intInput = Reader.of(2, 0, 3, 0);
|
||||
i.expect("Reader<Integer> intInput = Reader.of(2, 0, 3, 0)", Reader.of(2, 0, 3, 0).toString(), "Reader");
|
||||
i.expect("intInput.read()", intInput.read(), 2);
|
||||
i.expect("intInput.consume().read()", intInput.consume().read(), 0);
|
||||
i.expect("intInput.consume().consume().read()", intInput.consume().consume().read(), 3);
|
||||
i.expect("intInput.consume().consume().consume().read()", intInput.consume().consume().consume().read(), 0);
|
||||
i.expectException("intInput.consume().consume().consume().consume().read()", () -> { intInput.consume().consume().consume().consume().read(); return; }, new java.util.NoSuchElementException());
|
||||
i.expectException("intInput.consume().consume().consume().consume().consume()", () -> { intInput.consume().consume().consume().consume().consume(); return; }, new java.util.NoSuchElementException());
|
||||
|
||||
|
||||
Reader<Object> noInput = Reader.of();
|
||||
i.expect("Reader<Object> noInput = Reader.of()", Reader.of().toString(), "EOF");
|
||||
i.expectException("noInput.read()", () -> { noInput.read(); return; }, new java.util.NoSuchElementException());
|
||||
i.expectException("noInput.consume()", () -> { noInput.read(); return; }, new java.util.NoSuchElementException());
|
||||
|
||||
Reader<String> strInput = Reader.of("CS", "2030", "S");
|
||||
i.expect("Reader<String> strInput = Reader.of(\"CS\", \"2030\", \"S\")", Reader.of("CS", "2030", "S").toString(), "Reader");
|
||||
i.expect("strInput.read()", strInput.read(), "CS");
|
||||
i.expect("strInput.consume().read()", strInput.consume().read(), "2030");
|
||||
i.expect("strInput.consume().consume().read()", strInput.consume().consume().read(), "S");
|
||||
i.expectException("strInput.consume().consume().consume().read()", () -> { strInput.consume().consume().consume().read(); return; }, new java.util.NoSuchElementException());
|
||||
i.expectException("strInput.consume().consume().consume().consume()", () -> { strInput.consume().consume().consume().consume(); return; }, new java.util.NoSuchElementException());
|
||||
}
|
||||
}
|
||||
17
cs2030s/labs/MockPE2/Test2.jsh
Normal file
17
cs2030s/labs/MockPE2/Test2.jsh
Normal file
@@ -0,0 +1,17 @@
|
||||
import cs2030s.fp.Immutator
|
||||
import cs2030s.fp.Reader
|
||||
|
||||
Reader<Integer> intInput = Reader.of(2, 0, 3, 0);
|
||||
intInput.consume().read();
|
||||
intInput.consume().consume().read();
|
||||
intInput.consume().consume().consume().read();
|
||||
intInput.consume().consume().consume().consume().read();
|
||||
intInput.consume().consume().consume().consume().consume();
|
||||
|
||||
Reader<Object> noInput = Reader.of();
|
||||
noInput.consume();
|
||||
|
||||
Reader.of("CS", "2030", "S").equals(Reader.of(2, 0, 3, 0));
|
||||
Reader.of("CS", "2030", "S").equals(Reader.of());
|
||||
Reader.of().equals(Reader.of(2, 0, 3, 0));
|
||||
Reader.of().equals(Reader.of());
|
||||
28
cs2030s/labs/MockPE2/Test3.java
Normal file
28
cs2030s/labs/MockPE2/Test3.java
Normal file
@@ -0,0 +1,28 @@
|
||||
import java.util.List;
|
||||
import cs2030s.fp.Immutator;
|
||||
import cs2030s.fp.Reader;
|
||||
|
||||
/**
|
||||
* Test 3 for CS2030S Mock PE 2 (AY22/23 Sem 2). Tests
|
||||
* for Reader map() methods
|
||||
*
|
||||
* @author Adi Yoga S. Prabawa
|
||||
*/
|
||||
class Test3 {
|
||||
/**
|
||||
* Main method for Test3.
|
||||
*
|
||||
* @param args Ignored and unused command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
Reader<String> strInput = Reader.of("CS", "2030", "S");
|
||||
Reader<Integer> intInput = strInput.map(s -> s.length());
|
||||
i.expect("intInput.read()", intInput.read(), 2);
|
||||
i.expect("intInput.consume().read()", intInput.consume().read(), 4);
|
||||
i.expect("intInput.consume().consume().read()", intInput.consume().consume().read(), 1);
|
||||
i.expectException("intInput.consume().consume().consume().read()", () -> { intInput.consume().consume().consume().read(); return; }, new java.util.NoSuchElementException());
|
||||
i.expectException("intInput.consume().consume().consume().consume()", () -> { intInput.consume().consume().consume().consume(); return; }, new java.util.NoSuchElementException());
|
||||
}
|
||||
}
|
||||
10
cs2030s/labs/MockPE2/Test3.jsh
Normal file
10
cs2030s/labs/MockPE2/Test3.jsh
Normal file
@@ -0,0 +1,10 @@
|
||||
import cs2030s.fp.Immutator
|
||||
import cs2030s.fp.Reader
|
||||
|
||||
Reader<String> strInput = Reader.of("CS", "2030", "S");
|
||||
Reader<Integer> intInput = strInput.map(s -> s.length());
|
||||
|
||||
intInput.consume().read();
|
||||
intInput.consume().consume().read();
|
||||
intInput.consume().consume().consume().read();
|
||||
intInput.consume().consume().consume().consume();
|
||||
29
cs2030s/labs/MockPE2/Test4.java
Normal file
29
cs2030s/labs/MockPE2/Test4.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.util.List;
|
||||
import cs2030s.fp.Immutator;
|
||||
import cs2030s.fp.Reader;
|
||||
|
||||
/**
|
||||
* Test 4 for CS2030S Mock PE 2 (AY22/23 Sem 2). Tests
|
||||
* for Reader flatMap() methods
|
||||
*
|
||||
* @author Adi Yoga S. Prabawa
|
||||
*/
|
||||
class Test4 {
|
||||
/**
|
||||
* Main method for Test4.
|
||||
*
|
||||
* @param args Ignored and unused command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
Reader<Integer> oriInput = Reader.of(1, 2);
|
||||
Reader<Integer> intInput = oriInput.flatMap(x -> Reader.of(x * 10, x * 20, x * 30));
|
||||
i.expect("intInput.read()", intInput.read(), 10);
|
||||
i.expect("intInput.consume().read()", intInput.consume().read(), 20);
|
||||
i.expect("intInput.consume().consume().read()", intInput.consume().consume().read(), 30);
|
||||
i.expect("intInput.consume().consume().consume().read()", intInput.consume().consume().consume().read(), 2);
|
||||
i.expectException("intInput.consume().consume().consume().consume().read()", () -> { intInput.consume().consume().consume().consume().read(); return; }, new java.util.NoSuchElementException());
|
||||
i.expectException("intInput.consume().consume().consume().consume().consume()", () -> { intInput.consume().consume().consume().consume().consume(); return; }, new java.util.NoSuchElementException());
|
||||
}
|
||||
}
|
||||
12
cs2030s/labs/MockPE2/Test4.jsh
Normal file
12
cs2030s/labs/MockPE2/Test4.jsh
Normal file
@@ -0,0 +1,12 @@
|
||||
import cs2030s.fp.Immutator
|
||||
import cs2030s.fp.Reader
|
||||
|
||||
Reader<Integer> intInput = Reader.of(1, 2);
|
||||
intInput = intInput.flatMap(x -> Reader.of(x * 10, x * 20, x * 30));
|
||||
|
||||
intInput.read();
|
||||
intInput.consume().read();
|
||||
intInput.consume().consume().read();
|
||||
intInput.consume().consume().consume().read();
|
||||
intInput.consume().consume().consume().consume().read();
|
||||
intInput.consume().consume().consume().consume().consume();
|
||||
27
cs2030s/labs/MockPE2/Test5.java
Normal file
27
cs2030s/labs/MockPE2/Test5.java
Normal file
@@ -0,0 +1,27 @@
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Test 5 for CS2030S Mock PE 2 (AY22/23 Sem 2). Tests
|
||||
* for Stream methods
|
||||
*
|
||||
* @author Adi Yoga S. Prabawa
|
||||
*/
|
||||
class Test5 {
|
||||
/**
|
||||
* Main method for Test5.
|
||||
*
|
||||
* @param args Ignored and unused command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
i.expect("Main.reverse(\"adam\")", Main.reverse("adam").toString(), "mada");
|
||||
i.expect("Main.reverse(\"eve\")", Main.reverse("eve").toString(), "eve");
|
||||
i.expect("Main.reverse(\"adi\")", Main.reverse("adi").toString(), "ida");
|
||||
i.expect("Main.reverse(\"racecar\")", Main.reverse("racecar").toString(), "racecar");
|
||||
|
||||
List<String> words = List.of("adam", "eve", "adi", "racecar", "madam", "anna");
|
||||
i.expect("only 'eve', 'racecar', 'madam', 'anna' are palindrome", Main.palindrome(words.stream()).collect(Collectors.toList()), List.of("eve", "racecar", "madam", "anna"));
|
||||
}
|
||||
}
|
||||
10
cs2030s/labs/MockPE2/Test5.jsh
Normal file
10
cs2030s/labs/MockPE2/Test5.jsh
Normal file
@@ -0,0 +1,10 @@
|
||||
import cs2030s.fp.Immutator
|
||||
import cs2030s.fp.Reader
|
||||
|
||||
Main.reverse("adam");
|
||||
Main.reverse("eve");
|
||||
Main.reverse("adi");
|
||||
Main.reverse("racecar");
|
||||
|
||||
List<String> words = List.of("adam", "eve", "adi", "racecar", "madam", "anna");
|
||||
Main.palindrome(words.stream()).forEach(System.out::println);
|
||||
13
cs2030s/labs/MockPE2/Utilities.java
Normal file
13
cs2030s/labs/MockPE2/Utilities.java
Normal file
@@ -0,0 +1,13 @@
|
||||
import java.util.stream.Stream;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Utilities {
|
||||
public static Stream<Character> split(String s) {
|
||||
List<Character> chars = new ArrayList<>();
|
||||
for (int i=0; i<s.length(); i++) {
|
||||
chars.add(s.charAt(i));
|
||||
}
|
||||
return chars.stream();
|
||||
}
|
||||
}
|
||||
19
cs2030s/labs/MockPE2/cs2030s/fp/Immutator.java
Normal file
19
cs2030s/labs/MockPE2/cs2030s/fp/Immutator.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Represent a function that transforms one value into another, possible of different types.
|
||||
* CS2030S Mock PE 2
|
||||
* AY22/23 Semester 2
|
||||
*
|
||||
* @param <R> The type of the result value
|
||||
* @param <P> The type of the input value
|
||||
*/
|
||||
package cs2030s.fp;
|
||||
|
||||
public interface Immutator<R, P> {
|
||||
/**
|
||||
* The function method to transforms the value p.
|
||||
*
|
||||
* @param p The input value
|
||||
* @return The value after applying the given immutator on p.
|
||||
*/
|
||||
R invoke(P p);
|
||||
}
|
||||
3
cs2030s/labs/MockPE2/cs2030s/fp/Reader.java
Normal file
3
cs2030s/labs/MockPE2/cs2030s/fp/Reader.java
Normal file
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* @author A0000000X
|
||||
*/
|
||||
Reference in New Issue
Block a user