1 # -*- encoding: binary -*-
5 class TestRaindrops < Test::Unit::TestCase
7 def test_raindrop_counter_max
8 assert_kind_of Integer, Raindrops::MAX
9 assert Raindrops::MAX > 0
10 printf "Raindrops::MAX = 0x%x\n", Raindrops::MAX
13 def test_raindrop_size
14 assert_kind_of Integer, Raindrops::SIZE
15 assert Raindrops::SIZE > 0
16 puts "Raindrops::SIZE = #{Raindrops::SIZE}"
20 assert_kind_of Integer, Raindrops::PAGE_SIZE
21 assert Raindrops::PAGE_SIZE > Raindrops::SIZE
24 def test_size_and_capa
26 assert_equal 4, rd.size
27 assert rd.capa >= rd.size
32 assert_equal [0, 0, 0, 0] , rd.to_ary
37 assert_equal 1, rd.incr(0)
38 assert_equal [1, 0, 0, 0], rd.to_ary
43 assert_equal 6, rd.incr(3, 6)
44 assert_equal [0, 0, 0, 6], rd.to_ary
50 assert_equal 5, rd.decr(3, 1)
51 assert_equal [0, 0, 0, 5], rd.to_ary
57 pid = fork { rd.incr(1) }
58 _, status = Process.waitpid2(pid)
59 assert status.success?
61 assert_equal [0, 5], rd.to_ary
66 fork { 1000000.times { rd.incr(0) } }
67 1000.times { rd.decr(0) }
68 statuses = Process.waitall
69 statuses.each { |pid, status| assert status.success? }
70 assert_equal [999000], rd.to_ary
75 assert_raises(ArgumentError) { rd.incr(-1) }
76 assert_raises(ArgumentError) { rd.incr(2) }
77 assert_raises(ArgumentError) { rd.incr(0xffffffff) }
81 @rd = Raindrops.new(1)
83 assert_equal 1, @rd.incr(0)
84 assert_equal 1, rd.incr(0)
85 assert_equal 2, rd.incr(0)
87 assert_equal 1, @rd[0]
91 @rd = Raindrops.new(1)
93 assert_equal 1, @rd.incr(0)
94 assert_equal 1, rd.incr(0)
95 assert_equal 2, rd.incr(0)
97 assert_equal 1, @rd[0]
101 expect = (1..256).map { 0 }
102 rd = Raindrops.new(256)
103 assert_equal expect, rd.to_ary
104 assert_nothing_raised { rd[255] = 5 }
105 assert_equal 5, rd[255]
106 assert_nothing_raised { rd[2] = 2 }
110 assert_equal expect, rd.to_ary
114 rd = Raindrops.new(4)
115 assert_equal 4, rd.size
116 assert_equal rd.capa, rd.size = rd.capa
117 assert_equal rd.capa, rd.to_ary.size
118 assert_equal 0, rd[rd.capa - 1]
119 assert_equal 1, rd.incr(rd.capa - 1)
120 assert_raises(ArgumentError) { rd[rd.capa] }
123 def test_resize_mremap
124 rd = Raindrops.new(4)
125 assert_equal 4, rd.size
127 rd.size = rd.capa + 1
128 assert_equal old_capa * 2, rd.capa
130 # mremap() is currently broken with MAP_SHARED
131 # https://bugzilla.kernel.org/show_bug.cgi?id=8691
132 assert_equal 0, rd[old_capa]
133 assert_equal rd.capa, rd.to_ary.size
134 assert_equal 0, rd[rd.capa - 1]
135 assert_equal 1, rd.incr(rd.capa - 1)
136 assert_raises(ArgumentError) { rd[rd.capa] }
138 end # if RUBY_PLATFORM =~ /linux/
142 assert_nil rd.evaporate!
143 assert_raises(StandardError) { rd.evaporate! }
146 def test_evaporate_with_fork
147 tmp = Raindrops.new 2
150 exit(tmp.evaporate! == nil)
152 _, status = Process.waitpid2(pid)
153 assert status.success?
154 assert_equal [ 1, 0 ], tmp.to_ary
156 assert_equal [ 1, 1 ], tmp.to_ary
159 exit([ 1, 2 ] == tmp.to_ary)
161 _, status = Process.waitpid2(pid)
162 assert status.success?
163 assert_equal [ 1, 2 ], tmp.to_ary