constraint 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,15 +2,15 @@
2
2
  # tc_contraint.rb
3
3
  # @Author: Thomas Link (samul@web.de)
4
4
  # @Created: 27-M�r-2005.
5
- # @Last Change: 09-Jun-2005.
6
- # @Revision: 0.105
5
+ # @Last Change: 14-Jun-2005.
6
+ # @Revision: 0.190
7
7
 
8
8
  require 'test/unit'
9
9
  require 'constraint'
10
10
 
11
11
 
12
12
  class NumericArray < Constraint::CArray
13
- or_constraint("Numeric") {|e| e.kind_of?(Numeric)}
13
+ or_constraint("Numeric") {|o, e| e.kind_of?(Numeric)}
14
14
  end
15
15
 
16
16
  class NumericArray123 < NumericArray
@@ -18,13 +18,27 @@ class NumericArray123 < NumericArray
18
18
  end
19
19
 
20
20
  class EvenNumericArray < NumericArray
21
- and_constraint("Even") {|e| e.modulo(2) == 0}
21
+ and_constraint("Even") {|o, e| e.modulo(2) == 0}
22
22
  end
23
23
 
24
24
  class EvenInteger < Constraint::Shell
25
- and_constraint("Even") {|e| e.modulo(2) == 0}
25
+ and_constraint("Even") {|o, e| e.modulo(2) == 0}
26
26
  end
27
-
27
+
28
+ class EvenIntegerLT10 < Constraint::Shell
29
+ and_constraint("Even") {|o, e| e.modulo(2) == 0}
30
+ and_constraint('LT10') {|o, e| e < 10}
31
+ on_constraint_violation('LT10') {|o, object| object >= 10 ? 9 : 0}
32
+
33
+ def generate_constrained_value(value)
34
+ if value
35
+ (value / 2).round * 2 + 2
36
+ else
37
+ 0
38
+ end
39
+ end
40
+ end
41
+
28
42
 
29
43
  class TC_Contraint < Test::Unit::TestCase
30
44
  # def setup
@@ -45,9 +59,9 @@ class TC_Contraint < Test::Unit::TestCase
45
59
  assert_equal([1,2,3], (numarr + [3]).constrained_value)
46
60
  assert_same(numarr, numarr << 4)
47
61
  assert_not_same(numarr, numarr + [4])
48
- assert_raise(ConstraintViolation) {NumericArray.new(["a",2,3])}
49
- assert_raise(ConstraintViolation) {NumericArray.new([2,nil])}
50
- assert_raise(ConstraintViolation) {numarr << "a"}
62
+ assert_raise(Constraint::Violation) {NumericArray.new(["a",2,3])}
63
+ assert_raise(Constraint::Violation) {NumericArray.new([2,nil])}
64
+ assert_raise(Constraint::Violation) {numarr << "a"}
51
65
  assert_equal(true, numarr.respond_to?(:[]))
52
66
  assert_equal(true, numarr.respond_to?(:with_constraints))
53
67
  assert_equal(false, numarr.respond_to?(:inexistent))
@@ -62,14 +76,14 @@ class TC_Contraint < Test::Unit::TestCase
62
76
  assert_same(numarr, numarr << 4)
63
77
  assert_not_same(numarr, numarr + [4])
64
78
  assert_instance_of(EvenNumericArray, numarr + [4])
65
- assert_raise(ConstraintViolation) {EvenNumericArray.new([1,2,3])}
66
- assert_raise(ConstraintViolation) {EvenNumericArray.new([nil,2])}
67
- assert_raise(ConstraintViolation) {numarr.dup << 3}
68
- assert_raise(ConstraintViolation) {numarr.dup << "a"}
69
- assert_raise(ConstraintViolation) {numarr.dup << nil}
70
- assert_raise(ConstraintViolation) {numarr.dup + [1]}
71
- assert_raise(ConstraintViolation) {numarr.dup + [[2]]}
72
- assert_raise(ConstraintViolation) {numarr.dup[0] = 1}
79
+ assert_raise(Constraint::Violation) {EvenNumericArray.new([1,2,3])}
80
+ assert_raise(Constraint::Violation) {EvenNumericArray.new([nil,2])}
81
+ assert_raise(Constraint::Violation) {numarr.dup << 3}
82
+ assert_raise(Constraint::Violation) {numarr.dup << "a"}
83
+ assert_raise(Constraint::Violation) {numarr.dup << nil}
84
+ assert_raise(Constraint::Violation) {numarr.dup + [1]}
85
+ assert_raise(Constraint::Violation) {numarr.dup + [[2]]}
86
+ assert_raise(Constraint::Violation) {numarr.dup[0] = 1}
73
87
  n = numarr.dup
74
88
  assert_nothing_raised {n[0] = 2}
75
89
  assert_equal([2,2,4], n.constrained_value)
@@ -80,19 +94,18 @@ class TC_Contraint < Test::Unit::TestCase
80
94
  enum = EvenInteger.new(2)
81
95
  assert_equal(2, enum.constrained_value)
82
96
  assert_equal(4, (enum + 2).constrained_value)
83
- assert_raise(ConstraintViolation) {enum + 1}
97
+ assert_raise(Constraint::Violation) {enum + 1}
84
98
  end
85
99
 
86
100
  def test_evenint_lt10
87
101
  enum = EvenInteger.new(2)
88
- enum.and_constraint("LT10") {|e| e < 10}
102
+ enum.and_constraint("LT10") {|o, e| e < 10}
89
103
  assert_equal(2, enum.constrained_value)
90
104
  assert_equal(4, (enum + 2).constrained_value)
91
- assert_raise(ConstraintViolation) {enum + 10}
92
- enum.or_constraint("GT20") {|e| e > 20}
105
+ assert_raise(Constraint::Violation) {enum + 10}
106
+ enum.or_constraint("GT20") {|o, e| e > 20}
93
107
  assert_nothing_raised {enum + 30}
94
- # should this one raise an exception or not?
95
- # assert_raise(ConstraintViolation) {enum + 111}
108
+ assert_raise(Constraint::Violation) {enum + 111}
96
109
  assert_equal(22, (enum + 20).constrained_value)
97
110
  enum = EvenInteger.new(2)
98
111
  assert_nothing_raised {enum + 10}
@@ -100,25 +113,58 @@ class TC_Contraint < Test::Unit::TestCase
100
113
 
101
114
  def test_evenint_lt10_rescue
102
115
  enum = EvenInteger.new(2)
103
- enum.and_constraint("LT10") {|e| e < 10}
104
- assert_raise(ConstraintViolation) {enum + 10}
105
- class << enum
106
- def handle_constraint_violation(name, object)
107
- case name
108
- when "LT10"
109
- if object > 10
110
- return 10
111
- else
112
- return 0
113
- end
114
- else
115
- return super
116
- end
117
- end
118
- end
119
- assert_equal(10, (enum + 20).constrained_value)
120
- assert_raise(ConstraintViolation) {enum + 1}
121
- assert_nothing_raised {enum + 101}
116
+ enum.and_constraint('LT10') {|o, e| e < 10}
117
+ assert_raise(Constraint::Violation) {enum + 10}
118
+ enum.on_constraint_violation('LT10') {|o, object| object >= 10 ? 9 : 0}
119
+ assert_equal(9, (enum + 20).constrained_value)
120
+ assert_raise(Constraint::Violation) {enum + 1}
121
+ assert_nothing_raised {enum + 100}
122
+ assert_raise(Constraint::Violation) {enum + 101}
123
+ enum = EvenInteger.new(2)
124
+ enum.and_constraint('LT10') {|o, e| e < 10}
125
+ assert_raise(Constraint::Violation) {enum + 10}
126
+ end
127
+
128
+ def test_evenint_rescue_handle
129
+ enum = EvenInteger.new(2)
130
+ enum.on_constraint_violation('Even') {|o, object| (object / 2).round * 2}
131
+ assert_equal(2, (enum + 1).constrained_value)
132
+ end
133
+
134
+ def test_evenintlt10
135
+ enum = EvenIntegerLT10.new(2)
136
+ assert_equal(9, (enum + 20).constrained_value)
137
+ assert_raise(Constraint::Violation) {enum + 1}
138
+ assert_raise(Constraint::Violation) {enum + 101}
139
+ assert_nothing_raised {enum + 100}
140
+ end
141
+
142
+ def test_generator_evenintlt10
143
+ enum = EvenIntegerLT10.new(2)
144
+ assert_equal(4, (enum.next_constrained_value!).constrained_value)
145
+ assert_equal(6, (enum.next_constrained_value).constrained_value)
146
+ assert_equal(4, enum.constrained_value)
147
+ coll = enum.collect_constrained_values_until {|e| e > 6}
148
+ assert_equal([4, 6], coll.collect {|e| e.constrained_value})
149
+ coll = enum.collect_constrained_values_while {|e| e <= 6}
150
+ assert_equal([4, 6], coll.collect {|e| e.constrained_value})
151
+ end
152
+
153
+ def test_inspect
154
+ arr = [1,2,3,4]
155
+ numarr = NumericArray.new(arr.dup)
156
+ assert_equal(arr.inspect, numarr.inspect)
157
+ assert_not_equal(arr.inspect, numarr.inspect_constraint_shell)
158
+ num = 2
159
+ enum = EvenIntegerLT10.new(num)
160
+ assert_equal(num.inspect, enum.inspect)
161
+ end
162
+
163
+ def test_equal
164
+ arr = [1,2,3,4]
165
+ numarr = NumericArray.new(arr.dup)
166
+ assert_equal(true, numarr === arr)
167
+ assert_equal(false, numarr.case_equal_constraint_shell(arr))
122
168
  end
123
169
  end
124
170
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: constraint
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.1"
7
- date: 2005-06-10
6
+ version: "0.2"
7
+ date: 2005-06-14
8
8
  summary: Ensure that objects comply with some constraints
9
9
  require_paths:
10
10
  - lib
@@ -28,6 +28,13 @@ authors:
28
28
  - Thomas Link
29
29
  files:
30
30
  - benchmarks/primes.rb
31
+ - examples/in_range.rb
32
+ - examples/primes.rb
33
+ - examples/queens.rb
34
+ - examples/queens_ca.rb
35
+ - examples/queens_cb.rb
36
+ - examples/queens_s.rb
37
+ - examples/run_queens.sh
31
38
  - lib/constraint.rb
32
39
  - test/tc_contraint.rb
33
40
  - test/ts_constraint.rb