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

91
LINES

< > BotCompany Repo | #1031858 // MapTester - test general functions of a Map implementation [dev.]

JavaX fragment (include)

// pass an empty instance
sclass MapTester {
  SS map;
  
  *(SS *map) {}
  *(SS *map, IF1<SS> *rotate) {}

  // parameters
  
  int n = 1000;
  int idLength = 5;
  
  // optional rotation to/from storage
  
  int rotateAtLeastEvery = 20;
  swappable SS rotate(SS map) { ret map; }

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

Author comment

Began life as a copy of #1031665

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031858
Snippet name: MapTester - test general functions of a Map implementation [dev.]
Eternal ID of this version: #1031858/3
Text MD5: 91088694a87353d26b25421e40dc8b3b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-16 06:42:52
Source code size: 2142 bytes / 91 lines
Pitched / IR pitched: No / No
Views / Downloads: 68 / 86
Version history: 2 change(s)
Referenced in: [show references]