~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to test/files/run/ctries-old/concmap.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
 
 
4
import collection.concurrent.TrieMap
 
5
 
 
6
 
 
7
object ConcurrentMapSpec extends Spec {
 
8
  
 
9
  val initsz = 500
 
10
  val secondsz = 750
 
11
  
 
12
  def test() {
 
13
    "support put" in {
 
14
      val ct = new TrieMap[Wrap, Int]
 
15
      for (i <- 0 until initsz) assert(ct.put(new Wrap(i), i) == None)
 
16
      for (i <- 0 until initsz) assert(ct.put(new Wrap(i), -i) == Some(i))
 
17
    }
 
18
    
 
19
    "support put if absent" in {
 
20
      val ct = new TrieMap[Wrap, Int]
 
21
      for (i <- 0 until initsz) ct.update(new Wrap(i), i)
 
22
      for (i <- 0 until initsz) assert(ct.putIfAbsent(new Wrap(i), -i) == Some(i))
 
23
      for (i <- 0 until initsz) assert(ct.putIfAbsent(new Wrap(i), -i) == Some(i))
 
24
      for (i <- initsz until secondsz) assert(ct.putIfAbsent(new Wrap(i), -i) == None)
 
25
      for (i <- initsz until secondsz) assert(ct.putIfAbsent(new Wrap(i), i) == Some(-i))
 
26
    }
 
27
    
 
28
    "support remove if mapped to a specific value" in {
 
29
      val ct = new TrieMap[Wrap, Int]
 
30
      for (i <- 0 until initsz) ct.update(new Wrap(i), i)
 
31
      for (i <- 0 until initsz) assert(ct.remove(new Wrap(i), -i - 1) == false)
 
32
      for (i <- 0 until initsz) assert(ct.remove(new Wrap(i), i) == true)
 
33
      for (i <- 0 until initsz) assert(ct.remove(new Wrap(i), i) == false)
 
34
    }
 
35
    
 
36
    "support replace if mapped to a specific value" in {
 
37
      val ct = new TrieMap[Wrap, Int]
 
38
      for (i <- 0 until initsz) ct.update(new Wrap(i), i)
 
39
      for (i <- 0 until initsz) assert(ct.replace(new Wrap(i), -i - 1, -i - 2) == false)
 
40
      for (i <- 0 until initsz) assert(ct.replace(new Wrap(i), i, -i - 2) == true)
 
41
      for (i <- 0 until initsz) assert(ct.replace(new Wrap(i), i, -i - 2) == false)
 
42
      for (i <- initsz until secondsz) assert(ct.replace(new Wrap(i), i, 0) == false)
 
43
    }
 
44
    
 
45
    "support replace if present" in {
 
46
      val ct = new TrieMap[Wrap, Int]
 
47
      for (i <- 0 until initsz) ct.update(new Wrap(i), i)
 
48
      for (i <- 0 until initsz) assert(ct.replace(new Wrap(i), -i) == Some(i))
 
49
      for (i <- 0 until initsz) assert(ct.replace(new Wrap(i), i) == Some(-i))
 
50
      for (i <- initsz until secondsz) assert(ct.replace(new Wrap(i), i) == None)
 
51
    }
 
52
    
 
53
    def assertEqual(a: Any, b: Any) = {
 
54
      if (a != b) println(a, b)
 
55
      assert(a == b)
 
56
    }
 
57
    
 
58
    "support replace if mapped to a specific value, using several threads" in {
 
59
      val ct = new TrieMap[Wrap, Int]
 
60
      val sz = 55000
 
61
      for (i <- 0 until sz) ct.update(new Wrap(i), i)
 
62
      
 
63
      class Updater(index: Int, offs: Int) extends Thread {
 
64
        override def run() {
 
65
          var repeats = 0
 
66
          for (i <- 0 until sz) {
 
67
            val j = (offs + i) % sz
 
68
            var k = Int.MaxValue
 
69
            do {
 
70
              if (k != Int.MaxValue) repeats += 1
 
71
              k = ct.lookup(new Wrap(j))
 
72
            } while (!ct.replace(new Wrap(j), k, -k))
 
73
          }
 
74
          //println("Thread %d repeats: %d".format(index, repeats))
 
75
        }
 
76
      }
 
77
      
 
78
      val threads = for (i <- 0 until 16) yield new Updater(i, sz / 32 * i)
 
79
      threads.foreach(_.start())
 
80
      threads.foreach(_.join())
 
81
      
 
82
      for (i <- 0 until sz) assertEqual(ct(new Wrap(i)), i)
 
83
      
 
84
      val threads2 = for (i <- 0 until 15) yield new Updater(i, sz / 32 * i)
 
85
      threads2.foreach(_.start())
 
86
      threads2.foreach(_.join())
 
87
      
 
88
      for (i <- 0 until sz) assertEqual(ct(new Wrap(i)), -i)
 
89
    }
 
90
    
 
91
    "support put if absent, several threads" in {
 
92
      val ct = new TrieMap[Wrap, Int]
 
93
      val sz = 110000
 
94
      
 
95
      class Updater(offs: Int) extends Thread {
 
96
        override def run() {
 
97
          for (i <- 0 until sz) {
 
98
            val j = (offs + i) % sz
 
99
            ct.putIfAbsent(new Wrap(j), j)
 
100
            assert(ct.lookup(new Wrap(j)) == j)
 
101
          }
 
102
        }
 
103
      }
 
104
      
 
105
      val threads = for (i <- 0 until 16) yield new Updater(sz / 32 * i)
 
106
      threads.foreach(_.start())
 
107
      threads.foreach(_.join())
 
108
      
 
109
      for (i <- 0 until sz) assert(ct(new Wrap(i)) == i)
 
110
    }
 
111
    
 
112
    "support remove if mapped to a specific value, several threads" in {
 
113
      val ct = new TrieMap[Wrap, Int]
 
114
      val sz = 55000
 
115
      for (i <- 0 until sz) ct.update(new Wrap(i), i)
 
116
      
 
117
      class Remover(offs: Int) extends Thread {
 
118
        override def run() {
 
119
          for (i <- 0 until sz) {
 
120
            val j = (offs + i) % sz
 
121
            ct.remove(new Wrap(j), j)
 
122
            assert(ct.get(new Wrap(j)) == None)
 
123
          }
 
124
        }
 
125
      }
 
126
      
 
127
      val threads = for (i <- 0 until 16) yield new Remover(sz / 32 * i)
 
128
      threads.foreach(_.start())
 
129
      threads.foreach(_.join())
 
130
      
 
131
      for (i <- 0 until sz) assert(ct.get(new Wrap(i)) == None)
 
132
    }
 
133
    
 
134
    "have all or none of the elements depending on the oddity" in {
 
135
      val ct = new TrieMap[Wrap, Int]
 
136
      val sz = 65000
 
137
      for (i <- 0 until sz) ct(new Wrap(i)) = i
 
138
      
 
139
      class Modifier(index: Int, offs: Int) extends Thread {
 
140
        override def run() {
 
141
          for (j <- 0 until sz) {
 
142
            val i = (offs + j) % sz
 
143
            var success = false
 
144
            do {
 
145
              if (ct.contains(new Wrap(i))) {
 
146
                success = ct.remove(new Wrap(i)) != None
 
147
              } else {
 
148
                success = ct.putIfAbsent(new Wrap(i), i) == None
 
149
              }
 
150
            } while (!success)
 
151
          }
 
152
        }
 
153
      }
 
154
      
 
155
      def modify(n: Int) = {
 
156
        val threads = for (i <- 0 until n) yield new Modifier(i, sz / n * i)
 
157
        threads.foreach(_.start())
 
158
        threads.foreach(_.join())
 
159
      }
 
160
      
 
161
      modify(16)
 
162
      for (i <- 0 until sz) assertEqual(ct.get(new Wrap(i)), Some(i))
 
163
      modify(15)
 
164
      for (i <- 0 until sz) assertEqual(ct.get(new Wrap(i)), None)
 
165
    }
 
166
    
 
167
    "compute size correctly" in {
 
168
      val ct = new TrieMap[Wrap, Int]
 
169
      val sz = 36450
 
170
      for (i <- 0 until sz) ct(new Wrap(i)) = i
 
171
      
 
172
      assertEqual(ct.size, sz)
 
173
      assertEqual(ct.size, sz)
 
174
    }
 
175
    
 
176
    "compute size correctly in parallel" in {
 
177
      val ct = new TrieMap[Wrap, Int]
 
178
      val sz = 36450
 
179
      for (i <- 0 until sz) ct(new Wrap(i)) = i
 
180
      val pct = ct.par
 
181
      
 
182
      assertEqual(pct.size, sz)
 
183
      assertEqual(pct.size, sz)
 
184
    }
 
185
    
 
186
  }
 
187
  
 
188
}