feat: change up organisation
This commit is contained in:
12
labs/cs2030s/Lab4/Action.java
Normal file
12
labs/cs2030s/Lab4/Action.java
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* The Action interface that can be called
|
||||
* on an object of type T to act.
|
||||
* Contains a single abstract method call.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
interface Action<T> {
|
||||
void call(T item);
|
||||
}
|
||||
13
labs/cs2030s/Lab4/Actionable.java
Normal file
13
labs/cs2030s/Lab4/Actionable.java
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* The Actionable interface that can
|
||||
* act when given an action.
|
||||
* Contains a single abstract method act.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
// act consumes the T and passes it to action, thus super
|
||||
interface Actionable<T> {
|
||||
public void act(Action<? super T> action);
|
||||
}
|
||||
14
labs/cs2030s/Lab4/Applicable.java
Normal file
14
labs/cs2030s/Lab4/Applicable.java
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* The Applicable interface that can probably
|
||||
* transform if given something that is
|
||||
* probably an Immutator.
|
||||
* Contains a single abstract method apply.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
interface Applicable<T> {
|
||||
<R> Applicable<R> apply(Probably<? extends Immutator<? extends R, ? super T>> probablyImmutator);
|
||||
}
|
||||
75
labs/cs2030s/Lab4/CS2030STest.java
Normal file
75
labs/cs2030s/Lab4/CS2030STest.java
Normal file
@@ -0,0 +1,75 @@
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import javax.tools.DiagnosticCollector;
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.PrintStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
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";
|
||||
|
||||
public void 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);
|
||||
}
|
||||
}
|
||||
|
||||
public static String clean(String txt) {
|
||||
String res = "";
|
||||
for (int i=0; i<txt.length(); i++) {
|
||||
if (txt.charAt(i) != '\r' && txt.charAt(i) != '\n') {
|
||||
res += txt.charAt(i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public void expectPrint(String test, Object expect, ByteArrayOutputStream baos, PrintStream old) {
|
||||
System.out.flush();
|
||||
System.setOut(old);
|
||||
expect(test, CS2030STest.clean(baos.toString()), expect);
|
||||
}
|
||||
|
||||
public void 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,
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
labs/cs2030s/Lab4/Immutator.java
Normal file
12
labs/cs2030s/Lab4/Immutator.java
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* The Immutator interface that can transform
|
||||
* to type T2, an object of type T1.
|
||||
* Contains a single abstract method invoke.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
interface Immutator<R, P> {
|
||||
public R invoke(P param);
|
||||
}
|
||||
13
labs/cs2030s/Lab4/Immutatorable.java
Normal file
13
labs/cs2030s/Lab4/Immutatorable.java
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* The Immutatorable interface that can
|
||||
* transform when given something that is
|
||||
* Immutator.
|
||||
* Contains a single abstract method transform.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
interface Immutatorable<T> {
|
||||
public <R> Immutatorable<R> transform(Immutator<? extends R, ? super T> immutator);
|
||||
}
|
||||
15
labs/cs2030s/Lab4/Improbable.java
Normal file
15
labs/cs2030s/Lab4/Improbable.java
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* A generic Immutator that takes in an object
|
||||
* that is T and returns an object that is probably T.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
class Improbable<T> implements Immutator<Probably<T>, T> {
|
||||
@Override
|
||||
public Probably<T> invoke(T param) {
|
||||
return Probably.just(param);
|
||||
}
|
||||
}
|
||||
27
labs/cs2030s/Lab4/IsModEq.java
Normal file
27
labs/cs2030s/Lab4/IsModEq.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* A non-generic Immutator with parameter
|
||||
* div and mod that takes in an integer val
|
||||
* and return the boolean value by checking
|
||||
* if the given value is equal to mod when
|
||||
* divided by div.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
class IsModEq implements Immutator<Boolean, Integer> {
|
||||
|
||||
private int div;
|
||||
private int check;
|
||||
|
||||
public IsModEq(int div, int check) {
|
||||
this.div = div;
|
||||
this.check = check;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean invoke(Integer val) {
|
||||
return val % div == check;
|
||||
}
|
||||
}
|
||||
247
labs/cs2030s/Lab4/Lab4.java
Normal file
247
labs/cs2030s/Lab4/Lab4.java
Normal file
@@ -0,0 +1,247 @@
|
||||
import java.util.Scanner;
|
||||
import java.io.PrintStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* The main class for CS2030S Lab 4.
|
||||
*
|
||||
* @author Wei Tsang
|
||||
* @version CS2030S AY21/22 Semester 2
|
||||
*/
|
||||
class Lab4 {
|
||||
|
||||
/**
|
||||
* Inner class for testing.
|
||||
*/
|
||||
static class Incr implements Immutator<Integer,Integer> {
|
||||
public Integer invoke(Integer t1) {
|
||||
return t1 + 1;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Inner class for testing.
|
||||
*/
|
||||
static class Length implements Immutator<Integer,String> {
|
||||
public Integer invoke(String t1) {
|
||||
return t1.length();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to clean a string from
|
||||
* any newline.
|
||||
*
|
||||
* @param txt Input string.
|
||||
* @return The cleaned string.
|
||||
*/
|
||||
public static String clean(String txt) {
|
||||
String res = "";
|
||||
for (int i=0; i<txt.length(); i++) {
|
||||
if (txt.charAt(i) != '\r' && txt.charAt(i) != '\n') {
|
||||
res += txt.charAt(i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Create a scanner to read from standard input.
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// Read a single integer from the test file
|
||||
// and then run the appropriate test case
|
||||
switch (sc.nextInt()) {
|
||||
case 1:
|
||||
test1();
|
||||
break;
|
||||
case 2:
|
||||
test2();
|
||||
break;
|
||||
case 3:
|
||||
test3();
|
||||
break;
|
||||
case 4:
|
||||
test4();
|
||||
break;
|
||||
case 5:
|
||||
test5();
|
||||
break;
|
||||
case 6:
|
||||
test6();
|
||||
break;
|
||||
}
|
||||
|
||||
// Clean up the scanner.
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test #1.
|
||||
*/
|
||||
public static void test1() {
|
||||
PrintStream old = System.out;
|
||||
ByteArrayOutputStream baos;
|
||||
PrintStream ps;
|
||||
|
||||
try {
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
new Print().call(17);
|
||||
System.out.flush();
|
||||
System.setOut(old);
|
||||
|
||||
System.out.println(Lab4.clean(baos.toString()));
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
|
||||
try {
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
new Print().call("string");
|
||||
System.out.flush();
|
||||
System.setOut(old);
|
||||
|
||||
System.out.println(Lab4.clean(baos.toString()));
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test #2.
|
||||
*/
|
||||
public static void test2() {
|
||||
PrintStream old = System.out;
|
||||
ByteArrayOutputStream baos;
|
||||
PrintStream ps;
|
||||
|
||||
try {
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
Probably.just(4).act(new Print());
|
||||
System.out.flush();
|
||||
System.setOut(old);
|
||||
|
||||
System.out.println(Lab4.clean(baos.toString()));
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
|
||||
try {
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
Probably.just("string").act(new Print());
|
||||
System.out.flush();
|
||||
System.setOut(old);
|
||||
|
||||
System.out.println(Lab4.clean(baos.toString()));
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
|
||||
try {
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
Probably.none().act(new Print());
|
||||
System.out.flush();
|
||||
System.setOut(old);
|
||||
|
||||
System.out.println(Lab4.clean(baos.toString()));
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test #3.
|
||||
*/
|
||||
public static void test3() {
|
||||
try {
|
||||
System.out.println(new Incr().invoke(4).toString());
|
||||
System.out.println(new Incr().invoke(new Incr().invoke(4)).toString());
|
||||
System.out.println(new Length().invoke("string").toString());
|
||||
System.out.println(new Incr().invoke(new Length().invoke("string")).toString());
|
||||
|
||||
System.out.println(new Improbable<Integer>().invoke(1).toString());
|
||||
System.out.println(new Improbable<String>().invoke(null).toString());
|
||||
System.out.println(new Improbable<Integer>().invoke(1).transform(new Incr()).toString());
|
||||
System.out.println(new Improbable<>().invoke(new Improbable<Integer>().invoke(1)).toString());
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test #4.
|
||||
*/
|
||||
public static void test4() {
|
||||
try {
|
||||
System.out.println(Probably.just(4).transform(new Incr()).toString());
|
||||
System.out.println(Probably.just(4).transform(new Incr()).transform(new Incr()).toString());
|
||||
System.out.println(Probably.just("string").transform(new Length()).toString());
|
||||
System.out.println(Probably.just("string").transform(new Length()).transform(new Incr()).toString());
|
||||
|
||||
System.out.println(Probably.<Integer>none().transform(new Incr()).toString());
|
||||
System.out.println(Probably.<String>none().transform(new Length()).toString());
|
||||
System.out.println(Probably.<String>just(null).transform(new Length()).transform(new Incr()).toString());
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test #5.
|
||||
*/
|
||||
public static void test5() {
|
||||
Probably<Immutator<Integer,Integer>> justIncr = Probably.just(new Incr());
|
||||
Probably<Immutator<Integer,String>> justLength = Probably.just(new Length());
|
||||
Probably<Immutator<Integer,Integer>> noIncr = Probably.none();
|
||||
Probably<Immutator<Integer,String>> noLength = Probably.none();
|
||||
|
||||
try {
|
||||
System.out.println(Probably.just(17).check(new IsModEq(3,2)).toString());
|
||||
System.out.println(Probably.just(18).check(new IsModEq(3,2)).toString());
|
||||
|
||||
System.out.println(Probably.just(16).transform(new Incr()).check(new IsModEq(3,2)).toString());
|
||||
System.out.println(Probably.just("string").transform(new Length()).transform(new Incr()).transform(new Incr()).check(new IsModEq(3,2)).toString());
|
||||
System.out.println(Probably.<Integer>just(null).check(new IsModEq(0,2)).toString());
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test #6.
|
||||
*/
|
||||
public static void test6() {
|
||||
Probably<Immutator<Integer,Integer>> justIncr = Probably.just(new Incr());
|
||||
Probably<Immutator<Integer,String>> justLength = Probably.just(new Length());
|
||||
Probably<Immutator<Integer,Integer>> noIncr = Probably.none();
|
||||
Probably<Immutator<Integer,String>> noLength = Probably.none();
|
||||
|
||||
try {
|
||||
System.out.println(Probably.just(17).apply(justIncr).toString());
|
||||
System.out.println(Probably.<Integer>none().apply(justIncr).toString());
|
||||
System.out.println(Probably.just(17).apply(noIncr).toString());
|
||||
System.out.println(Probably.<Integer>none().apply(noIncr).toString());
|
||||
|
||||
System.out.println(Probably.just("string").apply(justLength).toString());
|
||||
System.out.println(Probably.<String>none().apply(justLength).toString());
|
||||
System.out.println(Probably.just("string").apply(noLength).toString());
|
||||
System.out.println(Probably.<String>none().apply(noLength).toString());
|
||||
} catch(Exception e) {
|
||||
System.out.println("Error occurred");
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
labs/cs2030s/Lab4/Lab4.pdf
Normal file
BIN
labs/cs2030s/Lab4/Lab4.pdf
Normal file
Binary file not shown.
60
labs/cs2030s/Lab4/MyTest.java
Normal file
60
labs/cs2030s/Lab4/MyTest.java
Normal file
@@ -0,0 +1,60 @@
|
||||
interface Function<T, R> {
|
||||
R apply(T t);
|
||||
}
|
||||
|
||||
interface Functor<T, F extends Functor<?, ?>> {
|
||||
<R> F map(Function<T, R> f);
|
||||
}
|
||||
|
||||
class Identity<T> implements Functor<T, Identity<?>> {
|
||||
private final T value;
|
||||
|
||||
public Identity(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Identity<R> map(Function<T, R> f) {
|
||||
final R result = f.apply(this.value);
|
||||
return new Identity<>(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Incr implements Immutator<Integer, Integer> {
|
||||
|
||||
@Override
|
||||
public Integer invoke(Integer param) {
|
||||
return param + 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Length implements Immutator<Integer, String> {
|
||||
|
||||
@Override
|
||||
public Integer invoke(String param) {
|
||||
return param.length();
|
||||
}
|
||||
}
|
||||
|
||||
class Print implements Action<Object> {
|
||||
|
||||
@Override
|
||||
public void call(Object item) {
|
||||
System.out.println(item);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MyTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Probably<Integer> maybeInt = Probably.just(10);
|
||||
maybeInt.act(new Print());
|
||||
System.out.println(Probably.just(2030).check(new IsModEq(0, 2)));
|
||||
|
||||
Identity<String> idString = new Identity<>("abc");
|
||||
Identity<Integer> idInt = idString.map(String::length);
|
||||
}
|
||||
}
|
||||
17
labs/cs2030s/Lab4/Print.java
Normal file
17
labs/cs2030s/Lab4/Print.java
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* A non-generic Action to print the String
|
||||
* representation of the object.
|
||||
* CS2030S Lab 4
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @author Yadunand Prem (10B)
|
||||
*/
|
||||
|
||||
class Print implements Action<Object> {
|
||||
|
||||
@Override
|
||||
public void call(Object item) {
|
||||
System.out.println(item);
|
||||
}
|
||||
|
||||
}
|
||||
134
labs/cs2030s/Lab4/Probably.java
Normal file
134
labs/cs2030s/Lab4/Probably.java
Normal file
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* This class implements something that
|
||||
* probably is just a value but may be nothing.
|
||||
* We will never return null in this class, but
|
||||
* we may return something that contains nothing
|
||||
* where the nothing is a null.
|
||||
*
|
||||
* @author Yadunand Prem (10B)
|
||||
* @version CS2030S AY22/23 Semester 1
|
||||
*/
|
||||
class Probably<T> implements Actionable<T>, Immutatorable<T>, Applicable<T> {
|
||||
private final T value;
|
||||
|
||||
private static final Probably<?> NONE = new Probably<>(null);
|
||||
|
||||
/**
|
||||
* Private constructor, can only be invoked inside.
|
||||
* This is called a factory method. We can only
|
||||
* create this using the two public static method.
|
||||
*
|
||||
* @param value T
|
||||
*/
|
||||
private Probably(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* It is probably nothing, no value inside.
|
||||
*
|
||||
* @param <T> type T
|
||||
* @return The shared NOTHING.
|
||||
*/
|
||||
public static <T> Probably<T> none() {
|
||||
@SuppressWarnings("unchecked")
|
||||
Probably<T> res = (Probably<T>) NONE;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* It is probably just the given value.
|
||||
* Unless the value is null, then nothing is
|
||||
* given again.
|
||||
*
|
||||
* @param <T> type T
|
||||
*
|
||||
* @param value Probably this is the value
|
||||
* unless it is null then we say
|
||||
* that there is no
|
||||
* @return The given value or nothing but
|
||||
* never null.
|
||||
*/
|
||||
public static <T> Probably<T> just(T value) {
|
||||
if (value == null) {
|
||||
return none();
|
||||
}
|
||||
return new Probably<>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for equality between something that
|
||||
* is probably a value but maybe nothing.
|
||||
*
|
||||
* @param obj The other value to be compared.
|
||||
* @return True if the two values are equal,
|
||||
* false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Probably<?>) {
|
||||
Probably<?> some = (Probably<?>) obj;
|
||||
if (this.value == some.value) {
|
||||
return true;
|
||||
}
|
||||
if (this.value == null || some.value == null) {
|
||||
return false;
|
||||
}
|
||||
return this.value.equals(some.value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* String representation of something that
|
||||
* is probably a value but maybe nothing.
|
||||
*
|
||||
* @return The string representation.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
if (this.value == null) {
|
||||
return "<>";
|
||||
} else {
|
||||
return "<" + this.value.toString() + ">";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void act(Action<? super T> action) {
|
||||
if (this.value == null) {
|
||||
return;
|
||||
}
|
||||
action.call(this.value);
|
||||
}
|
||||
|
||||
public Probably<T> check(Immutator<Boolean, ? super T> eq) {
|
||||
if (this.value == null) {
|
||||
return none();
|
||||
}
|
||||
if (eq.invoke(this.value)) {
|
||||
return this;
|
||||
}
|
||||
return none();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Probably<R> transform(Immutator<? extends R, ? super T> immutator) {
|
||||
if (this.value == null) {
|
||||
return none();
|
||||
}
|
||||
return just(immutator.invoke(this.value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Probably<R> apply(Probably<? extends Immutator<? extends R, ? super T>> probablyImmutator) {
|
||||
if (probablyImmutator.value != null) {
|
||||
return this.transform(probablyImmutator.value);
|
||||
}
|
||||
return none();
|
||||
}
|
||||
|
||||
}
|
||||
32
labs/cs2030s/Lab4/Test1.java
Normal file
32
labs/cs2030s/Lab4/Test1.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import java.io.PrintStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
class Test1 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
PrintStream old = System.out;
|
||||
ByteArrayOutputStream baos;
|
||||
PrintStream ps;
|
||||
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
new Print().call(17);
|
||||
we.expectPrint("new Print().call(17)",
|
||||
"17",
|
||||
baos,
|
||||
old);
|
||||
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
new Print().call("string");
|
||||
we.expectPrint("new Print().call(\"string\")",
|
||||
"string",
|
||||
baos,
|
||||
old);
|
||||
}
|
||||
}
|
||||
42
labs/cs2030s/Lab4/Test2.java
Normal file
42
labs/cs2030s/Lab4/Test2.java
Normal file
@@ -0,0 +1,42 @@
|
||||
import java.io.PrintStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
class Test2 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
PrintStream old = System.out;
|
||||
ByteArrayOutputStream baos;
|
||||
PrintStream ps;
|
||||
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
Probably.just(4).act(new Print());
|
||||
we.expectPrint("Probably.just(4).act(new Print())",
|
||||
"4",
|
||||
baos,
|
||||
old);
|
||||
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
Probably.just("string").act(new Print());
|
||||
we.expectPrint("Probably.just(\"string\").act(new Print())",
|
||||
"string",
|
||||
baos,
|
||||
old);
|
||||
|
||||
baos = new ByteArrayOutputStream();
|
||||
ps = new PrintStream(baos);
|
||||
System.setOut(ps);
|
||||
|
||||
Probably.none().act(new Print());
|
||||
we.expectPrint("Probably.none().act(new Print())",
|
||||
"",
|
||||
baos,
|
||||
old);
|
||||
}
|
||||
}
|
||||
43
labs/cs2030s/Lab4/Test3.java
Normal file
43
labs/cs2030s/Lab4/Test3.java
Normal file
@@ -0,0 +1,43 @@
|
||||
class Test3 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
class Incr implements Immutator<Integer, Integer> {
|
||||
public Integer invoke(Integer t1) {
|
||||
return t1 + 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Length implements Immutator<Integer, String> {
|
||||
public Integer invoke(String t1) {
|
||||
return t1.length();
|
||||
}
|
||||
}
|
||||
|
||||
we.expect("new Incr().invoke(4)",
|
||||
new Incr().invoke(4).toString(),
|
||||
"5");
|
||||
we.expect("new Incr().invoke(new Incr().invoke(4))",
|
||||
new Incr().invoke(new Incr().invoke(4)).toString(),
|
||||
"6");
|
||||
we.expect("new Length().invoke(\"string\")",
|
||||
new Length().invoke("string").toString(),
|
||||
"6");
|
||||
we.expect("new Incr().invoke(new Length().invoke(\"string\"))",
|
||||
new Incr().invoke(new Length().invoke("string")).toString(),
|
||||
"7");
|
||||
|
||||
we.expect("new Improbable<>().invoke(1)",
|
||||
new Improbable<Integer>().invoke(1).toString(),
|
||||
"<1>");
|
||||
we.expect("new Improbable<String>().invoke(null)",
|
||||
new Improbable<String>().invoke(null).toString(),
|
||||
"<>");
|
||||
we.expect("new Improbable<Integer>().invoke(1).transform(new Incr())",
|
||||
new Improbable<Integer>().invoke(1).transform(new Incr()).toString(),
|
||||
"<2>");
|
||||
we.expect("new Improbable<>().invoke(new Improbable<>().invoke(1))",
|
||||
new Improbable<>().invoke(new Improbable<Integer>().invoke(1)).toString(),
|
||||
"<<1>>");
|
||||
}
|
||||
}
|
||||
40
labs/cs2030s/Lab4/Test4.java
Normal file
40
labs/cs2030s/Lab4/Test4.java
Normal file
@@ -0,0 +1,40 @@
|
||||
class Test4 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
class Incr implements Immutator<Integer, Integer> {
|
||||
public Integer invoke(Integer t1) {
|
||||
return t1 + 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Length implements Immutator<Integer, String> {
|
||||
public Integer invoke(String t1) {
|
||||
return t1.length();
|
||||
}
|
||||
}
|
||||
|
||||
we.expect("Probably.just(4).transform(new Incr())",
|
||||
Probably.just(4).transform(new Incr()).toString(),
|
||||
"<5>");
|
||||
we.expect("Probably.just(4).transform(new Incr()).transform(new Incr())",
|
||||
Probably.just(4).transform(new Incr()).transform(new Incr()).toString(),
|
||||
"<6>");
|
||||
we.expect("Probably.just(\"string\").transform(new Length())",
|
||||
Probably.just("string").transform(new Length()).toString(),
|
||||
"<6>");
|
||||
we.expect("Probably.just(\"string\").transform(new Length()).transform(new Incr())",
|
||||
Probably.just("string").transform(new Length()).transform(new Incr()).toString(),
|
||||
"<7>");
|
||||
|
||||
we.expect("Probably.<Integer>none().transform(new Incr())",
|
||||
Probably.<Integer>none().transform(new Incr()).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.<String>none().transform(new Length())",
|
||||
Probably.<String>none().transform(new Length()).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.<String>just(null).transform(new Length()).transform(new Incr())",
|
||||
Probably.<String>just(null).transform(new Length()).transform(new Incr()).toString(),
|
||||
"<>");
|
||||
}
|
||||
}
|
||||
34
labs/cs2030s/Lab4/Test5.java
Normal file
34
labs/cs2030s/Lab4/Test5.java
Normal file
@@ -0,0 +1,34 @@
|
||||
class Test5 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
class Incr implements Immutator<Integer, Integer> {
|
||||
public Integer invoke(Integer t1) {
|
||||
return t1 + 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Length implements Immutator<Integer, String> {
|
||||
public Integer invoke(String t1) {
|
||||
return t1.length();
|
||||
}
|
||||
}
|
||||
|
||||
we.expect("Probably.just(17).check(new IsModEq(3,2)) // 17 % 3 is equal to 2",
|
||||
Probably.just(17).check(new IsModEq(3, 2)).toString(),
|
||||
"<17>");
|
||||
we.expect("Probably.just(18).check(new IsModEq(3,2)) // 18 % 3 is not equal to 2",
|
||||
Probably.just(18).check(new IsModEq(3,2)).toString(),
|
||||
"<>");
|
||||
|
||||
we.expect("Probably.just(16).transform(new Incr()).check(new IsModEq(3,2)) // 17 % 3 is not equal to 2",
|
||||
Probably.just(16).transform(new Incr()).check(new IsModEq(3,2)).toString(),
|
||||
"<17>");
|
||||
we.expect("Probably.just(\"string\").transform(new Length()).check(new IsModEq(3,2))",
|
||||
Probably.just("string").transform(new Length()).transform(new Incr()).transform(new Incr()).check(new IsModEq(3,2)).toString(),
|
||||
"<8>");
|
||||
we.expect("Probably.<Integer>just(null).check(new IsModEq(0,2))",
|
||||
Probably.<Integer>just(null).check(new IsModEq(0,2)).toString(),
|
||||
"<>");
|
||||
}
|
||||
}
|
||||
48
labs/cs2030s/Lab4/Test6.java
Normal file
48
labs/cs2030s/Lab4/Test6.java
Normal file
@@ -0,0 +1,48 @@
|
||||
class Test6 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
class Incr implements Immutator<Integer, Integer> {
|
||||
public Integer invoke(Integer t1) {
|
||||
return t1 + 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Length implements Immutator<Integer, String> {
|
||||
public Integer invoke(String t1) {
|
||||
return t1.length();
|
||||
}
|
||||
}
|
||||
|
||||
Probably<Immutator<Integer, Integer>> justIncr = Probably.just(new Incr());
|
||||
Probably<Immutator<Integer, String>> justLength = Probably.just(new Length());
|
||||
Probably<Immutator<Integer, Integer>> noIncr = Probably.none();
|
||||
Probably<Immutator<Integer, String>> noLength = Probably.none();
|
||||
|
||||
we.expect("Probably.just(17).<Integer>apply(justIncr)",
|
||||
Probably.just(17).apply(justIncr).toString(),
|
||||
"<18>");
|
||||
we.expect("Probably.<Integer>none().<Integer>apply(justIncr)",
|
||||
Probably.<Integer>none().apply(justIncr).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.just(17).<Integer>apply(noIncr)",
|
||||
Probably.just(17).apply(noIncr).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.<Integer>none().<Integer>apply(noIncr)",
|
||||
Probably.<Integer>none().apply(noIncr).toString(),
|
||||
"<>");
|
||||
|
||||
we.expect("Probably.just(\"string\").<Integer>apply(justLength)",
|
||||
Probably.just("string").apply(justLength).toString(),
|
||||
"<6>");
|
||||
we.expect("Probably.<String>none().<Integer>apply(justLength)",
|
||||
Probably.<String>none().apply(justLength).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.just(\"string\").<Integer>apply(noLength)",
|
||||
Probably.just("string").apply(noLength).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.<String>none().<Integer>apply(noLength)",
|
||||
Probably.<String>none().apply(noLength).toString(),
|
||||
"<>");
|
||||
}
|
||||
}
|
||||
58
labs/cs2030s/Lab4/TestProbably.java
Normal file
58
labs/cs2030s/Lab4/TestProbably.java
Normal file
@@ -0,0 +1,58 @@
|
||||
class TestProbably {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest we = new CS2030STest();
|
||||
|
||||
we.expect("Probably.just(4)",
|
||||
Probably.just(4).toString(),
|
||||
"<4>");
|
||||
we.expect("Probably.just(Probably.just(0))",
|
||||
Probably.just(Probably.just(0)).toString(),
|
||||
"<<0>>");
|
||||
we.expect("Probably.just(Probably.just(Probably.just(\"null\")))",
|
||||
Probably.just(Probably.just(Probably.just("null"))).toString(),
|
||||
"<<<null>>>");
|
||||
we.expect("Probably.just(Probably.just(Probably.none()))",
|
||||
Probably.just(Probably.just(Probably.none())).toString(),
|
||||
"<<<>>>");
|
||||
we.expect("Probably.just(Probably.just(null))",
|
||||
Probably.just(Probably.just(null)).toString(),
|
||||
"<<>>");
|
||||
|
||||
we.expect("Probably.just(4).equals(Probably.just(4))",
|
||||
Probably.just(4).equals(Probably.just(4)),
|
||||
true);
|
||||
we.expect("Probably.just(4).equals(4)",
|
||||
Probably.just(4).equals(4),
|
||||
false);
|
||||
we.expect("Probably.just(Probably.just(0)).equals(Probably.just(0))",
|
||||
Probably.just(Probably.just(0)).equals(Probably.just(0)),
|
||||
false);
|
||||
we.expect("Probably.just(Probably.just(0)).equals(Probably.just(Probably.just(0)))",
|
||||
Probably.just(Probably.just(0)).equals(Probably.just(Probably.just(0))),
|
||||
true);
|
||||
|
||||
we.expect("Probably.just(\"string\")",
|
||||
Probably.just("string").toString(),
|
||||
"<string>");
|
||||
|
||||
we.expect("Probably.just(\"string\").equals(Probably.just(4))",
|
||||
Probably.just("string").equals(Probably.just(4)),
|
||||
false);
|
||||
we.expect("Probably.just(\"string\").equals(Probably.just(\"null\"))",
|
||||
Probably.just("string").equals(Probably.just("null")),
|
||||
false);
|
||||
|
||||
we.expect("Probably.just(null)",
|
||||
Probably.just(null).toString(),
|
||||
"<>");
|
||||
we.expect("Probably.none()",
|
||||
Probably.none().toString(),
|
||||
"<>");
|
||||
we.expect("Probably.none().equals(Probably.just(null))",
|
||||
Probably.none().equals(Probably.just(null)),
|
||||
true);
|
||||
we.expect("Probably.none() == Probably.just(null)",
|
||||
Probably.none() == Probably.just(null),
|
||||
true);
|
||||
}
|
||||
}
|
||||
1
labs/cs2030s/Lab4/inputs/Lab4.1.in
Normal file
1
labs/cs2030s/Lab4/inputs/Lab4.1.in
Normal file
@@ -0,0 +1 @@
|
||||
1
|
||||
1
labs/cs2030s/Lab4/inputs/Lab4.2.in
Normal file
1
labs/cs2030s/Lab4/inputs/Lab4.2.in
Normal file
@@ -0,0 +1 @@
|
||||
2
|
||||
1
labs/cs2030s/Lab4/inputs/Lab4.3.in
Normal file
1
labs/cs2030s/Lab4/inputs/Lab4.3.in
Normal file
@@ -0,0 +1 @@
|
||||
3
|
||||
1
labs/cs2030s/Lab4/inputs/Lab4.4.in
Normal file
1
labs/cs2030s/Lab4/inputs/Lab4.4.in
Normal file
@@ -0,0 +1 @@
|
||||
4
|
||||
1
labs/cs2030s/Lab4/inputs/Lab4.5.in
Normal file
1
labs/cs2030s/Lab4/inputs/Lab4.5.in
Normal file
@@ -0,0 +1 @@
|
||||
5
|
||||
1
labs/cs2030s/Lab4/inputs/Lab4.6.in
Normal file
1
labs/cs2030s/Lab4/inputs/Lab4.6.in
Normal file
@@ -0,0 +1 @@
|
||||
6
|
||||
2
labs/cs2030s/Lab4/outputs/Lab4.1.out
Normal file
2
labs/cs2030s/Lab4/outputs/Lab4.1.out
Normal file
@@ -0,0 +1,2 @@
|
||||
17
|
||||
string
|
||||
3
labs/cs2030s/Lab4/outputs/Lab4.2.out
Normal file
3
labs/cs2030s/Lab4/outputs/Lab4.2.out
Normal file
@@ -0,0 +1,3 @@
|
||||
4
|
||||
string
|
||||
|
||||
8
labs/cs2030s/Lab4/outputs/Lab4.3.out
Normal file
8
labs/cs2030s/Lab4/outputs/Lab4.3.out
Normal file
@@ -0,0 +1,8 @@
|
||||
5
|
||||
6
|
||||
6
|
||||
7
|
||||
<1>
|
||||
<>
|
||||
<2>
|
||||
<<1>>
|
||||
7
labs/cs2030s/Lab4/outputs/Lab4.4.out
Normal file
7
labs/cs2030s/Lab4/outputs/Lab4.4.out
Normal file
@@ -0,0 +1,7 @@
|
||||
<5>
|
||||
<6>
|
||||
<6>
|
||||
<7>
|
||||
<>
|
||||
<>
|
||||
<>
|
||||
5
labs/cs2030s/Lab4/outputs/Lab4.5.out
Normal file
5
labs/cs2030s/Lab4/outputs/Lab4.5.out
Normal file
@@ -0,0 +1,5 @@
|
||||
<17>
|
||||
<>
|
||||
<17>
|
||||
<8>
|
||||
<>
|
||||
8
labs/cs2030s/Lab4/outputs/Lab4.6.out
Normal file
8
labs/cs2030s/Lab4/outputs/Lab4.6.out
Normal file
@@ -0,0 +1,8 @@
|
||||
<18>
|
||||
<>
|
||||
<>
|
||||
<>
|
||||
<6>
|
||||
<>
|
||||
<>
|
||||
<>
|
||||
Reference in New Issue
Block a user