rdl 2.0.1 → 2.1.0

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 (252) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +13 -0
  3. data/CHANGES.md +35 -0
  4. data/README.md +153 -116
  5. data/bin/rdl_query +1 -1
  6. data/extras/type_tests/typetests.rb +905 -0
  7. data/lib/rdl.rb +0 -1
  8. data/lib/rdl/boot.rb +108 -77
  9. data/lib/rdl/boot_rails.rb +2 -8
  10. data/lib/rdl/config.rb +44 -17
  11. data/lib/rdl/contracts/flat.rb +1 -1
  12. data/lib/rdl/info.rb +3 -3
  13. data/lib/rdl/query.rb +11 -11
  14. data/lib/rdl/typecheck.rb +399 -136
  15. data/lib/rdl/types/finite_hash.rb +3 -2
  16. data/lib/rdl/types/generic.rb +7 -6
  17. data/lib/rdl/types/intersection.rb +3 -2
  18. data/lib/rdl/types/lexer.rex +2 -3
  19. data/lib/rdl/types/lexer.rex.rb +1 -4
  20. data/lib/rdl/types/method.rb +7 -6
  21. data/lib/rdl/types/nominal.rb +10 -1
  22. data/lib/rdl/types/parser.racc +7 -8
  23. data/lib/rdl/types/parser.tab.rb +108 -109
  24. data/lib/rdl/types/structural.rb +1 -0
  25. data/lib/rdl/types/tuple.rb +2 -2
  26. data/lib/rdl/types/type.rb +8 -8
  27. data/lib/rdl/types/type_inferencer.rb +1 -1
  28. data/lib/rdl/types/union.rb +2 -1
  29. data/lib/rdl/util.rb +28 -3
  30. data/lib/rdl/wrap.rb +216 -165
  31. data/lib/rdl_disable.rb +22 -15
  32. data/lib/types/core.rb +2 -4
  33. data/lib/types/core/_aliases.rb +14 -0
  34. data/lib/types/core/abbrev.rb +3 -0
  35. data/lib/types/core/array.rb +139 -0
  36. data/lib/types/core/base64.rb +8 -0
  37. data/lib/types/core/basic_object.rb +12 -0
  38. data/lib/types/core/benchmark.rb +9 -0
  39. data/lib/types/core/bigdecimal.rb +223 -0
  40. data/lib/types/core/bigmath.rb +10 -0
  41. data/lib/types/core/bignum.rb +214 -0
  42. data/lib/types/core/class.rb +15 -0
  43. data/lib/types/core/complex.rb +123 -0
  44. data/lib/types/core/coverage.rb +4 -0
  45. data/lib/types/core/csv.rb +3 -0
  46. data/lib/types/core/date.rb +4 -0
  47. data/lib/types/core/dir.rb +37 -0
  48. data/lib/types/core/encoding.rb +21 -0
  49. data/lib/types/core/enumerable.rb +96 -0
  50. data/lib/types/core/enumerator.rb +24 -0
  51. data/lib/types/core/exception.rb +15 -0
  52. data/lib/types/core/file.rb +125 -0
  53. data/lib/types/core/fileutils.rb +4 -0
  54. data/lib/types/core/fixnum.rb +213 -0
  55. data/lib/types/core/float.rb +199 -0
  56. data/lib/types/core/gem.rb +19 -0
  57. data/lib/types/core/hash.rb +72 -0
  58. data/lib/types/core/integer.rb +194 -0
  59. data/lib/types/core/io.rb +101 -0
  60. data/lib/types/core/kernel.rb +89 -0
  61. data/lib/types/core/marshal.rb +3 -0
  62. data/lib/types/core/matchdata.rb +24 -0
  63. data/lib/types/core/math.rb +50 -0
  64. data/lib/types/core/module.rb +81 -0
  65. data/lib/types/core/nil.rb +11 -0
  66. data/lib/types/core/numeric.rb +56 -0
  67. data/lib/types/core/object.rb +73 -0
  68. data/lib/types/core/pathname.rb +104 -0
  69. data/lib/types/core/proc.rb +12 -0
  70. data/lib/types/core/process.rb +110 -0
  71. data/lib/types/core/random.rb +13 -0
  72. data/lib/types/core/range.rb +37 -0
  73. data/lib/types/core/rational.rb +207 -0
  74. data/lib/types/core/regexp.rb +28 -0
  75. data/lib/types/core/set.rb +56 -0
  76. data/lib/types/core/string.rb +140 -0
  77. data/lib/types/core/strscan.rb +6 -0
  78. data/lib/types/core/symbol.rb +27 -0
  79. data/lib/types/core/time.rb +66 -0
  80. data/lib/types/core/uri.rb +18 -0
  81. data/lib/types/core/yaml.rb +3 -0
  82. data/lib/types/devise.rb +1 -0
  83. data/lib/types/devise/controller_helpers.rb +3 -0
  84. data/lib/types/devise/parameter_sanitizer.rb +2 -0
  85. data/lib/types/pundit.rb +2 -0
  86. data/lib/types/rails/_helpers.rb +50 -0
  87. data/lib/types/rails/abstract_controller/translation.rb +2 -0
  88. data/lib/types/rails/action_controller/base.rb +3 -0
  89. data/lib/types/rails/action_controller/instrumentation.rb +6 -0
  90. data/lib/types/rails/action_controller/metal.rb +3 -0
  91. data/lib/types/rails/action_controller/mime_responds.rb +13 -0
  92. data/lib/types/rails/action_controller/parameters.rb +3 -0
  93. data/lib/types/{rails-5.x → rails}/action_controller/strong_parameters.rb +4 -8
  94. data/lib/types/rails/action_dispatch/flashhash.rb +8 -0
  95. data/lib/types/rails/action_dispatch/routing.rb +10 -0
  96. data/lib/types/rails/action_mailer/base.rb +2 -0
  97. data/lib/types/rails/action_mailer/message_delivery.rb +2 -0
  98. data/lib/types/rails/action_view/helpers_sanitizehelper.rb +2 -0
  99. data/lib/types/rails/action_view/helpers_urlhelper.rb +5 -0
  100. data/lib/types/rails/active_model/errors.rb +14 -0
  101. data/lib/types/rails/active_model/validations.rb +2 -0
  102. data/lib/types/rails/active_record/associations.rb +208 -0
  103. data/lib/types/rails/active_record/base.rb +2 -0
  104. data/lib/types/rails/active_record/core.rb +2 -0
  105. data/lib/types/rails/active_record/finder_methods.rb +2 -0
  106. data/lib/types/rails/active_record/model_schema.rb +37 -0
  107. data/lib/types/rails/active_record/relation.rb +11 -0
  108. data/lib/types/rails/active_record/schema_types.rb +51 -0
  109. data/lib/types/rails/active_record/validations.rb +2 -0
  110. data/lib/types/rails/active_support/base.rb +2 -0
  111. data/lib/types/rails/active_support/logger.rb +3 -0
  112. data/lib/types/rails/active_support/tagged_logging.rb +2 -0
  113. data/lib/types/rails/active_support/time_with_zone.rb +13 -0
  114. data/lib/types/rails/active_support/time_zone.rb +2 -0
  115. data/lib/types/rails/fixnum.rb +2 -0
  116. data/lib/types/rails/integer.rb +2 -0
  117. data/lib/types/rails/rack/request.rb +2 -0
  118. data/lib/types/rails/string.rb +3 -0
  119. data/lib/types/rails/time.rb +1 -0
  120. data/rdl.gemspec +2 -2
  121. data/test/disabled_test_rdoc.rb +8 -8
  122. data/test/test_alias.rb +1 -0
  123. data/test/test_dsl.rb +4 -4
  124. data/test/test_generic.rb +45 -38
  125. data/test/test_intersection.rb +10 -10
  126. data/test/test_le.rb +103 -102
  127. data/test/test_member.rb +33 -33
  128. data/test/test_parser.rb +101 -96
  129. data/test/test_query.rb +84 -84
  130. data/test/test_rdl.rb +87 -52
  131. data/test/test_rdl_type.rb +26 -9
  132. data/test/test_type_contract.rb +32 -31
  133. data/test/test_typecheck.rb +802 -436
  134. data/test/test_types.rb +39 -39
  135. data/test/test_wrap.rb +3 -2
  136. metadata +91 -120
  137. data/extras/type_tests/%.rb +0 -171
  138. data/extras/type_tests/&.rb +0 -159
  139. data/extras/type_tests/**.rb +0 -222
  140. data/extras/type_tests/*.rb +0 -177
  141. data/extras/type_tests/+.rb +0 -170
  142. data/extras/type_tests/-.rb +0 -171
  143. data/extras/type_tests/1scomp.rb +0 -157
  144. data/extras/type_tests/<.rb +0 -170
  145. data/extras/type_tests/<<.rb +0 -159
  146. data/extras/type_tests/>>.rb +0 -159
  147. data/extras/type_tests/[].rb +0 -163
  148. data/extras/type_tests/^.rb +0 -159
  149. data/extras/type_tests/abs.rb +0 -155
  150. data/extras/type_tests/abs2.rb +0 -164
  151. data/extras/type_tests/angle.rb +0 -157
  152. data/extras/type_tests/arg.rb +0 -157
  153. data/extras/type_tests/bit_length.rb +0 -157
  154. data/extras/type_tests/ceil.rb +0 -157
  155. data/extras/type_tests/ceilRational.rb +0 -160
  156. data/extras/type_tests/conj.rb +0 -158
  157. data/extras/type_tests/defwhere.rb +0 -86
  158. data/extras/type_tests/denominator.rb +0 -157
  159. data/extras/type_tests/div.rb +0 -172
  160. data/extras/type_tests/divslash.rb +0 -179
  161. data/extras/type_tests/even?.rb +0 -157
  162. data/extras/type_tests/fdiv.rb +0 -244
  163. data/extras/type_tests/finite?.rb +0 -157
  164. data/extras/type_tests/floor.rb +0 -157
  165. data/extras/type_tests/floorRational.rb +0 -161
  166. data/extras/type_tests/hash.rb +0 -157
  167. data/extras/type_tests/imag.rb +0 -158
  168. data/extras/type_tests/infinite?.rb +0 -157
  169. data/extras/type_tests/modulo.rb +0 -171
  170. data/extras/type_tests/nan?.rb +0 -157
  171. data/extras/type_tests/neg.rb +0 -155
  172. data/extras/type_tests/next.rb +0 -157
  173. data/extras/type_tests/next_float.rb +0 -157
  174. data/extras/type_tests/numerator.rb +0 -157
  175. data/extras/type_tests/phase.rb +0 -157
  176. data/extras/type_tests/prev_float.rb +0 -157
  177. data/extras/type_tests/quo.rb +0 -179
  178. data/extras/type_tests/rationalize.rb +0 -157
  179. data/extras/type_tests/rationalizeArg.rb +0 -198
  180. data/extras/type_tests/real.rb +0 -157
  181. data/extras/type_tests/real?.rb +0 -157
  182. data/extras/type_tests/round.rb +0 -157
  183. data/extras/type_tests/roundArg.rb +0 -169
  184. data/extras/type_tests/size.rb +0 -157
  185. data/extras/type_tests/to_c.rb +0 -157
  186. data/extras/type_tests/to_f.rb +0 -155
  187. data/extras/type_tests/to_i.rb +0 -157
  188. data/extras/type_tests/to_r.rb +0 -157
  189. data/extras/type_tests/to_s.rb +0 -157
  190. data/extras/type_tests/truncate.rb +0 -157
  191. data/extras/type_tests/truncateArg.rb +0 -166
  192. data/extras/type_tests/type tests +0 -1
  193. data/extras/type_tests/zero?.rb +0 -155
  194. data/extras/type_tests/|.rb +0 -159
  195. data/lib/types/core-ruby-2.x/_aliases.rb +0 -15
  196. data/lib/types/core-ruby-2.x/abbrev.rb +0 -5
  197. data/lib/types/core-ruby-2.x/array.rb +0 -137
  198. data/lib/types/core-ruby-2.x/base64.rb +0 -10
  199. data/lib/types/core-ruby-2.x/basic_object.rb +0 -14
  200. data/lib/types/core-ruby-2.x/benchmark.rb +0 -11
  201. data/lib/types/core-ruby-2.x/bigdecimal.rb +0 -224
  202. data/lib/types/core-ruby-2.x/bigmath.rb +0 -12
  203. data/lib/types/core-ruby-2.x/bignum.rb +0 -214
  204. data/lib/types/core-ruby-2.x/class.rb +0 -17
  205. data/lib/types/core-ruby-2.x/complex.rb +0 -124
  206. data/lib/types/core-ruby-2.x/coverage.rb +0 -6
  207. data/lib/types/core-ruby-2.x/csv.rb +0 -5
  208. data/lib/types/core-ruby-2.x/date.rb +0 -6
  209. data/lib/types/core-ruby-2.x/dir.rb +0 -38
  210. data/lib/types/core-ruby-2.x/encoding.rb +0 -23
  211. data/lib/types/core-ruby-2.x/enumerable.rb +0 -98
  212. data/lib/types/core-ruby-2.x/enumerator.rb +0 -26
  213. data/lib/types/core-ruby-2.x/exception.rb +0 -17
  214. data/lib/types/core-ruby-2.x/file.rb +0 -126
  215. data/lib/types/core-ruby-2.x/fileutils.rb +0 -6
  216. data/lib/types/core-ruby-2.x/fixnum.rb +0 -213
  217. data/lib/types/core-ruby-2.x/float.rb +0 -199
  218. data/lib/types/core-ruby-2.x/gem.rb +0 -247
  219. data/lib/types/core-ruby-2.x/hash.rb +0 -72
  220. data/lib/types/core-ruby-2.x/integer.rb +0 -197
  221. data/lib/types/core-ruby-2.x/io.rb +0 -103
  222. data/lib/types/core-ruby-2.x/kernel.rb +0 -90
  223. data/lib/types/core-ruby-2.x/marshal.rb +0 -5
  224. data/lib/types/core-ruby-2.x/matchdata.rb +0 -26
  225. data/lib/types/core-ruby-2.x/math.rb +0 -53
  226. data/lib/types/core-ruby-2.x/module.rb +0 -83
  227. data/lib/types/core-ruby-2.x/nil.rb +0 -12
  228. data/lib/types/core-ruby-2.x/numeric.rb +0 -56
  229. data/lib/types/core-ruby-2.x/object.rb +0 -75
  230. data/lib/types/core-ruby-2.x/pathname.rb +0 -106
  231. data/lib/types/core-ruby-2.x/proc.rb +0 -16
  232. data/lib/types/core-ruby-2.x/process.rb +0 -127
  233. data/lib/types/core-ruby-2.x/random.rb +0 -17
  234. data/lib/types/core-ruby-2.x/range.rb +0 -39
  235. data/lib/types/core-ruby-2.x/rational.rb +0 -209
  236. data/lib/types/core-ruby-2.x/regexp.rb +0 -30
  237. data/lib/types/core-ruby-2.x/set.rb +0 -58
  238. data/lib/types/core-ruby-2.x/string.rb +0 -143
  239. data/lib/types/core-ruby-2.x/strscan.rb +0 -7
  240. data/lib/types/core-ruby-2.x/symbol.rb +0 -29
  241. data/lib/types/core-ruby-2.x/time.rb +0 -68
  242. data/lib/types/core-ruby-2.x/uri.rb +0 -20
  243. data/lib/types/core-ruby-2.x/yaml.rb +0 -5
  244. data/lib/types/rails-5.x/_helpers.rb +0 -52
  245. data/lib/types/rails-5.x/action_controller/mime_responds.rb +0 -11
  246. data/lib/types/rails-5.x/action_dispatch/routing.rb +0 -10
  247. data/lib/types/rails-5.x/active_model/errors.rb +0 -15
  248. data/lib/types/rails-5.x/active_model/validations.rb +0 -5
  249. data/lib/types/rails-5.x/active_record/associations.rb +0 -190
  250. data/lib/types/rails-5.x/active_record/core.rb +0 -3
  251. data/lib/types/rails-5.x/active_record/model_schema.rb +0 -39
  252. data/lib/types/rails-5.x/fixnum.rb +0 -3
@@ -1,159 +0,0 @@
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
- test_result = w.is_a?(Object.const_get(expected_type))
21
- if !test_result
22
- puts "Arg1= #{x}"
23
- puts "Arg2= #{y}"
24
- puts "Res= #{w}"
25
- puts "Expected type: #{expected_type}"
26
- puts "Received type: #{w.class}"
27
- end
28
- #puts test_result
29
- return test_result
30
- end
31
-
32
- def gen_fixnum()
33
- return Random.rand(MIN_FIXNUM..MAX_FIXNUM)
34
- end
35
-
36
- def gen_bignum()
37
- r = Random.rand()
38
- if r<0.5
39
- return Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
40
- else
41
- return -1*Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
42
- end
43
- end
44
-
45
- def gen_float()
46
- r = Random.rand()
47
- if r<0.5
48
- return (2**32)*Random.rand
49
- else
50
- return -1*(2**32)*Random.rand
51
- end
52
- end
53
-
54
- def gen_bigdec()
55
- r = Random.rand()
56
- if r<0.33
57
- return BigDecimal.new(gen_fixnum())
58
- elsif r<0.66
59
- return BigDecimal.new(gen_bignum())
60
- else
61
- return BigDecimal.new(gen_float(),0)
62
- end
63
- end
64
-
65
- def gen_complex()
66
- x = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
67
- #puts x
68
- y = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
69
- #puts y
70
- return Complex(x,y)
71
- end
72
-
73
- def gen_rational()
74
- x = gen_fixnum()
75
- y = gen_fixnum()
76
- return Rational(x,y)
77
- end
78
-
79
-
80
- def gen_number(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
81
- r = Random.rand()
82
- if r<probFixnum then
83
- #Fixnum type
84
- x = gen_fixnum()
85
- elsif r<(probFixnum+probBignum)
86
- x = gen_bignum()
87
- elsif r<(probFixnum+probBignum+probBigDec)
88
- #BigDecimal type
89
- x = gen_bigdec()
90
- elsif r<(probFixnum+probBignum+probBigDec+probInf)
91
- x=Float::INFINITY
92
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
93
- x=Float::NAN
94
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
95
- #Random Float type
96
- x = gen_float()
97
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
98
- x= gen_complex()
99
- elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
100
- x = gen_rational()
101
- end
102
- return x
103
- end
104
-
105
- def rounds(x)
106
- counter = 0
107
- for i in 0..x
108
- b= test()
109
- if (!b) then
110
- counter=counter+1
111
- end
112
-
113
- end
114
- return counter
115
- end
116
-
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
-
@@ -1,222 +0,0 @@
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.2,0.2,0.1,0.05,0.05,0.2,0.1,0.1)
12
- type_one = x.class.to_s
13
- if x.is_a?(Float)&&(x==Float::INFINITY|| x.nan?)
14
- y = gen_number(0.25,0.25,0,0.1,0.1,0.2,0,0.1)
15
- type_two = y.class.to_s
16
- elsif x.is_a?(Complex)
17
- y = gen_number(0.3,0.3,0,0,0,0.3,0,0.1)
18
- type_two = y.class.to_s
19
- elsif type_one=="BigDecimal"
20
- if x<0
21
- y = gen_number_2(0.25,0.25,0.2,0,0,0.3,0,0)
22
- type_two = y.class.to_s
23
- else
24
- y = gen_number(0.25,0.25,0.2,0,0,0.2,0,0.1)
25
- type_two = y.class.to_s
26
- end
27
- elsif x<0
28
- y = gen_number_2(0.2,0.2,0.1,0.05,0.05,0.2,0.1,0.1)
29
- type_two = y.class.to_s
30
- else
31
- y = gen_number_2(0.2,0.2,0.1,0.05,0.05,0.2,0.1,0.1)
32
- type_two = y.class.to_s
33
- end
34
- puts "Arg1= #{x} and type = #{type_one}"
35
- puts "Arg2= #{y} and type = #{type_two}"
36
- operation_type = query(type_one+'#**',y)
37
- expected_type = operation_type[operation_type.index('->')+3..-1]
38
- w = x**y
39
- puts "here"
40
- test_result = w.is_a?(Object.const_get(expected_type))
41
- if !test_result
42
- puts "Arg1= #{x} (type: #{type_one})"
43
- puts "Arg2= #{y} (type: #{type_two})"
44
- puts "Res= #{w}"
45
- puts "Expected type: #{expected_type}"
46
- puts "Received type: #{w.class}"
47
- end
48
- #puts test_result
49
- return test_result
50
- end
51
-
52
- def gen_fixnum()
53
- return Random.rand(MIN_FIXNUM..MAX_FIXNUM)
54
- end
55
-
56
- def gen_bignum()
57
- r = Random.rand()
58
- if r<0.5
59
- return Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
60
- else
61
- return -1*Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
62
- end
63
- end
64
-
65
- def gen_float()
66
- r = Random.rand()
67
- if r<0.5
68
- return (2**32)*Random.rand
69
- else
70
- return -1*(2**32)*Random.rand
71
- end
72
- end
73
-
74
- def gen_bigdec()
75
- r = Random.rand()
76
- if r<0.33
77
- return BigDecimal.new(gen_fixnum())
78
- elsif r<0.66
79
- return BigDecimal.new(gen_bignum())
80
- else
81
- return BigDecimal.new((2**32)*Random.rand,0)
82
- end
83
- end
84
-
85
- def gen_bigdec_2()
86
- r = Random.rand()
87
- if r<0.33
88
- return BigDecimal.new(gen_fixnum())
89
- elsif r<0.66
90
- return BigDecimal.new(gen_bignum())
91
- else
92
- return BigDecimal.new(gen_float(),0)
93
- end
94
- end
95
-
96
-
97
- def gen_complex()
98
- x = gen_number(0.25,0.25,0.1,0.05,0.05,0.2,0,0.1)
99
- #puts x
100
- if x.is_a?(BigDecimal)
101
- y = gen_number(0.25,0.25,0.2,0,0,0.2,0,0.1)
102
- elsif x.is_a?(Float)&&(x==Float::INFINITY|| x.nan?)
103
- y = gen_number(0.25,0.25,0,0.1,0.1,0.2,0,0.1)
104
- else
105
- y = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
106
- #puts y
107
- end
108
- return Complex(x,y)
109
- end
110
-
111
- def gen_rational()
112
- x = gen_fixnum()
113
- y = gen_fixnum()
114
- return Rational(x,y)
115
- end
116
-
117
-
118
- def gen_number(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
119
- r = Random.rand()
120
- if r<probFixnum then
121
- #Fixnum type
122
- x = gen_fixnum()
123
- elsif r<(probFixnum+probBignum)
124
- x = gen_bignum()
125
- elsif r<(probFixnum+probBignum+probBigDec)
126
- #BigDecimal type
127
- x = gen_bigdec()
128
- elsif r<(probFixnum+probBignum+probBigDec+probInf)
129
- x=Float::INFINITY
130
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
131
- x=Float::NAN
132
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
133
- #Random Float type
134
- x = gen_float()
135
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
136
- x= gen_complex()
137
- elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
138
- x = gen_rational()
139
- end
140
- return x
141
- end
142
-
143
- def gen_number_2(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
144
- r = Random.rand()
145
- if r<probFixnum then
146
- #Fixnum type
147
- x = gen_fixnum()
148
- elsif r<(probFixnum+probBignum)
149
- x = gen_bignum()
150
- elsif r<(probFixnum+probBignum+probBigDec)
151
- #BigDecimal type
152
- x = gen_bigdec_2()
153
- elsif r<(probFixnum+probBignum+probBigDec+probInf)
154
- x=Float::INFINITY
155
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
156
- x=Float::NAN
157
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
158
- #Random Float type
159
- x = gen_float()
160
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
161
- x= gen_complex()
162
- elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
163
- x = gen_rational()
164
- end
165
- return x
166
- end
167
-
168
- def rounds(x)
169
- counter = 0
170
- for i in 0..x
171
- b= test()
172
- if (!b) then
173
- counter=counter+1
174
- end
175
-
176
- end
177
- return counter
178
- end
179
-
180
-
181
- def query(q,y)
182
- $__rdl_contract_switch.off {
183
- if q =~ /^(\w+(#|\.))?(\w+(!|\?|=)?|!|~|\+|\*\*|-|\*|\/|%|<<|>>|&|\||\^|<|<=|=>|>|==|===|!=|=~|!~|<=>|\[\]|\[\]=)$/
184
- klass = nil
185
- klass_pref = nil
186
- meth = nil
187
- if q =~ /(.+)#(.+)/
188
- klass = $1
189
- klass_pref = "#{klass}#"
190
- meth = $2.to_sym
191
- elsif q =~ /(.+)\.(.+)/
192
- klass_pref = "#{$1}."
193
- klass = RDL::Util.add_singleton_marker($1)
194
- meth = $2.to_sym
195
- else
196
- klass = self.class.to_s
197
- klass_pref = "#{klass}#"
198
- meth = q.to_sym
199
- end
200
- if RDL::Wrap.has_contracts?(klass, meth, :type)
201
- typs = RDL::Wrap.get_contracts(klass, meth, :type)
202
- typs.each { |t|
203
- #puts "#{klass_pref}#{meth}: #{t}"
204
- t_string = "#{t}"
205
- t_string =~ /\((\w*)\)(.+)/
206
-
207
- #puts y
208
- if y.is_a?(Object.const_get($1))
209
- return t_string
210
- end
211
- #return "#{t}"
212
- }
213
- nil
214
- else
215
- #puts "No type for #{klass_pref}#{meth}"
216
- end
217
- else
218
- #puts "Not implemented"
219
- end
220
- }
221
- end
222
-
@@ -1,177 +0,0 @@
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.2,0.2,0.1,0.05,0.05,0.2,0.1,0.1)
12
- type_one = x.class.to_s
13
- if type_one=="BigDecimal"
14
- y = gen_number(0.25,0.25,0.2,0,0,0.2,0,0.1)
15
- type_two = y.class.to_s
16
- elsif x.is_a?(Float)&&(x==Float::INFINITY|| x.nan?)
17
- y = gen_number(0.25,0.25,0,0.1,0.1,0.2,0,0.1)
18
- type_two = y.class.to_s
19
- elsif x.is_a?(Complex)
20
- y = gen_number(0.3,0.3,0,0,0,0.3,0,0.1)
21
- type_two = y.class.to_s
22
- else
23
- y = gen_number(0.2,0.2,0.1,0.05,0.05,0.2,0.1,0.1)
24
- type_two = y.class.to_s
25
- end
26
- #puts "Arg1= #{x} and type = #{type_one}"
27
- #puts "Arg2= #{y} and type = #{type_two}"
28
- operation_type = query(type_one+'#*',y)
29
- expected_type = operation_type[operation_type.index('->')+3..-1]
30
- w = x*y
31
- #puts "here"
32
- test_result = w.is_a?(Object.const_get(expected_type))
33
- if !test_result
34
- puts "Arg1= #{x} (type: #{type_one})"
35
- puts "Arg2= #{y} (type: #{type_two})"
36
- puts "Res= #{w}"
37
- puts "Expected type: #{expected_type}"
38
- puts "Received type: #{w.class}"
39
- end
40
- #puts test_result
41
- return test_result
42
- end
43
-
44
- def gen_fixnum()
45
- return Random.rand(MIN_FIXNUM..MAX_FIXNUM)
46
- end
47
-
48
- def gen_bignum()
49
- r = Random.rand()
50
- if r<0.5
51
- return Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
52
- else
53
- return -1*Random.rand(MAX_FIXNUM+1..MAX_FIXNUM*1000)
54
- end
55
- end
56
-
57
- def gen_float()
58
- r = Random.rand()
59
- if r<0.5
60
- return Float::MAX*Random.rand
61
- else
62
- return -1*Float::MAX*Random.rand
63
- end
64
- end
65
-
66
- def gen_bigdec()
67
- r = Random.rand()
68
- if r<0.33
69
- return BigDecimal.new(gen_fixnum())
70
- elsif r<0.66
71
- return BigDecimal.new(gen_bignum())
72
- else
73
- return BigDecimal.new(gen_float(),0)
74
- end
75
- end
76
-
77
- def gen_complex()
78
- x = gen_number(0.25,0.25,0.1,0.05,0.05,0.2,0,0.1)
79
- #puts x
80
- if x.is_a?(BigDecimal)
81
- y = gen_number(0.25,0.25,0.2,0,0,0.2,0,0.1)
82
- elsif x.is_a?(Float)&&(x==Float::INFINITY|| x.nan?)
83
- y = gen_number(0.25,0.25,0,0.1,0.1,0.2,0,0.1)
84
- else
85
- y = gen_number(0.2,0.2,0.1,0.05,0.05,0.3,0,0.1)
86
- #puts y
87
- end
88
- return Complex(x,y)
89
- end
90
-
91
- def gen_rational()
92
- x = gen_fixnum()
93
- y = gen_fixnum()
94
- return Rational(x,y)
95
- end
96
-
97
-
98
- def gen_number(probFixnum,probBignum,probBigDec,probInf,probNAN,probFloat,probComplex,probRational)
99
- r = Random.rand()
100
- if r<probFixnum then
101
- #Fixnum type
102
- x = gen_fixnum()
103
- elsif r<(probFixnum+probBignum)
104
- x = gen_bignum()
105
- elsif r<(probFixnum+probBignum+probBigDec)
106
- #BigDecimal type
107
- x = gen_bigdec()
108
- elsif r<(probFixnum+probBignum+probBigDec+probInf)
109
- x=Float::INFINITY
110
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN)
111
- x=Float::NAN
112
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat)
113
- #Random Float type
114
- x = gen_float()
115
- elsif r<(probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex)
116
- x= gen_complex()
117
- elsif (probFixnum+probBignum+probBigDec+probInf+probNAN+probFloat+probComplex+probRational)
118
- x = gen_rational()
119
- end
120
- return x
121
- end
122
-
123
- def rounds(x)
124
- counter = 0
125
- for i in 0..x
126
- b= test()
127
- if (!b) then
128
- counter=counter+1
129
- end
130
-
131
- end
132
- return counter
133
- end
134
-
135
-
136
- def query(q,y)
137
- $__rdl_contract_switch.off {
138
- if q =~ /^(\w+(#|\.))?(\w+(!|\?|=)?|!|~|\+|\*\*|-|\*|\/|%|<<|>>|&|\||\^|<|<=|=>|>|==|===|!=|=~|!~|<=>|\[\]|\[\]=)$/
139
- klass = nil
140
- klass_pref = nil
141
- meth = nil
142
- if q =~ /(.+)#(.+)/
143
- klass = $1
144
- klass_pref = "#{klass}#"
145
- meth = $2.to_sym
146
- elsif q =~ /(.+)\.(.+)/
147
- klass_pref = "#{$1}."
148
- klass = RDL::Util.add_singleton_marker($1)
149
- meth = $2.to_sym
150
- else
151
- klass = self.class.to_s
152
- klass_pref = "#{klass}#"
153
- meth = q.to_sym
154
- end
155
- if RDL::Wrap.has_contracts?(klass, meth, :type)
156
- typs = RDL::Wrap.get_contracts(klass, meth, :type)
157
- typs.each { |t|
158
- #puts "#{klass_pref}#{meth}: #{t}"
159
- t_string = "#{t}"
160
- t_string =~ /\((\w*)\)(.+)/
161
-
162
- #puts y
163
- if y.is_a?(Object.const_get($1))
164
- return t_string
165
- end
166
- #return "#{t}"
167
- }
168
- nil
169
- else
170
- #puts "No type for #{klass_pref}#{meth}"
171
- end
172
- else
173
- #puts "Not implemented"
174
- end
175
- }
176
- end
177
-