summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rinda/test_tuplebag.rb
blob: 3e30127f0f0c583775219b54d81b2ab9407614a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require 'test/unit'
require 'rinda/tuplespace'

class TestTupleBag < Test::Unit::TestCase

  def setup
    @tb = Rinda::TupleBag.new
  end

  def test_delete
    assert_nothing_raised do
      val = @tb.delete tup(:val, 1)
      assert_equal nil, val
    end

    t = tup(:val, 1)
    @tb.push t

    val = @tb.delete t

    assert_equal t, val

    assert_equal [], @tb.find_all(tem(:val, 1))

    t1 = tup(:val, 1)
    t2 = tup(:val, 1)
    @tb.push t1
    @tb.push t2

    val = @tb.delete t1

    assert_equal t1, val

    assert_equal [t2], @tb.find_all(tem(:val, 1))
  end

  def test_delete_unless_alive
    assert_equal [], @tb.delete_unless_alive

    t1 = tup(:val, nil)
    t2 = tup(:val, nil)

    @tb.push t1
    @tb.push t2

    assert_equal [], @tb.delete_unless_alive

    t1.cancel

    assert_equal [t1], @tb.delete_unless_alive, 'canceled'

    t2.renew Object.new

    assert_equal [t2], @tb.delete_unless_alive, 'expired'
  end

  def test_find
    template = tem(:val, nil)

    assert_equal nil, @tb.find(template)

    t1 = tup(:other, 1)
    @tb.push t1

    assert_equal nil, @tb.find(template)

    t2 = tup(:val, 1)
    @tb.push t2

    assert_equal t2, @tb.find(template)

    t2.cancel

    assert_equal nil, @tb.find(template), 'canceled'

    t3 = tup(:val, 3)
    @tb.push t3

    assert_equal t3, @tb.find(template)

    t3.renew Object.new

    assert_equal nil, @tb.find(template), 'expired'
  end

  def test_find_all
    template = tem(:val, nil)

    t1 = tup(:other, 1)
    @tb.push t1

    assert_equal [], @tb.find_all(template)

    t2 = tup(:val, 2)
    t3 = tup(:val, 3)

    @tb.push t2
    @tb.push t3

    assert_equal [t2, t3], @tb.find_all(template)

    t2.cancel

    assert_equal [t3], @tb.find_all(template), 'canceled'

    t3.renew Object.new

    assert_equal [], @tb.find_all(template), 'expired'
  end

  def test_find_all_template
    tuple = tup(:val, 1)

    t1 = tem(:other, nil)
    @tb.push t1

    assert_equal [], @tb.find_all_template(tuple)

    t2 = tem(:val, nil)
    t3 = tem(:val, nil)

    @tb.push t2
    @tb.push t3

    assert_equal [t2, t3], @tb.find_all_template(tuple)

    t2.cancel

    assert_equal [t3], @tb.find_all_template(tuple), 'canceled'

    t3.renew Object.new

    assert_equal [], @tb.find_all_template(tuple), 'expired'
  end

  def test_has_expires_eh
    assert !@tb.has_expires?

    t = tup(:val, 1)
    @tb.push t

    assert @tb.has_expires?

    t.renew Object.new

    assert !@tb.has_expires?
  end

  def test_push
    t = tup(:val, 1)

    @tb.push t

    assert_equal t, @tb.find(tem(:val, 1))
  end

  ##
  # Create a tuple with +ary+ for its contents

  def tup(*ary)
    Rinda::TupleEntry.new ary
  end

  ##
  # Create a template with +ary+ for its contents

  def tem(*ary)
    Rinda::TemplateEntry.new ary
  end

end