├── .checkignore ├── .tasks └── satori │ ├── D │ ├── src │ │ └── main │ │ │ └── java │ │ │ ├── Stop.java │ │ │ ├── Zuber.java │ │ │ ├── Koniec.java │ │ │ ├── TajnyAgent.java │ │ │ ├── RozmowaKontrolowana.java │ │ │ └── Agent.java │ └── pom.xml │ ├── G │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── StreamUtils.java │ └── pom.xml │ ├── E │ ├── src │ │ └── main │ │ │ └── java │ │ │ ├── GenericFunctionsException.java │ │ │ ├── Function.java │ │ │ ├── Functions.java │ │ │ ├── StrConcat.java │ │ │ └── StrRvs.java │ └── pom.xml │ ├── B │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── a.java │ └── pom.xml │ ├── H │ ├── src │ │ └── main │ │ │ └── java │ │ │ ├── Relay.java │ │ │ └── AbstractRunner.java │ └── pom.xml │ ├── C │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── ListIsFunction.java │ └── pom.xml │ ├── F │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── SmartFactory.java │ └── pom.xml │ └── A │ ├── pom.xml │ └── src │ └── main │ └── java │ └── PhoneBook.java ├── clone.sh ├── satori ├── A │ ├── KacperTopolski │ │ └── KacperTopolskiPhoneBookTest.java │ ├── LeonidDorochko │ │ └── LeonidDorochkoPhoneBookTest.java │ ├── VladKoz │ │ └── VladKozPhoneBookTest.java │ └── MichalHorodecki │ │ └── MichalHorodeckiPhoneBookTest.java ├── H │ ├── Test0.java │ └── LeonidDorochkoTest.java ├── G │ └── TestPublic.java ├── E │ ├── DominikMatuszekFunctionsTest.java │ ├── MichalHorodeckiFunctionsTest.java │ ├── KacperTopolskiFunctionsTest.java │ └── JakubOskwarekFunctionsTest.java ├── D │ ├── AgentTest.java │ ├── KacperTopolskiAgentTest.java │ └── MichalHorodeckiAgentTest.java ├── F │ ├── SmartFactoryTest.java │ ├── KacperTopolskiSmartFactoryTrollTests.java │ ├── GigaTestyVladaKoza.java │ ├── SzymonWojtulewiczTests.java │ ├── JakubBiniedaStolenTests.java │ └── MichalHorodeckiSmartFactoryTest.java ├── C │ ├── ListIsFunctionTest.java │ ├── MichalHorodeckiListIsFunctionTest.java │ └── KacperTopolskiListIsFunctionTest.java └── B │ ├── JakubOskwarekRepartitionedOfficialATest.java │ └── OpenTest.java ├── .github └── workflows │ └── maven.yml ├── check.sh └── README.md /.checkignore: -------------------------------------------------------------------------------- 1 | satori/E 2 | satori/G 3 | -------------------------------------------------------------------------------- /.tasks/satori/D/src/main/java/Stop.java: -------------------------------------------------------------------------------- 1 | public class Stop extends RuntimeException { 2 | } 3 | -------------------------------------------------------------------------------- /.tasks/satori/D/src/main/java/Zuber.java: -------------------------------------------------------------------------------- 1 | public class Zuber extends RuntimeException{ 2 | 3 | } -------------------------------------------------------------------------------- /.tasks/satori/D/src/main/java/Koniec.java: -------------------------------------------------------------------------------- 1 | public class Koniec extends RuntimeException { 2 | } 3 | -------------------------------------------------------------------------------- /.tasks/satori/D/src/main/java/TajnyAgent.java: -------------------------------------------------------------------------------- 1 | public interface TajnyAgent { 2 | void transmituj(); 3 | } 4 | -------------------------------------------------------------------------------- /.tasks/satori/D/src/main/java/RozmowaKontrolowana.java: -------------------------------------------------------------------------------- 1 | public class RozmowaKontrolowana extends RuntimeException { 2 | } 3 | -------------------------------------------------------------------------------- /.tasks/satori/G/src/main/java/StreamUtils.java: -------------------------------------------------------------------------------- 1 | public class StreamUtils { 2 | public static ... generateRest(..., ...) {} 3 | } 4 | -------------------------------------------------------------------------------- /.tasks/satori/E/src/main/java/GenericFunctionsException.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | @SuppressWarnings("serial") 4 | public class GenericFunctionsException extends Exception { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /.tasks/satori/E/src/main/java/Function.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.List; 3 | 4 | public interface Function { 5 | int arity(); 6 | S compute(List args) throws GenericFunctionsException; 7 | } -------------------------------------------------------------------------------- /.tasks/satori/B/src/main/java/a.java: -------------------------------------------------------------------------------- 1 | /** 2 | * PO 2021/22, Problem B - Klasa Świra 3 | * @author YOUR NAME 4 | */ 5 | 6 | public class a { 7 | public a a; 8 | public int a() { return 0; } 9 | public a a(int a) { return this; } 10 | } 11 | -------------------------------------------------------------------------------- /.tasks/satori/H/src/main/java/Relay.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.io.Reader; 4 | 5 | public class Relay { 6 | public Relay(Reader reader) { } 7 | public void register(int id, Thread competitor) { } 8 | public void startRelayRace() { } 9 | public boolean dispatch() { return false; } 10 | } -------------------------------------------------------------------------------- /.tasks/satori/E/src/main/java/Functions.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * PO 2021/22, Problem E - Funkcje wieloargumentowe 4 | * @author YOUR NAME 5 | */ 6 | 7 | public class Functions { 8 | 9 | public static ... constant ( ... ) { } 10 | 11 | public static ... proj (int n, int k) { } 12 | 13 | public static ... compose ( ... , ... ) { } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /.tasks/satori/C/src/main/java/ListIsFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * PO 2021/22, Problem C - ListIsFunction 3 | * @author YOUR NAME 4 | */ 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | public class ListIsFunction { 10 | public List asList() { return null; } 11 | public Map asMap() { return null; } 12 | } 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.tasks/satori/F/src/main/java/SmartFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * PO 2021/22, Problem F - Jak łatwo zaliczyć programowanie 3 | * @autor YOUR NAME 4 | */ 5 | 6 | 7 | public class SmartFactory 8 | { 9 | public static class HellNoException extends RuntimeException { } 10 | 11 | public static T fixIt(Class target, Object obj) 12 | { 13 | return null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.tasks/satori/H/src/main/java/AbstractRunner.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.util.Random; 4 | 5 | public abstract class AbstractRunner implements Runnable { 6 | 7 | final protected Random naps = new Random(); 8 | final protected int id; 9 | final protected Relay relay; 10 | 11 | public AbstractRunner(int id, Relay relay) { 12 | this.id = id; 13 | this.relay = relay; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.tasks/satori/E/src/main/java/StrConcat.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class StrConcat implements Function { 4 | @Override 5 | public int arity() { return 2; } 6 | @Override 7 | public String compute(List args) throws GenericFunctionsException { 8 | if (args.size() != arity()) 9 | throw new GenericFunctionsException(); 10 | return args.get(0).toString() + args.get(1); 11 | } 12 | } -------------------------------------------------------------------------------- /.tasks/satori/E/src/main/java/StrRvs.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class StrRvs implements Function { 4 | @Override 5 | public int arity() { return 1; } 6 | @Override 7 | public String compute(List args) throws GenericFunctionsException { 8 | if (args.size() != arity()) throw new GenericFunctionsException(); 9 | return new StringBuilder(args.get(0)).reverse().toString(); 10 | } 11 | } -------------------------------------------------------------------------------- /.tasks/satori/D/src/main/java/Agent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * PO 2021/22, Problem D - Alfabet Morse'a 3 | * @author YOUR NAME 4 | */ 5 | 6 | import java.nio.CharBuffer; 7 | 8 | public class Agent implements Readable, TajnyAgent { 9 | 10 | Agent() { } 11 | Agent(TajnyAgent remote) { } 12 | Agent(String password) { } 13 | Agent(TajnyAgent remote, String password) { } 14 | 15 | public void transmituj() { } 16 | 17 | @Override 18 | public int read(CharBuffer cb) { return 0; } 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /clone.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | clone-into() { 4 | task=${1} 5 | dest=${2} 6 | if [ ! -d .tasks/satori/${task} ]; then 7 | echo "Task not found" 8 | exit 0 9 | fi 10 | mkdir -p ${dest}/${task} 11 | mkdir -p ${dest}/${task}/src/test/java 12 | 13 | cp -r ./.tasks/satori/${task} ${dest}/ 14 | cp -r ./satori/${task}/* ${dest}/${task}/src/test/java 15 | } 16 | 17 | 18 | 19 | if [ -n "${1}" ]; then 20 | if [ -n "${2}" ]; then 21 | clone-into ${1} ${2} 22 | else 23 | echo "Destination directory required." 24 | fi 25 | else 26 | echo "Task name required." 27 | fi 28 | -------------------------------------------------------------------------------- /satori/A/KacperTopolski/KacperTopolskiPhoneBookTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import static org.junit.Assert.*; 3 | 4 | public class KacperTopolskiPhoneBookTest 5 | { 6 | @Test(timeout = 200) 7 | public void changing_format_should_be_fast() { 8 | int el = 2500, changes = 10000; 9 | PhoneBook pb = new PhoneBook(el); 10 | 11 | for (int i = 0; i < el; ++i) 12 | pb.add("" + (i + 123456789)); 13 | 14 | for (int i = 0; i < changes; ++i) { 15 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 16 | pb.changeFormat(PhoneBook.NumberFormat.DIGITS); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ main ] 9 | pull_request: 10 | branches: [ main ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 17 20 | uses: actions/setup-java@v2 21 | with: 22 | java-version: '17' 23 | distribution: 'temurin' 24 | cache: maven 25 | - name: Build tests with maven 26 | run: bash check.sh 27 | -------------------------------------------------------------------------------- /satori/H/Test0.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.io.StringReader; 4 | 5 | class Test0 { 6 | public static void main(String[] args) { 7 | Relay relay = new Relay(new StringReader("0 1 2 1 1 2 1")); 8 | Thread[] runners = new Thread[3]; 9 | for (int i = 0; i < runners.length; i++) { 10 | runners[i] = new Thread(new AbstractRunner(i, relay) { 11 | @Override 12 | public void run() { 13 | while (relay.dispatch()) { 14 | System.out.println(id); 15 | synchronized (this) { 16 | try { 17 | wait(naps.nextInt(10) + 1); 18 | } catch (InterruptedException e) { } 19 | } 20 | } 21 | } 22 | }); 23 | } 24 | 25 | for (int i = 0; i < runners.length; i++) { 26 | relay.register(i, runners[i]); 27 | runners[i].start(); 28 | } 29 | relay.startRelayRace(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.tasks/satori/A/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.13.2 26 | test 27 | 28 | 29 | 30 | UTF-8 31 | 17 32 | 33 | 34 | -------------------------------------------------------------------------------- /.tasks/satori/B/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.13.2 26 | test 27 | 28 | 29 | 30 | UTF-8 31 | 17 32 | 33 | 34 | -------------------------------------------------------------------------------- /.tasks/satori/C/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.13.2 26 | test 27 | 28 | 29 | 30 | UTF-8 31 | 17 32 | 33 | 34 | -------------------------------------------------------------------------------- /.tasks/satori/D/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.13.2 26 | test 27 | 28 | 29 | 30 | UTF-8 31 | 17 32 | 33 | 34 | -------------------------------------------------------------------------------- /satori/G/TestPublic.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import static org.junit.Assert.assertEquals; 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.function.BinaryOperator; 7 | import java.util.function.UnaryOperator; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | 12 | public class TestPublic { 13 | @Test 14 | public void test() { 15 | 16 | List s1 = StreamUtils.generateRest(Stream.of(1, 1), 17 | (a, b)-> a+b).limit(7).collect( Collectors.toList() ); 18 | assertEquals("[1, 1, 2, 3, 5, 8, 13]", s1.toString() ); 19 | 20 | ArrayList s2 = new ArrayList(); 21 | StreamUtils.generateRest(Stream.of("Ala", "ma", "kota"), 22 | (a, b)-> a+b).limit(7).forEach(s2::add); 23 | assertEquals("[Ala, ma, kota, makota, kotamakota, makotakotamakota, kotamakotamakotakotamakota]", s2.toString() ); 24 | 25 | ArrayList s3 = new ArrayList(); 26 | StreamUtils.generateRest(Stream.of(i -> 0), 27 | (BinaryOperator>) (f, g) -> 28 | (x -> x==0? 1: x*g.apply(x-1))). 29 | limit(10).map(f->f.apply(7)).forEach(s3::add); 30 | assertEquals("[0, 0, 0, 0, 0, 0, 0, 0, 5040, 5040]", s3.toString() ); 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /satori/E/DominikMatuszekFunctionsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import org.junit.experimental.runners.Enclosed; 3 | import org.junit.runner.RunWith; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | 12 | public class DominikMatuszekFunctionsTest { 13 | @Test 14 | public void constant_function_has_got_exactly_0_arguments(){ 15 | String result = "żart_o_gościu_na_żółtym_motorku"; 16 | String toAdd = "ten żart jest świetny ale jeśli go zacznę opowiadać w ten sposób to chyba zostanę wyrzucony przez okno"; 17 | 18 | Function testFunction = Functions.constant(result); //funkcja ze Stringa w Stringa, czemu nie? 19 | ArrayList args = new ArrayList<>(); 20 | 21 | try { 22 | assertEquals(testFunction.compute(args), result); 23 | } 24 | catch(Exception e){ 25 | fail(); //proszę tu nie rzucać żadnych exceptionów 26 | } 27 | 28 | for(int i=1;i<100;i++){ 29 | args.add(toAdd); 30 | try{ 31 | testFunction.compute(args); 32 | fail(); //więcej niż zero argumentów jest verboten 33 | } 34 | catch(Exception e){ 35 | //tu ma nic nie być 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /.tasks/satori/E/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | -Xlint 20 | -Werror 21 | 22 | true 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | junit 31 | junit 32 | 4.13.2 33 | test 34 | 35 | 36 | 37 | UTF-8 38 | 17 39 | 40 | 41 | -------------------------------------------------------------------------------- /.tasks/satori/H/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | -Xlint 20 | -Werror 21 | 22 | true 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | junit 31 | junit 32 | 4.13.2 33 | test 34 | 35 | 36 | 37 | UTF-8 38 | 17 39 | 40 | 41 | -------------------------------------------------------------------------------- /.tasks/satori/G/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | -Xlint:all 20 | -Werror 21 | 22 | true 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | junit 31 | junit 32 | 4.13.2 33 | test 34 | 35 | 36 | 37 | UTF-8 38 | 17 39 | 40 | 41 | -------------------------------------------------------------------------------- /satori/D/AgentTest.java: -------------------------------------------------------------------------------- 1 | 2 | import org.junit.Test; 3 | import java.util.Scanner; 4 | import static org.junit.Assert.*; 5 | 6 | public class AgentTest { 7 | @Test 8 | public void test1() { 9 | Scanner s = new Scanner(new Agent(new Agent("tajny/komunikat/państwa/podziemnego."))); 10 | assertEquals("tajny/komunikat/państwa/podziemnego.", s.next()); 11 | } 12 | 13 | @Test 14 | public void test2() { 15 | String[] tekst = String.valueOf("pawiany wchodzą na ściany żyrafy wchodzą do szafy " + 16 | "pawiany wchodzą na ściany żyrafy wchodzą do szafy " + 17 | "pawiany wchodzą").split("\\s+"); 18 | Scanner s = new Scanner(new Agent(new Agent())); 19 | s.useDelimiter("([/!])"); 20 | for (String str : tekst) 21 | assertEquals(str, s.next()); 22 | } 23 | @Test 24 | public void test3() { 25 | Agent agent = new Agent(new TajnyAgent() { 26 | int i; 27 | @Override 28 | public void transmituj() { 29 | if(i++ == 0) throw new Zuber(); 30 | throw new Koniec(); 31 | } 32 | }); 33 | Scanner s = new Scanner(agent), t = new Scanner(new Agent(agent)); 34 | t.useDelimiter("([/!])"); 35 | assertEquals("pawiany",t.next()); 36 | assertFalse(s.hasNext()); 37 | assertEquals("zuber", t.next()); 38 | assertEquals("wchodzą", t.next()); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.tasks/satori/F/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | tcs.po 5 | satori 6 | jar 7 | 1.0-SNAPSHOT 8 | satori 9 | http://maven.apache.org 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | true 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | junit 27 | junit 28 | 4.13.2 29 | test 30 | 31 | 32 | 33 | UTF-8 34 | 17 35 | 36 | 37 | -------------------------------------------------------------------------------- /.tasks/satori/A/src/main/java/PhoneBook.java: -------------------------------------------------------------------------------- 1 | /** 2 | * PO 2021/22, Problem A - PhoneBook 3 | * @author YOUR NAME 4 | */ 5 | 6 | public class PhoneBook { 7 | public enum NumberFormat{ DIGITS, HYPHENED } 8 | 9 | public PhoneBook() {} 10 | public PhoneBook(NumberFormat format) {} 11 | public PhoneBook(int capacity) {} 12 | public PhoneBook(NumberFormat format, int capacity) {} 13 | 14 | public PhoneBook copyBook(){ 15 | return null; 16 | } 17 | 18 | public int size(){ 19 | return 0; 20 | } 21 | public int capacity(){ 22 | return 0; 23 | } 24 | public boolean isEmpty(){ 25 | return false; 26 | } 27 | public boolean isFull(){ 28 | return false; 29 | } 30 | 31 | public PhoneBook add(String number){ 32 | return null; 33 | } 34 | public PhoneBook add(PhoneBook subBook){ 35 | return null; 36 | } 37 | public void changeFormat(NumberFormat format){ 38 | } 39 | 40 | public boolean contains(String number){ 41 | return false; 42 | } 43 | 44 | public boolean contains(PhoneBook pb){ 45 | return false; 46 | } 47 | public boolean elementOf(PhoneBook pb){ 48 | return false; 49 | } 50 | public boolean subsetOf(PhoneBook pb){ 51 | return false; 52 | } 53 | public boolean supersetOf(PhoneBook pb){ 54 | return false; 55 | } 56 | public boolean equals(Object o){ 57 | return false; 58 | } 59 | 60 | public String toString(){ 61 | return null; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /satori/H/LeonidDorochkoTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import static org.junit.Assert.*; 3 | import java.io.StringReader; 4 | import static java.lang.Thread.sleep; 5 | 6 | public class LeonidDorochkoTest { 7 | static class OurRunnable implements Runnable { 8 | int id; 9 | StringBuilder answer; 10 | Relay relay; 11 | 12 | OurRunnable(int id, StringBuilder answer, Relay relay) { 13 | this.id = id; 14 | this.answer = answer; 15 | this.relay = relay; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | while (relay.dispatch()) { 21 | synchronized (this) { 22 | answer.append(Integer.toString(id)); 23 | try { 24 | wait(100); 25 | } catch (InterruptedException e) { } 26 | } 27 | } 28 | } 29 | } 30 | 31 | @Test 32 | public void sleep_before_starting_race() { 33 | Relay relay = new Relay(new StringReader("2 1 1 2 2 1")); 34 | StringBuilder answer = new StringBuilder(); 35 | 36 | Thread first = new Thread(new OurRunnable(1, answer, relay)); 37 | Thread second = new Thread(new OurRunnable(2, answer, relay)); 38 | 39 | relay.register(1, first); 40 | relay.register(2, second); 41 | 42 | first.start(); 43 | second.start(); 44 | 45 | try { 46 | sleep(1000); 47 | } catch (InterruptedException e) { 48 | e.printStackTrace(); 49 | } 50 | 51 | relay.startRelayRace(); 52 | 53 | try { 54 | sleep(1000); 55 | } catch (InterruptedException e) { 56 | e.printStackTrace(); 57 | } 58 | 59 | assertEquals("211221", answer.toString()); 60 | // if you have empty string, that means that threads are in wait state 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | init-test() { 4 | task=${1} 5 | echo "Copying tests into .tasks/${task}/src/test/java/" 6 | echo -n "Found tests: " 7 | tests=$(ls ${task}) 8 | echo ${tests} 9 | mkdir -p .tasks/${task}/src/test/java 10 | cp -r ${task}/* .tasks/${task}/src/test/java/ 11 | } 12 | 13 | execute-test() { 14 | task=${1} 15 | echo "Compiling tests for task ${task}" 16 | mvn test-compile -f .tasks/${task}/pom.xml 17 | if [ $? -ne 0 ]; then 18 | return 1 19 | fi 20 | } 21 | 22 | cleanup-test() { 23 | task=${1} 24 | echo "Cleaning up after testing task ${task}" 25 | rm -r .tasks/${task}/src/test 26 | rm -r .tasks/${task}/target 27 | } 28 | 29 | check-task() { 30 | task=${1} 31 | if [ ! -d .tasks/${task} ]; then 32 | echo "Task ${task} not found. Ignoring. " 33 | exit 0 34 | fi 35 | 36 | if [ ! -d ${task} ]; then 37 | echo "No tests for ${task} found. Skipping." 38 | exit 0 39 | fi 40 | 41 | if grep ${task} .checkignore; then 42 | echo "Task ${task} is set to be ignored. Skipping." 43 | echo "" 44 | return 0 45 | fi 46 | 47 | echo "Running tests for task ${task}" 48 | 49 | 50 | 51 | init-test ${task} 52 | execute-test ${task} 53 | exec_exit_code=$? 54 | if [ ${exec_exit_code} -ne 0 ]; then 55 | echo "Test build for task ${task} failed." 56 | else 57 | echo "Test build for task ${task} succeeded." 58 | fi 59 | cleanup-test ${task} 60 | echo "" 61 | 62 | return ${exec_exit_code} 63 | } 64 | 65 | check-all() { 66 | exit_code=0; 67 | for task in $(ls .tasks/satori) 68 | do 69 | check-task satori/${task} 70 | if [ $? -ne 0 ]; then 71 | exit_code=1 72 | fi 73 | done 74 | 75 | if [ ${exit_code} -ne 0 ]; then 76 | echo "Project build failed." 77 | else 78 | echo "Project build succeeded"; 79 | fi 80 | 81 | exit ${exit_code} 82 | } 83 | 84 | 85 | if [ -n "${1}" ]; then 86 | check-task satori/${1} 87 | else 88 | check-all 89 | fi 90 | 91 | -------------------------------------------------------------------------------- /satori/F/SmartFactoryTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.*; 6 | 7 | 8 | public class SmartFactoryTest 9 | { 10 | @Test 11 | public void test0() 12 | { 13 | String ala = "Ala has a cat"; 14 | //Warning, raw type 15 | Collection col = SmartFactory.fixIt(Collection.class, ala); 16 | System.out.println(col); 17 | try 18 | { 19 | col.iterator(); 20 | } 21 | catch (SmartFactory.HellNoException e) 22 | { 23 | System.out.println("Well.... doesn't work."); 24 | } 25 | } 26 | 27 | public static class A { } 28 | 29 | public static class B 30 | { 31 | public String ala; 32 | public CharSequence ma; 33 | } 34 | 35 | public static class C 36 | { 37 | public static int i; 38 | public final int id = i++; 39 | public C other; 40 | public C yetAnother; 41 | public Object anObject; 42 | } 43 | 44 | public interface I 45 | { 46 | A test(); 47 | } 48 | 49 | public interface J 50 | { 51 | B test(); 52 | } 53 | 54 | public interface K 55 | { 56 | C test(); 57 | } 58 | 59 | @Test 60 | public void testBasic() 61 | { 62 | assertNotNull(SmartFactory.fixIt(I.class, null).test()); 63 | B b = SmartFactory.fixIt(J.class, null).test(); 64 | assertEquals("", b.ala); 65 | assertEquals("", b.ma); 66 | assertTrue(b.ala == b.ma); 67 | } 68 | 69 | @Test 70 | public void testNumber() 71 | { 72 | C c = SmartFactory.fixIt(K.class, null).test(); 73 | assertEquals(1, C.i); 74 | assertTrue(c == c.other); 75 | assertTrue(c == c.yetAnother); 76 | assertTrue(c == c.anObject); 77 | c = SmartFactory.fixIt(K.class, null).test(); 78 | assertEquals(2, C.i); 79 | assertTrue(c == c.other); 80 | assertTrue(c == c.yetAnother); 81 | assertTrue(c == c.anObject); 82 | } 83 | 84 | 85 | } 86 | -------------------------------------------------------------------------------- /satori/F/KacperTopolskiSmartFactoryTrollTests.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import org.junit.Ignore; 3 | 4 | import static org.junit.Assert.*; 5 | 6 | public class KacperTopolskiSmartFactoryTrollTests { 7 | static class A { 8 | 9 | } 10 | static class B extends A { 11 | public B(int x) { 12 | 13 | } 14 | } 15 | static class C { 16 | public A a; 17 | public B b = new B(0); 18 | } 19 | interface X { 20 | C f(); 21 | } 22 | @Ignore 23 | @Test 24 | public void troll1() { 25 | C c = SmartFactory.fixIt(X.class, null).f(); 26 | } 27 | 28 | static class D1 { 29 | public E1 e; 30 | public F f; 31 | public D1() { 32 | 33 | } 34 | public D1(int x) { 35 | 36 | } 37 | } 38 | static class E1 extends D1 { 39 | public E1() { 40 | f = new F(0); 41 | } 42 | } 43 | static class D2 { 44 | public E2 e; 45 | public F f; 46 | public D2() { 47 | f = new F(0); 48 | e = new E2(); 49 | e.f = f; 50 | e.e = e; 51 | } 52 | public D2(int x) { 53 | 54 | } 55 | } 56 | static class E2 extends D2 { 57 | public E2() { 58 | super(0); 59 | } 60 | } 61 | static class F { 62 | public boolean bool; 63 | public F() { 64 | bool = false; 65 | } 66 | public F(int x) { 67 | bool = true; 68 | } 69 | } 70 | interface Y { 71 | D1 d1(); 72 | D2 d2(); 73 | } 74 | 75 | @Ignore 76 | @Test 77 | public void troll2() { 78 | Y y = SmartFactory.fixIt(Y.class, null); 79 | 80 | D1 d1 = y.d1(); 81 | assertEquals(E1.class, d1.getClass()); 82 | assertSame(d1, d1.e); 83 | assertTrue(d1.f.bool); 84 | 85 | D2 d2 = y.d2(); 86 | assertEquals(D2.class, d2.getClass()); 87 | assertTrue(d2.f.bool); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /satori/C/ListIsFunctionTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import static org.junit.Assert.*; 3 | 4 | import java.util.Iterator; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class ListIsFunctionTest { 9 | @Test 10 | public void testBasic() { 11 | ListIsFunction temp = new ListIsFunction(); 12 | assertEquals(Object.class, temp.getClass().getSuperclass()); 13 | List tempList = temp.asList(); 14 | Map tempMap = temp.asMap(); 15 | assertEquals("[]", tempList.toString()); 16 | assertEquals("{}",tempMap.toString()); 17 | tempList.add("Ala"); 18 | tempList.add("ma"); 19 | tempList.add("kota"); 20 | 21 | assertEquals("{0=Ala, 1=ma, 2=kota}",tempMap.toString()); 22 | assertEquals("[0, 1, 2]",tempMap.keySet().toString()); 23 | assertEquals("[Ala, ma, kota]",tempMap.values().toString()); 24 | tempMap.put(1, "miala"); 25 | assertEquals("[Ala, miala, kota]",tempList.toString()); 26 | tempMap.put(3,"malego"); 27 | assertEquals("[Ala, miala, kota, malego]",tempList.toString()); 28 | tempMap.remove(3); 29 | assertEquals("[Ala, miala, kota]",tempList.toString()); 30 | 31 | Iterator it = tempMap.keySet().iterator(); 32 | assertEquals("Ala",tempMap.get(it.next())); 33 | assertEquals("miala",tempMap.get(it.next())); 34 | try { 35 | it.remove(); 36 | fail("No exception!"); 37 | } catch(Exception e) { 38 | assertEquals(java.lang.IllegalStateException.class, e.getClass()); 39 | } 40 | assertEquals("kota",tempMap.get(it.next())); 41 | it.remove(); 42 | assertEquals("[Ala, miala]", tempList.toString()); 43 | 44 | try { 45 | tempMap.remove(0); 46 | fail("No exception!"); 47 | } catch(Exception e) { 48 | assertEquals(java.lang.IllegalArgumentException.class, e.getClass()); 49 | } 50 | tempMap.remove(1); 51 | tempMap.remove(0); 52 | assertEquals("[]", tempList.toString()); 53 | assertTrue(tempList.isEmpty()); 54 | assertTrue(tempMap.isEmpty()); 55 | } 56 | } -------------------------------------------------------------------------------- /satori/F/GigaTestyVladaKoza.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class GigaTestyVladaKoza { 6 | interface Interface { 7 | Object foo(String c) throws Exception; 8 | } 9 | 10 | class Class { 11 | String foo(Object c) throws IllegalAccessException { 12 | return c.toString(); 13 | } 14 | } 15 | 16 | @Test 17 | public void return_type_arguments_and_errors_can_be_casted() { 18 | Interface proxy = SmartFactory.fixIt(Interface.class, new Class()); 19 | try { 20 | assertEquals(proxy.foo("42"), "42"); 21 | } catch (Exception e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | 26 | interface Interface2 { 27 | void foo(Integer a); 28 | } 29 | 30 | class Class2 { 31 | void foo(Integer c) { 32 | 33 | } 34 | 35 | void foo(Number c) { 36 | 37 | } 38 | } 39 | 40 | @Test 41 | public void too_many_good_functions_throws_hello() throws Exception { 42 | Interface2 proxy = SmartFactory.fixIt(Interface2.class, new Class2()); 43 | try { 44 | proxy.foo(4); 45 | fail(); 46 | } catch (SmartFactory.HellNoException e) { 47 | 48 | } 49 | } 50 | 51 | public static class ClassCieslik { 52 | public static Object[] ar; 53 | } 54 | 55 | public interface InterfaceCieslik { 56 | ClassCieslik test(); 57 | } 58 | 59 | @Test 60 | public void testCieslik() { 61 | ClassCieslik a = SmartFactory.fixIt(InterfaceCieslik.class, null).test(); 62 | } 63 | 64 | public interface Class2Cieslik { 65 | Interface2Cieslik test(); 66 | } 67 | 68 | public static class Interface2Cieslik { 69 | public static Interface2Cieslik a; 70 | public static String b; 71 | public static String c = "KOT"; 72 | 73 | } 74 | 75 | @Test 76 | public void test2Cieslik() { 77 | Interface2Cieslik a = SmartFactory.fixIt(Class2Cieslik.class, null).test(); 78 | assertNotNull(a.a); 79 | assertEquals("", a.b); 80 | assertEquals("KOT", a.c); 81 | } 82 | 83 | public interface InterfaceMikos { 84 | String throwsE() throws Exception; 85 | 86 | String doesntThrowE(); 87 | } 88 | 89 | public static class ClassMikos { 90 | String throwsE() { 91 | return "good"; 92 | } 93 | 94 | String doesntThrowE() throws Exception { 95 | return "bad"; 96 | } 97 | } 98 | 99 | @Test 100 | public void test1Mikos() { 101 | assertNotEquals("bad", SmartFactory.fixIt(InterfaceMikos.class, new ClassMikos()) 102 | .doesntThrowE()); 103 | try { 104 | assertEquals(SmartFactory.fixIt(InterfaceMikos.class, new ClassMikos()).throwsE(), "good"); 105 | } catch (Exception e) { 106 | e.printStackTrace(); 107 | } 108 | 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # oop-unit-tests 2 | Repository for sharing unit tests for OOP course 3 | 4 | There is `clone.sh` bash script available which clones skeleton and tests for a task. Usage: `./clone.sh {task} {dest}` - creates `{task}` directory in `{dest}` 5 | 6 | # How to contribute with tests 7 | 8 | ## Git 9 | 10 | 1. If you don't have the repo on your machine: 11 | 1. Fork the repository on Github 12 | 2. Clone your repository into your machine 13 | 3. Add remote upstream `git remote add upstream https://github.com/mhorod/oop-unit-tests` 14 | 15 | 16 | 2. Sync local repository with remote 17 | - If you haven't commited your changes: 18 | 1. Switch to `main` by running `git checkout main` 19 | 2. Execute `git pull upstream main` to sync changes 20 | - If you commited your changes rebase instead of merging: 21 | 1. `git fetch` 22 | 2. `git rebase origin/main` 23 | 24 | 3. Create branch for your changes and switch to it 25 | 1. `git branch {branch_name}` 26 | 2. `git checkout {branch_name}` 27 | 28 | 4. Add your tests to directory `oop-unit-tests/satori/{task}/` 29 | Don't create new directories there. 30 | 31 | 5. Make sure your tests compile by executing `bash check.sh` 32 | - note: you can build a single task by providing the task name as an argument, e.g. `bash check.sh A` 33 | 6. Commit your changes and push them 34 | 7. Go to your repository on Github and create a pull request 35 | 36 | ## code style 37 | This is recommended style guide for writing tests. While not necessary, following it will be highly appreciated. 38 | 39 | ### Classes 40 | In single java project there can exist only one class of given name. In order to avoid name conflicts prefix your test class with name and surname., e.g. `class JohnDoeTests {}` 41 | 42 | With `JUnit` you can group your tests in nested classes with `RunWith(@Enclosed.class)` 43 | 44 | Example test with nested classes: 45 | ```java 46 | import org.junit.*; 47 | import org.junit.experimental.runners.Enclosed; 48 | import org.junit.runner.RunWith; 49 | 50 | import static org.junit.Assert.*; 51 | 52 | @RunWith(Enclosed.class) 53 | public class ExampleTest 54 | { 55 | public static class ExampleGroupTest 56 | { 57 | @Test 58 | public void example_test_method() {} 59 | } 60 | } 61 | ``` 62 | 63 | ### Methods 64 | Use `snake_case` for naming test methods and use names that convey expected behavior. 65 | e.g. `size` is a bad name for a test while `size_of_new_list_is_zero` is better. 66 | 67 | Try to make your tests concise and focused on single thing. 68 | Test behavior rather than interface. 69 | If you have many assertions consider splling the test into more functions. 70 | 71 | # Running tests in IntelliJ 72 | 73 | Put files with unit tests in directory `src/test/java/` in your solution project. 74 | 75 | 1. In toolbar click between `Build` and `Run` icons 76 | 2. `Edit Configurations` 77 | 3. `Add new configuration > JUnit` 78 | 4. `JUnit > Add new run configuration` 79 | 5. Select `Class` to run single test file 80 | 6. Select `All in directory` and select directory to run all tests 81 | 7. Press `Run` icon to run tests 82 | -------------------------------------------------------------------------------- /satori/E/MichalHorodeckiFunctionsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | 3 | 4 | import static org.junit.Assert.*; 5 | 6 | import java.util.*; 7 | 8 | public class MichalHorodeckiFunctionsTest 9 | { 10 | static class InnerFunction implements Function 11 | { 12 | 13 | @Override 14 | public int arity() 15 | { 16 | return 1; 17 | } 18 | 19 | @Override 20 | public Integer compute(List args) 21 | throws GenericFunctionsException 22 | { 23 | if (args == null || args.size() != arity()) 24 | throw new GenericFunctionsException(); 25 | return args.get(0).hashCode(); 26 | } 27 | } 28 | 29 | static class OuterFunction implements Function 30 | { 31 | 32 | @Override 33 | public int arity() 34 | { 35 | return 2; 36 | } 37 | 38 | @Override 39 | public Integer compute(List args) throws GenericFunctionsException 40 | { 41 | if (args == null || args.size() != arity()) 42 | throw new GenericFunctionsException(); 43 | return args.get(0).hashCode(); 44 | } 45 | } 46 | 47 | @Test 48 | public void composition_stress_test() 49 | { 50 | // Constructs input that aims to test all aspects of compose method at once 51 | // f: Object -> Number 52 | // g, h: Number -> Integer 53 | // result: Integer --(g, h)--> Number --(f)--> Object 54 | 55 | var f = new OuterFunction(); 56 | var g = new InnerFunction(); 57 | var h = new InnerFunction(); 58 | List args = new ArrayList<>(); 59 | args.add(g); 60 | args.add(h); 61 | try 62 | { 63 | Functions.compose(f, args); 64 | } 65 | catch (Exception e) 66 | { 67 | fail("Provided functions should compose."); 68 | } 69 | } 70 | 71 | @Test 72 | public void compose_throws_exception_when_arguments_are_null() 73 | { 74 | var f = new OuterFunction(); 75 | var g = new InnerFunction(); 76 | var h = new InnerFunction(); 77 | List args = new ArrayList<>(); 78 | args.add(g); 79 | args.add(h); 80 | try 81 | { 82 | Functions.compose(null, args); 83 | fail("Function should throw an exception when outer function is null"); 84 | } 85 | catch (Exception e) 86 | { 87 | assertEquals(GenericFunctionsException.class, e.getClass()); 88 | } 89 | 90 | try 91 | { 92 | Functions.compose(f, null); 93 | fail("Function should throw an exception when list of inner functions is null"); 94 | } 95 | catch (Exception e) 96 | { 97 | assertEquals(GenericFunctionsException.class, e.getClass()); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /satori/F/SzymonWojtulewiczTests.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.io.IOException; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | public class SzymonWojtulewiczTests { 8 | 9 | static class ClassWithAPrivateConstructor { 10 | private ClassWithAPrivateConstructor(){}; 11 | } 12 | 13 | interface SimpleInterface{ 14 | ClassWithAPrivateConstructor foo(); 15 | } 16 | 17 | @Test 18 | public void onlyUsePublicConstructors(){ 19 | SimpleInterface dummy = SmartFactory.fixIt(SimpleInterface.class, null); 20 | assertThrows(SmartFactory.HellNoException.class, dummy::foo); 21 | } 22 | 23 | interface SimpleExceptionThrower{ 24 | String foo() throws Exception; 25 | String goo(Integer a) throws Exception; 26 | } 27 | 28 | static class ComplexExceptionThrower{ 29 | String foo() throws IOException { 30 | return "complex"; 31 | } 32 | String goo(Integer a) throws Throwable { 33 | // Exception e = (Throwable) t; //won't work 34 | return "Exception.class is not assignable from Throwable.class :(("; 35 | } 36 | String goo(Number a) throws IOException { 37 | return "But this should work"; 38 | } 39 | } 40 | 41 | static class JustThrower { 42 | public static class MyOwnException extends Exception {} 43 | String foo() throws MyOwnException { 44 | throw new MyOwnException(); 45 | } 46 | } 47 | 48 | @Test 49 | public void exceptionsMatchingScopes() throws Exception { 50 | SimpleExceptionThrower dummy = SmartFactory.fixIt(SimpleExceptionThrower.class, new ComplexExceptionThrower()); 51 | assertEquals("complex", dummy.foo()); 52 | } 53 | @Test 54 | public void exceptionsNotMatchingScopes() throws Exception { 55 | SimpleExceptionThrower dummy = SmartFactory.fixIt(SimpleExceptionThrower.class, new ComplexExceptionThrower()); 56 | assertEquals("But this should work", dummy.goo(0)); 57 | } 58 | 59 | @Test 60 | public void exceptionsUnwrapping() { 61 | SimpleExceptionThrower dummy = SmartFactory.fixIt(SimpleExceptionThrower.class, new JustThrower()); 62 | assertThrows(JustThrower.MyOwnException.class, dummy::foo); 63 | } 64 | 65 | static class Parent{ 66 | public First first; 67 | public Second second; 68 | public Third third; 69 | } 70 | 71 | static class First{ 72 | public Second next; 73 | } 74 | 75 | static class Second{ 76 | public Third next; 77 | } 78 | 79 | static class Third{ 80 | public First next; 81 | } 82 | 83 | interface ParentGetter{ 84 | Parent foo(); 85 | } 86 | 87 | @Test 88 | public void cyclicReferences(){ 89 | ParentGetter dummy = SmartFactory.fixIt(ParentGetter.class, null); 90 | Parent parent = dummy.foo(); 91 | assertSame(parent.first, parent.third.next); 92 | assertSame(parent.second, parent.first.next); 93 | assertSame(parent.third, parent.second.next); 94 | } 95 | 96 | static class ClassWithAPrivateField{ 97 | private String thisIsAPrivateField; 98 | } 99 | 100 | interface ClassWithAPrivateFieldGetter{ 101 | ClassWithAPrivateField get(); 102 | } 103 | 104 | @Test 105 | public void privateFieldsAreOfNoInterest(){ 106 | ClassWithAPrivateFieldGetter dummy = SmartFactory.fixIt(ClassWithAPrivateFieldGetter.class, null); 107 | 108 | ClassWithAPrivateField secret = dummy.get(); 109 | 110 | assertNull(secret.thisIsAPrivateField); 111 | } 112 | } -------------------------------------------------------------------------------- /satori/F/JakubBiniedaStolenTests.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.*; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.PrintStream; 5 | import java.util.Collection; 6 | 7 | import org.junit.*; 8 | 9 | public class JakubBiniedaStolenTests { 10 | private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); 11 | private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); 12 | private final PrintStream originalOut = System.out; 13 | private final PrintStream originalErr = System.err; 14 | 15 | @Before 16 | public void setUpStreams() { 17 | System.setOut(new PrintStream(outContent)); 18 | System.setErr(new PrintStream(errContent)); 19 | } 20 | 21 | @After 22 | public void restoreStreams() { 23 | System.setOut(originalOut); 24 | System.setErr(originalErr); 25 | } 26 | 27 | @Test 28 | public void Test0() { 29 | String ala = "Ala has a cat"; 30 | //Warning, raw type 31 | Collection col = SmartFactory.fixIt(Collection.class, ala); 32 | System.out.println(col); 33 | try { 34 | col.iterator(); 35 | }catch(SmartFactory.HellNoException e){ 36 | System.out.println("Well.... doesn't work."); 37 | } 38 | 39 | 40 | assertEquals("Ala has a cat\nWell.... doesn't work.", outContent.toString().trim()); 41 | } 42 | 43 | public static class A { } 44 | public static class B extends A { 45 | public B(int i) { } 46 | } 47 | 48 | public static interface OutA { 49 | A test(); 50 | } 51 | 52 | public static class OutAClass implements OutA { 53 | public A test() { 54 | System.out.println("Test2inA"); 55 | return new A(); 56 | } 57 | } 58 | 59 | public static interface OutB { 60 | B test(); 61 | } 62 | 63 | @Test 64 | public void Test3() { 65 | OutB fakeOutB = SmartFactory.fixIt(OutB.class, new OutAClass()); 66 | try { 67 | System.out.println(fakeOutB.test()); 68 | } catch (SmartFactory.HellNoException e) { System.out.println("unhandled - OK"); } 69 | assertEquals("unhandled - OK", outContent.toString().trim()); 70 | } 71 | 72 | class Bad {} 73 | 74 | public static interface ThrowingA { 75 | Bad test() throws Exception; 76 | } 77 | public static class NoExcClass { 78 | public Bad test(){ System.out.println("NoExcClass"); return null; } 79 | } 80 | 81 | @Test 82 | public void Test4() { 83 | ThrowingA tA = SmartFactory.fixIt(ThrowingA.class, new NoExcClass()); 84 | try { 85 | tA.test(); 86 | } catch(Exception oof) { 87 | System.err.println(oof); 88 | System.out.println("not good"); 89 | } 90 | 91 | assertEquals("NoExcClass", outContent.toString().trim()); 92 | } 93 | 94 | public static class Parent {} 95 | public static class Child extends Parent {} 96 | 97 | public static class Wrapper { 98 | public Child child; 99 | } 100 | 101 | public static class HasBoth { 102 | public Parent parent; 103 | public Wrapper wrapper; 104 | } 105 | 106 | interface Creator { 107 | HasBoth create(); 108 | } 109 | 110 | @Test 111 | public void ThanksJakubOskwarek() { 112 | var proxy = SmartFactory.fixIt(Creator.class, null); 113 | var fabricated = proxy.create(); 114 | assertSame(fabricated.wrapper.child, fabricated.parent); 115 | } 116 | 117 | public static class C { 118 | public static Object[] ar; 119 | } 120 | public interface GetC { C test(); } 121 | 122 | @Test 123 | public void arrayGood() { 124 | try { 125 | C c = SmartFactory.fixIt(GetC.class, null).test(); 126 | } catch(Exception ohNo) { 127 | fail("Array is not a exception"); 128 | } 129 | } 130 | 131 | static class D { 132 | public int i; 133 | D(int i) {this.i = i;} 134 | } 135 | 136 | static class E extends D { 137 | E() {super(0);} 138 | } 139 | 140 | interface F { 141 | D getD(); 142 | E getE(); 143 | } 144 | 145 | static class giveB { 146 | public E getE() { 147 | return new E(); 148 | } 149 | } 150 | 151 | @Test(expected=SmartFactory.HellNoException.class) 152 | public void iunnoAnymore(){ 153 | F f = SmartFactory.fixIt(F.class, new giveB()); 154 | D d = f.getD(); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /satori/E/KacperTopolskiFunctionsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import static org.junit.Assert.*; 3 | import java.util.List; 4 | import java.util.Arrays; 5 | 6 | public class KacperTopolskiFunctionsTest { 7 | static class A1Class {} 8 | static class A2Class extends A1Class {} 9 | static class A3Class extends A2Class {} 10 | static class B1Class {} 11 | static class B2Class extends B1Class {} 12 | static class B3Class extends B2Class {} 13 | static class C1Class {} 14 | static class C2Class extends C1Class {} 15 | static class C3Class extends C2Class {} 16 | 17 | static class U { 18 | static Function n_ary_null(int n) { 19 | return new Function<>() { 20 | @Override 21 | public int arity() { 22 | return n; 23 | } 24 | 25 | @Override 26 | public S compute(List args) { 27 | return null; 28 | } 29 | }; 30 | } 31 | } 32 | 33 | @Test 34 | public void constant_null_test() throws GenericFunctionsException { 35 | Function f = Functions.constant(null); 36 | 37 | assertEquals(0, f.arity()); 38 | assertNull(f.compute(Arrays.asList())); 39 | } 40 | void single_proj_out_of_bounds(int i, int j) { 41 | try { 42 | Functions.proj(i, j); 43 | String m = "!! no exception for proj(" + i + ", " + j + ")"; 44 | System.out.println(m); 45 | fail(m); 46 | } catch (GenericFunctionsException ignored) {} 47 | } 48 | @Test 49 | public void proj_out_of_bounds() { 50 | single_proj_out_of_bounds(-1, -1); 51 | single_proj_out_of_bounds(-1, 0); 52 | single_proj_out_of_bounds(0, 0); 53 | single_proj_out_of_bounds(2, -1); 54 | single_proj_out_of_bounds(2, 2); 55 | single_proj_out_of_bounds(2, 3); 56 | } 57 | @Test 58 | public void proj_derived_types() throws GenericFunctionsException { 59 | Functions.proj(1, 0); 60 | } 61 | @Test 62 | public void compose_null_tests() throws GenericFunctionsException { 63 | try { 64 | Functions.compose( 65 | null, 66 | null 67 | ); 68 | fail(); 69 | } catch (GenericFunctionsException ignored) {} 70 | 71 | try { 72 | Functions.compose( 73 | null, 74 | Arrays.asList( 75 | U.n_ary_null(1) 76 | ) 77 | ); 78 | fail(); 79 | } catch (GenericFunctionsException ignored) {} 80 | 81 | try { 82 | Functions.compose( 83 | U.n_ary_null(1), 84 | null 85 | ); 86 | fail(); 87 | } catch (GenericFunctionsException ignored) {} 88 | 89 | try { 90 | Functions.compose( 91 | U.n_ary_null(2), 92 | Arrays.asList( 93 | U.n_ary_null(1), 94 | null 95 | ) 96 | ); 97 | fail(); 98 | } catch (GenericFunctionsException ignored) {} 99 | 100 | Function f = Functions.compose( 101 | U.n_ary_null(1), 102 | Arrays.asList( 103 | U.n_ary_null(2) 104 | ) 105 | ); 106 | 107 | assertEquals(2, f.arity()); 108 | 109 | assertNull(f.compute( 110 | Arrays.asList( 111 | (A1Class) null, 112 | (A1Class) null 113 | ) 114 | )); 115 | } 116 | @Test 117 | public void compose_different_arity() { 118 | try { 119 | Functions.compose( 120 | U.n_ary_null(2), 121 | Arrays.asList( 122 | U.n_ary_null(1), 123 | U.n_ary_null(2) 124 | ) 125 | ); 126 | fail(); 127 | } catch (GenericFunctionsException ignored) {} 128 | } 129 | @Test 130 | public void compose_with_inheritance() throws GenericFunctionsException { 131 | Function f = Functions.compose( 132 | U.n_ary_null(3), 133 | Arrays.asList( 134 | U.n_ary_null(2), 135 | U.n_ary_null(2), 136 | U.n_ary_null(2) 137 | ) 138 | ); 139 | 140 | assertEquals(2, f.arity()); 141 | 142 | assertNull(f.compute(Arrays.asList( 143 | new A2Class(), 144 | new A3Class() 145 | ))); 146 | } 147 | } 148 | 149 | 150 | -------------------------------------------------------------------------------- /satori/B/JakubOskwarekRepartitionedOfficialATest.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class JakubOskwarekRepartitionedOfficialATest { 9 | 10 | @Test 11 | public void a_contains_all_numbers_added_in_separate_statements() { 12 | a a = new a(); 13 | a.a(1); 14 | a.a(2); 15 | a.a(3); 16 | assertEquals("[1, 2, 3]", String.valueOf(a)); 17 | } 18 | 19 | @Test 20 | public void a_contains_all_numbers_added_via_method_chaining() { 21 | a a = new a().a(1).a(2).a(3); 22 | assertEquals("[1, 2, 3]", String.valueOf(a)); 23 | } 24 | 25 | @Test 26 | public void single_a_prints_sorted() { 27 | a a = new a(); 28 | a.a(1); 29 | a.a(3); 30 | a.a(2); 31 | assertEquals("[1, 2, 3]", String.valueOf(a)); 32 | } 33 | 34 | @Test 35 | public void double_a_prints_in_original_order() { 36 | a a = new a(); 37 | a.a(1).a(3).a(2); 38 | assertEquals("[1, 3, 2]", String.valueOf(a.a)); 39 | } 40 | 41 | @Test 42 | public void elements_can_be_popped_from_back_in_reversed_insertion_order() { 43 | a a = new a(); 44 | a.a(1).a(5).a(2); 45 | assertEquals("[1, 2, 5]", String.valueOf(a)); 46 | assertEquals("[1, 5, 2]", String.valueOf(a.a)); 47 | assertEquals("2", String.valueOf(a.a())); // popping here 48 | assertEquals("[1, 5]", String.valueOf(a)); 49 | assertEquals("[1, 5]", String.valueOf(a.a)); 50 | assertEquals("5", String.valueOf(a.a())); // popping here 51 | } 52 | 53 | @Test 54 | public void elements_popped_from_back_can_be_pushed_to_front() { 55 | a a = new a(); 56 | a.a(1).a(5).a(2).a(4).a(3); 57 | assertEquals("[1, 2, 3, 4, 5]", String.valueOf(a)); 58 | assertEquals("[1, 5, 2, 4, 3]", String.valueOf(a.a)); 59 | a.a(a.a()); // this line is the essential one 60 | assertEquals("[1, 2, 3, 4, 5]", String.valueOf(a)); 61 | assertEquals("[3, 1, 5, 2, 4]", String.valueOf(a.a)); 62 | } 63 | 64 | @Test 65 | public void reference_returned_from_pushing_to_front_prints_sorted() { 66 | a a = new a(); 67 | a.a(1).a(5); 68 | assertEquals("[1, 3, 5]", a.a(3).toString()); 69 | } 70 | 71 | @Test 72 | public void reference_returned_from_pushing_popped_element_to_front_prints_in_original_order() { 73 | a a = new a(); 74 | a.a(1).a(5).a(3); 75 | assertEquals("[3, 1, 5]", a.a(a.a()).toString()); 76 | } 77 | 78 | @Test 79 | public void following_the_nested_a_does_not_change_the_result_of_pushing_to_back() { 80 | a a = new a(); 81 | a.a(1).a(1).a(2).a(1).a(1); 82 | assertEquals("[1, 1, 2, 1, 1]", String.valueOf(a.a)); 83 | a.a.a(3).a.a(1).a(1).a.a.a(4); 84 | assertEquals("[1, 1, 2, 1, 1, 3, 1, 1, 4]", String.valueOf(a.a)); 85 | a = a.a; // this line is the essential one 86 | a.a(1).a(1).a.a.a(5); 87 | assertEquals("[1, 1, 2, 1, 1, 3, 1, 1, 4, 1, 1, 5]", String.valueOf(a)); 88 | assertEquals("[1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5]", String.valueOf(a.a)); // now double `a` prints sorted 89 | } 90 | 91 | @Test 92 | public void elements_can_be_dumped_to_an_array_of_ints() { 93 | a a = new a(); 94 | a.a(1).a(5).a(2).a(4).a(3); 95 | assertEquals("[3, 4, 2, 5, 1]", Arrays.toString(new int[]{a.a(), a.a(), a.a(), a.a(), a.a()})); 96 | } 97 | 98 | @Test 99 | public void elements_can_be_pushed_to_back_at_every_nesting_level() { 100 | a a = new a(); 101 | a.a.a.a.a.a.a.a.a.a(1); 102 | a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a(3); 103 | a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a(2); 104 | assertEquals("[1, 2, 3]", String.valueOf(a)); 105 | assertEquals("[1, 3, 2]", String.valueOf(a.a)); 106 | } 107 | 108 | @Test 109 | public void as_nest_indefinitely() { 110 | a a = new a(); 111 | a.a(1).a(3).a(2); 112 | for(int i = 0; i < 1000000; i++) { 113 | assertEquals("[1, 2, 3]", String.valueOf(a)); 114 | a = a.a; 115 | assertEquals("[1, 3, 2]", String.valueOf(a)); 116 | a = a.a; 117 | } 118 | } 119 | 120 | @Test 121 | public void number_of_intermediate_as_does_not_matter_when_pushing_to_back() { 122 | a a = new a(); 123 | assertEquals("[1, 2, 3]", String.valueOf(a.a(1).a(3).a(2))); 124 | a = new a(); 125 | assertEquals("[1, 3, 2]", String.valueOf(a.a(1).a(3).a(2).a)); 126 | a = new a(); 127 | assertEquals("[1, 2, 3]", String.valueOf(a.a.a(1).a(3).a(2))); 128 | a = new a(); 129 | assertEquals("[1, 2, 3]", String.valueOf(a.a.a(1).a.a.a(3).a.a.a.a.a(2))); 130 | } 131 | 132 | @Test 133 | public void operations_can_be_nested() { 134 | a a = new a(); 135 | assertEquals("[1, 2, 3, 4, 5]", String.valueOf(a.a(1).a(2).a(3).a(4).a(5))); 136 | assertEquals("[5, 1, 2, 3, 4]", String.valueOf(a.a(a.a()))); 137 | assertEquals("[4, 5, 1, 2, 3]", String.valueOf(a.a(a.a()))); 138 | assertEquals("[1, 2, 3, 4, 5, 6]", String.valueOf(a.a(6))); 139 | assertEquals("[6, 4, 5, 1, 2, 3]", String.valueOf(a.a(a.a()))); 140 | } 141 | 142 | @Test 143 | public void operations_can_be_sequenced_with_nesting() { 144 | a a = new a(); 145 | assertEquals("[1, 2, 3, 4, 5]", String.valueOf(a.a(1).a(2).a(3).a(4).a(5))); 146 | assertEquals("[5, 1, 2, 3, 4]", String.valueOf(a.a.a(a.a()))); 147 | assertEquals("[4, 5, 1, 2, 3]", String.valueOf(a.a(a.a()))); 148 | assertEquals("[1, 2, 3, 4, 5, 6]", String.valueOf(a.a(6))); 149 | assertEquals("[3, 6, 4, 5, 1, 2]", String.valueOf(a.a(a.a()).a(a.a()))); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /satori/B/OpenTest.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class OpenTest { 8 | 9 | @Test 10 | public void test0() { 11 | a a = new a(); 12 | a.a(1); 13 | a.a(2); 14 | a.a(3); 15 | assertEquals("[1, 2, 3]",String.valueOf(a)); 16 | } 17 | 18 | @Test 19 | public void test1() { 20 | a a = new a(); 21 | a.a(1); 22 | a.a(3); 23 | a.a(2); 24 | assertEquals("[1, 2, 3]",String.valueOf(a)); 25 | } 26 | 27 | @Test 28 | public void test2() { 29 | a a = new a(); 30 | a.a(1).a(3).a(2); 31 | assertEquals("[1, 2, 3]",String.valueOf(a)); 32 | assertEquals("[1, 3, 2]",String.valueOf(a.a)); 33 | } 34 | 35 | @Test 36 | public void test3() { 37 | a a = new a(); 38 | a.a(1).a(5).a(2).a(4).a(3); 39 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a)); 40 | assertEquals("[1, 5, 2, 4, 3]",String.valueOf(a.a)); 41 | assertEquals("3",String.valueOf(a.a())); 42 | assertEquals("[1, 2, 4, 5]",String.valueOf(a)); 43 | assertEquals("[1, 5, 2, 4]",String.valueOf(a.a)); 44 | assertEquals("4",String.valueOf(a.a())); 45 | assertEquals("[1, 2, 5]",String.valueOf(a)); 46 | assertEquals("[1, 5, 2]",String.valueOf(a.a)); 47 | assertEquals("2",String.valueOf(a.a())); 48 | assertEquals("[1, 5]",String.valueOf(a)); 49 | assertEquals("[1, 5]",String.valueOf(a.a)); 50 | assertEquals("5",String.valueOf(a.a())); 51 | assertEquals("[1]",String.valueOf(a)); 52 | assertEquals("[1]",String.valueOf(a.a)); 53 | assertEquals("1",String.valueOf(a.a())); 54 | assertEquals("[]",String.valueOf(a)); 55 | assertEquals("[]",String.valueOf(a.a)); 56 | } 57 | 58 | @Test 59 | public void test4() { 60 | a a = new a(); 61 | a.a(1).a(5).a(2).a(4).a(3); 62 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a)); 63 | assertEquals("[1, 5, 2, 4, 3]",String.valueOf(a.a)); 64 | a.a(a.a()); 65 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a)); 66 | assertEquals("[3, 1, 5, 2, 4]",String.valueOf(a.a)); 67 | a.a(a.a()); 68 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a)); 69 | assertEquals("[4, 3, 1, 5, 2]",String.valueOf(a.a)); 70 | a.a(a.a()).a(a.a()).a(a.a()); 71 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a)); 72 | assertEquals("[1, 5, 2, 4, 3]",String.valueOf(a.a)); 73 | a.a(1).a(5); 74 | assertEquals("[1, 1, 2, 3, 4, 5, 5]",String.valueOf(a)); 75 | assertEquals("[1, 5, 2, 4, 3, 1, 5]",String.valueOf(a.a)); 76 | a.a(a.a()); 77 | assertEquals("[1, 1, 2, 3, 4, 5, 5]",String.valueOf(a)); 78 | assertEquals("[5, 1, 5, 2, 4, 3, 1]",String.valueOf(a.a)); 79 | a.a(a.a()).a(a.a()).a(a.a()); 80 | assertEquals("[1, 1, 2, 3, 4, 5, 5]",String.valueOf(a)); 81 | assertEquals("[4, 3, 1, 5, 1, 5, 2]",String.valueOf(a.a)); 82 | } 83 | 84 | @Test 85 | public void test5() { 86 | a a = new a(); 87 | a.a(1).a(1).a(2).a(1).a(1); 88 | assertEquals("[1, 1, 2, 1, 1]",String.valueOf(a.a)); 89 | a.a.a(3).a.a(1).a(1).a.a.a(4); 90 | assertEquals("[1, 1, 2, 1, 1, 3, 1, 1, 4]",String.valueOf(a.a)); 91 | a = a.a; 92 | a.a(1).a(1).a.a.a(5); 93 | assertEquals("[1, 1, 2, 1, 1, 3, 1, 1, 4, 1, 1, 5]",String.valueOf(a)); 94 | } 95 | 96 | @Test 97 | public void test6() { 98 | a a = new a(); 99 | a.a(1).a(5).a(2).a(4).a(3); 100 | assertEquals("[3, 4, 2, 5, 1]",Arrays.toString(new int[] { a.a(), a.a(), a.a(), a.a(), 101 | a.a() })); 102 | } 103 | 104 | @Test 105 | public void test7() { 106 | a a = new a(); 107 | a.a.a.a.a.a.a.a.a.a(1); 108 | a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a(3); 109 | a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a(2); 110 | assertEquals("[1, 2, 3]",String.valueOf(a)); 111 | } 112 | 113 | @Test 114 | public void test8() { 115 | a a = new a(); 116 | a.a(1).a(3).a(2); 117 | assertEquals("[1, 2, 3]",String.valueOf(a)); 118 | assertEquals("[1, 3, 2]",String.valueOf(a.a)); 119 | assertEquals("[1, 2, 3]",String.valueOf(a.a.a)); 120 | assertEquals("[1, 3, 2]",String.valueOf(a.a.a.a)); 121 | assertEquals("[1, 2, 3]",String.valueOf(a.a.a.a.a)); 122 | for (int i = 0; i < 1000000; i++) { 123 | assertEquals("[1, 2, 3]",String.valueOf(a)); 124 | a = a.a; 125 | assertEquals("[1, 3, 2]",String.valueOf(a)); 126 | a = a.a; 127 | } 128 | } 129 | @Test 130 | public void test9() { 131 | a a = new a(); 132 | assertEquals("[1, 2, 3]",String.valueOf(a.a(1).a(3).a(2))); 133 | a = new a(); 134 | assertEquals("[1, 3, 2]",String.valueOf(a.a(1).a(3).a(2).a)); 135 | a = new a(); 136 | assertEquals("[1, 2, 3]",String.valueOf(a.a.a(1).a(3).a(2))); 137 | a = new a(); 138 | assertEquals("[1, 2, 3]",String.valueOf(a.a.a(1).a.a.a(3).a.a.a.a.a(2))); 139 | } 140 | 141 | @Test 142 | public void test10() { 143 | a a = new a(); 144 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a.a(1).a(2).a(3).a(4).a(5))); 145 | assertEquals("[5, 1, 2, 3, 4]",String.valueOf(a.a(a.a()))); 146 | assertEquals("[4, 5, 1, 2, 3]",String.valueOf(a.a(a.a()))); 147 | assertEquals("[1, 2, 3, 4, 5, 6]",String.valueOf(a.a(6))); 148 | assertEquals("[6, 4, 5, 1, 2, 3]",String.valueOf(a.a(a.a()))); 149 | } 150 | 151 | @Test 152 | public void test11(){ 153 | a a = new a(); 154 | assertEquals("[1, 2, 3, 4, 5]",String.valueOf(a.a(1).a(2).a(3).a(4).a(5))); 155 | assertEquals("[5, 1, 2, 3, 4]",String.valueOf(a.a.a(a.a()))); 156 | assertEquals("[4, 5, 1, 2, 3]",String.valueOf(a.a(a.a()))); 157 | assertEquals("[1, 2, 3, 4, 5, 6]",String.valueOf(a.a(6))); 158 | assertEquals("[3, 6, 4, 5, 1, 2]",String.valueOf(a.a(a.a()).a(a.a()))); 159 | } 160 | 161 | } 162 | -------------------------------------------------------------------------------- /satori/D/KacperTopolskiAgentTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import static org.junit.Assert.*; 3 | import java.util.Scanner; 4 | 5 | @SuppressWarnings({"rawtypes", "unchecked"}) 6 | public class KacperTopolskiAgentTest { 7 | static class SimpleAgent implements TajnyAgent { 8 | int i = 0; 9 | RuntimeException[] var; 10 | SimpleAgent(RuntimeException... varargs) { 11 | var = varargs; 12 | } 13 | @Override 14 | public void transmituj() { 15 | RuntimeException e = i < var.length ? var[i++] : new Koniec(); 16 | if (e != null) 17 | throw e; 18 | } 19 | }; 20 | 21 | void does_ch_work(String ch, RuntimeException... varargs) { 22 | Agent A = new Agent(ch); 23 | 24 | for (int i = 0; i < varargs.length; ++i) { 25 | if (varargs[i] == null) { 26 | A.transmituj(); 27 | continue; 28 | } 29 | 30 | try { 31 | A.transmituj(); 32 | fail("no exception"); 33 | } catch (Exception e) { 34 | var cl = e.getClass(); 35 | var ex_cl = varargs[i].getClass(); 36 | 37 | if (ex_cl == RuntimeException.class) 38 | assertFalse(cl == Koniec.class || cl == RozmowaKontrolowana.class || cl == Stop.class || cl == Zuber.class); 39 | else 40 | assertEquals(ex_cl, cl); 41 | } 42 | } 43 | 44 | SimpleAgent S = new SimpleAgent(varargs); 45 | Agent B = new Agent(S); 46 | Scanner SB = new Scanner(B); 47 | assertEquals(ch, SB.next()); 48 | } 49 | 50 | @Test 51 | public void alphabet_test() { 52 | does_ch_work("a", new RuntimeException(), null, new Stop(), new Koniec()); 53 | does_ch_work("ą", new RuntimeException(), null, new RuntimeException(), null, new Stop(), new Koniec()); 54 | does_ch_work("b", null, new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 55 | does_ch_work("c", null, new RuntimeException(), null, new RuntimeException(), new Stop(), new Koniec()); 56 | does_ch_work("ć", null, new RuntimeException(), null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 57 | does_ch_work("d", null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 58 | does_ch_work("e", new RuntimeException(), new Stop(), new Koniec()); 59 | does_ch_work("ę", new RuntimeException(), new RuntimeException(), null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 60 | does_ch_work("f", new RuntimeException(), new RuntimeException(), null, new RuntimeException(), new Stop(), new Koniec()); 61 | does_ch_work("g", null, null, new RuntimeException(), new Stop(), new Koniec()); 62 | does_ch_work("h", new RuntimeException(), new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 63 | does_ch_work("i", new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 64 | does_ch_work("j", new RuntimeException(), null, null, null, new Stop(), new Koniec()); 65 | does_ch_work("k", null, new RuntimeException(), null, new Stop(), new Koniec()); 66 | does_ch_work("l", new RuntimeException(), null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 67 | does_ch_work("ł", new RuntimeException(), null, new RuntimeException(), new RuntimeException(), null, new Stop(), new Koniec()); 68 | does_ch_work("m", null, null, new Stop(), new Koniec()); 69 | does_ch_work("n", null, new RuntimeException(), new Stop(), new Koniec()); 70 | does_ch_work("ń", null, null, new RuntimeException(), null, null, new Stop(), new Koniec()); 71 | does_ch_work("o", null, null, null, new Stop(), new Koniec()); 72 | does_ch_work("ó", null, null, null, new RuntimeException(), new Stop(), new Koniec()); 73 | does_ch_work("p", new RuntimeException(), null, null, new RuntimeException(), new Stop(), new Koniec()); 74 | does_ch_work("q", null, null, new RuntimeException(), null, new Stop(), new Koniec()); 75 | does_ch_work("r", new RuntimeException(), null, new RuntimeException(), new Stop(), new Koniec()); 76 | does_ch_work("s", new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 77 | does_ch_work("ś", new RuntimeException(), new RuntimeException(), new RuntimeException(), null, new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 78 | does_ch_work("t", null, new Stop(), new Koniec()); 79 | does_ch_work("u", new RuntimeException(), new RuntimeException(), null, new Stop(), new Koniec()); 80 | does_ch_work("v", new RuntimeException(), new RuntimeException(), new RuntimeException(), null, new Stop(), new Koniec()); 81 | does_ch_work("w", new RuntimeException(), null, null, new Stop(), new Koniec()); 82 | does_ch_work("x", null, new RuntimeException(), new RuntimeException(), null, new Stop(), new Koniec()); 83 | does_ch_work("y", null, new RuntimeException(), null, null, new Stop(), new Koniec()); 84 | does_ch_work("z", null, null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 85 | does_ch_work("ź", null, null, new RuntimeException(), new RuntimeException(), null, new RuntimeException(), new Stop(), new Koniec()); 86 | does_ch_work("ż", null, null, new RuntimeException(), new RuntimeException(), null, new Stop(), new Koniec()); 87 | does_ch_work("0", null, null, null, null, null, new Stop(), new Koniec()); 88 | does_ch_work("1", new RuntimeException(), null, null, null, null, new Stop(), new Koniec()); 89 | does_ch_work("2", new RuntimeException(), new RuntimeException(), null, null, null, new Stop(), new Koniec()); 90 | does_ch_work("3", new RuntimeException(), new RuntimeException(), new RuntimeException(), null, null, new Stop(), new Koniec()); 91 | does_ch_work("4", new RuntimeException(), new RuntimeException(), new RuntimeException(), new RuntimeException(), null, new Stop(), new Koniec()); 92 | does_ch_work("5", new RuntimeException(), new RuntimeException(), new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 93 | does_ch_work("6", null, new RuntimeException(), new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 94 | does_ch_work("7", null, null, new RuntimeException(), new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 95 | does_ch_work("8", null, null, null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 96 | does_ch_work("9", null, null, null, null, new RuntimeException(), new Stop(), new Koniec()); 97 | does_ch_work(".", new RuntimeException(), null, new RuntimeException(), null, new RuntimeException(), null, new Stop(), new Koniec()); 98 | does_ch_work(",", null, null, new RuntimeException(), new RuntimeException(), null, null, new Stop(), new Koniec()); 99 | does_ch_work("!", null, new RuntimeException(), null, new RuntimeException(), null, null, new Stop(), new Koniec()); 100 | does_ch_work("?", new RuntimeException(), new RuntimeException(), null, null, new RuntimeException(), new RuntimeException(), new Stop(), new Koniec()); 101 | does_ch_work("/", null, new RuntimeException(), new RuntimeException(), null, new RuntimeException(), new Stop(), new Koniec()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /satori/A/LeonidDorochko/LeonidDorochkoPhoneBookTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class LeonidDorochkoPhoneBookTest { 6 | 7 | @Test 8 | public void mutable_copy_test() { 9 | PhoneBook a = new PhoneBook().add("111000000"); 10 | PhoneBook pb = new PhoneBook().add(a); 11 | assertEquals("{\n {\n 111000000\n }\n}\n", pb.toString()); 12 | assertEquals("{\n 111000000\n}\n", a.toString()); 13 | 14 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 15 | assertEquals("{\n {\n 111-000-000\n }\n}\n", pb.toString()); 16 | assertEquals("{\n 111000000\n}\n", a.toString()); 17 | 18 | a.changeFormat(PhoneBook.NumberFormat.HYPHENED); 19 | assertEquals("{\n {\n 111-000-000\n }\n}\n", pb.toString()); 20 | assertEquals("{\n 111-000-000\n}\n", a.toString()); 21 | 22 | a.changeFormat(PhoneBook.NumberFormat.DIGITS); 23 | assertEquals("{\n {\n 111-000-000\n }\n}\n", pb.toString()); 24 | assertEquals("{\n 111000000\n}\n", a.toString()); 25 | } 26 | 27 | @Test 28 | public void two_books_change_format_of_one() { 29 | PhoneBook book1 = new PhoneBook().add("111000111"); 30 | PhoneBook book2 = new PhoneBook().add("222000222"); 31 | assertEquals("{\n 111000111\n}\n", book1.toString()); 32 | assertEquals("{\n 222000222\n}\n", book2.toString()); 33 | book1.changeFormat(PhoneBook.NumberFormat.HYPHENED); 34 | assertEquals("{\n 111-000-111\n}\n", book1.toString()); 35 | assertEquals("{\n 222000222\n}\n", book2.toString()); 36 | } 37 | 38 | @Test 39 | public void few_books_nested_then_change_format(){ 40 | PhoneBook a = new PhoneBook(11).add("111111111"); 41 | PhoneBook b = new PhoneBook(12).add(a); 42 | PhoneBook c = new PhoneBook(PhoneBook.NumberFormat.HYPHENED, 12).add(b); 43 | assertEquals("{\n {\n {\n 111-111-111\n }\n }\n}\n", c.toString()); 44 | assertTrue(c.contains("111-111-111")); 45 | assertFalse(c.contains("111111111")); 46 | PhoneBook d = new PhoneBook(PhoneBook.NumberFormat.DIGITS, 20).add(c); 47 | assertEquals("{\n {\n {\n {\n 111111111\n }\n }\n }\n}\n", d.toString()); 48 | assertTrue(d.contains("111111111")); 49 | assertFalse(d.contains("111-111-111")); 50 | } 51 | 52 | @Test 53 | public void add_myself_then_change_format () { 54 | PhoneBook book1 = new PhoneBook().add("111000111").add("222000222"); 55 | PhoneBook book2 = new PhoneBook().add(book1); 56 | PhoneBook book3 = new PhoneBook().add(book2); 57 | book3.add(book3); 58 | PhoneBook book4 = new PhoneBook().add(book3).add(book2).add(book1); 59 | book4.add(book4); 60 | 61 | 62 | assertEquals("{\n 111000111\n 222000222\n}\n", book1.toString()); 63 | assertEquals("{\n {\n 111000111\n 222000222\n }\n}\n", book2.toString()); 64 | assertEquals("{\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n}\n", book3.toString()); 65 | assertEquals("{\n {\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n }\n {\n {\n 111000111\n 222000222\n }\n }\n {\n 111000111\n 222000222\n }\n}\n", book4.toString()); 66 | 67 | book1.changeFormat(PhoneBook.NumberFormat.HYPHENED); 68 | assertEquals("{\n 111-000-111\n 222-000-222\n}\n", book1.toString()); 69 | assertEquals("{\n {\n 111000111\n 222000222\n }\n}\n", book2.toString()); 70 | assertEquals("{\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n}\n", book3.toString()); 71 | assertEquals("{\n {\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n }\n {\n {\n 111000111\n 222000222\n }\n }\n {\n 111000111\n 222000222\n }\n}\n", book4.toString()); 72 | 73 | book2.changeFormat(PhoneBook.NumberFormat.HYPHENED); 74 | assertEquals("{\n 111-000-111\n 222-000-222\n}\n", book1.toString()); 75 | assertEquals("{\n {\n 111-000-111\n 222-000-222\n }\n}\n", book2.toString()); 76 | assertEquals("{\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n}\n", book3.toString()); 77 | assertEquals("{\n {\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n }\n {\n {\n 111000111\n 222000222\n }\n }\n {\n 111000111\n 222000222\n }\n}\n", book4.toString()); 78 | 79 | book3.changeFormat(PhoneBook.NumberFormat.HYPHENED); 80 | assertEquals("{\n 111-000-111\n 222-000-222\n}\n", book1.toString()); 81 | assertEquals("{\n {\n 111-000-111\n 222-000-222\n }\n}\n", book2.toString()); 82 | assertEquals("{\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n }\n}\n", book3.toString()); 83 | assertEquals("{\n {\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n }\n {\n {\n 111000111\n 222000222\n }\n }\n {\n 111000111\n 222000222\n }\n}\n", book4.toString()); 84 | 85 | book4.changeFormat(PhoneBook.NumberFormat.HYPHENED); 86 | assertEquals("{\n 111-000-111\n 222-000-222\n}\n", book1.toString()); 87 | assertEquals("{\n {\n 111-000-111\n 222-000-222\n }\n}\n", book2.toString()); 88 | assertEquals("{\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n }\n}\n", book3.toString()); 89 | assertEquals("{\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n }\n }\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n 111-000-111\n 222-000-222\n }\n}\n", book4.toString()); 90 | 91 | book3.changeFormat(PhoneBook.NumberFormat.DIGITS); 92 | assertEquals("{\n 111-000-111\n 222-000-222\n}\n", book1.toString()); 93 | assertEquals("{\n {\n 111-000-111\n 222-000-222\n }\n}\n", book2.toString()); 94 | assertEquals("{\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n}\n", book3.toString()); 95 | assertEquals("{\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n }\n }\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n 111-000-111\n 222-000-222\n }\n}\n", book4.toString()); 96 | 97 | book2.changeFormat(PhoneBook.NumberFormat.DIGITS); 98 | assertEquals("{\n 111-000-111\n 222-000-222\n}\n", book1.toString()); 99 | assertEquals("{\n {\n 111000111\n 222000222\n }\n}\n", book2.toString()); 100 | assertEquals("{\n {\n {\n 111000111\n 222000222\n }\n }\n {\n {\n {\n 111000111\n 222000222\n }\n }\n }\n}\n", book3.toString()); 101 | assertEquals("{\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n }\n }\n {\n {\n 111-000-111\n 222-000-222\n }\n }\n {\n 111-000-111\n 222-000-222\n }\n}\n", book4.toString()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /satori/A/VladKoz/VladKozPhoneBookTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Random; 4 | 5 | import static junit.framework.TestCase.assertEquals; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class VladKozPhoneBookTest 10 | { 11 | @Test 12 | public void addingElementsWithHostileIntent() 13 | { 14 | PhoneBook a = new PhoneBook(); 15 | a.add("asdad"); 16 | a.add("3 2 1"); 17 | a.add("helo helo 3 2 0"); 18 | a.add("slyszales kiedys zart o falowanych blaszkach?"); 19 | a.add("01111111"); 20 | a.changeFormat(PhoneBook.NumberFormat.HYPHENED); 21 | a.add("111-111-111"); 22 | a.add("111-111-11"); 23 | a.add("111-1111-11"); 24 | 25 | a.add("przychodzi matematyk do baru"); 26 | a.add("atam0000000"); 27 | a.add("zbiorpusty0"); 28 | a.add("zbi-orp-ust"); 29 | a.add("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 30 | 31 | assertEquals("{\n 111-111-111\n}\n", a.toString()); 32 | } 33 | 34 | @Test 35 | public void testCopy() 36 | { 37 | PhoneBook g1 = new PhoneBook().add("111000001").add("111000002").add("111000003"); 38 | PhoneBook g2 = g1.copyBook(); 39 | g2.changeFormat(PhoneBook.NumberFormat.HYPHENED); 40 | assertEquals(3, g1.size()); 41 | assertTrue(g1.equals(g2)); 42 | g2.add("222-000-001"); 43 | assertEquals(3, g1.size()); 44 | assertEquals(4, g2.size()); 45 | assertFalse(g1.equals(g2)); 46 | assertTrue(g1.subsetOf(g2)); 47 | } 48 | 49 | @Test 50 | public void EmptyAdd() 51 | { 52 | var pb = new PhoneBook(1); 53 | var empty = new PhoneBook(99); 54 | for (int i = 0; i < 10; i++) 55 | { 56 | pb.add(empty); 57 | } 58 | assertFalse(pb.isEmpty()); 59 | assertEquals(pb.size(), 0); 60 | var notEmpty = new PhoneBook(1).add("111111111"); 61 | pb.add(notEmpty); 62 | assertEquals(1, pb.size()); 63 | assertEquals(""" 64 | { 65 | { 66 | } 67 | { 68 | 111111111 69 | } 70 | } 71 | """, pb.toString()); 72 | 73 | var obj = new PhoneBook(); 74 | assertTrue(obj.isEmpty()); 75 | } 76 | 77 | @Test 78 | public void isEmptyEpty() 79 | { 80 | var empty100proc = new PhoneBook(0); 81 | assertTrue(empty100proc.isEmpty()); 82 | assertTrue(empty100proc.isFull()); 83 | assertEquals(0, empty100proc.capacity()); 84 | // RTE on 4'th test 85 | empty100proc.add("123"); 86 | empty100proc.add(empty100proc); 87 | empty100proc.copyBook(); 88 | assertFalse(empty100proc.contains("123")); 89 | assertFalse(empty100proc.contains(empty100proc)); 90 | assertEquals(empty100proc, empty100proc); 91 | 92 | var pbWithEmpty = new PhoneBook(0); 93 | pbWithEmpty.add(pbWithEmpty); 94 | 95 | var pbEmpyADd = new PhoneBook(); 96 | pbEmpyADd.add("011111111"); 97 | pbEmpyADd.add("111-111-111"); 98 | pbEmpyADd.changeFormat(PhoneBook.NumberFormat.HYPHENED); 99 | pbEmpyADd.add("011-111-111"); 100 | pbEmpyADd.add("111111111"); 101 | assertTrue(pbEmpyADd.isEmpty()); 102 | } 103 | 104 | private void RandomFillNum(PhoneBook b, int n, int seed, PhoneBook.NumberFormat format) 105 | { 106 | var rand = new Random(); 107 | rand.setSeed(seed); 108 | for (int i = 0; i < n; i++) 109 | { 110 | if (format == PhoneBook.NumberFormat.DIGITS) 111 | { 112 | b.add(randomTrioString(rand) + 113 | randomTrioString(rand) + 114 | randomTrioString(rand)); 115 | } else 116 | { 117 | b.add(randomTrioString(rand) + "-" 118 | + randomTrioString(rand) + "-" 119 | + randomTrioString(rand)); 120 | } 121 | } 122 | } 123 | 124 | private void RandomFillBooks(PhoneBook b, int n, int seed, 125 | PhoneBook.NumberFormat format) 126 | { 127 | var rand = new Random(); 128 | rand.setSeed(seed); 129 | for (int i = 0; i < n; i++) 130 | { 131 | var tmp = new PhoneBook(format); 132 | RandomFillNum(tmp, 2, seed + i, format); 133 | b.add(tmp); 134 | } 135 | } 136 | 137 | 138 | private String randomTrioString(Random rand) 139 | { 140 | return ((Integer) (rand.nextInt(899) + 100)).toString(); 141 | } 142 | 143 | @Test 144 | public void testEquals() 145 | { 146 | { 147 | var pb1 = new PhoneBook(0); 148 | var pb2 = new PhoneBook(PhoneBook.NumberFormat.HYPHENED); 149 | assertEquals(pb1, pb2); 150 | } 151 | { 152 | var pb1 = new PhoneBook(null, -4); 153 | var pb2 = new PhoneBook(); 154 | RandomFillNum(pb1, 12, 42, PhoneBook.NumberFormat.DIGITS); 155 | RandomFillNum(pb2, 12, 42, PhoneBook.NumberFormat.DIGITS); 156 | assertEquals(pb1, pb2); 157 | assertEquals(pb1.capacity(), pb1.size()); 158 | assertEquals(pb1, pb2); 159 | } 160 | { 161 | var pb1 = new PhoneBook().add("111111111").add("222222222"); 162 | var pb2 = new PhoneBook().add("222222222").add("111111111"); 163 | RandomFillNum(pb1, 2, 42, PhoneBook.NumberFormat.DIGITS); 164 | RandomFillNum(pb2, 2, 42, PhoneBook.NumberFormat.DIGITS); 165 | 166 | assertEquals(pb1, pb2); 167 | } 168 | { 169 | var pb1 = new PhoneBook(PhoneBook.NumberFormat.HYPHENED); 170 | var pb2 = new PhoneBook(); 171 | 172 | RandomFillBooks(pb1, 2, 42, PhoneBook.NumberFormat.HYPHENED); 173 | var tmp = new PhoneBook(); 174 | RandomFillNum(tmp, 2, 42, PhoneBook.NumberFormat.DIGITS); 175 | var tmp2 = new PhoneBook(); 176 | RandomFillNum(tmp2, 2, 43, PhoneBook.NumberFormat.DIGITS); 177 | 178 | pb2.add(tmp2); 179 | pb2.add(tmp); 180 | assertEquals(pb1, pb2); 181 | } 182 | { 183 | var pb1 = new PhoneBook(); 184 | var pb2 = new PhoneBook(10); 185 | var pb3 = new PhoneBook(PhoneBook.NumberFormat.HYPHENED); 186 | var pb4 = new PhoneBook(PhoneBook.NumberFormat.DIGITS); 187 | var pb5 = new PhoneBook(PhoneBook.NumberFormat.HYPHENED, 10); 188 | 189 | RandomFillBooks(pb1, 11, 42, PhoneBook.NumberFormat.DIGITS); 190 | RandomFillBooks(pb2, 11, 42, PhoneBook.NumberFormat.DIGITS); 191 | RandomFillBooks(pb3, 11, 42, PhoneBook.NumberFormat.HYPHENED); 192 | RandomFillBooks(pb4, 11, 42, PhoneBook.NumberFormat.DIGITS); 193 | RandomFillBooks(pb5, 11, 42, PhoneBook.NumberFormat.HYPHENED); 194 | 195 | assertEquals(pb1, pb2); 196 | assertEquals(pb3, pb2); 197 | assertEquals(pb3, pb4); 198 | assertEquals(pb5, pb4); 199 | assertEquals(pb5, pb5); 200 | } 201 | { 202 | var pb2 = new PhoneBook(11); 203 | var pb5 = new PhoneBook(PhoneBook.NumberFormat.HYPHENED, 11); 204 | 205 | RandomFillBooks(pb2, 11, 42,PhoneBook.NumberFormat.DIGITS); 206 | RandomFillBooks(pb5, 11, 42,PhoneBook.NumberFormat.HYPHENED); 207 | 208 | assertEquals(pb2, pb5); 209 | } 210 | } 211 | 212 | @Test 213 | 214 | public void testToString() 215 | { 216 | 217 | { 218 | var pbST = new PhoneBook(); 219 | pbST.add(pbST); 220 | pbST.add(pbST); 221 | pbST.add(pbST); 222 | assertEquals(""" 223 | { 224 | { 225 | } 226 | { 227 | { 228 | } 229 | } 230 | { 231 | { 232 | } 233 | { 234 | { 235 | } 236 | } 237 | } 238 | } 239 | """, pbST.toString()); 240 | } 241 | } 242 | 243 | @Test 244 | 245 | public void copyBook() 246 | { 247 | 248 | var pb42 = new PhoneBook().add("420000000"); 249 | var pb43 = new PhoneBook().add("430000000"); 250 | var pbpb43 = new PhoneBook().add(pb43); 251 | var book = new PhoneBook(42).add(pb42).add(pb43).add(pbpb43); 252 | // { 42 43 {43} } 253 | var copyPb = book.copyBook(); 254 | assertEquals(copyPb, book); 255 | 256 | pb42.add("420000000"); 257 | pb43.changeFormat(PhoneBook.NumberFormat.HYPHENED); 258 | assertEquals(copyPb, book); 259 | 260 | book.changeFormat(PhoneBook.NumberFormat.HYPHENED); 261 | assertEquals(copyPb, book); 262 | assertEquals(pb43.add("111111111").size(), 1); 263 | } 264 | 265 | @Test 266 | 267 | public void size() 268 | { 269 | var ofiara = new PhoneBook(); 270 | for (int i = 0; i < 15; i++) 271 | { 272 | 273 | ofiara.add(new PhoneBook()); 274 | } 275 | assertEquals(0, ofiara.size()); 276 | 277 | 278 | for (int i = 0; i < 15; i++) 279 | { 280 | 281 | ofiara.add(new PhoneBook().add("123123123")); 282 | } 283 | assertEquals(1, ofiara.size()); 284 | } 285 | 286 | @Test 287 | 288 | public void isEmpty() 289 | { 290 | 291 | var bookE = new PhoneBook(); 292 | var bookN = new PhoneBook().add("123123123"); 293 | var bookPB = new PhoneBook().add(bookE); 294 | assertEquals(bookE.size(), 0); 295 | assertEquals(bookN.size(), 1); 296 | assertEquals(bookPB.size(), 0); 297 | assertTrue(bookE.isEmpty()); 298 | assertFalse(bookPB.isEmpty()); 299 | } 300 | 301 | @Test 302 | 303 | public void elementOf() 304 | { 305 | 306 | PhoneBook g0 = new PhoneBook().add("111000001").add("111000003"); 307 | PhoneBook g1 = new PhoneBook().add("111000001").add("111000002").add("111000003"); 308 | PhoneBook g2 = new PhoneBook().add("222000001").add("222000002").add("222000003"); 309 | PhoneBook pb1 = new PhoneBook(7).add(g1).add(g2); 310 | PhoneBook pb2 = new PhoneBook().add(g2).add(g1); 311 | pb1.add(g0); 312 | pb2.add(g0); 313 | assertFalse(pb1.equals(pb2)); 314 | } 315 | } -------------------------------------------------------------------------------- /satori/C/MichalHorodeckiListIsFunctionTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import org.junit.experimental.runners.Enclosed; 3 | import org.junit.runner.RunWith; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | import java.util.*; 8 | 9 | @SuppressWarnings({"rawtypes", "unchecked"}) 10 | @RunWith(Enclosed.class) 11 | public class MichalHorodeckiListIsFunctionTest 12 | { 13 | // Test operations that should not throw an exception 14 | public static class OperationsWithoutExceptionsTest 15 | { 16 | @Test 17 | public void new_list_is_empty() 18 | { 19 | var list = new ListIsFunction(); 20 | assertTrue(list.asList().isEmpty()); 21 | assertTrue(list.asMap().isEmpty()); 22 | assertTrue(list.asMap().entrySet().isEmpty()); 23 | assertTrue(list.asMap().keySet().isEmpty()); 24 | } 25 | 26 | @Test 27 | public void new_list_has_size_of_zero() 28 | { 29 | var list = new ListIsFunction(); 30 | assertEquals(0, list.asList().size()); 31 | assertEquals(0, list.asMap().size()); 32 | assertEquals(0, list.asMap().entrySet().size()); 33 | assertEquals(0, list.asMap().keySet().size()); 34 | } 35 | 36 | @Test 37 | public void adding_element_to_list_adds_it_to_map() 38 | { 39 | var list = new ListIsFunction(); 40 | list.asList().add("A"); 41 | assertFalse(list.asMap().isEmpty()); 42 | assertEquals(1, list.asMap().size()); 43 | assertTrue(list.asMap().containsKey(0)); 44 | assertTrue(list.asMap().containsValue("A")); 45 | } 46 | 47 | @Test 48 | public void adding_element_to_map_adds_it_to_list() 49 | { 50 | var list = new ListIsFunction(); 51 | list.asMap().put(0, "A"); 52 | list.asMap().put(1, "B"); 53 | assertFalse(list.asList().isEmpty()); 54 | assertEquals(2, list.asList().size()); 55 | assertTrue(list.asList().contains("A")); 56 | assertTrue(list.asList().contains("B")); 57 | } 58 | 59 | @Test 60 | public void removing_element_from_map_removes_it_from_list() 61 | { 62 | var list = new ListIsFunction(); 63 | list.asList().add("A"); 64 | list.asList().add("B"); 65 | list.asMap().remove(1); 66 | assertEquals("[A]", list.asList().toString()); 67 | } 68 | 69 | @Test 70 | public void list_allows_removing_every_index() 71 | { 72 | var list = new ListIsFunction(); 73 | list.asList().add("A"); 74 | list.asList().add("B"); 75 | list.asList().add("C"); 76 | list.asList().remove(1); 77 | list.asList().remove(0); 78 | list.asList().remove(0); 79 | assertTrue(list.asList().isEmpty()); 80 | } 81 | 82 | @Test 83 | public void clearing_list_clears_map() 84 | { 85 | var list = new ListIsFunction(); 86 | list.asList().add("A"); 87 | list.asList().add("B"); 88 | list.asList().clear(); 89 | 90 | assertTrue(list.asMap().isEmpty()); 91 | assertEquals(0, list.asMap().size()); 92 | } 93 | 94 | @Test 95 | public void map_prints_elements_in_order_of_list() 96 | { 97 | var list = new ListIsFunction(); 98 | for (int i = 5; i > 0; i--) 99 | list.asList().add(i); 100 | 101 | assertEquals("{0=5, 1=4, 2=3, 3=2, 4=1}", list.asMap().toString()); 102 | } 103 | 104 | @Test 105 | public void map_iterates_over_elements_in_order_of_list() 106 | { 107 | var list = new ListIsFunction(); 108 | for (int i = 0; i < 5; i++) 109 | list.asList().add(i); 110 | 111 | var iterator = list.asMap().keySet().iterator(); 112 | for (int i = 0; i < 5; i++) 113 | assertEquals(i, iterator.next()); 114 | } 115 | 116 | @Test 117 | public void list_accepts_objects_as_values() 118 | { 119 | var list = new ListIsFunction(); 120 | list.asList().add(null); 121 | list.asList().add(new Object()); 122 | list.asList().add(list); 123 | list.asList().add(1); 124 | list.asList().add(""); 125 | assertEquals(5, list.asList().size()); 126 | assertEquals(5, list.asMap().size()); 127 | } 128 | 129 | @Test 130 | public void map_accepts_objects_as_values() 131 | { 132 | var list = new ListIsFunction(); 133 | list.asMap().put(0, null); 134 | list.asMap().put(1, new Object()); 135 | list.asMap().put(2, list); 136 | list.asMap().put(3, 4); 137 | list.asMap().put(4, ""); 138 | assertEquals(5, list.asList().size()); 139 | assertEquals(5, list.asMap().size()); 140 | } 141 | 142 | @Test 143 | public void removing_key_out_of_list_bounds_does_nothing() 144 | { 145 | var list = new ListIsFunction(); 146 | list.asList().add("A"); 147 | list.asList().add("B"); 148 | list.asMap().remove(-1); 149 | list.asMap().remove(2); 150 | assertEquals("[A, B]", list.asList().toString()); 151 | } 152 | } 153 | 154 | // Tests if exceptions are thrown in appropriate places when using Map functionality 155 | public static class MapExceptionsTest 156 | { 157 | /** 158 | * Call a function with expectation of throwing an exception without a message 159 | */ 160 | public static void assertThrowsWithoutMessage(Class exceptionClass, Runnable f) 161 | { 162 | try 163 | { 164 | f.run(); 165 | fail("expected " + exceptionClass.getSimpleName() + 166 | " but nothing was thrown"); 167 | } 168 | catch (Exception e) 169 | { 170 | assertEquals(exceptionClass, e.getClass()); 171 | assertNull(e.getMessage()); 172 | } 173 | } 174 | 175 | @Test 176 | public void removing_key_from_beginning_throws_exception() 177 | { 178 | var list = new ListIsFunction(); 179 | list.asList().add("A"); 180 | list.asList().add("B"); 181 | Runnable action = () -> list.asMap().remove(0); 182 | assertThrowsWithoutMessage(IllegalArgumentException.class, action); 183 | } 184 | 185 | @Test 186 | public void removing_key_from_middle_throws_exception() 187 | { 188 | var list = new ListIsFunction(); 189 | list.asList().add("A"); 190 | list.asList().add("B"); 191 | list.asList().add("C"); 192 | Runnable action = () -> list.asMap().remove(1); 193 | assertThrowsWithoutMessage(IllegalArgumentException.class, action); 194 | } 195 | 196 | @Test 197 | public void removing_iterator_without_calling_next_first_throws_exception() 198 | { 199 | var list = new ListIsFunction(); 200 | list.asList().add("A"); 201 | var iterator = list.asMap().keySet().iterator(); 202 | Runnable action = iterator::remove; 203 | assertThrowsWithoutMessage(IllegalStateException.class, action); 204 | } 205 | 206 | @Test 207 | public void removing_iterator_from_beginning_throws_exception() 208 | { 209 | var list = new ListIsFunction(); 210 | list.asList().add("A"); 211 | list.asList().add("B"); 212 | var iterator = list.asMap().keySet().iterator(); 213 | iterator.next(); 214 | Runnable action = iterator::remove; 215 | assertThrowsWithoutMessage(IllegalStateException.class, action); 216 | } 217 | 218 | @Test 219 | public void removing_iterator_from_middle_throws_exception() 220 | { 221 | var list = new ListIsFunction(); 222 | list.asList().add("A"); 223 | list.asList().add("B"); 224 | list.asList().add("C"); 225 | var iterator = list.asMap().keySet().iterator(); 226 | iterator.next(); 227 | iterator.next(); 228 | Runnable action = iterator::remove; 229 | assertThrowsWithoutMessage(IllegalStateException.class, action); 230 | } 231 | 232 | @Test 233 | public void calling_next_on_iterator_after_last_element_throws_exception() 234 | { 235 | var list = new ListIsFunction(); 236 | list.asList().add("A"); 237 | list.asList().add("B"); 238 | var iterator = list.asMap().keySet().iterator(); 239 | iterator.next(); 240 | iterator.next(); 241 | Runnable action = iterator::next; 242 | assertThrowsWithoutMessage(NoSuchElementException.class, action); 243 | } 244 | 245 | @Test 246 | public void putting_null_key_throws_exception_without_message() 247 | { 248 | var list = new ListIsFunction(); 249 | Runnable action = () -> list.asMap().put(null, ""); 250 | assertThrowsWithoutMessage(NullPointerException.class, action); 251 | } 252 | 253 | @Test 254 | public void putting_non_integer_key_throws_exception_without_message() 255 | { 256 | var list = new ListIsFunction(); 257 | Runnable action = () -> list.asMap().put("", ""); 258 | assertThrowsWithoutMessage(ClassCastException.class, action); 259 | } 260 | 261 | @Test 262 | public void putting_key_out_of_bounds_throws_exception_without_message() 263 | { 264 | var list = new ListIsFunction(); 265 | list.asList().add("A"); 266 | Runnable add_before = () -> list.asMap().put(-1, ""); 267 | Runnable add_after = () -> list.asMap().put(2, ""); 268 | assertThrowsWithoutMessage(IllegalArgumentException.class, add_before); 269 | assertThrowsWithoutMessage(IllegalArgumentException.class, add_after); 270 | } 271 | 272 | @Test 273 | public void removing_null_key_throws_exception_without_message() 274 | { 275 | var list = new ListIsFunction(); 276 | Runnable action = () -> list.asMap().remove(null); 277 | assertThrowsWithoutMessage(NullPointerException.class, action); 278 | } 279 | 280 | @Test 281 | public void removing_non_integer_key_throws_exception_without_message() 282 | { 283 | var list = new ListIsFunction(); 284 | Runnable action = () -> list.asMap().remove(""); 285 | assertThrowsWithoutMessage(ClassCastException.class, action); 286 | } 287 | 288 | 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /satori/D/MichalHorodeckiAgentTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import org.junit.experimental.runners.Enclosed; 3 | import org.junit.runner.RunWith; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Scanner; 7 | 8 | import static org.junit.Assert.*; 9 | 10 | 11 | @RunWith(Enclosed.class) 12 | public class MichalHorodeckiAgentTest 13 | { 14 | public static final String allCharacters = 15 | "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "ąćęłńóśżź" + ".,!?/"; 16 | 17 | // Test if agent receives transmissions properly 18 | public static class ReceivingTest 19 | { 20 | @Test 21 | public void every_character_can_be_encoded_and_then_decoded() 22 | { 23 | // Test if all characters at once are transmittable 24 | var textTransmitter = new Agent(allCharacters); 25 | var textReceiver = new Agent(textTransmitter); 26 | var textScanner = new Scanner(textReceiver); 27 | assertEquals(allCharacters, textScanner.next()); 28 | } 29 | 30 | @Test 31 | public void each_character_can_be_encoded_and_decoded_separately() 32 | { 33 | // Test transmitting a single character 34 | // Checking all letters to check potential edge cases 35 | for (char letter : allCharacters.toCharArray()) 36 | { 37 | var letterTransmitter = new Agent("" + letter); 38 | var letterReceiver = new Agent(letterTransmitter); 39 | var letterScanner = new Scanner(letterReceiver); 40 | assertEquals("" + letter, letterScanner.next()); 41 | assertFalse(letterScanner.hasNext()); 42 | } 43 | } 44 | 45 | @Test 46 | @Ignore // This test isn't checked by satori 47 | public void read_consumes_text_to_word_delimiter() 48 | { 49 | // Test if Agent consumes text to word delimiter even if the scanner 50 | // uses letter as a delimiter 51 | var textTransmitter = new Agent("firstxcde/secondxcde/foo"); 52 | 53 | Scanner s1 = new Scanner(new Agent(textTransmitter)); 54 | Scanner s2 = new Scanner(new Agent(textTransmitter)); 55 | 56 | s1.useDelimiter("x"); 57 | s2.useDelimiter("x"); 58 | 59 | // s1.read() should consume whole word 60 | assertEquals("first", s1.next()); 61 | // s2.read() should start reading from the next word 62 | assertEquals("second", s2.next()); 63 | // s1.read() returns the rest of first word and consumes everything 64 | assertEquals("cde/foo", s1.next()); 65 | } 66 | 67 | @Test 68 | public void agent_does_not_request_data_after_transmission_end() 69 | { 70 | String[] transmission = {".", "-", "stop"}; 71 | 72 | var transmitter = new MockTransmitter(transmission); 73 | var receiver = new Scanner(new Agent(transmitter)); 74 | receiver.next(); // consume the letter 75 | // hasNext calls `read` which should *not* call transmituj anymore 76 | assertFalse(receiver.hasNext()); 77 | } 78 | 79 | @Test 80 | public void rozmowa_kontrolowana_is_ignored() 81 | { 82 | String[] transmission = {".", "kontrola", "-", "kontrola", "kontrola", "stop", ".", "-", "stop", "kontrola"}; 83 | String expected = "aa"; 84 | 85 | var transmitter = new MockTransmitter(transmission); 86 | var receiver = new Scanner(new Agent(transmitter)); 87 | assertEquals(expected, receiver.next()); 88 | } 89 | } 90 | 91 | // Test if agent transmits data properly 92 | public static class TransmissionTest 93 | { 94 | @Test 95 | public void message_ends_with_koniec() 96 | { 97 | var agent = new Agent("aaa"); 98 | var receiver = new MockReceiver(); 99 | receiver.receiveN(agent, 9); // receive letters and stops 100 | assertEquals("koniec", receiver.receiveOne(agent)); 101 | } 102 | 103 | @Test 104 | public void character_ends_with_stop() 105 | { 106 | var agent = new Agent("aaa"); 107 | var receiver = new MockReceiver(); 108 | for (int i = 0; i < 3; i++) 109 | { 110 | receiver.receiveN(agent, 2); // a = .- 111 | assertEquals("stop", receiver.receiveOne(agent)); 112 | } 113 | } 114 | 115 | @Test 116 | @Ignore // This test isn't checked by satori 117 | public void empty_message_is_just_koniec() 118 | { 119 | var emptyAgent = new Agent(""); 120 | var receiver = new MockReceiver(); 121 | assertEquals("koniec", receiver.receiveOne(emptyAgent)); 122 | } 123 | 124 | @Test 125 | public void test_transmituj_does_not_listen_to_transmission() 126 | { 127 | // Test if transmitter does not consume data from agent it listens to 128 | 129 | var innerAgent = new Agent("foo/bar/baz/"); 130 | 131 | // Testing behavior of those agents 132 | // We are connecting two agents to inner to make sure no caching occurs 133 | var firstInnerListener = new Agent(innerAgent); 134 | var secondInnerListener = new Agent(innerAgent); 135 | 136 | var outerAgent = new Agent(firstInnerListener); 137 | var outerScanner = new Scanner(outerAgent); 138 | 139 | var secondInnerListenerScanner = new Scanner(secondInnerListener); 140 | 141 | outerScanner.useDelimiter("/"); 142 | secondInnerListenerScanner.useDelimiter("/"); 143 | 144 | outerScanner.next(); // outer.read() -> firstInnerListener.transmituj() 145 | assertEquals("foo", secondInnerListenerScanner.next()); 146 | } 147 | 148 | @Test 149 | public void read_does_not_advance_transmission() 150 | { 151 | var innerAgent = new Agent(); 152 | var agent = new Agent(innerAgent, "foo/bar/baz/"); 153 | // Listens to agent transmission 154 | var agentListener = new Scanner(new Agent(agent)); 155 | // Calls read 156 | var agentReader = new Scanner(agent); 157 | agentListener.useDelimiter("/"); 158 | agentReader.useDelimiter("/"); 159 | 160 | agentReader.next(); 161 | assertEquals("foo", agentListener.next()); 162 | agentReader.next(); 163 | agentReader.next(); 164 | assertEquals("bar", agentListener.next()); 165 | } 166 | 167 | @Test 168 | public void transmission_state_is_independent_of_listeners() 169 | { 170 | var transmitter = new Agent("a/b/c/d/e/"); 171 | Scanner[] listeners = {null, null, null}; 172 | for (int i = 0; i < 3; i++) 173 | { 174 | listeners[i] = new Scanner(new Agent(transmitter)); 175 | listeners[i].useDelimiter("/"); 176 | } 177 | 178 | // Order of listeners and calls doesn't really matter 179 | // All that is important is that different listeners are called 180 | // throughout the test 181 | assertEquals("a", listeners[1].next()); 182 | assertEquals("b", listeners[0].next()); 183 | assertEquals("c", listeners[0].next()); 184 | assertEquals("d", listeners[2].next()); 185 | assertEquals("e", listeners[1].next()); 186 | } 187 | } 188 | 189 | // Test everything related to this little mf 190 | public static class ZuberTest 191 | { 192 | @Test 193 | public void zuber_does_not_stop_reading() 194 | { 195 | String[] transmission = {".", "zuber", "-", "stop"}; 196 | var zuberTransmitter = new MockTransmitter(transmission); 197 | var zuberListener = new Agent(zuberTransmitter); 198 | var scanner = new Scanner(zuberListener); 199 | assertEquals("a", scanner.next()); 200 | } 201 | 202 | @Test 203 | public void zuber_is_transmitted_after_finishing_word() 204 | { 205 | String[] transmission = {".", "zuber", "-", "stop"}; 206 | var zuberTransmitter = new MockTransmitter(transmission); 207 | 208 | var zuberListener = new Agent(zuberTransmitter, "foo/bar/baz/"); 209 | new Scanner(zuberListener).next(); // Consume Zuber 210 | 211 | // scanner that outputs zuberListener transmission 212 | var scanner = new Scanner(new Agent(zuberListener)); 213 | scanner.useDelimiter("/"); 214 | 215 | 216 | assertEquals("foo", scanner.next()); 217 | assertEquals("zuber", scanner.next()); 218 | assertEquals("bar", scanner.next()); 219 | } 220 | 221 | @Test 222 | public void zuber_is_transmitted_each_time_it_occurs() 223 | { 224 | // transmitted message: a/a 225 | String[] transmission = {".", "zuber", "-", "stop", "-", ".", ".", "-", ".", "stop", "zuber", ".", "-", "stop"}; 226 | var zuberTransmitter = new MockTransmitter(transmission); 227 | 228 | var zuberListener = new Agent(zuberTransmitter, "foo/bar/baz/"); 229 | var zuberTrigger = new Scanner(zuberListener); 230 | zuberTrigger.useDelimiter("/"); 231 | zuberTrigger.next(); 232 | 233 | // scanner that outputs zuberListener transmission 234 | var scanner = new Scanner(new Agent(zuberListener)); 235 | scanner.useDelimiter("/"); 236 | 237 | 238 | assertEquals("foo", scanner.next()); 239 | assertEquals("zuber", scanner.next()); 240 | assertEquals("bar", scanner.next()); 241 | zuberTrigger.next(); 242 | // transmission of baz hasn't started yet, so zuber is transmitted immediately 243 | assertEquals("zuber", scanner.next()); 244 | assertEquals("baz", scanner.next()); 245 | } 246 | } 247 | } 248 | 249 | // Below are two utility classes to simplify testing 250 | 251 | /** 252 | * Emulates reading-only agent 253 | * Receives and stores data transmitted by agents 254 | */ 255 | class MockReceiver 256 | { 257 | String[] expected; 258 | ArrayList actual; 259 | 260 | public MockReceiver() { this(null); } 261 | 262 | public MockReceiver(String[] expected) 263 | { 264 | this.expected = expected; 265 | actual = new ArrayList<>(); 266 | } 267 | 268 | public String receiveOne(TajnyAgent from) 269 | { 270 | try 271 | { 272 | from.transmituj(); 273 | return "-"; 274 | } 275 | catch (Exception e) 276 | { 277 | return switch (e.getClass().getSimpleName()) 278 | { 279 | case "Zuber" -> "zuber"; 280 | case "RozmowaKontrolowana" -> "kontrola"; 281 | case "Stop" -> "stop"; 282 | case "Koniec" -> "koniec"; 283 | default -> "."; 284 | }; 285 | } 286 | } 287 | 288 | public void receiveAll(TajnyAgent from) 289 | { 290 | while (true) 291 | { 292 | var received = receiveOne(from); 293 | if (received.equals("koniec")) 294 | break; 295 | else 296 | actual.add(received); 297 | } 298 | } 299 | 300 | public ArrayList receiveN(TajnyAgent from, int n) 301 | { 302 | ArrayList result = new ArrayList<>(); 303 | for (int i = 0; i < n; i++) 304 | result.add(receiveOne(from)); 305 | return result; 306 | } 307 | } 308 | 309 | /** 310 | * Emulates transmitting-only agent 311 | * Converts stream of strings into stream of exceptions 312 | */ 313 | class MockTransmitter implements TajnyAgent 314 | { 315 | int index = 0; 316 | boolean finished = false; 317 | String[] codepoints; 318 | 319 | public MockTransmitter(String[] codepoints) 320 | { 321 | this.codepoints = codepoints; 322 | } 323 | 324 | @Override 325 | public void transmituj() 326 | { 327 | if (finished) 328 | fail("Attempt to read after transmission has finished."); 329 | 330 | if (index == codepoints.length) 331 | { 332 | finished = true; 333 | throw new Koniec(); 334 | } 335 | else 336 | { 337 | var data = codepoints[index]; 338 | index++; 339 | switch (data) 340 | { 341 | case "." -> throw new RuntimeException(); 342 | case "stop" -> throw new Stop(); 343 | case "kontrola" -> throw new RozmowaKontrolowana(); 344 | case "zuber" -> throw new Zuber(); 345 | // koniec is handled automatically 346 | } 347 | } 348 | } 349 | } 350 | -------------------------------------------------------------------------------- /satori/E/JakubOskwarekFunctionsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import org.junit.experimental.runners.Enclosed; 3 | import org.junit.runner.RunWith; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | @RunWith(Enclosed.class) 12 | public class JakubOskwarekFunctionsTest { 13 | public static class ConstantTest { 14 | // If some of these tests don't compile, please consider this: 15 | // Should *creating* a constant function ever throw an exception? 16 | 17 | @Test 18 | public void can_create_simple_constant_functions() throws GenericFunctionsException { 19 | Function integerFunction = Functions.constant(42); 20 | assertEquals((Integer)42, integerFunction.compute(Collections.emptyList())); 21 | 22 | Function stringFunction = Functions.constant("Sigma z falką"); 23 | assertEquals("Sigma z falką", stringFunction.compute(Collections.emptyList())); 24 | } 25 | 26 | @Test 27 | public void constant_function_always_returns_the_value_it_was_constructed_with() throws GenericFunctionsException { 28 | Function text = Functions.constant("Gucio z rootem"); 29 | final int always = 1000; // adjust according to your notion of "always" 30 | for(int i = 0; i < always; i++) 31 | assertEquals("Gucio z rootem", text.compute(Collections.emptyList())); 32 | } 33 | 34 | @Test 35 | public void arity_of_a_constant_function_is_zero() { 36 | Function text = Functions.constant("Szybka analiza"); 37 | assertEquals(0, text.arity()); 38 | } 39 | 40 | @Test 41 | public void passing_a_nonempty_list_of_arguments_to_a_constant_function_results_in_generic_functions_exception() { 42 | Function text = Functions.constant("I'm constant and take no arguments"); 43 | List arguments = List.of("Bro", "u sure 'bout that?"); 44 | assertThrows(GenericFunctionsException.class, () -> text.compute(arguments)); 45 | } 46 | 47 | @Test 48 | public void null_is_a_valid_constant_value() throws GenericFunctionsException { 49 | Function stringNothing = Functions.constant(null); 50 | assertNull(stringNothing.compute(Collections.emptyList())); 51 | 52 | Function integerNothing = Functions.constant(null); 53 | assertNull(integerNothing.compute(Collections.emptyList())); 54 | } 55 | 56 | @Test 57 | public void declared_argument_type_can_be_anything() throws GenericFunctionsException { 58 | Function someFunction = Functions.constant("Yeah, whatever"); 59 | //noinspection RedundantTypeArguments 60 | assertEquals("Yeah, whatever", someFunction.compute(Collections.emptyList())); 61 | } 62 | 63 | @Test 64 | public void provided_argument_type_can_be_a_subclass_of_the_declared_one() throws GenericFunctionsException { 65 | Function someFunction = Functions.constant("Even more whatever"); 66 | assertEquals("Even more whatever", someFunction.compute(Collections.emptyList())); 67 | } 68 | } 69 | 70 | public static class ProjTest { 71 | @Test 72 | public void can_create_simple_projection_functions() throws GenericFunctionsException { 73 | Function integerFunction = Functions.proj(3, 1); 74 | assertEquals((Integer)42, integerFunction.compute(List.of(32, 42, 52))); 75 | 76 | Function stringFunction = Functions.proj(7, 2); 77 | List sevenArguments = List.of("According", "to", "all", "known", "laws", "of", "aviation"); 78 | assertEquals("all", stringFunction.compute(sevenArguments)); 79 | } 80 | 81 | @Test 82 | public void projection_always_returns_the_kth_of_its_arguments() throws GenericFunctionsException { 83 | Function function = Functions.proj(3, 1); 84 | final int always = 1000; // adjust according to your notion of "always" 85 | for(int i = 0; i < always; i++) 86 | assertEquals("ma", function.compute(List.of("Ala", "ma", "kota"))); 87 | } 88 | 89 | @Test 90 | public void arity_of_a_projection_is_n() throws GenericFunctionsException { 91 | Function function = Functions.proj(42, 16); 92 | assertEquals(42, function.arity()); 93 | } 94 | 95 | @Test 96 | public void passing_wrong_number_of_arguments_to_a_projection_results_in_generic_functions_exception() 97 | throws GenericFunctionsException 98 | { 99 | Function function = Functions.proj(3, 1); 100 | assertThrows(GenericFunctionsException.class, () -> function.compute(List.of("A", "priori"))); 101 | assertThrows(GenericFunctionsException.class, () -> function.compute(List.of("A", "priori", "and", "a", "posteriori"))); 102 | } 103 | 104 | @Test 105 | public void cannot_construct_a_projection_with_nonpositive_number_of_arguments() { 106 | assertThrows(GenericFunctionsException.class, () -> Functions.proj(-1, 0)); 107 | assertThrows(GenericFunctionsException.class, () -> Functions.proj(0, 0)); 108 | } 109 | 110 | @Test 111 | public void cannot_construct_a_projection_with_invalid_index() { 112 | assertThrows(GenericFunctionsException.class, () -> Functions.proj(3, -1)); 113 | assertThrows(GenericFunctionsException.class, () -> Functions.proj(3, 3)); 114 | assertThrows(GenericFunctionsException.class, () -> Functions.proj(3, 5)); 115 | } 116 | 117 | @Test 118 | public void nulls_are_valid_arguments_for_a_projection() throws GenericFunctionsException { 119 | Function function = Functions.proj(3, 1); 120 | 121 | // Creating argument lists the long way because List.of doesn't like null arguments 122 | List firstArguments = new ArrayList<>(); 123 | firstArguments.add(null); 124 | firstArguments.add("I am a proud not-null"); 125 | firstArguments.add(null); 126 | assertEquals("I am a proud not-null", function.compute(firstArguments)); 127 | 128 | List secondArguments = new ArrayList<>(); 129 | secondArguments.add("I am not null"); 130 | secondArguments.add(null); 131 | secondArguments.add("Neither am I"); 132 | assertNull(null, function.compute(secondArguments)); 133 | } 134 | 135 | @Test 136 | public void argument_type_of_a_projection_can_be_a_subclass_of_its_return_type() 137 | throws GenericFunctionsException 138 | { 139 | Function function = Functions.proj(3, 1); 140 | assertEquals("Two", function.compute(List.of("One", "Two", "Three"))); 141 | } 142 | 143 | @Test 144 | public void provided_argument_type_can_be_a_subclass_of_the_declared_one() throws GenericFunctionsException { 145 | Function someFunction = Functions.proj(3, 1); 146 | List arguments = List.of(1, 2, 3); 147 | assertEquals(2, someFunction.compute(arguments)); 148 | } 149 | } 150 | 151 | public static class ComposeTest { 152 | // If some of these tests don't compile, please consider loosening the type constraints in generics 153 | 154 | @Test 155 | public void can_create_simple_function_compositions() throws GenericFunctionsException { 156 | Function getLength = new Function<>() { 157 | @Override 158 | public int arity() { 159 | return 1; 160 | } 161 | 162 | @Override 163 | public Integer compute(List args) throws GenericFunctionsException { 164 | if(args == null || args.size() != arity()) 165 | throw new GenericFunctionsException(); 166 | return args.get(0).length(); 167 | } 168 | }; 169 | 170 | Function describe = new Function<>() { 171 | @Override 172 | public int arity() { 173 | return 1; 174 | } 175 | 176 | @Override 177 | public String compute(List args) throws GenericFunctionsException { 178 | if(args == null || args.size() != arity()) 179 | throw new GenericFunctionsException(); 180 | return "Length is: " + args.get(0); 181 | } 182 | }; 183 | 184 | Function composition = Functions.compose(describe, List.of(getLength)); 185 | assertEquals("Length is: 3", composition.compute(List.of("APL"))); 186 | } 187 | 188 | @Test 189 | public void composing_with_a_constant_function_returns_a_constant_function() throws GenericFunctionsException { 190 | Function text = Functions.constant("I am the chosen one"); 191 | Function reverse = new StrRvs(); 192 | Function composition = Functions.compose(reverse, List.of(text)); 193 | final int always = 1000; // adjust according to your notion of "always" 194 | for(int i = 0; i < always; i++) 195 | assertEquals("eno nesohc eht ma I", composition.compute(Collections.emptyList())); 196 | assertEquals("eno nesohc eht ma I", composition.compute(Collections.emptyList())); 197 | } 198 | 199 | @Test 200 | public void arity_of_composition_is_the_common_arity_of_the_inner_functions() throws GenericFunctionsException { 201 | Function enumerate = new Function<>() { 202 | @Override 203 | public int arity() { 204 | return 3; 205 | } 206 | 207 | @Override 208 | public String compute(List args) throws GenericFunctionsException { 209 | if(args == null || args.size() != arity()) 210 | throw new GenericFunctionsException(); 211 | return "1." + args.get(0) + ",2." + args.get(1) + ",3." + args.get(2); 212 | } 213 | }; 214 | Function composition = Functions.compose( 215 | enumerate, 216 | List.of(new StrRvs(), new StrRvs(), new StrRvs()) 217 | ); 218 | assertEquals(1, composition.arity()); 219 | assertEquals("1.pilf,2.pilf,3.pilf", composition.compute(List.of("flip"))); 220 | } 221 | 222 | @Test 223 | public void composing_with_inner_functions_of_various_different_arities_results_in_generic_functions_exception() 224 | throws GenericFunctionsException 225 | { 226 | Function outer = Functions.proj(2, 1); 227 | List> inner = List.of( 228 | Functions.constant("Never gonna give you up"), 229 | Functions.proj(3, 2) 230 | ); 231 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(outer, inner)); 232 | } 233 | 234 | @Test 235 | public void outer_function_begin_null_results_in_generic_functions_exception() { 236 | List> inner = List.of( 237 | Functions.constant("Inner functions are ok"), 238 | Functions.constant("and so what?") 239 | ); 240 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(null, inner)); 241 | } 242 | 243 | @Test 244 | public void list_of_inner_functions_being_null_results_in_generic_functions_exception() 245 | throws GenericFunctionsException 246 | { 247 | Function outer = Functions.proj(2, 1); 248 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(outer, null)); 249 | } 250 | 251 | @Test 252 | public void one_of_the_inner_functions_being_null_results_in_generic_functions_exception() 253 | throws GenericFunctionsException 254 | { 255 | Function outer = Functions.proj(2, 1); 256 | List> inner = new ArrayList<>(); // List.of doesn't like nulls; 257 | inner.add(Functions.constant("I am cool but down there is...")); 258 | inner.add(null); 259 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(outer, inner)); 260 | } 261 | 262 | @Test 263 | public void the_number_of_inner_functions_must_match_the_arity_of_the_outer_function() 264 | throws GenericFunctionsException 265 | { 266 | Function outer = Functions.proj(2, 1); 267 | 268 | List> tooMany = List.of( 269 | Functions.constant("There are"), 270 | Functions.constant("too many"), 271 | Functions.constant("inner functions here") 272 | ); 273 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(outer, tooMany)); 274 | 275 | List> tooFew = List.of( 276 | Functions.constant("Just one sad inner function here :(") 277 | ); 278 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(outer, tooFew)); 279 | assertThrows(GenericFunctionsException.class, () -> Functions.compose(outer, Collections.emptyList())); 280 | } 281 | 282 | @Test 283 | public void the_arity_of_the_outer_function_can_be_zero() 284 | throws GenericFunctionsException 285 | { 286 | Function outer = Functions.constant("I'm the one at the sail, I'm the master of my sea!"); 287 | Function composition = Functions.compose(outer, Collections.emptyList()); 288 | assertEquals("I'm the one at the sail, I'm the master of my sea!", composition.compute(Collections.emptyList())); 289 | } 290 | } 291 | } 292 | -------------------------------------------------------------------------------- /satori/C/KacperTopolskiListIsFunctionTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import org.junit.experimental.runners.Enclosed; 3 | import org.junit.runner.RunWith; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | import java.util.*; 8 | 9 | @SuppressWarnings({"rawtypes", "unchecked"}) 10 | @RunWith(Enclosed.class) 11 | public class KacperTopolskiListIsFunctionTest { 12 | public static class SizeTest { 13 | @Test 14 | public void is_empty_works_correctly() { 15 | ListIsFunction f = new ListIsFunction(); 16 | 17 | assertTrue(f.asList().isEmpty()); 18 | assertTrue(f.asMap().isEmpty()); 19 | assertTrue(f.asMap().entrySet().isEmpty()); 20 | assertTrue(f.asMap().keySet().isEmpty()); 21 | 22 | assertEquals(0, f.asList().size()); 23 | assertEquals(0, f.asMap().size()); 24 | assertEquals(0, f.asMap().entrySet().size()); 25 | assertEquals(0, f.asMap().keySet().size()); 26 | 27 | f.asList().add(50); 28 | 29 | assertFalse(f.asList().isEmpty()); 30 | assertFalse(f.asMap().isEmpty()); 31 | assertFalse(f.asMap().entrySet().isEmpty()); 32 | assertFalse(f.asMap().keySet().isEmpty()); 33 | 34 | assertEquals(1, f.asList().size()); 35 | assertEquals(1, f.asMap().size()); 36 | assertEquals(1, f.asMap().entrySet().size()); 37 | assertEquals(1, f.asMap().keySet().size()); 38 | 39 | f.asList().remove(0); 40 | 41 | assertTrue(f.asList().isEmpty()); 42 | assertTrue(f.asMap().isEmpty()); 43 | assertTrue(f.asMap().entrySet().isEmpty()); 44 | assertTrue(f.asMap().keySet().isEmpty()); 45 | 46 | assertEquals(0, f.asList().size()); 47 | assertEquals(0, f.asMap().size()); 48 | assertEquals(0, f.asMap().entrySet().size()); 49 | assertEquals(0, f.asMap().keySet().size()); 50 | } 51 | @Test 52 | public void size_works_correctly() { 53 | ListIsFunction f = new ListIsFunction(); 54 | for (int i = 0; i < 50; ++i) { 55 | assertEquals(f.asList().size(), i); 56 | assertEquals(f.asMap().size(), i); 57 | assertEquals(f.asMap().entrySet().size(), i); 58 | assertEquals(f.asMap().keySet().size(), i); 59 | 60 | f.asList().add((i & 1) == 1 ? "oop" : 42); 61 | } 62 | 63 | } 64 | @Test 65 | public void clear_works_correctly() { 66 | ListIsFunction f = new ListIsFunction(); 67 | List l = f.asList(); 68 | Map m = f.asMap(); 69 | 70 | l.add(1); 71 | assertFalse(l.isEmpty()); 72 | assertFalse(m.isEmpty()); 73 | 74 | m.clear(); 75 | assertTrue(l.isEmpty()); 76 | assertTrue(m.isEmpty()); 77 | 78 | m.put(0, 1); 79 | assertFalse(l.isEmpty()); 80 | assertFalse(m.isEmpty()); 81 | 82 | l.clear(); 83 | assertTrue(l.isEmpty()); 84 | assertTrue(m.isEmpty()); 85 | } 86 | @Test(timeout = 250) 87 | public void size_is_unbounded() { 88 | ListIsFunction f = new ListIsFunction(); 89 | for (int i = 0; i < 12345; ++i) { 90 | assertEquals(2*i, f.asList().size()); 91 | assertEquals(2*i, f.asMap().size()); 92 | 93 | f.asMap().put(2*i, i); 94 | f.asList().add(-1); 95 | } 96 | } 97 | } 98 | public static class MapRemoveTest { 99 | @Test 100 | public void map_remove_on_empty_does_nothing() { 101 | ListIsFunction f = new ListIsFunction(); 102 | 103 | f.asMap().remove(-10); 104 | assertTrue(f.asList().isEmpty()); 105 | 106 | f.asMap().remove(-1); 107 | assertTrue(f.asMap().isEmpty()); 108 | 109 | f.asMap().remove(0); 110 | assertTrue(f.asList().isEmpty()); 111 | 112 | f.asMap().remove(1); 113 | assertTrue(f.asMap().isEmpty()); 114 | 115 | f.asMap().remove(2); 116 | assertTrue(f.asList().isEmpty()); 117 | 118 | f.asMap().remove(10); 119 | assertTrue(f.asMap().isEmpty()); 120 | } 121 | @Test 122 | public void map_remove_out_of_bounds_does_nothing() { 123 | ListIsFunction f = new ListIsFunction(); 124 | f.asList().add("aaa"); 125 | f.asList().add("bbb"); 126 | 127 | f.asMap().remove(-10); 128 | assertEquals(2, f.asList().size()); 129 | 130 | f.asMap().remove(-1); 131 | assertEquals(2, f.asMap().size()); 132 | 133 | f.asMap().remove(2); 134 | assertEquals(2, f.asList().size()); 135 | 136 | f.asMap().remove(3); 137 | assertEquals(2, f.asMap().size()); 138 | 139 | f.asMap().remove(10); 140 | assertEquals(2, f.asList().size()); 141 | } 142 | @Test 143 | public void map_remove_last_works() { 144 | ListIsFunction f = new ListIsFunction(); 145 | f.asList().add("aaa"); 146 | f.asList().add("bbb"); 147 | 148 | f.asMap().remove(1); 149 | assertEquals(1, f.asMap().size()); 150 | assertFalse(f.asList().isEmpty()); 151 | 152 | f.asMap().remove(0); 153 | assertEquals(0, f.asList().size()); 154 | assertTrue(f.asMap().isEmpty()); 155 | } 156 | @Test 157 | public void map_remove_in_the_middle_throws() { 158 | ListIsFunction f = new ListIsFunction(); 159 | f.asList().add("aaa"); 160 | f.asList().add("bbb"); 161 | f.asList().add("ccc"); 162 | 163 | try { 164 | f.asMap().remove(0); 165 | fail("map can't remove in the middle"); 166 | } catch(Exception e) { 167 | assertEquals(java.lang.IllegalArgumentException.class, e.getClass()); 168 | assertEquals("java.lang.IllegalArgumentException", e.toString()); 169 | 170 | assertEquals(3, f.asMap().size()); 171 | } 172 | 173 | try { 174 | f.asMap().remove(1); 175 | fail("map can't remove in the middle"); 176 | } catch(Exception e) { 177 | assertEquals(java.lang.IllegalArgumentException.class, e.getClass()); 178 | assertEquals("java.lang.IllegalArgumentException", e.toString()); 179 | 180 | assertEquals(3, f.asList().size()); 181 | } 182 | } 183 | } 184 | public static class MapPutTest { 185 | @Test 186 | public void map_put_works_with_strange_values() { 187 | ListIsFunction f = new ListIsFunction(); 188 | 189 | f.asMap().put(0, ""); 190 | assertEquals(1, f.asList().size()); 191 | 192 | f.asMap().put(1, new Object()); 193 | assertEquals(2, f.asMap().size()); 194 | 195 | f.asMap().put(2, null); 196 | assertEquals(3, f.asMap().size()); 197 | } 198 | @Test 199 | public void map_put_replace_works_with_strange_values() { 200 | ListIsFunction f = new ListIsFunction(); 201 | 202 | assertNull(f.asMap().put(0, "")); 203 | assertEquals(1, f.asList().size()); 204 | 205 | Object o = new Object(); 206 | assertEquals("", f.asMap().put(0, o)); 207 | assertEquals(1, f.asMap().size()); 208 | 209 | assertEquals(o, f.asMap().put(0, null)); 210 | assertEquals(1, f.asMap().size()); 211 | 212 | assertNull(f.asMap().put(0, null)); 213 | assertEquals(1, f.asList().size()); 214 | } 215 | @Test 216 | public void map_put_bad_keys() { 217 | ListIsFunction f = new ListIsFunction(); 218 | for (int i = 0; i < 5; ++i) 219 | f.asList().add(i); 220 | 221 | try { 222 | f.asMap().put(null, "in"); 223 | fail("null key should result in exception"); 224 | } catch (Exception e) { 225 | assertEquals(java.lang.NullPointerException.class, e.getClass()); 226 | assertEquals("java.lang.NullPointerException", e.toString()); 227 | 228 | assertEquals(5, f.asList().size()); 229 | } 230 | 231 | try { 232 | f.asMap().put("2", "in"); 233 | fail("non-integer key should result in exception"); 234 | } catch (Exception e) { 235 | assertEquals(java.lang.ClassCastException.class, e.getClass()); 236 | assertEquals("java.lang.ClassCastException", e.toString()); 237 | 238 | assertEquals(5, f.asMap().size()); 239 | } 240 | 241 | try { 242 | f.asMap().put(new Object(), "in"); 243 | fail("non-integer key should result in exception"); 244 | } catch (Exception e) { 245 | assertEquals(java.lang.ClassCastException.class, e.getClass()); 246 | assertEquals("java.lang.ClassCastException", e.toString()); 247 | 248 | assertEquals(5, f.asList().size()); 249 | } 250 | 251 | try { 252 | f.asMap().put(-1, "in"); 253 | fail("negative key should result in exception"); 254 | } catch (Exception e) { 255 | assertEquals(java.lang.IllegalArgumentException.class, e.getClass()); 256 | assertEquals("java.lang.IllegalArgumentException", e.toString()); 257 | 258 | assertEquals(5, f.asMap().size()); 259 | } 260 | 261 | try { 262 | f.asMap().put(6, "in"); 263 | fail("too big key should result in exception"); 264 | } catch (Exception e) { 265 | assertEquals(java.lang.IllegalArgumentException.class, e.getClass()); 266 | assertEquals("java.lang.IllegalArgumentException", e.toString()); 267 | 268 | assertEquals(5, f.asList().size()); 269 | } 270 | 271 | assertEquals("[0, 1, 2, 3, 4]", f.asList().toString()); 272 | } 273 | } 274 | public static class MapEntrySetTest { 275 | @Test 276 | public void entry_set_is_a_view() { 277 | ListIsFunction f = new ListIsFunction(); 278 | Set es = f.asMap().entrySet(); 279 | Set ks = f.asMap().keySet(); 280 | 281 | assertEquals(0, es.size()); 282 | assertTrue(es.isEmpty()); 283 | assertEquals("[]", es.toString()); 284 | 285 | assertEquals(0, ks.size()); 286 | assertTrue(ks.isEmpty()); 287 | assertEquals("[]", ks.toString()); 288 | 289 | f.asList().add(13); 290 | 291 | assertEquals(1, es.size()); 292 | assertFalse(es.isEmpty()); 293 | assertEquals("[0=13]", es.toString()); 294 | 295 | assertEquals(1, ks.size()); 296 | assertFalse(ks.isEmpty()); 297 | assertEquals("[0]", ks.toString()); 298 | 299 | f.asList().add("test"); 300 | 301 | assertEquals(2, es.size()); 302 | assertFalse(es.isEmpty()); 303 | assertEquals("[0=13, 1=test]", es.toString()); 304 | 305 | assertEquals(2, ks.size()); 306 | assertFalse(ks.isEmpty()); 307 | assertEquals("[0, 1]", ks.toString()); 308 | 309 | f.asList().clear(); 310 | 311 | assertEquals(0, es.size()); 312 | assertTrue(es.isEmpty()); 313 | assertEquals("[]", es.toString()); 314 | 315 | assertEquals(0, ks.size()); 316 | assertTrue(ks.isEmpty()); 317 | assertEquals("[]", ks.toString()); 318 | } 319 | @Test 320 | public void entry_set_returns_correct_set() { 321 | ListIsFunction f = new ListIsFunction(); 322 | f.asList().add("()"); 323 | f.asList().add(1); 324 | f.asList().add('c'); 325 | Set s_ret = f.asMap().entrySet(); 326 | 327 | Set s_ex = new HashSet(); 328 | s_ex.add(new AbstractMap.SimpleEntry(0, "()")); 329 | s_ex.add(new AbstractMap.SimpleEntry(1, 1)); 330 | s_ex.add(new AbstractMap.SimpleEntry(2, 'c')); 331 | 332 | assertEquals(s_ex, s_ret); 333 | assertEquals("[(), 1, c]", f.asList().toString()); 334 | assertEquals(3, f.asList().size()); 335 | } 336 | @Test 337 | public void entry_set_works_with_strange_values() { 338 | ListIsFunction f = new ListIsFunction(); 339 | Set s = f.asMap().entrySet(); 340 | 341 | f.asList().add(null); 342 | f.asList().add(""); 343 | 344 | assertEquals("[0=null, 1=]", s.toString()); 345 | assertEquals(2, s.size()); 346 | 347 | f.asList().add(new Object()); 348 | assertEquals(3, s.size()); 349 | } 350 | } 351 | public static class MapEntrySetIteratorTest { 352 | @Test 353 | public void entry_set_iterator_next() { 354 | ListIsFunction f = new ListIsFunction(); 355 | f.asList().add(-1); 356 | f.asList().add(null); 357 | Iterator it = f.asMap().entrySet().iterator(); 358 | 359 | assertTrue(it.hasNext()); 360 | assertEquals(it.next(), new AbstractMap.SimpleEntry(0, -1)); 361 | 362 | assertTrue(it.hasNext()); 363 | assertEquals(it.next(), new AbstractMap.SimpleEntry(1, null)); 364 | 365 | assertFalse(it.hasNext()); 366 | try { 367 | it.next(); 368 | fail("there is no next element"); 369 | } catch(Exception e) { 370 | assertEquals(java.util.NoSuchElementException.class, e.getClass()); 371 | assertEquals("java.util.NoSuchElementException", e.toString()); 372 | } 373 | 374 | assertEquals("[-1, null]", f.asList().toString()); 375 | assertEquals(2, f.asList().size()); 376 | } 377 | @Test 378 | public void entry_set_iterator_remove_first_throws() { 379 | ListIsFunction f = new ListIsFunction(); 380 | f.asList().add("()"); 381 | f.asList().add(1); 382 | f.asList().add('c'); 383 | Iterator it = f.asMap().entrySet().iterator(); 384 | 385 | try { 386 | it.remove(); 387 | fail("remove() has to be called after next()"); 388 | } catch(Exception e) { 389 | assertEquals(java.lang.IllegalStateException.class, e.getClass()); 390 | assertEquals("java.lang.IllegalStateException", e.toString()); 391 | } 392 | 393 | assertEquals("[(), 1, c]", f.asList().toString()); 394 | assertEquals(3, f.asList().size()); 395 | } 396 | @Test 397 | public void entry_set_iterator_remove_in_the_middle_throws() { 398 | ListIsFunction f = new ListIsFunction(); 399 | f.asList().add("()"); 400 | f.asList().add(1); 401 | f.asList().add('c'); 402 | Iterator it = f.asMap().entrySet().iterator(); 403 | 404 | it.next(); 405 | it.next(); 406 | 407 | try { 408 | it.remove(); 409 | fail("map iterator can't remove in the middle"); 410 | } catch(Exception e) { 411 | assertEquals(java.lang.IllegalStateException.class, e.getClass()); 412 | assertEquals("java.lang.IllegalStateException", e.toString()); 413 | } 414 | 415 | assertEquals("[(), 1, c]", f.asList().toString()); 416 | assertEquals(3, f.asList().size()); 417 | } 418 | @Test 419 | public void entry_set_iterator_remove_last_works() { 420 | ListIsFunction f = new ListIsFunction(); 421 | f.asList().add("()"); 422 | f.asList().add(1); 423 | f.asList().add('c'); 424 | Iterator it = f.asMap().entrySet().iterator(); 425 | 426 | it.next(); 427 | it.next(); 428 | it.next(); 429 | it.remove(); 430 | 431 | assertEquals("[(), 1]", f.asList().toString()); 432 | assertEquals(2, f.asList().size()); 433 | } 434 | @Test 435 | public void entry_set_iterator_remove_last_twice_throws() { 436 | ListIsFunction f = new ListIsFunction(); 437 | f.asList().add("()"); 438 | f.asList().add(1); 439 | f.asList().add('c'); 440 | Iterator it = f.asMap().entrySet().iterator(); 441 | 442 | it.next(); 443 | it.next(); 444 | it.next(); 445 | it.remove(); 446 | try { 447 | it.remove(); 448 | fail("map iterator can't remove twice"); 449 | } catch(Exception e) { 450 | assertEquals(java.lang.IllegalStateException.class, e.getClass()); 451 | assertEquals("java.lang.IllegalStateException", e.toString()); 452 | } 453 | 454 | assertEquals("[(), 1]", f.asList().toString()); 455 | assertEquals(2, f.asList().size()); 456 | } 457 | } 458 | } 459 | -------------------------------------------------------------------------------- /satori/F/MichalHorodeckiSmartFactoryTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | 3 | import org.junit.experimental.runners.Enclosed; 4 | import org.junit.runner.RunWith; 5 | 6 | import java.lang.reflect.Executable; 7 | 8 | import static org.junit.Assert.*; 9 | 10 | @RunWith(Enclosed.class) 11 | public class MichalHorodeckiSmartFactoryTest 12 | { 13 | // Test invoking methods already implemented on given object 14 | // Tested methods contain only exact types 15 | public static class FirstPhaseSimpleTest 16 | { 17 | 18 | interface MockInterface 19 | { 20 | void interface_returns_void(); 21 | String no_arguments(); 22 | String has_arguments(String a); 23 | String only_match_with_that_name(String a); 24 | String package_private(String a); 25 | void has_side_effects(); 26 | String throws_an_exception() throws Exception; 27 | String throws_explicit_runtime_exception(); 28 | String throws_wrong_exception() throws Exception; 29 | } 30 | 31 | class MockClass 32 | { 33 | public static class MockException extends Exception { } 34 | 35 | int x = 0; 36 | boolean interface_returns_void_was_called = false; 37 | 38 | public String interface_returns_void() 39 | { 40 | interface_returns_void_was_called = true; 41 | return "abc"; 42 | } 43 | 44 | public String no_arguments() 45 | { 46 | return "foo"; 47 | } 48 | 49 | public String has_arguments(String a) 50 | { 51 | return "bar"; 52 | } 53 | 54 | public String only_match_with_that_name(String a) 55 | { 56 | return "baz"; 57 | } 58 | 59 | public String only_match_with_that_name(Integer a) 60 | { 61 | return "boo"; 62 | } 63 | 64 | String package_private(String a) 65 | { 66 | return "bam"; 67 | } 68 | 69 | void has_side_effects() 70 | { 71 | x = 42; 72 | } 73 | 74 | String throws_an_exception() throws Exception 75 | { 76 | throw new MockException(); 77 | } 78 | 79 | String throws_explicit_runtime_exception() throws RuntimeException 80 | { 81 | return "quz"; 82 | } 83 | 84 | String throws_wrong_exception() throws Throwable 85 | { 86 | return "quux"; 87 | } 88 | } 89 | 90 | @Test 91 | public void non_void_object_method_can_be_used_if_interface_returns_void() 92 | { 93 | var obj = new MockClass(); 94 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 95 | proxy.interface_returns_void(); 96 | assertTrue(obj.interface_returns_void_was_called); 97 | } 98 | 99 | @Test 100 | public void methods_with_no_arguments_are_proxied() 101 | { 102 | var obj = new MockClass(); 103 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 104 | assertEquals(obj.no_arguments(), proxy.no_arguments()); 105 | } 106 | 107 | @Test 108 | public void methods_with_arguments_are_proxied() 109 | { 110 | var obj = new MockClass(); 111 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 112 | assertEquals(obj.has_arguments(""), proxy.has_arguments("")); 113 | } 114 | 115 | @Test 116 | public void methods_with_repeating_name_are_proxied_with_correct_arguments() 117 | { 118 | var obj = new MockClass(); 119 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 120 | assertEquals(obj.only_match_with_that_name(""), 121 | proxy.only_match_with_that_name("")); 122 | } 123 | 124 | @Test 125 | public void package_private_methods_are_proxied() 126 | { 127 | var obj = new MockClass(); 128 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 129 | assertEquals(obj.package_private(""), proxy.package_private("")); 130 | } 131 | 132 | @Test 133 | public void proxy_causes_side_effects_on_supplied_object() 134 | { 135 | var obj = new MockClass(); 136 | assertEquals(0, obj.x); 137 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 138 | proxy.has_side_effects(); 139 | assertEquals(42, obj.x); 140 | } 141 | 142 | @Test 143 | public void exceptions_thrown_from_proxied_class_are_unwrapped() 144 | { 145 | var obj = new MockClass(); 146 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 147 | try 148 | { 149 | proxy.throws_an_exception(); 150 | fail("obj.throws_an_exception should be called"); 151 | } 152 | catch (Exception e) 153 | { 154 | assertEquals(MockClass.MockException.class, e.getClass()); 155 | } 156 | } 157 | 158 | @Test 159 | public void methods_that_explicitly_throw_runtime_exception_are_proxied() 160 | { 161 | var obj = new MockClass(); 162 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 163 | assertEquals(obj.throws_explicit_runtime_exception(), 164 | proxy.throws_explicit_runtime_exception()); 165 | } 166 | 167 | @Test 168 | public void methods_that_throw_wrong_exceptions_are_not_proxied() 169 | { 170 | var obj = new MockClass(); 171 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 172 | String expected = "", actual; 173 | try 174 | { 175 | actual = proxy.throws_wrong_exception(); 176 | } 177 | catch (Throwable e) 178 | { 179 | fail("No exception is actually thrown"); 180 | return; 181 | } 182 | assertEquals(expected, actual); 183 | } 184 | } 185 | 186 | // Test invoking methods already implemented on given object 187 | // Tested methods contain different types than the interface 188 | // i.e. return types, parameters, and exceptions inherit from appropriate types 189 | public static class FirstPhasePolymorphismTest 190 | { 191 | static class ExceptionA extends Exception { } 192 | 193 | static class ExceptionB extends Exception { } 194 | 195 | static class ExceptionC extends ExceptionA { } 196 | 197 | static class ExceptionD extends ExceptionB { } 198 | 199 | interface MockInterface 200 | { 201 | Number simple_return_specification(); 202 | 203 | String simple_argument_specification(String o); 204 | String multiple_matches_argument_specification(Integer o); 205 | 206 | String multiarg_argument_specification(Integer a, String b); 207 | 208 | String simple_exception_specification() throws Throwable; 209 | String multiexception_exception_specification() 210 | throws ExceptionA, ExceptionB; 211 | 212 | } 213 | 214 | static class MockClass 215 | { 216 | Integer simple_return_specification() { return 42; } 217 | 218 | String simple_argument_specification(String o) 219 | { 220 | return "foo"; 221 | } 222 | 223 | String multiple_matches_argument_specification(Integer o) 224 | { 225 | return "bar"; 226 | } 227 | 228 | String multiple_matches_argument_specification(Object o) 229 | { 230 | return "baz"; 231 | } 232 | 233 | String multiarg_argument_specification(Object a, Object b) 234 | { 235 | return "qux"; 236 | } 237 | 238 | String simple_exception_specification() throws Exception 239 | { 240 | return "quz"; 241 | } 242 | 243 | String multiexception_exception_specification() 244 | throws ExceptionC, ExceptionD 245 | { 246 | return "spam"; 247 | } 248 | } 249 | 250 | @Test 251 | public void simple_return_specification() 252 | { 253 | MockClass obj = new MockClass(); 254 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 255 | assertEquals(obj.simple_return_specification(), 256 | proxy.simple_return_specification()); 257 | } 258 | 259 | 260 | @Test 261 | public void simple_argument_specification() 262 | { 263 | MockClass obj = new MockClass(); 264 | String a = ""; 265 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 266 | assertEquals(obj.simple_argument_specification(a), 267 | proxy.simple_argument_specification(a)); 268 | } 269 | 270 | @Test 271 | public void multiple_matches_argument_specification() 272 | { 273 | 274 | MockClass obj = new MockClass(); 275 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 276 | try 277 | { 278 | proxy.multiple_matches_argument_specification(42); 279 | fail("Expected HellNoException"); 280 | } 281 | catch (SmartFactory.HellNoException e) 282 | { 283 | 284 | } 285 | } 286 | 287 | @Test 288 | public void multiarg_argument_specification() 289 | { 290 | MockClass obj = new MockClass(); 291 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 292 | assertEquals(obj.multiarg_argument_specification(42, ""), 293 | proxy.multiarg_argument_specification(42, "")); 294 | } 295 | 296 | @Test 297 | public void simple_exception_specification() 298 | { 299 | MockClass obj = new MockClass(); 300 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 301 | try 302 | { 303 | assertEquals(obj.simple_exception_specification(), 304 | proxy.simple_exception_specification()); 305 | } 306 | catch (Throwable e) 307 | { 308 | fail("No exception is actually thrown"); 309 | } 310 | 311 | } 312 | 313 | @Test 314 | public void multiexception_exception_specification() 315 | { 316 | MockClass obj = new MockClass(); 317 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, obj); 318 | try 319 | { 320 | assertEquals(obj.multiexception_exception_specification(), 321 | proxy.multiexception_exception_specification()); 322 | } 323 | catch (Throwable e) 324 | { 325 | fail("No exception is actually thrown"); 326 | } 327 | 328 | } 329 | } 330 | 331 | // Test invoking methods that are not implemented on given object 332 | // and constructing the return type 333 | public static class SecondPhaseTest 334 | { 335 | interface MockInterface 336 | { 337 | void returns_void(); 338 | int returns_int(); 339 | 340 | ContainsArray contains_array(); 341 | InitializedFields initialized_fields(); 342 | CycleA cycle(); 343 | InheritingFields inheriting_fields(); 344 | ComposingInheritingFields composing_inheriting_fields(); 345 | StaticFields static_fields(); 346 | ThrowingConstructor throwing_constructor(); 347 | } 348 | 349 | static class ContainsArray 350 | { 351 | public Object[] a; 352 | public Number[] b; 353 | public Integer[] c; 354 | } 355 | 356 | static class InitializedFields 357 | { 358 | public static String initialized_string = "KOT"; 359 | public static String uninitialized_string; 360 | 361 | public Integer[] initialized_array = {1, 2, 3}; 362 | public Integer[] uninitialized_array; 363 | } 364 | 365 | static class CycleA 366 | { 367 | public CycleB next; 368 | } 369 | 370 | static class CycleB 371 | { 372 | public CycleA next; 373 | } 374 | 375 | static class InheritingFields 376 | { 377 | public static class Base { } 378 | 379 | public static class Derived extends Base { } 380 | 381 | public static class DerivedDerived extends Derived { } 382 | 383 | 384 | // Initializing in this order will create three different objects 385 | public Base base; 386 | public Derived derived; 387 | public DerivedDerived derivedDerived; 388 | } 389 | 390 | static class ComposingInheritingFields 391 | { 392 | public static class Base { } 393 | 394 | public static class Derived extends Base { } 395 | 396 | public static class DerivedDerived extends Derived { } 397 | 398 | public static class DerivedDerivedWrapper 399 | { 400 | DerivedDerived derivedDerived; 401 | } 402 | 403 | 404 | // Initializing in this order will create three different objects 405 | // Correct implementation initializes derivedDerived first 406 | // i.e. it should traverse all composition levels 407 | public Base base; 408 | public Derived derived; 409 | public DerivedDerivedWrapper derivedDerivedWrapper; 410 | } 411 | 412 | 413 | static class StaticFields 414 | { 415 | public static Object uninitialized; 416 | public static Object initialized = new Object(); 417 | } 418 | 419 | static class ThrowingConstructor 420 | { 421 | public ThrowingConstructor() 422 | { 423 | throw new RuntimeException("hahaha"); 424 | } 425 | } 426 | 427 | 428 | @Test 429 | public void void_is_not_constructed() 430 | { 431 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 432 | try 433 | { 434 | proxy.returns_void(); 435 | } 436 | catch (Exception e) 437 | { 438 | fail("No exception should be thrown"); 439 | } 440 | } 441 | 442 | @Test 443 | public void class_containing_array_field() 444 | { 445 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 446 | ContainsArray x = proxy.contains_array(); 447 | assertNull(x.a); 448 | assertNull(x.b); 449 | assertNull(x.c); 450 | } 451 | 452 | @Test 453 | public void initialized_fields_cannot_be_used_to_initialize_other_fields() 454 | { 455 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 456 | InitializedFields x = proxy.initialized_fields(); 457 | assertNotEquals(x.initialized_array, x.uninitialized_array); 458 | assertNotEquals(x.initialized_string, x.uninitialized_string); 459 | } 460 | 461 | @Test 462 | public void cycle() 463 | { 464 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 465 | CycleA a = proxy.cycle(); 466 | assertEquals(a, a.next.next); 467 | assertEquals(a.next, a.next.next.next); 468 | } 469 | 470 | @Test 471 | public void inheriting_fields_reference_the_same_object() 472 | { 473 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 474 | InheritingFields x = proxy.inheriting_fields(); 475 | assertEquals(x.base, x.derived); 476 | assertEquals(x.derived, x.derivedDerived); 477 | } 478 | 479 | @Test 480 | public void composed_inheriting_fields_reference_the_same_object() 481 | { 482 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 483 | ComposingInheritingFields x = proxy.composing_inheriting_fields(); 484 | assertEquals(x.base, x.derived); 485 | assertEquals(x.derived, x.derivedDerivedWrapper.derivedDerived); 486 | } 487 | 488 | @Test 489 | public void static_fields_are_initialized() 490 | { 491 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 492 | StaticFields x = proxy.static_fields(); 493 | assertNotNull(StaticFields.uninitialized); 494 | } 495 | 496 | @Test 497 | public void static_fields_cannot_be_used_to_initialize_other_field() 498 | { 499 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 500 | StaticFields x = proxy.static_fields(); 501 | assertNotSame(StaticFields.uninitialized, StaticFields.initialized); 502 | } 503 | 504 | 505 | @Test 506 | public void constructing_class_with_throwing_constructor_fails() 507 | { 508 | MockInterface proxy = SmartFactory.fixIt(MockInterface.class, null); 509 | assertThrows(SmartFactory.HellNoException.class, 510 | proxy::throwing_constructor); 511 | } 512 | } 513 | } 514 | -------------------------------------------------------------------------------- /satori/A/MichalHorodecki/MichalHorodeckiPhoneBookTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | import org.junit.experimental.runners.Enclosed; 3 | import org.junit.runner.RunWith; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | @RunWith(Enclosed.class) 8 | public class MichalHorodeckiPhoneBookTest 9 | { 10 | // Test if different constructors correctly assign default values 11 | public static class ConstructorTest 12 | { 13 | @Test 14 | public void by_default_book_has_capacity_ten_and_format_digits() 15 | { 16 | PhoneBook pb = new PhoneBook(); 17 | for (int i = 0; i < 10; i++) 18 | pb.add("12345678" + i); 19 | assertEquals(10, pb.size()); 20 | assertTrue(pb.isFull()); 21 | } 22 | 23 | @Test 24 | public void by_default_book_has_capacity_of_ten() 25 | { 26 | PhoneBook pb = new PhoneBook(PhoneBook.NumberFormat.HYPHENED); 27 | for (int i = 0; i < 10; i++) 28 | pb.add("123-456-78" + i); 29 | assertEquals(10, pb.size()); 30 | assertTrue(pb.isFull()); 31 | } 32 | 33 | @Test 34 | public void by_default_book_has_format_digits() 35 | { 36 | PhoneBook pb = new PhoneBook(5); 37 | for (int i = 0; i < 5; i++) 38 | pb.add("12345678" + i); 39 | assertEquals(5, pb.size()); 40 | assertTrue(pb.isFull()); 41 | } 42 | 43 | @Test 44 | public void constructor_called_with_invalid_data_uses_default_values() 45 | { 46 | PhoneBook pb = new PhoneBook(null, -5); 47 | assertEquals(10, pb.capacity()); 48 | pb.add("123456789"); 49 | assertFalse(pb.isEmpty()); 50 | } 51 | } 52 | 53 | // Test inclusion relations on phone books and numbers 54 | public static class InclusionTest 55 | { 56 | @Test 57 | public void book_contains_valid_numbers_that_were_added() 58 | { 59 | PhoneBook pb = new PhoneBook(); 60 | pb.add("123456789"); 61 | pb.add("456789012"); 62 | assertTrue(pb.contains("123456789")); 63 | assertTrue(pb.contains("456789012")); 64 | } 65 | 66 | @Test 67 | public void book_does_not_contain_numbers_in_different_format() 68 | { 69 | PhoneBook pb = new PhoneBook(); 70 | pb.add("123456789"); 71 | pb.add("456789012"); 72 | assertFalse(pb.contains("123-456-789")); 73 | assertFalse(pb.contains("456-789-012")); 74 | } 75 | 76 | @Test 77 | public void book_with_changed_format_contains_numbers_in_changed_format() 78 | { 79 | PhoneBook pb = new PhoneBook(); 80 | pb.add("123456789"); 81 | pb.add("456789012"); 82 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 83 | assertTrue(pb.contains("123-456-789")); 84 | assertTrue(pb.contains("456-789-012")); 85 | } 86 | 87 | @Test 88 | public void book_does_not_contain_null() 89 | { 90 | PhoneBook pb = new PhoneBook().add("123456789"); 91 | assertFalse(pb.contains((String) null)); 92 | } 93 | 94 | @Test 95 | public void book_containing_book_of_numbers_contains_those_numbers() 96 | { 97 | PhoneBook numbers = new PhoneBook().add("123456789").add("456789012"); 98 | PhoneBook books = new PhoneBook().add(numbers); 99 | assertTrue(books.contains("123456789")); 100 | assertTrue(books.contains("456789012")); 101 | } 102 | 103 | @Test 104 | public void book_containing_book_of_numbers_contains_those_numbers_in_changed_format() 105 | { 106 | PhoneBook numbers = new PhoneBook().add("123456789").add("456789012"); 107 | PhoneBook books = new PhoneBook().add(numbers); 108 | books.changeFormat(PhoneBook.NumberFormat.HYPHENED); 109 | assertFalse(books.contains("123456789")); 110 | assertFalse(books.contains("456789012")); 111 | assertTrue(books.contains("123-456-789")); 112 | assertTrue(books.contains("456-789-012")); 113 | } 114 | 115 | @Test 116 | public void book_containing_book_of_numbers_contains_those_numbers_in_adjusted_format() 117 | { 118 | PhoneBook numbers = new PhoneBook().add("123456789").add("456789012"); 119 | PhoneBook books = new PhoneBook(PhoneBook.NumberFormat.HYPHENED).add(numbers); 120 | assertFalse(books.contains("123456789")); 121 | assertFalse(books.contains("456789012")); 122 | assertTrue(books.contains("123-456-789")); 123 | assertTrue(books.contains("456-789-012")); 124 | } 125 | 126 | @Test 127 | public void book_is_not_element_of_null() 128 | { 129 | PhoneBook pb = new PhoneBook(); 130 | assertFalse(pb.elementOf(null)); 131 | } 132 | 133 | @Test 134 | public void book_is_element_of_book_it_was_added_to() 135 | { 136 | PhoneBook element = new PhoneBook(); 137 | PhoneBook container = new PhoneBook(); 138 | container.add(element); 139 | assertTrue(element.elementOf(container)); 140 | } 141 | 142 | @Test 143 | public void book_does_not_contain_itself() 144 | { 145 | PhoneBook pb = new PhoneBook(); 146 | pb.add(pb); 147 | assertFalse(pb.contains(pb)); 148 | } 149 | 150 | @Test 151 | public void book_is_not_subset_of_null() 152 | { 153 | assertFalse(new PhoneBook().subsetOf(null)); 154 | } 155 | 156 | @Test 157 | public void book_is_not_superset_of_null() 158 | { 159 | assertFalse(new PhoneBook().supersetOf(null)); 160 | } 161 | 162 | @Test 163 | public void empty_book_is_its_subset_and_superset() 164 | { 165 | PhoneBook empty = new PhoneBook(); 166 | assertTrue(empty.supersetOf(empty)); 167 | assertTrue(empty.subsetOf(empty)); 168 | } 169 | 170 | 171 | @Test 172 | public void empty_book_is_subset_of_non_empty_book() 173 | { 174 | PhoneBook subset = new PhoneBook(); 175 | PhoneBook superset = new PhoneBook().add("123456789"); 176 | assertTrue(subset.subsetOf(superset)); 177 | } 178 | 179 | @Test 180 | public void non_empty_book_is_superset_of_empty_book() 181 | { 182 | PhoneBook subset = new PhoneBook(); 183 | PhoneBook superset = new PhoneBook().add("123456789"); 184 | assertTrue(superset.supersetOf(subset)); 185 | } 186 | 187 | @Test 188 | public void format_is_ignored_when_checking_subset() 189 | { 190 | PhoneBook subset = new PhoneBook().add("123456789"); 191 | subset.changeFormat(PhoneBook.NumberFormat.HYPHENED); 192 | PhoneBook superset = new PhoneBook().add("123456789").add("456789012"); 193 | assertTrue(subset.subsetOf(superset)); 194 | } 195 | 196 | @Test 197 | public void format_is_ignored_when_checking_superset() 198 | { 199 | PhoneBook subset = new PhoneBook().add("123456789"); 200 | subset.changeFormat(PhoneBook.NumberFormat.HYPHENED); 201 | PhoneBook superset = new PhoneBook().add("123456789").add("456789012"); 202 | assertTrue(superset.supersetOf(subset)); 203 | } 204 | 205 | @Test 206 | public void set_inclusion_is_not_recursive() 207 | { 208 | PhoneBook almost_superset = new PhoneBook().add(new PhoneBook().add("123456789")); 209 | PhoneBook almost_subset = new PhoneBook().add("123456789"); 210 | assertFalse(almost_subset.subsetOf(almost_superset)); 211 | assertFalse(almost_superset.supersetOf(almost_subset)); 212 | } 213 | 214 | @Test 215 | public void containing_books_is_not_recursive() 216 | { 217 | PhoneBook pb = new PhoneBook().add(new PhoneBook().add(new PhoneBook()).add("123456789")); 218 | PhoneBook almost_contained = new PhoneBook().add("123456789"); 219 | assertFalse(pb.contains(almost_contained)); 220 | } 221 | 222 | @Test 223 | public void nested_inclusion_test() 224 | { 225 | PhoneBook a = new PhoneBook().add("123456789"); 226 | PhoneBook b = new PhoneBook().add("223456789"); 227 | PhoneBook c = new PhoneBook().add("323456789"); 228 | PhoneBook x = new PhoneBook().add("423456789"); 229 | PhoneBook y = new PhoneBook().add("523456789"); 230 | 231 | PhoneBook abc = new PhoneBook().add(a).add(b).add(c); 232 | PhoneBook xy = new PhoneBook().add(x).add(y); 233 | PhoneBook first = new PhoneBook().add(abc).add(x); 234 | PhoneBook second = new PhoneBook().add(abc).add(xy); 235 | 236 | assertFalse(first.subsetOf(second)); 237 | assertFalse(second.supersetOf(first)); 238 | } 239 | } 240 | 241 | // Test conditions of two books being equal 242 | public static class EqualityTest 243 | { 244 | @Test 245 | public void two_newly_created_books_are_equal() 246 | { 247 | assertEquals(new PhoneBook(), new PhoneBook()); 248 | } 249 | 250 | @Test 251 | public void books_containing_the_same_numbers_in_the_same_format_are_equal() 252 | { 253 | PhoneBook first = new PhoneBook(); 254 | PhoneBook second = new PhoneBook(); 255 | first.add("123456789"); 256 | first.add("456789012"); 257 | 258 | second.add("123456789"); 259 | second.add("456789012"); 260 | assertEquals(first, second); 261 | } 262 | 263 | @Test 264 | public void books_containing_the_same_numbers_in_different_format_are_equal() 265 | { 266 | PhoneBook digits = new PhoneBook(); 267 | PhoneBook hyphened = new PhoneBook(PhoneBook.NumberFormat.HYPHENED); 268 | digits.add("123456789"); 269 | digits.add("456789012"); 270 | 271 | hyphened.add("123-456-789"); 272 | hyphened.add("456-789-012"); 273 | assertEquals(digits, hyphened); 274 | } 275 | 276 | @Test 277 | public void books_of_different_capacity_are_equal_when_contain_equal_numbers() 278 | { 279 | PhoneBook small = new PhoneBook(4); 280 | PhoneBook big = new PhoneBook(10); 281 | small.add("123456789"); 282 | big.add("123456789"); 283 | assertEquals(small, big); 284 | } 285 | 286 | @Test 287 | public void book_is_not_equal_to_null() 288 | { 289 | assertNotEquals(null, new PhoneBook()); 290 | } 291 | 292 | @Test 293 | public void containing_equal_numbers_in_subbooks_is_insufficient_to_be_equal() 294 | { 295 | PhoneBook pb1 = new PhoneBook().add("123456789"); 296 | PhoneBook pb2 = new PhoneBook().add(new PhoneBook().add("123456789")); 297 | assertNotEquals(pb1, pb2); 298 | } 299 | } 300 | 301 | // Test if serialization to strings works as expected 302 | public static class ToStringTest 303 | { 304 | 305 | @Test 306 | public void empty_book_to_string() 307 | { 308 | String expected = "{\n}\n"; 309 | String actual = new PhoneBook().toString(); 310 | assertEquals(expected, actual); 311 | } 312 | 313 | @Test 314 | public void book_containing_empty_book_to_string() 315 | { 316 | String expected = "{\n {\n }\n}\n"; 317 | String actual = new PhoneBook().add(new PhoneBook()).toString(); 318 | assertEquals(expected, actual); 319 | } 320 | 321 | @Test 322 | public void book_with_two_empty_books_added_to_string() 323 | { 324 | String expected = "{\n {\n }\n}\n"; 325 | String actual = new PhoneBook().add(new PhoneBook()).add(new PhoneBook()).toString(); 326 | assertEquals(expected, actual); 327 | } 328 | 329 | 330 | @Test 331 | public void book_string_contains_numbers_in_current_format() 332 | { 333 | String expected = "{\n 123-456-789\n}\n"; 334 | PhoneBook pb = new PhoneBook().add("123456789"); 335 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 336 | assertEquals(expected, pb.toString()); 337 | } 338 | 339 | @Test 340 | public void subbooks_string_contains_numbers_in_current_format() 341 | { 342 | String expected = "{\n {\n 123-456-789\n }\n}\n"; 343 | PhoneBook pb = new PhoneBook().add(new PhoneBook().add("123456789")); 344 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 345 | assertEquals(expected, pb.toString()); 346 | } 347 | 348 | @Test 349 | public void book_of_books_to_string() 350 | { 351 | String expected = "{\n {\n 123456789\n }\n}\n"; 352 | PhoneBook inner = new PhoneBook().add("123456789"); 353 | PhoneBook outer = new PhoneBook().add(inner); 354 | assertEquals(expected, outer.toString()); 355 | } 356 | } 357 | 358 | // Test conditions of being (not) empty 359 | public static class EmptinessTest 360 | { 361 | @Test 362 | public void newly_constructed_book_is_empty() 363 | { 364 | PhoneBook pb = new PhoneBook(); 365 | assertTrue(pb.isEmpty()); 366 | assertEquals(0, pb.size()); 367 | } 368 | 369 | @Test 370 | public void book_containing_empty_book_is_not_empty() 371 | { 372 | PhoneBook pb = new PhoneBook(); 373 | pb.add(new PhoneBook()); 374 | assertFalse(pb.isEmpty()); 375 | } 376 | 377 | @Test 378 | public void book_containing_empty_book_has_size_of_zero() 379 | { 380 | PhoneBook pb = new PhoneBook(); 381 | pb.add(new PhoneBook()); 382 | assertEquals(0, pb.size()); 383 | } 384 | 385 | @Test 386 | public void book_with_capacity_of_zero_is_not_empty_after_adding_empty_book() 387 | { 388 | PhoneBook pb = new PhoneBook(0); 389 | pb.add(new PhoneBook()); 390 | assertFalse(pb.isEmpty()); 391 | } 392 | 393 | @Test 394 | public void book_with_capacity_of_zero_is_not_empty_after_adding_non_empty_book() 395 | { 396 | PhoneBook pb = new PhoneBook(0); 397 | pb.add(new PhoneBook().add("123456789")); 398 | assertTrue(pb.isEmpty()); 399 | } 400 | 401 | @Test 402 | public void book_with_capacity_of_zero_is_empty_after_adding_number() 403 | { 404 | PhoneBook pb = new PhoneBook(0); 405 | pb.add("123456789"); 406 | assertTrue(pb.isEmpty()); 407 | } 408 | } 409 | 410 | // Test how adding various data affects size of a book 411 | public static class AddingAndCheckingSizeTest 412 | { 413 | @Test 414 | public void adding_valid_number_increments_size() 415 | { 416 | PhoneBook pb = new PhoneBook(); 417 | pb.add("123456789"); 418 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 419 | pb.add("456-789-012"); 420 | assertEquals(2, pb.size()); 421 | } 422 | 423 | @Test 424 | public void adding_number_of_different_format_does_not_change_size() 425 | { 426 | PhoneBook pb = new PhoneBook(); 427 | pb.add("123-456-789"); 428 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 429 | pb.add("123456789"); 430 | assertEquals(0, pb.size()); 431 | } 432 | 433 | @Test 434 | public void adding_invalid_number_does_not_change_size() 435 | { 436 | PhoneBook pb = new PhoneBook(); 437 | pb.add("123"); // Number is too short 438 | pb.add("012345678"); // Number cannot start with 0 439 | pb.add(""); // Number cannot be empty 440 | pb.add("falowana blaszka"); // not a number 441 | pb.add((String) null); // Cannot add null 442 | 443 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 444 | pb.add("1-234-56789"); // Hyphens in invalid places 445 | pb.add("-1-2-3-4-5-6-7-8-9-"); // Invalid number of hyphens 446 | pb.add("12-45-789"); // Invalid number of digits 447 | pb.add("012-345-678"); // Hyphened number cannot start with 0 448 | 449 | assertEquals(0, pb.size()); 450 | } 451 | 452 | @Test 453 | public void adding_existing_number_does_not_change_size() 454 | { 455 | PhoneBook pb = new PhoneBook(); 456 | pb.add("123456789"); 457 | pb.add("456789012"); 458 | pb.add("123456789"); 459 | assertEquals(2, pb.size()); 460 | } 461 | 462 | @Test 463 | public void adding_existing_number_after_changing_format_does_not_change_size() 464 | { 465 | PhoneBook pb = new PhoneBook(); 466 | pb.add("123456789"); 467 | pb.changeFormat(PhoneBook.NumberFormat.HYPHENED); 468 | pb.add("123-456-789"); 469 | assertEquals(1, pb.size()); 470 | } 471 | 472 | @Test 473 | public void adding_number_to_book_of_books_does_not_change_size() 474 | { 475 | PhoneBook pb = new PhoneBook(); 476 | pb.add(new PhoneBook().add("456789012")); 477 | pb.add("123456789"); 478 | assertEquals(1, pb.size()); 479 | } 480 | 481 | @Test 482 | public void adding_book_to_book_of_numbers_does_not_change_size() 483 | { 484 | PhoneBook pb = new PhoneBook(); 485 | pb.add("123456789"); 486 | pb.add(new PhoneBook().add("456789012")); 487 | assertEquals(1, pb.size()); 488 | } 489 | 490 | @Test 491 | public void adding_book_after_adding_invalid_number_changes_size() 492 | { 493 | PhoneBook pb = new PhoneBook(); 494 | pb.add("012345678"); 495 | pb.add("123-456-789"); 496 | pb.add((String) null); 497 | pb.add(new PhoneBook().add("456789012").add("123456789").add("111222333")); 498 | assertEquals(3, pb.size()); 499 | } 500 | 501 | @Test 502 | public void adding_book_of_different_format_adds_it_with_changed_format() 503 | { 504 | PhoneBook books = new PhoneBook(); 505 | PhoneBook hyphened = new PhoneBook(PhoneBook.NumberFormat.HYPHENED); 506 | hyphened.add("123-456-789"); 507 | books.add(hyphened); 508 | assertTrue(books.contains(hyphened)); 509 | assertTrue(books.contains("123456789")); 510 | } 511 | 512 | @Test 513 | public void adding_book_which_size_fills_capacity_changes_changes_size_to_full() 514 | { 515 | PhoneBook books = new PhoneBook(3); 516 | PhoneBook large = new PhoneBook().add("123456789").add("456789012").add("111222333"); 517 | books.add(large); 518 | assertEquals(3, books.size()); 519 | assertTrue(books.contains(large)); 520 | assertTrue(books.isFull()); 521 | } 522 | 523 | @Test 524 | public void adding_book_that_would_exceed_capacity_does_not_change_size() 525 | { 526 | PhoneBook books = new PhoneBook(2); 527 | PhoneBook large = new PhoneBook().add("123456789").add("456789012").add("111222333"); 528 | books.add(large); 529 | assertEquals(0, books.size()); 530 | assertFalse(books.contains(large)); 531 | } 532 | 533 | @Test 534 | public void size_of_book_of_books_is_sum_of_sizes_of_contained_books() 535 | { 536 | PhoneBook pb1 = new PhoneBook().add("123456789").add("456789012"); 537 | PhoneBook pb2 = new PhoneBook().add("111222333"); 538 | PhoneBook books = new PhoneBook().add(pb1).add(pb2); 539 | assertEquals(3, books.size()); 540 | } 541 | 542 | @Test 543 | public void adding_number_after_adding_null_book_increments_size() 544 | { 545 | PhoneBook pb = new PhoneBook(); 546 | pb.add((PhoneBook) null); 547 | pb.add("123456789"); 548 | assertEquals(1, pb.size()); 549 | } 550 | 551 | @Test 552 | public void adding_existing_book_does_not_change_size() 553 | { 554 | PhoneBook books = new PhoneBook(); 555 | PhoneBook pb = new PhoneBook().add("123456789"); 556 | books.add(pb).add(pb); 557 | assertEquals(1, pb.size()); 558 | } 559 | 560 | @Test 561 | public void can_add_more_that_ten_numbers() 562 | { 563 | PhoneBook pb = new PhoneBook(20); 564 | for (int i = 0; i < 20; i++) 565 | pb.add(String.format("1234567%02d", i)); 566 | assertEquals(20, pb.size()); 567 | } 568 | 569 | @Test 570 | public void can_add_at_most_ten_books() 571 | { 572 | PhoneBook pb = new PhoneBook(20); 573 | for (int i = 0; i < 10; i++) 574 | pb.add(pb); 575 | // pb contains ten books without any numbers 576 | // If the below was added the size would equal ten 577 | pb.add(new PhoneBook().add("123456789")); 578 | assertEquals(0, pb.size()); 579 | } 580 | 581 | @Test 582 | public void capacity_can_exceed_ten() 583 | { 584 | PhoneBook pb = new PhoneBook(32); 585 | PhoneBook content = new PhoneBook().add("123456789"); 586 | pb.add(content); 587 | for (int i = 0; i < 5; i++) // Double the size with each iteration 588 | pb.add(pb); 589 | assertEquals(32, pb.size()); 590 | } 591 | 592 | @Test 593 | public void adding_book_containing_many_empty_books_increases_size() 594 | { 595 | PhoneBook pb = new PhoneBook(1); 596 | PhoneBook empty_books = new PhoneBook(); 597 | for (int i = 0; i < 10; i++) 598 | empty_books.add(empty_books); 599 | PhoneBook number = new PhoneBook().add("123456789"); 600 | PhoneBook added = new PhoneBook().add(empty_books).add(number); 601 | pb.add(added); 602 | assertEquals(1, pb.size()); 603 | } 604 | } 605 | 606 | // Test if objects are copied accordingly 607 | public static class CopyingTest 608 | { 609 | @Test 610 | public void changing_added_subbook_does_not_change_size() 611 | { 612 | PhoneBook numbers = new PhoneBook().add("123456789"); 613 | PhoneBook pb = new PhoneBook().add(numbers); 614 | PhoneBook added = new PhoneBook(); 615 | pb.add(added); 616 | added.add("456789012"); 617 | assertEquals(1, pb.size()); 618 | assertFalse(pb.contains(added)); 619 | } 620 | 621 | @Test 622 | public void adding_number_to_copy_does_not_change_original_book() 623 | { 624 | PhoneBook original = new PhoneBook().add("123456789"); 625 | PhoneBook copy = original.copyBook(); 626 | String number = "456789012"; 627 | copy.add(number); 628 | 629 | assertNotEquals(original, copy); 630 | assertEquals(1, original.size()); 631 | assertFalse(original.contains(number)); 632 | } 633 | 634 | @Test 635 | public void changing_format_of_copy_does_not_change_format_of_original_book() 636 | { 637 | PhoneBook original = new PhoneBook().add("123456789"); 638 | PhoneBook copy = original.copyBook(); 639 | String number = "456-789-012"; // Different format than original 640 | copy.changeFormat(PhoneBook.NumberFormat.HYPHENED); 641 | 642 | copy.add(number); 643 | original.add(number); // The number is hyphened and shouldn't be added to original 644 | 645 | assertNotEquals(original, copy); 646 | assertFalse(original.contains(number)); 647 | assertEquals(1, original.size()); 648 | } 649 | 650 | @Test 651 | public void copy_of_book_of_books_is_independent_of_original() 652 | { 653 | PhoneBook original = new PhoneBook().add(new PhoneBook()); 654 | PhoneBook copy = original.copyBook(); 655 | copy.add(new PhoneBook().add("123456789")); 656 | 657 | assertNotEquals(original, copy); 658 | assertEquals(0, original.size()); 659 | } 660 | 661 | @Test 662 | public void book_of_numbers_is_equal_to_its_copy() 663 | { 664 | PhoneBook numbers = new PhoneBook().add("123456789").add("456789012"); 665 | PhoneBook copy = numbers.copyBook(); 666 | assertEquals(numbers, copy); 667 | } 668 | 669 | @Test 670 | public void book_of_books_is_equal_to_its_copy() 671 | { 672 | PhoneBook pb1 = new PhoneBook().add("123456789"); 673 | PhoneBook pb2 = new PhoneBook().add("456789012"); 674 | PhoneBook books = new PhoneBook().add(pb1).add(pb2); 675 | PhoneBook copy = books.copyBook(); 676 | assertEquals(books, copy); 677 | } 678 | } 679 | 680 | } 681 | --------------------------------------------------------------------------------