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

134
LINES

< > BotCompany Repo | #1031665 // ListTester - successor of test_List - test general functions of a List implementation

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6687L/43K).

// pass an empty instance
sclass ListTester {
  LS l;
  
  *(LS *l) {}
  *(LS *l, IF1<LS> *rotate) {}

  // parameters
  
  int n = 1000;
  int idLength = 5;
  
  // optional rotation to/from storagew
  
  int rotateAtLeastEvery = 20;
  swappable LS rotate(LS l) { ret l; }

  // output
  
  int rotationsDone;
  
  // internal
  
  int rotCountdown;
  Random random = predictableRandom();
  
  // Simple test
  
  run {
    assertEqualsVerbose(0, l.size());
    for (int i = -1; i < 2; i++) { int _i = i ; assertException(r { l.get(_i) }); }
    l.add("hello");
    rot();
    assertEqualsVerbose(1, l.size());
    assertException(r { l.get(-1) });
    assertEqualsVerbose("hello", l.get(0));
    assertException(r { l.get(1) });
    
    // n random insertions, complete check
    
    new LS refl;
    l.clear();
    repeat n {
      int i = random(random, l(refl)+1);
      S id = randomID(random, idLength);
      print("add " + i + " " + id);
      refl.add(i, id);
      l.add(i, id);
      rot();
      assertEquals(l(l), l(refl));
    }
    assertEqualsVerbose(l(l), l(refl));
    for i over refl:
      assertEquals(l.get(i), refl.get(i));
      
    // overwriting
    repeat n {
      int i = random(random, l(refl));
  
      S id = randomID(random);
      print("set " + i + " " + id);
      assertEquals(l.set(i, id), refl.set(i, id));
      assertEquals(l(l), l(refl));
      rot();
    }
      
    // n random deletions, check after each turn
    repeat n {
      int i = random(random, l(refl));
      print("remove " + i);
      assertEquals(l.remove(i), refl.remove(i));
      assertEqualsVerbose(l(l), l(refl));
      rot();
      for j over refl:
        assertEquals(l.get(j), refl.get(j));
    }
  
    test2();
  }
  
  void test2 {
    assertIndexOfAndContainsVerbose(l, "a", -1);
    l.add("a");
    rot();
    assertIndexOfAndContainsVerbose(l, "a", 0);
    assertIndexOfAndContainsVerbose(l, "b", -1);
    l.add("b");
    rot();
    assertIndexOfAndContainsVerbose(l, "a", 0);
    assertIndexOfAndContainsVerbose(l, "b", 1);
    l.add("a");
    rot();
    assertIndexOfAndContainsVerbose(l, "a", 0);
    l.remove(2);
    rot();
    assertIndexOfAndContainsVerbose(l, "a", 0);
    l.remove("a");
    rot();
    assertIndexOfAndContainsVerbose(l, "a", -1);
    assertIndexOfAndContainsVerbose(l, "b", 0);
    
    l.clear();
    rot();
    
    addAll(l, "a", "b", "c", "d");
    rot();
    l.subList(1, 3).clear();
    rot();
    assertEquals(ll("a", "d"), cloneList(l));
    
    LS sub = l.subList(1, 2);
    sub.set(0, "x");
    rot();
    assertEquals(1, sub.size()); // check modCount handling
    
    l.clear();
    rot();
    
    ok();
  }
  
  void ok {
    infoBox(className(l) + " works (tested up to size " + n + ")! :)");
  }
  
  void rot {
    if (rotCountdown <= 0)
      rotCountdown = random(random, rotateAtLeastEvery)+1;
    if (--rotCountdown <= 0) {
      l = rotate(l);
      rotationsDone++;
    }
  }
}

Author comment

Began life as a copy of #1025482

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031665
Snippet name: ListTester - successor of test_List - test general functions of a List implementation
Eternal ID of this version: #1031665/12
Text MD5: a1fc1362bce172d2aa37ffd2ecf9e00e
Transpilation MD5: 85e37813c672fd42fab7c3c83c7de83c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-25 09:57:30
Source code size: 3090 bytes / 134 lines
Pitched / IR pitched: No / No
Views / Downloads: 167 / 340
Version history: 11 change(s)
Referenced in: [show references]