java/ch/wlkl/env/Test.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.wlkl.env;

import static ch.wlkl.env.Env.*;
import static ch.wlkl.env.Ut.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author walter
 */
public class Test {

    All2S a = new All2S();

    public void all2s(String t, Object o) {
        outPa(t + (t.length() == 0 ? "" : " ") + (o == null ? "" : o.getClass().getSimpleName()) + " ==> ");
        out(a.a2r(o));
        env.io.outFi("  toString: " + o);
        if (o != null && o.getClass().isArray()) {
            env.io.outFi("  deepToString: " + Arrays.deepToString(new Object[]{o}));
        }
    }

    public void all2s(IOTest t) {
        t.begin("all2s", "this Test ==> Test@1", "new All2s All2S ==> All2S@2", "num Double ==> 234.56", "null  ==> null", "String String ==> ein String", "String[] String[] ==> String[]@3=[2: eins, zwei]", "String[][] ==> String[][]@4=[6: [nullStr], [eins, zwei], [], null, [41], [51, 52]]", "String[][] ==> String[][]@5=[6: [null], [eins, zwei], [], null, [41], [51, 52]]", "int[] ==> int[]@6=[6: 0, 1, 2, -3, 45, -678]", "int[][] ==> int[][]@7=[6: null, [1], [], [0, 1, 2, -3, 45, -678], [], [7, -7]]", "short[] ==> short[]@8=[6: 0, 1, 2, -3, 45, -678]", "ArrayList ==> ArrayList@9={2: 3, -4}", "ArrayList ==> ArrayList@10={2: ArrayList@9, ArrayList@11={}}", "ArrayList ==> ArrayList@12={2: Short[]@13=[3: 3, 4, 5], Short[]@14=[1: 6]}", "Object[] ==> Object[]@15=[3: ArrayList@9, ArrayList@10, ArrayList@12]"
        );
        a = new All2S();
        all2s("this", this);
        all2s("new All2s", new All2S());
        all2s("num", 234.56);
        all2s("null", null);
        all2s("String", "ein String");
        all2s("String[]", new String[]{"eins", "zwei"});
        all2s("", new String[][]{{"nullStr"}, {"eins", "zwei"}, {}, null, {"41"}, {"51", "52"}});
        all2s("", new String[][]{{"null"}, {"eins", "zwei"}, {}, null, {"41"}, {"51", "52"}});
        all2s("", new int[]{0, 1, 2, -3, 45, -678});
        all2s("", new int[][]{null, {1}, {}, {0, 1, 2, -3, 45, -678}, {}, {7, -7}});
        all2s("", new short[]{0, 1, 2, -3, 45, -678});
        ArrayList<Short> l = new ArrayList();
        l.add((short) 3);
        l.add((short) -4);
        all2s("", l);
        ArrayList<ArrayList<Short>> l2 = new ArrayList();
        l2.add(l);
        l2.add(new ArrayList<Short>());
        all2s("", l2);
        ArrayList<Short[]> l3 = new ArrayList();
        l3.add(new Short[]{3, 4, 5});
        l3.add(new Short[]{6});
        all2s("", l3);
        all2s("", new Object[]{l, l2, l3});
        t.end();
    }
    
    static class Fam implements A2S {
        String name;
        Fam father;
        Fam[] chi;
        static HashMap<String, Fam> n2f = new HashMap();

        public Fam(String n, Fam... c) {
            name = n;
            father = null;
            chi = c;     
            for(Fam d: c) {
                assert d.father == null;
                d.father = this;
            }
            assert ! n2f.containsKey(n);
            n2f.put(n, this);
        }

        @Override
        public void a2sName(All2S a) {
            a.name(this, 3, name);
        }

        @Override
        public void a2s(All2S a) {
            a.a2s("father=", father, ", chi=", chi);
        }

        static Fam n(String n) {
            return n2f.get(n);
        }
        
        static Fam makeFam(int n) {
            n2f.clear();
            Fam f = new Fam("deif", new Fam("cyril"), new Fam("merlin"), new Fam("d>h"));
            if ( n >= 1 ) 
                 f = new Fam("walti", new Fam("gregor"), f, new Fam("flo", new Fam("alissia"), new Fam("juna")));
            if ( n>= 2) {
                f = new Fam("gerhard", new Fam("heiri", new Fam("ruth"), new Fam("ursi"), f));
                n("d>h").chi = new Fam[] {n("heiri")};
            }
            return f;                
        }
    }

    public void all2sfam(IOTest t) {
        t.begin("famGerhard"
          , "von Urahn => Fam-gerhard@1=father=null, chi=Fam[]@2=[1: Fam-heiri@3]"
          , "  Fam-heiri@3=father=Fam-gerhard@1, chi=Fam[]@4=[3: Fam-ruth@5, Fam-ursi@6, Fam-walti@7]"
          , "  Fam-ruth@5=father=Fam-heiri@3, chi=Fam[]-[0:]"
          , "  Fam-ursi@6=father=Fam-heiri@3, chi=Fam[]-[0:]"
          , "  Fam-walti@7=father=Fam-heiri@3, chi=Fam[]@10=[3: Fam-gregor@11, Fam-deif@12, Fam-flo@13]"
          , "  Fam-gregor@11=father=Fam-walti@7, chi=Fam[]-[0:]"
          , "  Fam-deif@12=father=Fam-walti@7, chi=Fam[]@15=[3: Fam-cyril@16, Fam-merlin@17, Fam-d>h@18]"
          , "  Fam-flo@13=father=Fam-walti@7, chi=Fam[]@19=[2: Fam-alissia@20, Fam-juna@21]"
          , "  Fam-cyril@16=father=Fam-deif@12, chi=Fam[]-[0:]"
          , "  Fam-merlin@17=father=Fam-deif@12, chi=Fam[]-[0:]"
          , "  Fam-d>h@18=father=Fam-deif@12, chi=Fam[]@24=[1: Fam-heiri@3]"
          , "  Fam-alissia@20=father=Fam-flo@13, chi=Fam[]-[0:]"
          , "  Fam-juna@21=father=Fam-flo@13, chi=Fam[]-[0:]"
          , "walti => Fam-walti@7"
          , "restart, david => Fam-deif@12=father=Fam-walti@7, chi=Fam[]@15=[3: Fam-cyril@16, Fam-merlin@17, Fam-d>h@18]"
          , "  Fam-walti@7=father=Fam-heiri@3, chi=Fam[]@10=[3: Fam-gregor@11, Fam-deif@12, Fam-flo@13]"
          , "  Fam-cyril@16=father=Fam-deif@12, chi=Fam[]-[0:]"
          , "  Fam-merlin@17=father=Fam-deif@12, chi=Fam[]-[0:]"
          , "  Fam-d>h@18=father=Fam-deif@12, chi=Fam[]@24=[1: Fam-heiri@3]"
          , "  Fam-heiri@3=father=Fam-gerhard@1, chi=Fam[]@4=[3: Fam-ruth@5, Fam-ursi@6, Fam-walti@7]"
          , "  Fam-gregor@11=father=Fam-walti@7, chi=Fam[]-[0:]"
          , "  Fam-flo@13=father=Fam-walti@7, chi=Fam[]@19=[2: Fam-alissia@20, Fam-juna@21]"
          , "  Fam-gerhard@1=father=null, chi=Fam[]@2=[1: Fam-heiri@3]"
          , "  Fam-ruth@5=father=Fam-heiri@3, chi=Fam[]-[0:]"
          , "  Fam-ursi@6=father=Fam-heiri@3, chi=Fam[]-[0:]"
          , "  Fam-alissia@20=father=Fam-flo@13, chi=Fam[]-[0:]"
          , "  Fam-juna@21=father=Fam-flo@13, chi=Fam[]-[0:]"
          , "afterClear, depth=2 heiri => Fam-heiri@1=father=Fam-gerhard@2, chi=Fam[]@3=[3: Fam-ruth@4, Fam-ursi@5, Fam-walti@6]"
        , "  Fam-gerhard@2=father=null, chi=Fam[]@7=[1: Fam-heiri@1]"
        , "  Fam-ruth@4=father=Fam-heiri@1, chi=Fam[]-[0:]"
        , "  Fam-ursi@5=father=Fam-heiri@1, chi=Fam[]-[0:]"
        , "  Fam-walti@6=father=Fam-heiri@1, chi=Fam[]@10=[3: Fam-gregor@11, Fam-deif@12, Fam-flo@13]"
        , "flo ==>Fam-flo@13=father=Fam-walti@6, chi=Fam[]@14=[2: Fam-alissia@15, Fam-juna@16]"
        , "  Fam-walti@6=father=Fam-heiri@1, chi=Fam[]@10=[3: Fam-gregor@11, Fam-deif@12, Fam-flo@13]"
        , "  Fam-alissia@15=father=Fam-flo@13, chi=Fam[]-[0:]"
        , "  Fam-juna@16=father=Fam-flo@13, chi=Fam[]-[0:]"
        );
        Fam.makeFam(9);
        out("von Urahn => ", Fam.n("gerhard"));
        out("walti => ", Fam.n("walti"));
        all2s.restart();
        out("restart, david => ", Fam.n("deif"));
        all2s.afterClear = true;
        all2s.depth = 2;
        all2s.clear();
        out("afterClear, depth=2 heiri => ", Fam.n("heiri"));
        out("flo ==>", Fam.n("flo"));
        
        t.end();
    }
    
    public void ioCompare(IOTest t) {
        for (int q = 0; q < 2; q++) {
            t.begin("test1", "test1 out 1", "test1 out 2", "test1 out 3",
                    "test1 fertig, richtig mit 0 diffs, ...");
            out("test1 out 1");
            out("test1 out 2");
            out("test1 out 3");
            out("test1 fertig, richtig mit 0 diffs, ...");
            t.end();
        }

        t.begin("test2", "Test2 out 1", "test2 out 2 ", "test2 out?3",
                "test2 fertig, richtig mit 3 diffs, ...");
        out("test2 out 1");
        out("test2 out 2");
        out("test2 out 3");
        out("test2 fertig, richtig mit 3 diffs, ...");
        t.end();

        t.begin("test3", "test3 out 1: old longer richtig mit 1 diffs, ...", "test3 out 2", "test3 out 3",
                "test3 fertig, richtig mit 1 errors, ...");
        out("test3 out 1: old longer richtig mit 1 diffs, ...");
        t.end();
        t.begin("test4", "test4 out 1");
        out("test4 out 1");
        out("test4 out 2");
        out("test4 fertig, richtig mit 2 diffs, ...");
        t.end();
        t.total("ioCompare");
    }

    public void ioMsg(IOTest t) {
        t.begin("ioMsg"
          , "msg(1, message eins, '  ', zwei, drei"
          , "*** message eins *********"
          , "  <<<info: zweidrei >>>"
          , "--- error error zwei ---------"
          , "  <<<before: before error zwei.>>>"
          , "  <<<info:   2zwei2 drei >>>"
          , "--- error error drei ---------"
          , "  <<<before: before drei...Fam-deif@1=father=null, chi=Fam[]@2=[3: Fam-cyril@3, Fam-merlin@4, Fam-d>h@5]"
          , "  Fam-cyril@3=father=Fam-deif@1, chi=Fam[]-[0:]"
          , "  Fam-merlin@4=father=Fam-deif@1, chi=Fam[]-[0:]"
          , "  Fam-d>h@5=father=Fam-deif@1, chi=Fam[]-[0:]>>>"
          , "  <<<info: merlin=Fam-merlin@4=father=Fam-deif@1, chi=Fam[]-[0:]"
          , "  Fam-deif@1=father=null, chi=Fam[]@2=[3: Fam-cyril@3, Fam-merlin@4, Fam-d>h@5]"
          , "  Fam-cyril@3=father=Fam-deif@1, chi=Fam[]-[0:]"
          , "  Fam-d>h@5=father=Fam-deif@1, chi=Fam[]-[0:] >>>"
          , "*** dy dy vier *********"
          , "  <<<before: before dy vier.>>>"
          , "  <<<info:   4-zwei4-drei >>>"
          , "--- error testOne ioMsg caught Error ---------"
          , "  <<<info: Error@9=dy dy vier >>>"
          );    
        out("msg(1, message eins, '  ', zwei, drei");
        t.msg(1, "message eins", "  ", "zwei", "drei");
        outPa("before error zwei.");
        t.err("error zwei", "  ", "2zwei", "2 drei");
        outPa("before drei...", Fam.makeFam(0));
        t.err("error drei", "merlin=", Fam.n("merlin"));
        outPa("before dy vier.");
        t.dy("dy vier", "  ", "4-zwei", "4-drei");
        out("after dy");
        t.end();
    }

    public void ut(IOTest t) {
        t.begin("ut"
          , "array(a, b) => String[]@1=[2: a, b]"
          , "array(1, 2) => Integer[]@2=[2: 1, 2]"
          , "array(1, 2.) => Number[]@3=[2: 1, 2.0]"
          , "array(1, 2. drei, [0]) => Serializable[]@4=[4: 1, 2.0, drei, Integer[]@5=[1: 0]]"
          , "list(1, 2.) => ArrayList@6={2: 1, 2.0}"
          , "addWords(abc  efg  hij   ) ==> ArrayList@7={3: abc, efg, hij}"
          , "addWords(   abc  efg  hij   ) ==> ArrayList@8={4: , abc, efg, hij}"
          );
        out("array(a, b) => ", array("a", "b"));
        out("array(1, 2) => ", array(1, 2));
        out("array(1, 2.) => ", array(1, 2.));
        out("array(1, 2. drei, [0]) => ", array(1, 2., "drei", array(0)));
        List<Number> l;
        out("list(1, 2.) => ", l = list(1, 2.f));
        out("addWords(abc  efg  hij   ) ==> ", addWords(new ArrayList<String>(), "abc efg \n\t\n  hij \n \t    "));
        out("addWords(   abc  efg  hij   ) ==> ", addWords(new ArrayList<String>(), " \t \n abc efg \n\t\n  hij \n \t    "));
        t.end();
    }
    
    public void testVisual(IOTest q) {
        q.test("testVisual"
                 ,t -> t.hd(0, "testVisual in " + getClass() + " needing visual controll IOTest=" + q)
                , t -> t.hd(0, "check visually - not automatic!")
                , t -> t.out("IOTest = ", t)
                , t -> out("env.out")
                , t -> ioCompare(t)
                , t -> t.hd(0, "testVisual end, should be: " + (q instanceof IOCompare ? "3" : "0") + " failed of 10 tests ... with 0 errors, 31 out and 0 debugs")
        );
    }

    public void testVisual() {
        testVisual(new IOTest());
        testVisual(new IOCompare());
    }
    
    public void test(IOTest q) {
        q.test(getClass().toString()
            , t -> ioMsg(t)
            , t -> ut(t)
            , t -> all2s(t)
            , t-> all2sfam(t)
        );
    }

    public static void main(String... a) {
        Test t = new Test();
        t.testVisual();
        t.test(new IOCompare());
    }
}