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

51
LINES

< > BotCompany Repo | #1031511 // test_ProbabilisticList (TODO: reflect new policy of replacing elements)

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

Transpiled version (5483L) is out of date.

1  
sclass Test_ProbabilisticList {
2  
  MultiSetMap<Double, S> a = multiSetMap_outerDescTreeMap_innerBetterLinkedHashSet();
3  
  new ProbabilisticList<S> b;
4  
5  
  int nProbabilities = 4;
6  
  int nObjects = 10;
7  
  int n = 1000;
8  
  
9  
  WithProbability<S> makeEntry() {
10  
    ret withProbability(
11  
      (double) random(nProbabilities),
12  
      str(charPlus('A', random(nObjects)));
13  
  }
14  
  
15  
  run {
16  
    repeat n {
17  
      // add one
18  
      WithProbability<S> e = makeEntry();
19  
      print("Adding " + e);
20  
      
21  
      if (e.probability() > 0) // simulate default cut-off at probability 0
22  
      // Simulate the right behavior for adding same object again
23  
        if (!a.contains(e.probability(), e!)) {
24  
          a.remove(a.keyForValue(e!), e!);
25  
            a.put(e.probability(), e!);
26  
        }
27  
      
28  
      b.add(e); // automatically overwrites the old probability
29  
      
30  
      check();
31  
      
32  
      // remove one
33  
      e = makeEntry(); // probability doesn't matter
34  
      print("Removing " + e!);
35  
      a.remove(a.keyForValue(e!), e!);
36  
      b.remove(e);
37  
      
38  
      check();
39  
    }
40  
  }
41  
    
42  
  void check {
43  
    LPair<Double, S> aEntries = asList(a.allEntries());
44  
    print(takeFirst(10, aEntries);
45  
    assertEquals(mapPairsToList(aEntries, (p, x) -> WithProbability(p, x)), asList(b.iterator());
46  
  }
47  
}
48  
49  
svoid test_ProbabilisticList() {
50  
  new Test_ProbabilisticList().run();
51  
}

Author comment

Began life as a copy of #1031508

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1031511
Snippet name: test_ProbabilisticList (TODO: reflect new policy of replacing elements)
Eternal ID of this version: #1031511/18
Text MD5: 4fa1fb796d7fa73135f2443617f0f99f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-30 16:40:21
Source code size: 1397 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 124 / 249
Version history: 17 change(s)
Referenced in: [show references]