linux-listener-stats: favor narrower display if possible
[raindrops.git] / test / test_raindrops.rb
blob66867969427fd42a1a9dbdf910b4bd0a2950d31e
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_size
20     rd = Raindrops.new(4)
21     assert_equal 4, rd.size
22   end
24   def test_ary
25     rd = Raindrops.new(4)
26     assert_equal [0, 0, 0, 0] , rd.to_ary
27   end
29   def test_incr_no_args
30     rd = Raindrops.new(4)
31     assert_equal 1, rd.incr(0)
32     assert_equal [1, 0, 0, 0], rd.to_ary
33   end
35   def test_incr_args
36     rd = Raindrops.new(4)
37     assert_equal 6, rd.incr(3, 6)
38     assert_equal [0, 0, 0, 6], rd.to_ary
39   end
41   def test_decr_args
42     rd = Raindrops.new(4)
43     rd[3] = 6
44     assert_equal 5, rd.decr(3, 1)
45     assert_equal [0, 0, 0, 5], rd.to_ary
46   end
48   def test_incr_shared
49     rd = Raindrops.new(2)
50     5.times do
51       pid = fork { rd.incr(1) }
52       _, status = Process.waitpid2(pid)
53       assert status.success?
54     end
55     assert_equal [0, 5], rd.to_ary
56   end
58   def test_incr_decr
59     rd = Raindrops.new(1)
60     fork { 1000000.times { rd.incr(0) } }
61     1000.times { rd.decr(0) }
62     statuses = Process.waitall
63     statuses.each { |pid, status| assert status.success? }
64     assert_equal [999000], rd.to_ary
65   end
67   def test_bad_incr
68     rd = Raindrops.new(1)
69     assert_raises(ArgumentError) { rd.incr(-1) }
70     assert_raises(ArgumentError) { rd.incr(2) }
71     assert_raises(ArgumentError) { rd.incr(0xffffffff) }
72   end
74   def test_dup
75     @rd = Raindrops.new(1)
76     rd = @rd.dup
77     assert_equal 1, @rd.incr(0)
78     assert_equal 1, rd.incr(0)
79     assert_equal 2, rd.incr(0)
80     assert_equal 2, rd[0]
81     assert_equal 1, @rd[0]
82   end
84   def test_clone
85     @rd = Raindrops.new(1)
86     rd = @rd.clone
87     assert_equal 1, @rd.incr(0)
88     assert_equal 1, rd.incr(0)
89     assert_equal 2, rd.incr(0)
90     assert_equal 2, rd[0]
91     assert_equal 1, @rd[0]
92   end
94   def test_big
95     expect = (1..256).map { 0 }
96     rd = Raindrops.new(256)
97     assert_equal expect, rd.to_ary
98     assert_nothing_raised { rd[255] = 5 }
99     assert_equal 5, rd[255]
100     assert_nothing_raised { rd[2] = 2 }
102     expect[255] = 5
103     expect[2] = 2
104     assert_equal expect, rd.to_ary
105   end