initial
[raindrops.git] / test / test_raindrops.rb
blob66fc208be6254cde121350799585ee97e9c9dedf
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'raindrops'
5 class TestRaindrops < Test::Unit::TestCase
7   def test_size
8     rd = Raindrops.new(4)
9     assert_equal 4, rd.size
10   end
12   def test_ary
13     rd = Raindrops.new(4)
14     assert_equal [0, 0, 0, 0] , rd.to_ary
15   end
17   def test_incr_no_args
18     rd = Raindrops.new(4)
19     assert_equal 1, rd.incr(0)
20     assert_equal [1, 0, 0, 0], rd.to_ary
21   end
23   def test_incr_args
24     rd = Raindrops.new(4)
25     assert_equal 6, rd.incr(3, 6)
26     assert_equal [0, 0, 0, 6], rd.to_ary
27   end
29   def test_decr_args
30     rd = Raindrops.new(4)
31     rd[3] = 6
32     assert_equal 5, rd.decr(3, 1)
33     assert_equal [0, 0, 0, 5], rd.to_ary
34   end
36   def test_incr_shared
37     rd = Raindrops.new(2)
38     5.times do
39       pid = fork { rd.incr(1) }
40       _, status = Process.waitpid2(pid)
41       assert status.success?
42     end
43     assert_equal [0, 5], rd.to_ary
44   end
46   def test_incr_decr
47     rd = Raindrops.new(1)
48     fork { 1000000.times { rd.incr(0) } }
49     1000.times { rd.decr(0) }
50     statuses = Process.waitall
51     statuses.each { |pid, status| assert status.success? }
52     assert_equal [999000], rd.to_ary
53   end
55   def test_bad_incr
56     rd = Raindrops.new(1)
57     assert_raises(ArgumentError) { rd.incr(-1) }
58     assert_raises(ArgumentError) { rd.incr(2) }
59     assert_raises(ArgumentError) { rd.incr(0xffffffff) }
60   end
62   def test_dup
63     @rd = Raindrops.new(1)
64     rd = @rd.dup
65     assert_equal 1, @rd.incr(0)
66     assert_equal 1, rd.incr(0)
67     assert_equal 2, rd.incr(0)
68     assert_equal 2, rd[0]
69     assert_equal 1, @rd[0]
70   end
72   def test_clone
73     @rd = Raindrops.new(1)
74     rd = @rd.clone
75     assert_equal 1, @rd.incr(0)
76     assert_equal 1, rd.incr(0)
77     assert_equal 2, rd.incr(0)
78     assert_equal 2, rd[0]
79     assert_equal 1, @rd[0]
80   end
82   def test_big
83     expect = 256.times.map { 0 }
84     rd = Raindrops.new(256)
85     assert_equal expect, rd.to_ary
86     assert_nothing_raised { rd[255] = 5 }
87     assert_equal 5, rd[255]
88     assert_nothing_raised { rd[2] = 2 }
90     expect[255] = 5
91     expect[2] = 2
92     assert_equal expect, rd.to_ary
93   end
95 end