rdl 1.1.0 → 1.1.1.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -0
  3. data/CHANGES.md +25 -0
  4. data/README.md +104 -64
  5. data/extras/type_tests/%.rb +171 -0
  6. data/extras/type_tests/&.rb +159 -0
  7. data/extras/type_tests/**.rb +222 -0
  8. data/extras/type_tests/*.rb +177 -0
  9. data/extras/type_tests/+.rb +170 -0
  10. data/extras/type_tests/-.rb +171 -0
  11. data/extras/type_tests/1scomp.rb +157 -0
  12. data/extras/type_tests/<.rb +170 -0
  13. data/extras/type_tests/<<.rb +159 -0
  14. data/extras/type_tests/>>.rb +159 -0
  15. data/extras/type_tests/[].rb +163 -0
  16. data/extras/type_tests/^.rb +159 -0
  17. data/extras/type_tests/abs.rb +155 -0
  18. data/extras/type_tests/abs2.rb +164 -0
  19. data/extras/type_tests/angle.rb +157 -0
  20. data/extras/type_tests/arg.rb +157 -0
  21. data/extras/type_tests/bit_length.rb +157 -0
  22. data/extras/type_tests/ceil.rb +157 -0
  23. data/extras/type_tests/ceilRational.rb +160 -0
  24. data/extras/type_tests/conj.rb +158 -0
  25. data/extras/type_tests/defwhere.rb +86 -0
  26. data/extras/type_tests/denominator.rb +157 -0
  27. data/extras/type_tests/div.rb +172 -0
  28. data/extras/type_tests/divslash.rb +179 -0
  29. data/extras/type_tests/even?.rb +157 -0
  30. data/extras/type_tests/fdiv.rb +244 -0
  31. data/extras/type_tests/finite?.rb +157 -0
  32. data/extras/type_tests/floor.rb +157 -0
  33. data/extras/type_tests/floorRational.rb +161 -0
  34. data/extras/type_tests/hash.rb +157 -0
  35. data/extras/type_tests/imag.rb +158 -0
  36. data/extras/type_tests/infinite?.rb +157 -0
  37. data/extras/type_tests/modulo.rb +171 -0
  38. data/extras/type_tests/nan?.rb +157 -0
  39. data/extras/type_tests/neg.rb +155 -0
  40. data/extras/type_tests/next.rb +157 -0
  41. data/extras/type_tests/next_float.rb +157 -0
  42. data/extras/type_tests/numerator.rb +157 -0
  43. data/extras/type_tests/phase.rb +157 -0
  44. data/extras/type_tests/prev_float.rb +157 -0
  45. data/extras/type_tests/quo.rb +179 -0
  46. data/extras/type_tests/rationalize.rb +157 -0
  47. data/extras/type_tests/rationalizeArg.rb +198 -0
  48. data/extras/type_tests/real.rb +157 -0
  49. data/extras/type_tests/real?.rb +157 -0
  50. data/extras/type_tests/round.rb +157 -0
  51. data/extras/type_tests/roundArg.rb +169 -0
  52. data/extras/type_tests/size.rb +157 -0
  53. data/extras/type_tests/to_c.rb +157 -0
  54. data/extras/type_tests/to_f.rb +155 -0
  55. data/extras/type_tests/to_i.rb +157 -0
  56. data/extras/type_tests/to_r.rb +157 -0
  57. data/extras/type_tests/to_s.rb +157 -0
  58. data/extras/type_tests/truncate.rb +157 -0
  59. data/extras/type_tests/truncateArg.rb +166 -0
  60. data/extras/type_tests/type tests +1 -0
  61. data/extras/type_tests/zero?.rb +155 -0
  62. data/extras/type_tests/|.rb +159 -0
  63. data/lib/rdl/contracts/and.rb +1 -1
  64. data/lib/rdl/contracts/flat.rb +2 -2
  65. data/lib/rdl/contracts/proc.rb +2 -1
  66. data/lib/rdl/types/.#lexer.rex +1 -0
  67. data/lib/rdl/types/dependent_arg.rb +47 -0
  68. data/lib/rdl/types/finitehash.rb +5 -5
  69. data/lib/rdl/types/generic.rb +3 -3
  70. data/lib/rdl/types/lexer.rex +5 -2
  71. data/lib/rdl/types/lexer.rex.rb +3 -0
  72. data/lib/rdl/types/method.rb +144 -15
  73. data/lib/rdl/types/nominal.rb +1 -1
  74. data/lib/rdl/types/parser.racc +6 -1
  75. data/lib/rdl/types/parser.tab.rb +272 -245
  76. data/lib/rdl/types/tuple.rb +1 -1
  77. data/lib/rdl/types/type_inferencer.rb +7 -7
  78. data/lib/rdl/wrap.rb +16 -11
  79. data/rdl.gemspec +3 -3
  80. data/test/test_dsl.rb +4 -5
  81. data/test/test_le.rb +5 -5
  82. data/test/test_lib_types.rb +34 -34
  83. data/test/test_member.rb +3 -3
  84. data/test/test_type_contract.rb +63 -1
  85. data/test/test_types.rb +2 -0
  86. data/types/ruby-2.x/_aliases.rb +2 -2
  87. data/types/ruby-2.x/bigdecimal.rb +246 -12
  88. data/types/ruby-2.x/bignum.rb +253 -0
  89. data/types/ruby-2.x/complex.rb +111 -22
  90. data/types/ruby-2.x/fixnum.rb +238 -31
  91. data/types/ruby-2.x/float.rb +217 -35
  92. data/types/ruby-2.x/integer.rb +17 -16
  93. data/types/ruby-2.x/numeric.rb +31 -21
  94. data/types/ruby-2.x/rational.rb +196 -18
  95. metadata +67 -4
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bigdecimal'
4
+ require 'rdl'
5
+ require 'rdl_types'
6
+
7
+ MAX_FIXNUM = 2**(0.size*8-2)-1 #Largest fixnum. -2 since 1 bit used for sign, 1 bit used as int marker.
8
+ MIN_FIXNUM = -(2**(0.size*8-2)) #Smallest fixnum.
9
+
10
+ def test()
11
+ x = gen_number(0.5,0.5,0,0,0,0,0,0)
12
+ type_one = x.class.to_s
13
+ y = Random.rand(-1000000000..1000000000)
14
+ type_two = y.class.to_s
15
+ #puts "Arg1= #{x} and type = #{type_one}"
16
+ #puts "Arg2= #{y} and type = #{type_two}"
17
+ operation_type = query(type_one+'#>>',y)
18
+ expected_type = operation_type[operation_type.index('->')+3..-1]
19
+ w = x>>y
20
+ #puts "here"
21
+ test_result = w.is_a?(Object.const_get(expected_type))
22
+ if !test_result
23
+ puts "Arg1= #{x} (type: #{type_one})"
24
+ puts "Arg2= #{y} (type: #{type_two})"
25
+ puts "Res= #{w}"
26
+ puts "Expected type: #{expected_type}"
27
+ puts "Received type: #{w.class}"
28
+ end
29
+ #puts test_result
30
+ return test_result
31
+ end
32
+
33
+ def gen_fixnum()
34
+ return Random.rand(MIN_FIXNUM..MAX_FIXNUM)
35
+ end
36
+
37
+ def gen_bignum()
38
+ r = Random.rand()
39
+ if r<0.5
40
+ return Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
41
+ else
42
+ return -1*Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
43
+ end
44
+ end
45
+
46
+ def gen_float()
47
+ r = Random.rand()
48
+ if r<0.5
49
+ return Float::MAX*Random.rand
50
+ else
51
+ return -1*Float::MAX*Random.rand
52
+ end
53
+ end
54
+
55
+ def gen_bigdec()
56
+ r = Random.rand()
57
+ if r<0.33
58
+ return BigDecimal.new(gen_fixnum())
59
+ elsif r<0.66
60
+ return BigDecimal.new(gen_bignum())
61
+ else
62
+ return BigDecimal.new(gen_float(),0)
63
+ end
64
+ end
65
+
66
+ def gen_complex()
67
+ x = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
68
+ #puts x
69
+ y = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
70
+ #puts y
71
+ return Complex(x,y)
72
+ end
73
+
74
+ def gen_rational()
75
+ x = gen_fixnum()
76
+ y = gen_fixnum()
77
+ return Rational(x,y)
78
+ end
79
+
80
+
81
+ def gen_number(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
82
+ r = Random.rand()
83
+ if r<probFixnum then
84
+ #Fixnum type
85
+ x = gen_fixnum()
86
+ elsif r<(probFixnum+probBignum)
87
+ x = gen_bignum()
88
+ elsif r<(probFixnum+probBignum+probBigDec)
89
+ #BigDecimal type
90
+ x = gen_bigdec()
91
+ elsif r<(probFixnum+probBignum+probBigDec+probInf)
92
+ x=Float::INFINITY
93
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
94
+ x=Float::NAN
95
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
96
+ #Random Float type
97
+ x = gen_float()
98
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
99
+ x= gen_complex()
100
+ elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
101
+ x = gen_rational()
102
+ end
103
+ return x
104
+ end
105
+
106
+ def rounds(x)
107
+ counter = 0
108
+ for i in 0..x
109
+ b= test()
110
+ if (!b) then
111
+ counter=counter+1
112
+ end
113
+
114
+ end
115
+ return counter
116
+ end
117
+
118
+ def query(q,y)
119
+ $__rdl_contract_switch.off {
120
+ if q =~ /^(\w+(#|\.))?(\w+(!|\?|=)?|!|~|\+|\*\*|-|\*|\/|%|<<|>>|&|\||\^|<|<=|=>|>|==|===|!=|=~|!~|<=>|\[\]|\[\]=)$/
121
+ klass = nil
122
+ klass_pref = nil
123
+ meth = nil
124
+ if q =~ /(.+)#(.+)/
125
+ klass = $1
126
+ klass_pref = "#{klass}#"
127
+ meth = $2.to_sym
128
+ elsif q =~ /(.+)\.(.+)/
129
+ klass_pref = "#{$1}."
130
+ klass = RDL::Util.add_singleton_marker($1)
131
+ meth = $2.to_sym
132
+ else
133
+ klass = self.class.to_s
134
+ klass_pref = "#{klass}#"
135
+ meth = q.to_sym
136
+ end
137
+ if RDL::Wrap.has_contracts?(klass, meth, :type)
138
+ typs = RDL::Wrap.get_contracts(klass, meth, :type)
139
+ typs.each { |t|
140
+ #puts "#{klass_pref}#{meth}: #{t}"
141
+ t_string = "#{t}"
142
+ t_string =~ /\((\w*)\)(.+)/
143
+
144
+ #puts y
145
+ if y.is_a?(Object.const_get($1))
146
+ return t_string
147
+ end
148
+ #return "#{t}"
149
+ }
150
+ nil
151
+ else
152
+ #puts "No type for #{klass_pref}#{meth}"
153
+ end
154
+ else
155
+ #puts "Not implemented"
156
+ end
157
+ }
158
+ end
159
+
@@ -0,0 +1,163 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bigdecimal'
4
+ require 'rdl'
5
+ require 'rdl_types'
6
+
7
+ MAX_FIXNUM = 2**(0.size*8-2)-1 #Largest fixnum. -2 since 1 bit used for sign, 1 bit used as int marker.
8
+ MIN_FIXNUM = -(2**(0.size*8-2)) #Smallest fixnum.
9
+
10
+ def test()
11
+ x = gen_number(0.5,0.5,0,0,0,0,0,0)
12
+ type_one = x.class.to_s
13
+ if (x.is_a?(Bignum))
14
+ y = gen_number(0.4,0.4,0,0,0,0,0,0.2)
15
+ else
16
+ y = gen_number(0.2,0.2,0.2,0,0,0.2,0,0.2)
17
+ end
18
+ type_two = y.class.to_s
19
+ #puts "Arg1= #{x} and type = #{type_one}"
20
+ #puts "Arg2= #{y} and type = #{type_two}"
21
+ operation_type = query(type_one+'#[]',y)
22
+ expected_type = operation_type[operation_type.index('->')+3..-1]
23
+ w = x[y]
24
+ #puts "here"
25
+ test_result = w.is_a?(Object.const_get(expected_type))
26
+ if !test_result
27
+ puts "Arg1= #{x} (type: #{type_one})"
28
+ puts "Arg2= #{y} (type: #{type_two})"
29
+ puts "Res= #{w}"
30
+ puts "Expected type: #{expected_type}"
31
+ puts "Received type: #{w.class}"
32
+ end
33
+ #puts test_result
34
+ return test_result
35
+ end
36
+
37
+ def gen_fixnum()
38
+ return Random.rand(MIN_FIXNUM..MAX_FIXNUM)
39
+ end
40
+
41
+ def gen_bignum()
42
+ r = Random.rand()
43
+ if r<0.5
44
+ return Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
45
+ else
46
+ return -1*Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
47
+ end
48
+ end
49
+
50
+ def gen_float()
51
+ r = Random.rand()
52
+ if r<0.5
53
+ return Float::MAX*Random.rand
54
+ else
55
+ return -1*Float::MAX*Random.rand
56
+ end
57
+ end
58
+
59
+ def gen_bigdec()
60
+ r = Random.rand()
61
+ if r<0.33
62
+ return BigDecimal.new(gen_fixnum())
63
+ elsif r<0.66
64
+ return BigDecimal.new(gen_bignum())
65
+ else
66
+ return BigDecimal.new(gen_float(),0)
67
+ end
68
+ end
69
+
70
+ def gen_complex()
71
+ x = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
72
+ #puts x
73
+ y = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
74
+ #puts y
75
+ return Complex(x,y)
76
+ end
77
+
78
+ def gen_rational()
79
+ x = gen_fixnum()
80
+ y = gen_fixnum()
81
+ return Rational(x,y)
82
+ end
83
+
84
+
85
+ def gen_number(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
86
+ r = Random.rand()
87
+ if r<probFixnum then
88
+ #Fixnum type
89
+ x = gen_fixnum()
90
+ elsif r<(probFixnum+probBignum)
91
+ x = gen_bignum()
92
+ elsif r<(probFixnum+probBignum+probBigDec)
93
+ #BigDecimal type
94
+ x = gen_bigdec()
95
+ elsif r<(probFixnum+probBignum+probBigDec+probInf)
96
+ x=Float::INFINITY
97
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
98
+ x=Float::NAN
99
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
100
+ #Random Float type
101
+ x = gen_float()
102
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
103
+ x= gen_complex()
104
+ elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
105
+ x = gen_rational()
106
+ end
107
+ return x
108
+ end
109
+
110
+ def rounds(x)
111
+ counter = 0
112
+ for i in 0..x
113
+ b= test()
114
+ if (!b) then
115
+ counter=counter+1
116
+ end
117
+
118
+ end
119
+ return counter
120
+ end
121
+
122
+ def query(q,y)
123
+ $__rdl_contract_switch.off {
124
+ if q =~ /^(\w+(#|\.))?(\w+(!|\?|=)?|!|~|\+|\*\*|-|\*|\/|%|<<|>>|&|\||\^|<|<=|=>|>|==|===|!=|=~|!~|<=>|\[\]|\[\]=)$/
125
+ klass = nil
126
+ klass_pref = nil
127
+ meth = nil
128
+ if q =~ /(.+)#(.+)/
129
+ klass = $1
130
+ klass_pref = "#{klass}#"
131
+ meth = $2.to_sym
132
+ elsif q =~ /(.+)\.(.+)/
133
+ klass_pref = "#{$1}."
134
+ klass = RDL::Util.add_singleton_marker($1)
135
+ meth = $2.to_sym
136
+ else
137
+ klass = self.class.to_s
138
+ klass_pref = "#{klass}#"
139
+ meth = q.to_sym
140
+ end
141
+ if RDL::Wrap.has_contracts?(klass, meth, :type)
142
+ typs = RDL::Wrap.get_contracts(klass, meth, :type)
143
+ typs.each { |t|
144
+ #puts "#{klass_pref}#{meth}: #{t}"
145
+ t_string = "#{t}"
146
+ t_string =~ /\((\w*)\)(.+)/
147
+
148
+ #puts y
149
+ if y.is_a?(Object.const_get($1))
150
+ return t_string
151
+ end
152
+ #return "#{t}"
153
+ }
154
+ nil
155
+ else
156
+ #puts "No type for #{klass_pref}#{meth}"
157
+ end
158
+ else
159
+ #puts "Not implemented"
160
+ end
161
+ }
162
+ end
163
+
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bigdecimal'
4
+ require 'rdl'
5
+ require 'rdl_types'
6
+
7
+ MAX_FIXNUM = 2**(0.size*8-2)-1 #Largest fixnum. -2 since 1 bit used for sign, 1 bit used as int marker.
8
+ MIN_FIXNUM = -(2**(0.size*8-2)) #Smallest fixnum.
9
+
10
+ def test()
11
+ x = gen_number(0.5,0.5,0,0,0,0,0,0)
12
+ type_one = x.class.to_s
13
+ y = gen_number(0.5,0.5,0,0,0,0,0,0)
14
+ type_two = y.class.to_s
15
+ #puts "Arg1= #{x} and type = #{type_one}"
16
+ #puts "Arg2= #{y} and type = #{type_two}"
17
+ operation_type = query(type_one+'#^',y)
18
+ expected_type = operation_type[operation_type.index('->')+3..-1]
19
+ w = x^y
20
+ #puts "here"
21
+ test_result = w.is_a?(Object.const_get(expected_type))
22
+ if !test_result
23
+ puts "Arg1= #{x} (type: #{type_one})"
24
+ puts "Arg2= #{y} (type: #{type_two})"
25
+ puts "Res= #{w}"
26
+ puts "Expected type: #{expected_type}"
27
+ puts "Received type: #{w.class}"
28
+ end
29
+ #puts test_result
30
+ return test_result
31
+ end
32
+
33
+ def gen_fixnum()
34
+ return Random.rand(MIN_FIXNUM..MAX_FIXNUM)
35
+ end
36
+
37
+ def gen_bignum()
38
+ r = Random.rand()
39
+ if r<0.5
40
+ return Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
41
+ else
42
+ return -1*Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
43
+ end
44
+ end
45
+
46
+ def gen_float()
47
+ r = Random.rand()
48
+ if r<0.5
49
+ return Float::MAX*Random.rand
50
+ else
51
+ return -1*Float::MAX*Random.rand
52
+ end
53
+ end
54
+
55
+ def gen_bigdec()
56
+ r = Random.rand()
57
+ if r<0.33
58
+ return BigDecimal.new(gen_fixnum())
59
+ elsif r<0.66
60
+ return BigDecimal.new(gen_bignum())
61
+ else
62
+ return BigDecimal.new(gen_float(),0)
63
+ end
64
+ end
65
+
66
+ def gen_complex()
67
+ x = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
68
+ #puts x
69
+ y = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
70
+ #puts y
71
+ return Complex(x,y)
72
+ end
73
+
74
+ def gen_rational()
75
+ x = gen_fixnum()
76
+ y = gen_fixnum()
77
+ return Rational(x,y)
78
+ end
79
+
80
+
81
+ def gen_number(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
82
+ r = Random.rand()
83
+ if r<probFixnum then
84
+ #Fixnum type
85
+ x = gen_fixnum()
86
+ elsif r<(probFixnum+probBignum)
87
+ x = gen_bignum()
88
+ elsif r<(probFixnum+probBignum+probBigDec)
89
+ #BigDecimal type
90
+ x = gen_bigdec()
91
+ elsif r<(probFixnum+probBignum+probBigDec+probInf)
92
+ x=Float::INFINITY
93
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
94
+ x=Float::NAN
95
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
96
+ #Random Float type
97
+ x = gen_float()
98
+ elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
99
+ x= gen_complex()
100
+ elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
101
+ x = gen_rational()
102
+ end
103
+ return x
104
+ end
105
+
106
+ def rounds(x)
107
+ counter = 0
108
+ for i in 0..x
109
+ b= test()
110
+ if (!b) then
111
+ counter=counter+1
112
+ end
113
+
114
+ end
115
+ return counter
116
+ end
117
+
118
+ def query(q,y)
119
+ $__rdl_contract_switch.off {
120
+ if q =~ /^(\w+(#|\.))?(\w+(!|\?|=)?|!|~|\+|\*\*|-|\*|\/|%|<<|>>|&|\||\^|<|<=|=>|>|==|===|!=|=~|!~|<=>|\[\]|\[\]=)$/
121
+ klass = nil
122
+ klass_pref = nil
123
+ meth = nil
124
+ if q =~ /(.+)#(.+)/
125
+ klass = $1
126
+ klass_pref = "#{klass}#"
127
+ meth = $2.to_sym
128
+ elsif q =~ /(.+)\.(.+)/
129
+ klass_pref = "#{$1}."
130
+ klass = RDL::Util.add_singleton_marker($1)
131
+ meth = $2.to_sym
132
+ else
133
+ klass = self.class.to_s
134
+ klass_pref = "#{klass}#"
135
+ meth = q.to_sym
136
+ end
137
+ if RDL::Wrap.has_contracts?(klass, meth, :type)
138
+ typs = RDL::Wrap.get_contracts(klass, meth, :type)
139
+ typs.each { |t|
140
+ #puts "#{klass_pref}#{meth}: #{t}"
141
+ t_string = "#{t}"
142
+ t_string =~ /\((\w*)\)(.+)/
143
+
144
+ #puts y
145
+ if y.is_a?(Object.const_get($1))
146
+ return t_string
147
+ end
148
+ #return "#{t}"
149
+ }
150
+ nil
151
+ else
152
+ #puts "No type for #{klass_pref}#{meth}"
153
+ end
154
+ else
155
+ #puts "Not implemented"
156
+ end
157
+ }
158
+ end
159
+