raindrops 0.8.1 - compatibility fixes
[raindrops.git] / test / test_raindrops.rb
blob67089b7f23e39901deb6eaa12b856ad91b5017b6
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'raindrops'
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
11   end
13   def test_raindrop_size
14     assert_kind_of Integer, Raindrops::SIZE
15     assert Raindrops::SIZE > 0
16     puts "Raindrops::SIZE = #{Raindrops::SIZE}"
17   end
19   def test_page_size
20     assert_kind_of Integer, Raindrops::PAGE_SIZE
21     assert Raindrops::PAGE_SIZE > Raindrops::SIZE
22   end
24   def test_size_and_capa
25     rd = Raindrops.new(4)
26     assert_equal 4, rd.size
27     assert rd.capa >= rd.size
28   end
30   def test_ary
31     rd = Raindrops.new(4)
32     assert_equal [0, 0, 0, 0] , rd.to_ary
33   end
35   def test_incr_no_args
36     rd = Raindrops.new(4)
37     assert_equal 1, rd.incr(0)
38     assert_equal [1, 0, 0, 0], rd.to_ary
39   end
41   def test_incr_args
42     rd = Raindrops.new(4)
43     assert_equal 6, rd.incr(3, 6)
44     assert_equal [0, 0, 0, 6], rd.to_ary
45   end
47   def test_decr_args
48     rd = Raindrops.new(4)
49     rd[3] = 6
50     assert_equal 5, rd.decr(3, 1)
51     assert_equal [0, 0, 0, 5], rd.to_ary
52   end
54   def test_incr_shared
55     rd = Raindrops.new(2)
56     5.times do
57       pid = fork { rd.incr(1) }
58       _, status = Process.waitpid2(pid)
59       assert status.success?
60     end
61     assert_equal [0, 5], rd.to_ary
62   end
64   def test_incr_decr
65     rd = Raindrops.new(1)
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
71   end
73   def test_bad_incr
74     rd = Raindrops.new(1)
75     assert_raises(ArgumentError) { rd.incr(-1) }
76     assert_raises(ArgumentError) { rd.incr(2) }
77     assert_raises(ArgumentError) { rd.incr(0xffffffff) }
78   end
80   def test_dup
81     @rd = Raindrops.new(1)
82     rd = @rd.dup
83     assert_equal 1, @rd.incr(0)
84     assert_equal 1, rd.incr(0)
85     assert_equal 2, rd.incr(0)
86     assert_equal 2, rd[0]
87     assert_equal 1, @rd[0]
88   end
90   def test_clone
91     @rd = Raindrops.new(1)
92     rd = @rd.clone
93     assert_equal 1, @rd.incr(0)
94     assert_equal 1, rd.incr(0)
95     assert_equal 2, rd.incr(0)
96     assert_equal 2, rd[0]
97     assert_equal 1, @rd[0]
98   end
100   def test_big
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 }
108     expect[255] = 5
109     expect[2] = 2
110     assert_equal expect, rd.to_ary
111   end
113   def test_resize
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] }
121   end
123   def test_resize_mremap
124     rd = Raindrops.new(4)
125     assert_equal 4, rd.size
126     old_capa = rd.capa
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] }
137     rescue RangeError
138   end # if RUBY_PLATFORM =~ /linux/
140   def test_evaporate
141     rd = Raindrops.new 1
142     assert_nil rd.evaporate!
143     assert_raises(StandardError) { rd.evaporate! }
144   end
146   def test_evaporate_with_fork
147     tmp = Raindrops.new 2
148     pid = fork do
149       tmp.incr 0
150       exit(tmp.evaporate! == nil)
151     end
152     _, status = Process.waitpid2(pid)
153     assert status.success?
154     assert_equal [ 1, 0 ], tmp.to_ary
155     tmp.incr 1
156     assert_equal [ 1, 1 ], tmp.to_ary
157     pid = fork do
158       tmp.incr 1
159       exit([ 1, 2 ] == tmp.to_ary)
160     end
161     _, status = Process.waitpid2(pid)
162     assert status.success?
163     assert_equal [ 1, 2 ], tmp.to_ary
164   end