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

144
LINES

< > BotCompany Repo | #1003606 // class GenTesting

JavaX fragment (include)

sclass GenTesting {
  O makeGenerators; // voidfunc(L<Gen> gens, L<S> log)
  
  // method to compare generator output & user line
  S comparison = "eqic";
  
  *(O *makeGenerators) {}

  MultiSet<S> scoreGenerators(L<S> log) {
    ret scoreGenerators(log, null);
  }
  
  MultiSet<S> scoreGenerators(L<S> log, BitSet interestingLines) {
    new MultiSet<S> scores;
    for (int i = 0; i < l(log); i++)
      if (interestingLines == null || interestingLines.get(i))
        scoreGenerators1(subList(log, 0, i+1), scores);
    print(/*asciiHeading2("SCORES")*/);
    for (S name : scores.getTopTen())
      print("  [" + scores.get(name) + "] " + name);
    print();
    ret scores;
  }
    
  void scoreGenerators1(L<S> log, MultiSet<S> scores) {
    if (empty(log)) ret;
    S line = last(log);
    log = dropLast(log);
    
    genLog_set(log);
    try {
      L<Gen> gens = makeGenerators(log);
      
      for (Gen gen : gens) {
        try {
          if (compare(callGen(gen), line))
            scores.add(gen.name);
        } catch {}
      }
    } finally {
      genLog_clear();
    }
  }
  
  S callSingle(L<S> log, O genName) {
    genLog_set(log);
    try {
      L<Gen> gens = makeGenerators(log);
      Gen gen = findByField(gens, "name", genName);
      if (gen == null) null;
      ret callGen(gen);
    } finally {
      genLog_clear();
    }
  }
  
  bool verifySingle(L<S> log, O genName) {
    if (empty(log)) false;
    S line = last(log);
    log = dropLast(log);
    
    genLog_set(log);
    try {
      L<Gen> gens = makeGenerators(log);
      Gen gen = findByField(gens, "name", genName);
      if (gen == null) false;
      
      try {
        if (compare(callGen(gen), line))
          true;
      } catch {}
      
      false;
    } finally {
      genLog_clear();
    }
  }
  
  L<Gen> makeGenerators(L<S> log) {
    new L<Gen> gens;
    callF(makeGenerators, gens, log);
    ret gens;
  }
  
  // returns score
  int scoreGenerator(L<S> log, S genName) {
    int score = 0;
    for (int i = 1; i < l(log); i++) {
      S expect = log.get(i), s = null;
      bool ok = false;
      try {
        s = callSingle(subList(log, 0, i), genName);
        ok = compare(s, expect);
      } catch e {
        s = exceptionToStringShort(e);
      }
      if (ok) {
        ++score;
        print(genName + " OK: " + s + (eq(s, expect) ? "" : " / " + expect));
      } else
        print(genName + " NO [" + s + "]: " + expect);
    }
    print();
    ret score;
  }
  
  bool compare(S a, S b) {
    if (eq(comparison, "eq"))
      ret eq(a, b);
    else if (eq(comparison, "eqic"))
      ret eqic(a, b);
    else if (eq(comparison, "match"))
      ret match(a, b);
    else
      throw fail("Unknown comparison: " + comparison);
  }
  
  // run a single generator on all lines and print each line
  void debugSingle(L<S> log, S name) {
    for (int i = 0; i < l(log); i++)
      debugSingle1(subList(log, 0, i+1), name);
  }
  
  void debugSingle1(L<S> log, S genName) {
    S line = last(log);
    log = dropLast(log);
    
    genLog_set(log);
    try {
      L<Gen> gens = makeGenerators(log);
      Gen gen = findByField(gens, "name", genName);
      if (gen == null) ret;
      
      bool ok = false;
      try {
        ok = compare(callGen(gen), line);
      } catch {}
      
      print((ok ? "OK" : "NO") + " " + line);
    } finally {
      genLog_clear();
    }
  }
}

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: 520 / 2117
Referenced in: [show references]