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

54
LINES

< > BotCompany Repo | #1003920 // Test Bracket Hygiene

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (947L/7K/23K).

!752

p {
  test("{[(a)]}", true); // ok
  test("[(a)"); // not closed
  test("a)"); // not opened
  test("(a)}"); // not opened
  test("(a]"); // mismatch
  print("\nALL TESTS OK");
}

static void test(S s) {
  test(s, false);
}

static void test(S s, bool ok) {
  assertEquals(s, ok, testImpl(s));
  print("ok");
}

static bool testImpl(S s) {
  print();
  print(s);
  
  // not <> as they are ambiguous (< is also a comparison operator)
  S op = "([{", close = ")]}";
  
  L<S> tok = javaTok(s);
  new Map<Int, Int> map;
  L<int> stack = getBracketMap2(tok, map, op, close);
  if (nempty(stack)) {
    print("Bad hygiene - " + n(l(stack), "bracket") + " not closed");
    false;
  }
  
  if (map.containsKey(0)) {
    print("Bad hygiene - bracket not opened (" + quote(tok.get(map.get(0))) + ")");
    false;
  }
  
  for (int i : keys(map)) {
    int j = map.get(i);
    S a = tok.get(i), b = tok.get(j);
    //print("i=" + i + ", j= " + j + ", a=" + quote(a) + ", b=" + quote(b));
    int ai = op.indexOf(a), bi = close.indexOf(b);
    if (ai != bi) {
      print("Bad hygiene - brackets don't match (" + quote(a) + " vs " + quote(b) + ")");
      false;
    }
  }
  
  print("Hygiene OK!");
  true;
}

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: #1003920
Snippet name: Test Bracket Hygiene
Eternal ID of this version: #1003920/1
Text MD5: f582b731637739a471f757b33bc5344d
Transpilation MD5: bc5b676a9adb56399b2e164a12108c21
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-30 18:30:17
Source code size: 1259 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 568 / 640
Referenced in: #1003922 - testBracketHygiene - uses Java brackets (except <>) and javaTok
#1003923 - Test testBracketHygiene