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).

1  
// pass an empty instance
2  
sclass ListTester {
3  
  LS l;
4  
  
5  
  *(LS *l) {}
6  
  *(LS *l, IF1<LS> *rotate) {}
7  
8  
  // parameters
9  
  
10  
  int n = 1000;
11  
  int idLength = 5;
12  
  
13  
  // optional rotation to/from storagew
14  
  
15  
  int rotateAtLeastEvery = 20;
16  
  swappable LS rotate(LS l) { ret l; }
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, l.size());
31  
    for (int i = -1; i < 2; i++) { int _i = i ; assertException(r { l.get(_i) }); }
32  
    l.add("hello");
33  
    rot();
34  
    assertEqualsVerbose(1, l.size());
35  
    assertException(r { l.get(-1) });
36  
    assertEqualsVerbose("hello", l.get(0));
37  
    assertException(r { l.get(1) });
38  
    
39  
    // n random insertions, complete check
40  
    
41  
    new LS refl;
42  
    l.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  
      l.add(i, id);
49  
      rot();
50  
      assertEquals(l(l), l(refl));
51  
    }
52  
    assertEqualsVerbose(l(l), l(refl));
53  
    for i over refl:
54  
      assertEquals(l.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(l.set(i, id), refl.set(i, id));
63  
      assertEquals(l(l), 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(l.remove(i), refl.remove(i));
72  
      assertEqualsVerbose(l(l), l(refl));
73  
      rot();
74  
      for j over refl:
75  
        assertEquals(l.get(j), refl.get(j));
76  
    }
77  
  
78  
    test2();
79  
  }
80  
  
81  
  void test2 {
82  
    assertIndexOfAndContainsVerbose(l, "a", -1);
83  
    l.add("a");
84  
    rot();
85  
    assertIndexOfAndContainsVerbose(l, "a", 0);
86  
    assertIndexOfAndContainsVerbose(l, "b", -1);
87  
    l.add("b");
88  
    rot();
89  
    assertIndexOfAndContainsVerbose(l, "a", 0);
90  
    assertIndexOfAndContainsVerbose(l, "b", 1);
91  
    l.add("a");
92  
    rot();
93  
    assertIndexOfAndContainsVerbose(l, "a", 0);
94  
    l.remove(2);
95  
    rot();
96  
    assertIndexOfAndContainsVerbose(l, "a", 0);
97  
    l.remove("a");
98  
    rot();
99  
    assertIndexOfAndContainsVerbose(l, "a", -1);
100  
    assertIndexOfAndContainsVerbose(l, "b", 0);
101  
    
102  
    l.clear();
103  
    rot();
104  
    
105  
    addAll(l, "a", "b", "c", "d");
106  
    rot();
107  
    l.subList(1, 3).clear();
108  
    rot();
109  
    assertEquals(ll("a", "d"), cloneList(l));
110  
    
111  
    LS sub = l.subList(1, 2);
112  
    sub.set(0, "x");
113  
    rot();
114  
    assertEquals(1, sub.size()); // check modCount handling
115  
    
116  
    l.clear();
117  
    rot();
118  
    
119  
    ok();
120  
  }
121  
  
122  
  void ok {
123  
    infoBox(className(l) + " works (tested up to size " + n + ")! :)");
124  
  }
125  
  
126  
  void rot {
127  
    if (rotCountdown <= 0)
128  
      rotCountdown = random(random, rotateAtLeastEvery)+1;
129  
    if (--rotCountdown <= 0) {
130  
      l = rotate(l);
131  
      rotationsDone++;
132  
    }
133  
  }
134  
}

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: 175 / 350
Version history: 11 change(s)
Referenced in: [show references]