Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

144
LINES

< > BotCompany Repo | #1003606 // class GenTesting

JavaX fragment (include)

1  
sclass GenTesting {
2  
  O makeGenerators; // voidfunc(L<Gen> gens, L<S> log)
3  
  
4  
  // method to compare generator output & user line
5  
  S comparison = "eqic";
6  
  
7  
  *(O *makeGenerators) {}
8  
9  
  MultiSet<S> scoreGenerators(L<S> log) {
10  
    ret scoreGenerators(log, null);
11  
  }
12  
  
13  
  MultiSet<S> scoreGenerators(L<S> log, BitSet interestingLines) {
14  
    new MultiSet<S> scores;
15  
    for (int i = 0; i < l(log); i++)
16  
      if (interestingLines == null || interestingLines.get(i))
17  
        scoreGenerators1(subList(log, 0, i+1), scores);
18  
    print(/*asciiHeading2("SCORES")*/);
19  
    for (S name : scores.getTopTen())
20  
      print("  [" + scores.get(name) + "] " + name);
21  
    print();
22  
    ret scores;
23  
  }
24  
    
25  
  void scoreGenerators1(L<S> log, MultiSet<S> scores) {
26  
    if (empty(log)) ret;
27  
    S line = last(log);
28  
    log = dropLast(log);
29  
    
30  
    genLog_set(log);
31  
    try {
32  
      L<Gen> gens = makeGenerators(log);
33  
      
34  
      for (Gen gen : gens) {
35  
        try {
36  
          if (compare(callGen(gen), line))
37  
            scores.add(gen.name);
38  
        } catch {}
39  
      }
40  
    } finally {
41  
      genLog_clear();
42  
    }
43  
  }
44  
  
45  
  S callSingle(L<S> log, O genName) {
46  
    genLog_set(log);
47  
    try {
48  
      L<Gen> gens = makeGenerators(log);
49  
      Gen gen = findByField(gens, "name", genName);
50  
      if (gen == null) null;
51  
      ret callGen(gen);
52  
    } finally {
53  
      genLog_clear();
54  
    }
55  
  }
56  
  
57  
  bool verifySingle(L<S> log, O genName) {
58  
    if (empty(log)) false;
59  
    S line = last(log);
60  
    log = dropLast(log);
61  
    
62  
    genLog_set(log);
63  
    try {
64  
      L<Gen> gens = makeGenerators(log);
65  
      Gen gen = findByField(gens, "name", genName);
66  
      if (gen == null) false;
67  
      
68  
      try {
69  
        if (compare(callGen(gen), line))
70  
          true;
71  
      } catch {}
72  
      
73  
      false;
74  
    } finally {
75  
      genLog_clear();
76  
    }
77  
  }
78  
  
79  
  L<Gen> makeGenerators(L<S> log) {
80  
    new L<Gen> gens;
81  
    callF(makeGenerators, gens, log);
82  
    ret gens;
83  
  }
84  
  
85  
  // returns score
86  
  int scoreGenerator(L<S> log, S genName) {
87  
    int score = 0;
88  
    for (int i = 1; i < l(log); i++) {
89  
      S expect = log.get(i), s = null;
90  
      bool ok = false;
91  
      try {
92  
        s = callSingle(subList(log, 0, i), genName);
93  
        ok = compare(s, expect);
94  
      } catch e {
95  
        s = exceptionToStringShort(e);
96  
      }
97  
      if (ok) {
98  
        ++score;
99  
        print(genName + " OK: " + s + (eq(s, expect) ? "" : " / " + expect));
100  
      } else
101  
        print(genName + " NO [" + s + "]: " + expect);
102  
    }
103  
    print();
104  
    ret score;
105  
  }
106  
  
107  
  bool compare(S a, S b) {
108  
    if (eq(comparison, "eq"))
109  
      ret eq(a, b);
110  
    else if (eq(comparison, "eqic"))
111  
      ret eqic(a, b);
112  
    else if (eq(comparison, "match"))
113  
      ret match(a, b);
114  
    else
115  
      throw fail("Unknown comparison: " + comparison);
116  
  }
117  
  
118  
  // run a single generator on all lines and print each line
119  
  void debugSingle(L<S> log, S name) {
120  
    for (int i = 0; i < l(log); i++)
121  
      debugSingle1(subList(log, 0, i+1), name);
122  
  }
123  
  
124  
  void debugSingle1(L<S> log, S genName) {
125  
    S line = last(log);
126  
    log = dropLast(log);
127  
    
128  
    genLog_set(log);
129  
    try {
130  
      L<Gen> gens = makeGenerators(log);
131  
      Gen gen = findByField(gens, "name", genName);
132  
      if (gen == null) ret;
133  
      
134  
      bool ok = false;
135  
      try {
136  
        ok = compare(callGen(gen), line);
137  
      } catch {}
138  
      
139  
      print((ok ? "OK" : "NO") + " " + line);
140  
    } finally {
141  
      genLog_clear();
142  
    }
143  
  }
144  
}

Author comment

Began life as a copy of #1003582

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003606
Snippet name: class GenTesting
Eternal ID of this version: #1003606/1
Text MD5: cf4263d0dc1b92484512e97952d2d0ad
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-03 18:13:55
Source code size: 3547 bytes / 144 lines
Pitched / IR pitched: No / No
Views / Downloads: 626 / 2213
Referenced in: [show references]