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)

1  
// pass an empty instance
2  
sclass MapTester {
3  
  SS map;
4  
  
5  
  *(SS *map) {}
6  
  *(SS *map, IF1<SS> *rotate) {}
7  
8  
  // parameters
9  
  
10  
  int n = 1000;
11  
  int idLength = 5;
12  
  
13  
  // optional rotation to/from storage
14  
  
15  
  int rotateAtLeastEvery = 20;
16  
  swappable SS rotate(SS map) { ret map; }
17  
18  
  // output
19  
  
20  
  int rotationsDone;
21  
  
22  
  // internal
23  
  
24  
  int rotCountdown;
25  
  Random random = predictableRandom();
26  
  
27  
  // Simple test
28  
  
29  
  run {
30  
    assertEqualsVerbose(0, map.size());
31  
    for (int i = -1; i < 2; i++) { int _i = i ; assertException(r { map.get(_i) }); }
32  
    map.add("hello");
33  
    rot();
34  
    assertEqualsVerbose(1, map.size());
35  
    assertException(r { map.get(-1) });
36  
    assertEqualsVerbose("hello", map.get(0));
37  
    assertException(r { map.get(1) });
38  
    
39  
    // n random insertions, complete check
40  
    
41  
    new SS refl;
42  
    map.clear();
43  
    repeat n {
44  
      int i = random(random, l(refl)+1);
45  
      S id = randomID(random, idLength);
46  
      print("add " + i + " " + id);
47  
      refl.add(i, id);
48  
      map.add(i, id);
49  
      rot();
50  
      assertEquals(l(map), l(refl));
51  
    }
52  
    assertEqualsVerbose(l(map), l(refl));
53  
    for i over refl:
54  
      assertEquals(map.get(i), refl.get(i));
55  
      
56  
    // overwriting
57  
    repeat n {
58  
      int i = random(random, l(refl));
59  
  
60  
      S id = randomID(random);
61  
      print("set " + i + " " + id);
62  
      assertEquals(map.set(i, id), refl.set(i, id));
63  
      assertEquals(l(map), l(refl));
64  
      rot();
65  
    }
66  
      
67  
    // n random deletions, check after each turn
68  
    repeat n {
69  
      int i = random(random, l(refl));
70  
      print("remove " + i);
71  
      assertEquals(map.remove(i), refl.remove(i));
72  
      assertEqualsVerbose(l(map), l(refl));
73  
      rot();
74  
      for j over refl:
75  
        assertEquals(map.get(j), refl.get(j));
76  
    }
77  
  }
78  
  
79  
  void ok {
80  
    infoBox(className(map) + " works (tested up to size " + n + ")! :)");
81  
  }
82  
  
83  
  void rot {
84  
    if (rotCountdown <= 0)
85  
      rotCountdown = random(random, rotateAtLeastEvery)+1;
86  
    if (--rotCountdown <= 0) {
87  
      map = rotate(map);
88  
      rotationsDone++;
89  
    }
90  
  }
91  
}

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: 135 / 156
Version history: 2 change(s)
Referenced in: [show references]