opal 1.0.3 → 1.1.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (408) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +13 -12
  3. data/.gitattributes +1 -1
  4. data/.github/FUNDING.yml +1 -0
  5. data/.github/workflows/build.yml +90 -0
  6. data/.jshintrc +1 -1
  7. data/.overcommit.yml +35 -0
  8. data/.rubocop.yml +44 -7
  9. data/{.rubocop_todo.yml → .rubocop/todo.yml} +4 -5
  10. data/CHANGELOG.md +152 -1
  11. data/Gemfile +2 -3
  12. data/HACKING.md +2 -9
  13. data/LICENSE +1 -1
  14. data/README.md +21 -18
  15. data/UNRELEASED.md +4 -2
  16. data/benchmark-ips/bm_array_pop_1.rb +8 -0
  17. data/benchmark-ips/bm_array_shift.rb +7 -0
  18. data/benchmark-ips/bm_js_symbols_vs_strings.rb +7 -2
  19. data/benchmark-ips/class_shovel_vs_singleton_class.rb +16 -0
  20. data/bin/build-browser-source-map-support +4 -0
  21. data/bin/format-filters +54 -0
  22. data/bin/git-submodule-fast-install +49 -0
  23. data/bin/remove-filters +39 -0
  24. data/bin/setup +4 -0
  25. data/bin/yarn +11 -0
  26. data/docs/releasing.md +18 -0
  27. data/docs/roda-sprockets.md +86 -0
  28. data/docs/sinatra.md +5 -12
  29. data/examples/rack/Gemfile +1 -0
  30. data/examples/rack/app/application.rb +3 -0
  31. data/examples/rack/config.ru +5 -4
  32. data/exe/opal-repl +2 -83
  33. data/lib/opal/ast/builder.rb +1 -1
  34. data/lib/opal/builder.rb +2 -10
  35. data/lib/opal/builder_processors.rb +1 -1
  36. data/lib/opal/cli.rb +4 -1
  37. data/lib/opal/cli_options.rb +11 -3
  38. data/lib/opal/cli_runners.rb +27 -37
  39. data/lib/opal/cli_runners/applescript.rb +5 -44
  40. data/lib/opal/cli_runners/chrome.js +7 -35
  41. data/lib/opal/cli_runners/chrome.rb +75 -17
  42. data/lib/opal/cli_runners/compiler.rb +17 -0
  43. data/lib/opal/cli_runners/nashorn.rb +9 -43
  44. data/lib/opal/cli_runners/nodejs.rb +18 -47
  45. data/lib/opal/cli_runners/server.rb +18 -6
  46. data/lib/opal/cli_runners/source-map-support-browser.js +6449 -0
  47. data/lib/opal/cli_runners/source-map-support-node.js +3704 -0
  48. data/lib/opal/cli_runners/source-map-support.js +639 -0
  49. data/lib/opal/cli_runners/system_runner.rb +45 -0
  50. data/lib/opal/compiler.rb +57 -29
  51. data/lib/opal/config.rb +0 -5
  52. data/lib/opal/erb.rb +1 -1
  53. data/lib/opal/magic_comments.rb +34 -0
  54. data/lib/opal/nodes/args/arity_check.rb +2 -1
  55. data/lib/opal/nodes/base.rb +1 -1
  56. data/lib/opal/nodes/call.rb +1 -4
  57. data/lib/opal/nodes/def.rb +2 -0
  58. data/lib/opal/nodes/iter.rb +1 -1
  59. data/lib/opal/nodes/literal.rb +19 -14
  60. data/lib/opal/nodes/logic.rb +5 -80
  61. data/lib/opal/nodes/masgn.rb +2 -0
  62. data/lib/opal/nodes/rescue.rb +1 -1
  63. data/lib/opal/nodes/super.rb +24 -10
  64. data/lib/opal/nodes/top.rb +5 -4
  65. data/lib/opal/parser/with_c_lexer.rb +2 -1
  66. data/lib/opal/parser/with_ruby_lexer.rb +1 -1
  67. data/lib/opal/path_reader.rb +2 -2
  68. data/lib/opal/paths.rb +8 -5
  69. data/lib/opal/repl.rb +103 -0
  70. data/lib/opal/rewriter.rb +4 -0
  71. data/lib/opal/rewriters/for_rewriter.rb +1 -1
  72. data/lib/opal/rewriters/js_reserved_words.rb +7 -7
  73. data/lib/opal/rewriters/numblocks.rb +31 -0
  74. data/lib/opal/rewriters/returnable_logic.rb +33 -0
  75. data/lib/opal/util.rb +11 -48
  76. data/lib/opal/version.rb +1 -1
  77. data/opal.gemspec +26 -26
  78. data/opal/corelib/array.rb +160 -118
  79. data/opal/corelib/array/pack.rb +5 -3
  80. data/opal/corelib/basic_object.rb +4 -4
  81. data/opal/corelib/class.rb +2 -1
  82. data/opal/corelib/comparable.rb +49 -31
  83. data/opal/corelib/constants.rb +5 -5
  84. data/opal/corelib/enumerable.rb +108 -46
  85. data/opal/corelib/enumerator.rb +27 -12
  86. data/opal/corelib/file.rb +3 -1
  87. data/opal/corelib/hash.rb +6 -1
  88. data/opal/corelib/helpers.rb +8 -28
  89. data/opal/corelib/io.rb +12 -7
  90. data/opal/corelib/kernel.rb +45 -14
  91. data/opal/corelib/kernel/format.rb +3 -1
  92. data/opal/corelib/math.rb +8 -6
  93. data/opal/corelib/method.rb +8 -0
  94. data/opal/corelib/module.rb +17 -2
  95. data/opal/corelib/number.rb +3 -12
  96. data/opal/corelib/proc.rb +16 -0
  97. data/opal/corelib/random/mersenne_twister.rb +147 -0
  98. data/opal/corelib/range.rb +3 -13
  99. data/opal/corelib/regexp.rb +10 -6
  100. data/opal/corelib/runtime.js +208 -81
  101. data/opal/corelib/string.rb +55 -49
  102. data/opal/corelib/string/encoding.rb +109 -32
  103. data/opal/corelib/string/unpack.rb +2 -17
  104. data/opal/corelib/struct.rb +14 -1
  105. data/opal/corelib/time.rb +4 -0
  106. data/opal/opal.rb +1 -1
  107. data/opal/opal/mini.rb +1 -1
  108. data/package.json +16 -0
  109. data/spec/README.md +10 -0
  110. data/spec/filters/bugs/array.rb +76 -0
  111. data/spec/filters/bugs/base64.rb +10 -0
  112. data/spec/filters/bugs/basicobject.rb +12 -0
  113. data/spec/filters/bugs/bigdecimal.rb +248 -0
  114. data/spec/filters/bugs/class.rb +12 -0
  115. data/spec/filters/bugs/complex.rb +7 -0
  116. data/spec/filters/bugs/date.rb +104 -0
  117. data/spec/filters/bugs/encoding.rb +259 -0
  118. data/spec/filters/bugs/enumerable.rb +26 -0
  119. data/spec/filters/bugs/enumerator.rb +48 -0
  120. data/spec/filters/bugs/exception.rb +120 -0
  121. data/spec/filters/bugs/file.rb +48 -0
  122. data/spec/filters/bugs/float.rb +74 -0
  123. data/spec/filters/bugs/hash.rb +60 -0
  124. data/spec/filters/bugs/integer.rb +78 -0
  125. data/spec/filters/bugs/io.rb +9 -0
  126. data/spec/filters/bugs/kernel.rb +401 -0
  127. data/spec/filters/bugs/language.rb +451 -0
  128. data/spec/filters/bugs/marshal.rb +50 -0
  129. data/spec/filters/bugs/math.rb +4 -0
  130. data/spec/filters/bugs/method.rb +79 -0
  131. data/spec/filters/bugs/module.rb +281 -0
  132. data/spec/filters/bugs/nilclass.rb +4 -0
  133. data/spec/filters/bugs/numeric.rb +27 -0
  134. data/spec/filters/bugs/openstruct.rb +8 -0
  135. data/spec/filters/bugs/pack_unpack.rb +138 -0
  136. data/spec/filters/bugs/pathname.rb +9 -0
  137. data/spec/filters/bugs/proc.rb +80 -0
  138. data/spec/filters/bugs/random.rb +20 -0
  139. data/spec/filters/bugs/range.rb +139 -0
  140. data/spec/filters/bugs/rational.rb +12 -0
  141. data/spec/filters/bugs/regexp.rb +91 -0
  142. data/spec/filters/bugs/set.rb +51 -0
  143. data/spec/filters/bugs/singleton.rb +7 -0
  144. data/spec/filters/bugs/string.rb +341 -0
  145. data/spec/filters/bugs/stringscanner.rb +78 -0
  146. data/spec/filters/bugs/struct.rb +26 -0
  147. data/spec/filters/bugs/symbol.rb +7 -0
  148. data/spec/filters/bugs/time.rb +109 -0
  149. data/spec/filters/bugs/unboundmethod.rb +33 -0
  150. data/spec/filters/bugs/warnings.rb +30 -0
  151. data/spec/filters/unsupported/array.rb +168 -0
  152. data/spec/filters/unsupported/basicobject.rb +15 -0
  153. data/spec/filters/unsupported/bignum.rb +55 -0
  154. data/spec/filters/unsupported/class.rb +5 -0
  155. data/spec/filters/unsupported/delegator.rb +6 -0
  156. data/spec/filters/unsupported/enumerable.rb +12 -0
  157. data/spec/filters/unsupported/enumerator.rb +14 -0
  158. data/spec/filters/unsupported/file.rb +4 -0
  159. data/spec/filters/unsupported/fixnum.rb +15 -0
  160. data/spec/filters/unsupported/float.rb +47 -0
  161. data/spec/filters/unsupported/freeze.rb +259 -0
  162. data/spec/filters/unsupported/hash.rb +44 -0
  163. data/spec/filters/unsupported/integer.rb +101 -0
  164. data/spec/filters/unsupported/kernel.rb +35 -0
  165. data/spec/filters/unsupported/language.rb +25 -0
  166. data/spec/filters/unsupported/marshal.rb +44 -0
  167. data/spec/filters/unsupported/matchdata.rb +63 -0
  168. data/spec/filters/unsupported/math.rb +4 -0
  169. data/spec/filters/unsupported/pathname.rb +4 -0
  170. data/spec/filters/unsupported/privacy.rb +287 -0
  171. data/spec/filters/unsupported/proc.rb +4 -0
  172. data/spec/filters/unsupported/random.rb +5 -0
  173. data/spec/filters/unsupported/range.rb +8 -0
  174. data/spec/filters/unsupported/regexp.rb +70 -0
  175. data/spec/filters/unsupported/set.rb +5 -0
  176. data/spec/filters/unsupported/singleton.rb +7 -0
  177. data/spec/filters/unsupported/string.rb +687 -0
  178. data/spec/filters/unsupported/struct.rb +7 -0
  179. data/spec/filters/unsupported/symbol.rb +21 -0
  180. data/spec/filters/unsupported/taint.rb +162 -0
  181. data/spec/filters/unsupported/thread.rb +10 -0
  182. data/spec/filters/unsupported/time.rb +204 -0
  183. data/spec/filters/unsupported/usage_of_files.rb +262 -0
  184. data/spec/lib/builder_processors_spec.rb +44 -0
  185. data/spec/lib/builder_spec.rb +133 -0
  186. data/spec/lib/cli_runners/server_spec.rb +25 -0
  187. data/spec/lib/cli_runners_spec.rb +16 -0
  188. data/spec/lib/cli_spec.rb +256 -0
  189. data/spec/lib/compiler_spec.rb +693 -0
  190. data/spec/lib/config_spec.rb +112 -0
  191. data/spec/lib/dependency_resolver_spec.rb +43 -0
  192. data/spec/lib/deprecations_spec.rb +17 -0
  193. data/spec/lib/fixtures/complex_sprockets.js.rb.erb +4 -0
  194. data/spec/lib/fixtures/file_with_directives.js +2 -0
  195. data/spec/lib/fixtures/jst_file.js.jst +1 -0
  196. data/spec/lib/fixtures/no_requires.rb +1 -0
  197. data/spec/lib/fixtures/opal_file.rb +2 -0
  198. data/spec/lib/fixtures/require_tree_test.rb +3 -0
  199. data/spec/lib/fixtures/required_file.js +1 -0
  200. data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
  201. data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
  202. data/spec/lib/fixtures/requires.rb +7 -0
  203. data/spec/lib/fixtures/source_location_test.rb +7 -0
  204. data/spec/lib/fixtures/source_map.rb +1 -0
  205. data/spec/lib/fixtures/source_map/subfolder/other_file.rb +1 -0
  206. data/spec/lib/fixtures/sprockets_file.js.rb +3 -0
  207. data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
  208. data/spec/lib/path_reader_spec.rb +47 -0
  209. data/spec/lib/paths_spec.rb +18 -0
  210. data/spec/lib/repl_spec.rb +28 -0
  211. data/spec/lib/rewriters/base_spec.rb +68 -0
  212. data/spec/lib/rewriters/binary_operator_assignment_spec.rb +153 -0
  213. data/spec/lib/rewriters/block_to_iter_spec.rb +28 -0
  214. data/spec/lib/rewriters/dot_js_syntax_spec.rb +108 -0
  215. data/spec/lib/rewriters/explicit_writer_return_spec.rb +186 -0
  216. data/spec/lib/rewriters/for_rewriter_spec.rb +92 -0
  217. data/spec/lib/rewriters/hashes/key_duplicates_rewriter_spec.rb +47 -0
  218. data/spec/lib/rewriters/js_reserved_words_spec.rb +119 -0
  219. data/spec/lib/rewriters/logical_operator_assignment_spec.rb +202 -0
  220. data/spec/lib/rewriters/opal_engine_check_spec.rb +84 -0
  221. data/spec/lib/rewriters/returnable_logic_spec.rb +52 -0
  222. data/spec/lib/rewriters/rubyspec/filters_rewriter_spec.rb +59 -0
  223. data/spec/lib/simple_server_spec.rb +56 -0
  224. data/spec/lib/source_map/file_spec.rb +67 -0
  225. data/spec/lib/source_map/index_spec.rb +80 -0
  226. data/spec/lib/spec_helper.rb +105 -0
  227. data/spec/mspec-opal/formatters.rb +197 -0
  228. data/spec/mspec-opal/runner.rb +172 -0
  229. data/spec/opal/compiler/irb_spec.rb +44 -0
  230. data/spec/opal/compiler/unicode_spec.rb +10 -0
  231. data/spec/opal/core/array/dup_spec.rb +23 -0
  232. data/spec/opal/core/array/intersection_spec.rb +38 -0
  233. data/spec/opal/core/array/minus_spec.rb +38 -0
  234. data/spec/opal/core/array/union_spec.rb +38 -0
  235. data/spec/opal/core/array/uniq_spec.rb +49 -0
  236. data/spec/opal/core/class/inherited_spec.rb +18 -0
  237. data/spec/opal/core/enumerable/all_break_spec.rb +5 -0
  238. data/spec/opal/core/enumerable/any_break_spec.rb +5 -0
  239. data/spec/opal/core/enumerable/collect_break_spec.rb +13 -0
  240. data/spec/opal/core/enumerable/count_break_spec.rb +5 -0
  241. data/spec/opal/core/enumerable/detect_break_spec.rb +5 -0
  242. data/spec/opal/core/enumerable/drop_while_break_spec.rb +5 -0
  243. data/spec/opal/core/enumerable/each_slice_break_spec.rb +6 -0
  244. data/spec/opal/core/enumerable/each_with_index_break_spec.rb +5 -0
  245. data/spec/opal/core/enumerable/each_with_object_break_spec.rb +5 -0
  246. data/spec/opal/core/enumerable/find_all_break_spec.rb +5 -0
  247. data/spec/opal/core/enumerable/find_index_break_spec.rb +5 -0
  248. data/spec/opal/core/enumerable/grep_break_spec.rb +5 -0
  249. data/spec/opal/core/enumerable/max_break_spec.rb +5 -0
  250. data/spec/opal/core/enumerable/max_by_break_spec.rb +5 -0
  251. data/spec/opal/core/enumerable/min_break_spec.rb +5 -0
  252. data/spec/opal/core/enumerable/min_by_break_spec.rb +5 -0
  253. data/spec/opal/core/enumerable/none_break_spec.rb +5 -0
  254. data/spec/opal/core/enumerable/one_break_spec.rb +5 -0
  255. data/spec/opal/core/enumerable/reduce_break_spec.rb +5 -0
  256. data/spec/opal/core/enumerable/take_while_break_spec.rb +5 -0
  257. data/spec/opal/core/enumerator/with_index_spec.rb +6 -0
  258. data/spec/opal/core/exception_spec.rb +8 -0
  259. data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
  260. data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
  261. data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
  262. data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
  263. data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
  264. data/spec/opal/core/fixtures/require_tree_files/nested/nested 1.rb +1 -0
  265. data/spec/opal/core/fixtures/require_tree_files/nested/nested 2.rb +1 -0
  266. data/spec/opal/core/fixtures/require_tree_files/other/other 1.rb +1 -0
  267. data/spec/opal/core/fixtures/require_tree_with_dot/file 1.rb +1 -0
  268. data/spec/opal/core/fixtures/require_tree_with_dot/file 2.rb +1 -0
  269. data/spec/opal/core/fixtures/require_tree_with_dot/file 3.rb +1 -0
  270. data/spec/opal/core/fixtures/require_tree_with_dot/index.rb +3 -0
  271. data/spec/opal/core/hash/internals_spec.rb +339 -0
  272. data/spec/opal/core/helpers_spec.rb +14 -0
  273. data/spec/opal/core/iterable_props_spec.rb +53 -0
  274. data/spec/opal/core/kernel/at_exit_spec.rb +70 -0
  275. data/spec/opal/core/kernel/freeze_spec.rb +15 -0
  276. data/spec/opal/core/kernel/instance_variables_spec.rb +110 -0
  277. data/spec/opal/core/kernel/methods_spec.rb +25 -0
  278. data/spec/opal/core/kernel/public_methods_spec.rb +25 -0
  279. data/spec/opal/core/kernel/require_tree_spec.rb +18 -0
  280. data/spec/opal/core/kernel/respond_to_spec.rb +15 -0
  281. data/spec/opal/core/language/DATA/characters_support_spec.rb +9 -0
  282. data/spec/opal/core/language/DATA/empty___END___spec.rb +7 -0
  283. data/spec/opal/core/language/DATA/multiple___END___spec.rb +10 -0
  284. data/spec/opal/core/language/arguments/mlhs_arg_spec.rb +18 -0
  285. data/spec/opal/core/language/keyword_arguments_spec.rb +9 -0
  286. data/spec/opal/core/language/numblocks_spec.rb +16 -0
  287. data/spec/opal/core/language/safe_navigator_spec.rb +7 -0
  288. data/spec/opal/core/language/while_spec.rb +31 -0
  289. data/spec/opal/core/language_spec.rb +29 -0
  290. data/spec/opal/core/marshal/dump_spec.rb +81 -0
  291. data/spec/opal/core/marshal/load_spec.rb +13 -0
  292. data/spec/opal/core/module_spec.rb +27 -0
  293. data/spec/opal/core/object_id_spec.rb +56 -0
  294. data/spec/opal/core/regexp/interpolation_spec.rb +40 -0
  295. data/spec/opal/core/regexp/match_spec.rb +78 -0
  296. data/spec/opal/core/runtime/bridged_classes_spec.rb +123 -0
  297. data/spec/opal/core/runtime/constants_spec.rb +16 -0
  298. data/spec/opal/core/runtime/eval_spec.rb +5 -0
  299. data/spec/opal/core/runtime/exit_spec.rb +29 -0
  300. data/spec/opal/core/runtime/is_a_spec.rb +48 -0
  301. data/spec/opal/core/runtime/loaded_spec.rb +20 -0
  302. data/spec/opal/core/runtime/main_methods_spec.rb +39 -0
  303. data/spec/opal/core/runtime/method_missing_spec.rb +68 -0
  304. data/spec/opal/core/runtime/rescue_spec.rb +37 -0
  305. data/spec/opal/core/runtime/string_spec.rb +25 -0
  306. data/spec/opal/core/runtime/truthy_spec.rb +61 -0
  307. data/spec/opal/core/runtime_spec.rb +58 -0
  308. data/spec/opal/core/string/each_byte_spec.rb +19 -0
  309. data/spec/opal/core/string/gsub_spec.rb +35 -0
  310. data/spec/opal/core/string/to_sym_spec.rb +9 -0
  311. data/spec/opal/core/string_spec.rb +28 -0
  312. data/spec/opal/core/struct/dup_spec.rb +11 -0
  313. data/spec/opal/core/time_spec.rb +68 -0
  314. data/spec/opal/stdlib/erb/erb_spec.rb +30 -0
  315. data/spec/opal/stdlib/erb/inline_block.opalerb +3 -0
  316. data/spec/opal/stdlib/erb/quoted.opalerb +1 -0
  317. data/spec/opal/stdlib/erb/simple.opalerb +1 -0
  318. data/spec/opal/stdlib/js_spec.rb +72 -0
  319. data/spec/opal/stdlib/json/ext_spec.rb +55 -0
  320. data/spec/opal/stdlib/json/parse_spec.rb +37 -0
  321. data/spec/opal/stdlib/logger/logger_spec.rb +308 -0
  322. data/spec/opal/stdlib/native/alias_native_spec.rb +27 -0
  323. data/spec/opal/stdlib/native/array_spec.rb +11 -0
  324. data/spec/opal/stdlib/native/date_spec.rb +12 -0
  325. data/spec/opal/stdlib/native/deprecated_include_spec.rb +8 -0
  326. data/spec/opal/stdlib/native/each_spec.rb +13 -0
  327. data/spec/opal/stdlib/native/element_reference_spec.rb +16 -0
  328. data/spec/opal/stdlib/native/exposure_spec.rb +33 -0
  329. data/spec/opal/stdlib/native/ext_spec.rb +19 -0
  330. data/spec/opal/stdlib/native/hash_spec.rb +67 -0
  331. data/spec/opal/stdlib/native/initialize_spec.rb +17 -0
  332. data/spec/opal/stdlib/native/method_missing_spec.rb +51 -0
  333. data/spec/opal/stdlib/native/native_alias_spec.rb +26 -0
  334. data/spec/opal/stdlib/native/native_class_spec.rb +18 -0
  335. data/spec/opal/stdlib/native/native_module_spec.rb +13 -0
  336. data/spec/opal/stdlib/native/native_reader_spec.rb +22 -0
  337. data/spec/opal/stdlib/native/native_writer_spec.rb +30 -0
  338. data/spec/opal/stdlib/native/new_spec.rb +92 -0
  339. data/spec/opal/stdlib/native/struct_spec.rb +12 -0
  340. data/spec/opal/stdlib/pp_spec.rb +5 -0
  341. data/spec/opal/stdlib/promise/always_spec.rb +49 -0
  342. data/spec/opal/stdlib/promise/error_spec.rb +15 -0
  343. data/spec/opal/stdlib/promise/rescue_spec.rb +53 -0
  344. data/spec/opal/stdlib/promise/then_spec.rb +79 -0
  345. data/spec/opal/stdlib/promise/trace_spec.rb +51 -0
  346. data/spec/opal/stdlib/promise/value_spec.rb +15 -0
  347. data/spec/opal/stdlib/promise/when_spec.rb +34 -0
  348. data/spec/opal/stdlib/source_map_spec.rb +8 -0
  349. data/spec/opal/stdlib/strscan/scan_spec.rb +11 -0
  350. data/spec/opal/stdlib/template/paths_spec.rb +10 -0
  351. data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
  352. data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
  353. data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
  354. data/spec/ruby_specs +183 -0
  355. data/spec/spec_helper.rb +31 -0
  356. data/spec/support/guard_platform.rb +4 -0
  357. data/spec/support/match_helpers.rb +57 -0
  358. data/spec/support/mspec_rspec_adapter.rb +33 -0
  359. data/spec/support/rewriters_helper.rb +54 -0
  360. data/spec/support/source_map_helper.rb +190 -0
  361. data/stdlib/base64.rb +2 -2
  362. data/stdlib/bigdecimal.rb +15 -3
  363. data/stdlib/bigdecimal/bignumber.js.rb +1 -1
  364. data/stdlib/bigdecimal/util.rb +148 -0
  365. data/stdlib/delegate.rb +8 -0
  366. data/stdlib/nodejs/fileutils.rb +1 -1
  367. data/stdlib/nodejs/kernel.rb +0 -13
  368. data/stdlib/nodejs/stacktrace.rb +4 -179
  369. data/stdlib/ostruct.rb +5 -0
  370. data/stdlib/pp.rb +586 -19
  371. data/stdlib/prettyprint.rb +556 -0
  372. data/stdlib/rbconfig/sizeof.rb +2 -0
  373. data/stdlib/securerandom.rb +32 -0
  374. data/stdlib/set.rb +36 -0
  375. data/stdlib/strscan.rb +15 -0
  376. data/tasks/benchmarking.rake +1 -1
  377. data/tasks/linting.rake +3 -5
  378. data/tasks/releasing.rake +2 -5
  379. data/tasks/testing.rake +16 -11
  380. data/tasks/testing/mspec_special_calls.rb +1 -19
  381. data/test/nodejs/fixtures/cat.png +0 -0
  382. data/test/nodejs/fixtures/hello.rb +1 -0
  383. data/test/nodejs/fixtures/iso88591.txt +1 -0
  384. data/test/nodejs/fixtures/utf8.txt +1 -0
  385. data/test/nodejs/fixtures/win1258.txt +1 -0
  386. data/test/nodejs/test_dir.rb +39 -0
  387. data/test/nodejs/test_env.rb +62 -0
  388. data/test/nodejs/test_error.rb +29 -0
  389. data/test/nodejs/test_file.rb +206 -0
  390. data/test/nodejs/test_file_encoding.rb +20 -0
  391. data/test/nodejs/test_io.rb +18 -0
  392. data/test/nodejs/test_opal_builder.rb +12 -0
  393. data/test/nodejs/test_pathname.rb +16 -0
  394. data/test/opal/cat.png +0 -0
  395. data/test/opal/http_server.rb +52 -0
  396. data/test/opal/test_base64.rb +115 -0
  397. data/test/opal/test_keyword.rb +590 -0
  398. data/test/opal/test_matrix.rb +661 -0
  399. data/test/opal/test_openuri.rb +53 -0
  400. data/test/opal/unsupported_and_bugs.rb +39 -0
  401. data/yarn.lock +1355 -0
  402. metadata +905 -40
  403. data/.travis.yml +0 -109
  404. data/appveyor.yml +0 -35
  405. data/lib/opal/nodes/runtime_helpers.rb +0 -51
  406. data/opal/corelib/random/MersenneTwister.js +0 -137
  407. data/opal/corelib/random/mersenne_twister.js.rb +0 -13
  408. data/stdlib/bigdecimal/kernel.rb +0 -5
@@ -0,0 +1,661 @@
1
+ # frozen_string_literal: false
2
+ require 'test/unit'
3
+ require 'matrix'
4
+
5
+ class SubMatrix < Matrix
6
+ end
7
+
8
+ class TestMatrix < Test::Unit::TestCase
9
+ def setup
10
+ @m1 = Matrix[[1,2,3], [4,5,6]]
11
+ @m2 = Matrix[[1,2,3], [4,5,6]]
12
+ @m3 = @m1.clone
13
+ @m4 = Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
14
+ @n1 = Matrix[[2,3,4], [5,6,7]]
15
+ @c1 = Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]]
16
+ @e1 = Matrix.empty(2,0)
17
+ @e2 = Matrix.empty(0,3)
18
+ @a3 = Matrix[[4, 1, -3], [0, 3, 7], [11, -4, 2]]
19
+ @a5 = Matrix[[2, 0, 9, 3, 9], [8, 7, 0, 1, 9], [7, 5, 6, 6, 5], [0, 7, 8, 3, 0], [7, 8, 2, 3, 1]]
20
+ @b3 = Matrix[[-7, 7, -10], [9, -3, -2], [-1, 3, 9]]
21
+ end
22
+
23
+ def test_matrix
24
+ assert_equal(1, @m1[0, 0])
25
+ assert_equal(2, @m1[0, 1])
26
+ assert_equal(3, @m1[0, 2])
27
+ assert_equal(4, @m1[1, 0])
28
+ assert_equal(5, @m1[1, 1])
29
+ assert_equal(6, @m1[1, 2])
30
+ end
31
+
32
+ def test_identity
33
+ assert_same @m1, @m1
34
+ # Replaced
35
+ # assert_not_same obj1, obj2
36
+ # to supported equivalent
37
+ # assert !obj1.equal?(obj2)
38
+ assert !@m1.equal?(@m2)
39
+ assert !@m1.equal?(@m3)
40
+ assert !@m1.equal?(@m4)
41
+ assert !@m1.equal?(@n1)
42
+ end
43
+
44
+ def test_equality
45
+ assert_equal @m1, @m1
46
+ assert_equal @m1, @m2
47
+ assert_equal @m1, @m3
48
+ assert_equal @m1, @m4
49
+ assert_not_equal @m1, @n1
50
+ end
51
+
52
+ def test_hash_equality
53
+ assert @m1.eql?(@m1)
54
+ assert @m1.eql?(@m2)
55
+ assert @m1.eql?(@m3)
56
+ # Fails because of expected difference between 1 and 1.0
57
+ # assert !@m1.eql?(@m4)
58
+ assert !@m1.eql?(@n1)
59
+
60
+ hash = { @m1 => :value }
61
+ assert hash.key?(@m1)
62
+ assert hash.key?(@m2)
63
+ assert hash.key?(@m3)
64
+ # Fails because of expected difference between 1 and 1.0
65
+ # assert !hash.key?(@m4)
66
+ assert !hash.key?(@n1)
67
+ end
68
+
69
+ def test_hash
70
+ assert_equal @m1.hash, @m1.hash
71
+ assert_equal @m1.hash, @m2.hash
72
+ assert_equal @m1.hash, @m3.hash
73
+ end
74
+
75
+ def test_uplus
76
+ assert_equal(@m1, +@m1)
77
+ end
78
+
79
+ def test_negate
80
+ assert_equal(Matrix[[-1, -2, -3], [-4, -5, -6]], -@m1)
81
+ assert_equal(@m1, -(-@m1))
82
+ end
83
+
84
+ def test_rank
85
+ [
86
+ [[0]],
87
+ [[0], [0]],
88
+ [[0, 0], [0, 0]],
89
+ [[0, 0], [0, 0], [0, 0]],
90
+ [[0, 0, 0]],
91
+ [[0, 0, 0], [0, 0, 0]],
92
+ [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
93
+ [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
94
+ ].each do |rows|
95
+ assert_equal 0, Matrix[*rows].rank
96
+ end
97
+
98
+ [
99
+ [[1], [0]],
100
+ [[1, 0], [0, 0]],
101
+ [[1, 0], [1, 0]],
102
+ [[0, 0], [1, 0]],
103
+ [[1, 0], [0, 0], [0, 0]],
104
+ [[0, 0], [1, 0], [0, 0]],
105
+ [[0, 0], [0, 0], [1, 0]],
106
+ [[1, 0], [1, 0], [0, 0]],
107
+ [[0, 0], [1, 0], [1, 0]],
108
+ [[1, 0], [1, 0], [1, 0]],
109
+ [[1, 0, 0]],
110
+ [[1, 0, 0], [0, 0, 0]],
111
+ [[0, 0, 0], [1, 0, 0]],
112
+ [[1, 0, 0], [1, 0, 0]],
113
+ [[1, 0, 0], [1, 0, 0]],
114
+ [[1, 0, 0], [0, 0, 0], [0, 0, 0]],
115
+ [[0, 0, 0], [1, 0, 0], [0, 0, 0]],
116
+ [[0, 0, 0], [0, 0, 0], [1, 0, 0]],
117
+ [[1, 0, 0], [1, 0, 0], [0, 0, 0]],
118
+ [[0, 0, 0], [1, 0, 0], [1, 0, 0]],
119
+ [[1, 0, 0], [0, 0, 0], [1, 0, 0]],
120
+ [[1, 0, 0], [1, 0, 0], [1, 0, 0]],
121
+ [[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
122
+ [[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
123
+ [[1, 0, 0], [1, 0, 0], [0, 0, 0], [0, 0, 0]],
124
+ [[1, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 0]],
125
+ [[1, 0, 0], [0, 0, 0], [0, 0, 0], [1, 0, 0]],
126
+ [[1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 0, 0]],
127
+ [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 0, 0]],
128
+ [[1, 0, 0], [1, 0, 0], [0, 0, 0], [1, 0, 0]],
129
+ [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]],
130
+
131
+ [[1]],
132
+ [[1], [1]],
133
+ [[1, 1]],
134
+ [[1, 1], [1, 1]],
135
+ [[1, 1], [1, 1], [1, 1]],
136
+ [[1, 1, 1]],
137
+ [[1, 1, 1], [1, 1, 1]],
138
+ [[1, 1, 1], [1, 1, 1], [1, 1, 1]],
139
+ [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]],
140
+ ].each do |rows|
141
+ matrix = Matrix[*rows]
142
+ assert_equal 1, matrix.rank
143
+ assert_equal 1, matrix.transpose.rank
144
+ end
145
+
146
+ [
147
+ [[1, 0], [0, 1]],
148
+ [[1, 0], [0, 1], [0, 0]],
149
+ [[1, 0], [0, 1], [0, 1]],
150
+ [[1, 0], [0, 1], [1, 1]],
151
+ [[1, 0, 0], [0, 1, 0]],
152
+ [[1, 0, 0], [0, 0, 1]],
153
+ [[1, 0, 0], [0, 1, 0], [0, 0, 0]],
154
+ [[1, 0, 0], [0, 0, 1], [0, 0, 0]],
155
+
156
+ [[1, 0, 0], [0, 0, 0], [0, 1, 0]],
157
+ [[1, 0, 0], [0, 0, 0], [0, 0, 1]],
158
+
159
+ [[1, 0], [1, 1]],
160
+ [[1, 2], [1, 1]],
161
+ [[1, 2], [0, 1], [1, 1]],
162
+ ].each do |rows|
163
+ m = Matrix[*rows]
164
+ assert_equal 2, m.rank
165
+ assert_equal 2, m.transpose.rank
166
+ end
167
+
168
+ [
169
+ [[1, 0, 0], [0, 1, 0], [0, 0, 1]],
170
+ [[1, 1, 0], [0, 1, 1], [1, 0, 1]],
171
+ [[1, 1, 0], [0, 1, 1], [1, 0, 1]],
172
+ [[1, 1, 0], [0, 1, 1], [1, 0, 1], [0, 0, 0]],
173
+ [[1, 1, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]],
174
+ [[1, 1, 1], [1, 1, 2], [1, 3, 1], [4, 1, 1]],
175
+ ].each do |rows|
176
+ m = Matrix[*rows]
177
+ assert_equal 3, m.rank
178
+ assert_equal 3, m.transpose.rank
179
+ end
180
+ end
181
+
182
+ def test_inverse
183
+ assert_equal(Matrix.empty(0, 0), Matrix.empty.inverse)
184
+ assert_equal(Matrix[[-1, 1], [0, -1]], Matrix[[-1, -1], [0, -1]].inverse)
185
+ assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.inverse }
186
+ end
187
+
188
+ def test_determinant
189
+ assert_equal(0, Matrix[[0,0],[0,0]].determinant)
190
+ assert_equal(45, Matrix[[7,6], [3,9]].determinant)
191
+ assert_equal(-18, Matrix[[2,0,1],[0,-2,2],[1,2,3]].determinant)
192
+ assert_equal(-7, Matrix[[0,0,1],[0,7,6],[1,3,9]].determinant)
193
+ assert_equal(42, Matrix[[7,0,1,0,12],[8,1,1,9,1],[4,0,0,-7,17],[-1,0,0,-4,8],[10,1,1,8,6]].determinant)
194
+ end
195
+
196
+ def test_new_matrix
197
+ assert_raise(TypeError) { Matrix[Object.new] }
198
+ o = Object.new
199
+ def o.to_ary; [1,2,3]; end
200
+ assert_equal(@m1, Matrix[o, [4,5,6]])
201
+ end
202
+
203
+ def test_round
204
+ a = Matrix[[1.0111, 2.32320, 3.04343], [4.81, 5.0, 6.997]]
205
+ b = Matrix[[1.01, 2.32, 3.04], [4.81, 5.0, 7.0]]
206
+ assert_equal(a.round(2), b)
207
+ end
208
+
209
+ def test_rows
210
+ assert_equal(@m1, Matrix.rows([[1, 2, 3], [4, 5, 6]]))
211
+ end
212
+
213
+ def test_rows_copy
214
+ rows1 = [[1], [1]]
215
+ rows2 = [[1], [1]]
216
+
217
+ m1 = Matrix.rows(rows1, copy = false)
218
+ m2 = Matrix.rows(rows2, copy = true)
219
+
220
+ rows1.uniq!
221
+ rows2.uniq!
222
+
223
+ assert_equal([[1]], m1.to_a)
224
+ assert_equal([[1], [1]], m2.to_a)
225
+ end
226
+
227
+ def test_to_matrix
228
+ assert @m1.equal? @m1.to_matrix
229
+ end
230
+
231
+ def test_columns
232
+ assert_equal(@m1, Matrix.columns([[1, 4], [2, 5], [3, 6]]))
233
+ end
234
+
235
+ def test_diagonal
236
+ assert_equal(Matrix.empty(0, 0), Matrix.diagonal( ))
237
+ assert_equal(Matrix[[3,0,0],[0,2,0],[0,0,1]], Matrix.diagonal(3, 2, 1))
238
+ assert_equal(Matrix[[4,0,0,0],[0,3,0,0],[0,0,2,0],[0,0,0,1]], Matrix.diagonal(4, 3, 2, 1))
239
+ end
240
+
241
+ def test_scalar
242
+ assert_equal(Matrix.empty(0, 0), Matrix.scalar(0, 1))
243
+ assert_equal(Matrix[[2,0,0],[0,2,0],[0,0,2]], Matrix.scalar(3, 2))
244
+ assert_equal(Matrix[[2,0,0,0],[0,2,0,0],[0,0,2,0],[0,0,0,2]], Matrix.scalar(4, 2))
245
+ end
246
+
247
+ def test_identity2
248
+ assert_equal(Matrix[[1,0,0],[0,1,0],[0,0,1]], Matrix.identity(3))
249
+ assert_equal(Matrix[[1,0,0],[0,1,0],[0,0,1]], Matrix.unit(3))
250
+ assert_equal(Matrix[[1,0,0],[0,1,0],[0,0,1]], Matrix.I(3))
251
+ assert_equal(Matrix[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], Matrix.identity(4))
252
+ end
253
+
254
+ def test_zero
255
+ assert_equal(Matrix[[0,0,0],[0,0,0],[0,0,0]], Matrix.zero(3))
256
+ assert_equal(Matrix[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]], Matrix.zero(4))
257
+ assert_equal(Matrix[[0]], Matrix.zero(1))
258
+ end
259
+
260
+ def test_row_vector
261
+ assert_equal(Matrix[[1,2,3,4]], Matrix.row_vector([1,2,3,4]))
262
+ end
263
+
264
+ def test_column_vector
265
+ assert_equal(Matrix[[1],[2],[3],[4]], Matrix.column_vector([1,2,3,4]))
266
+ end
267
+
268
+ def test_empty
269
+ m = Matrix.empty(2, 0)
270
+ assert_equal(Matrix[ [], [] ], m)
271
+ n = Matrix.empty(0, 3)
272
+ assert_equal(Matrix.columns([ [], [], [] ]), n)
273
+ assert_equal(Matrix[[0, 0, 0], [0, 0, 0]], m * n)
274
+ end
275
+
276
+ def test_row
277
+ assert_equal(Vector[1, 2, 3], @m1.row(0))
278
+ assert_equal(Vector[4, 5, 6], @m1.row(1))
279
+ a = []; @m1.row(0) {|x| a << x }
280
+ assert_equal([1, 2, 3], a)
281
+ end
282
+
283
+ def test_column
284
+ assert_equal(Vector[1, 4], @m1.column(0))
285
+ assert_equal(Vector[2, 5], @m1.column(1))
286
+ assert_equal(Vector[3, 6], @m1.column(2))
287
+ a = []; @m1.column(0) {|x| a << x }
288
+ assert_equal([1, 4], a)
289
+ end
290
+
291
+ def test_collect
292
+ assert_equal(Matrix[[1, 4, 9], [16, 25, 36]], @m1.collect {|x| x ** 2 })
293
+ end
294
+
295
+ def test_minor
296
+ assert_equal(Matrix[[1, 2], [4, 5]], @m1.minor(0..1, 0..1))
297
+ assert_equal(Matrix[[2], [5]], @m1.minor(0..1, 1..1))
298
+ assert_equal(Matrix[[4, 5]], @m1.minor(1..1, 0..1))
299
+ assert_equal(Matrix[[1, 2], [4, 5]], @m1.minor(0, 2, 0, 2))
300
+ assert_equal(Matrix[[4, 5]], @m1.minor(1, 1, 0, 2))
301
+ assert_equal(Matrix[[2], [5]], @m1.minor(0, 2, 1, 1))
302
+ assert_raise(ArgumentError) { @m1.minor(0) }
303
+ end
304
+
305
+ def test_first_minor
306
+ assert_equal(Matrix.empty(0, 0), Matrix[[1]].first_minor(0, 0))
307
+ assert_equal(Matrix.empty(0, 2), Matrix[[1, 4, 2]].first_minor(0, 1))
308
+ assert_equal(Matrix[[1, 3]], @m1.first_minor(1, 1))
309
+ assert_equal(Matrix[[4, 6]], @m1.first_minor(0, 1))
310
+ assert_equal(Matrix[[1, 2]], @m1.first_minor(1, 2))
311
+ assert_raise(RuntimeError) { Matrix.empty(0, 0).first_minor(0, 0) }
312
+ assert_raise(ArgumentError) { @m1.first_minor(4, 0) }
313
+ assert_raise(ArgumentError) { @m1.first_minor(0, -1) }
314
+ assert_raise(ArgumentError) { @m1.first_minor(-1, 4) }
315
+ end
316
+
317
+ def test_cofactor
318
+ assert_equal(1, Matrix[[1]].cofactor(0, 0))
319
+ assert_equal(9, Matrix[[7,6],[3,9]].cofactor(0, 0))
320
+ assert_equal(0, Matrix[[0,0],[0,0]].cofactor(0, 0))
321
+ assert_equal(3, Matrix[[0,0,1],[0,7,6],[1,3,9]].cofactor(1, 0))
322
+ assert_equal(-21, Matrix[[7,0,1,0,12],[8,1,1,9,1],[4,0,0,-7,17],[-1,0,0,-4,8],[10,1,1,8,6]].cofactor(2, 3))
323
+ assert_raise(RuntimeError) { Matrix.empty(0, 0).cofactor(0, 0) }
324
+ assert_raise(ArgumentError) { Matrix[[0,0],[0,0]].cofactor(-1, 4) }
325
+ assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { Matrix[[2,0,1],[0,-2,2]].cofactor(0, 0) }
326
+ end
327
+
328
+ def test_adjugate
329
+ assert_equal(Matrix.empty, Matrix.empty.adjugate)
330
+ assert_equal(Matrix[[1]], Matrix[[5]].adjugate)
331
+ assert_equal(Matrix[[9,-6],[-3,7]], Matrix[[7,6],[3,9]].adjugate)
332
+ assert_equal(Matrix[[45,3,-7],[6,-1,0],[-7,0,0]], Matrix[[0,0,1],[0,7,6],[1,3,9]].adjugate)
333
+ assert_equal(Matrix.identity(5), (@a5.adjugate * @a5) / (@a5.det))
334
+ assert_equal(Matrix.I(3), Matrix.I(3).adjugate)
335
+ assert_equal((@a3 * @b3).adjugate, @b3.adjugate * @a3.adjugate)
336
+ assert_equal(4**(@a3.row_count-1) * @a3.adjugate, (4 * @a3).adjugate)
337
+ assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.adjugate }
338
+ end
339
+
340
+ def test_laplace_expansion
341
+ assert_equal(1, Matrix[[1]].laplace_expansion(row: 0))
342
+ assert_equal(45, Matrix[[7,6], [3,9]].laplace_expansion(row: 1))
343
+ assert_equal(0, Matrix[[0,0],[0,0]].laplace_expansion(column: 0))
344
+ assert_equal(-7, Matrix[[0,0,1],[0,7,6],[1,3,9]].laplace_expansion(column: 2))
345
+
346
+ assert_equal(Vector[3, -2], Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(row: 0))
347
+
348
+ assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.laplace_expansion(row: 1) }
349
+ assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion() }
350
+ assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(foo: 1) }
351
+ assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(row: 1, column: 1) }
352
+ assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(row: 2) }
353
+ assert_raise(ArgumentError) { Matrix[[0,0,1],[0,7,6],[1,3,9]].laplace_expansion(column: -1) }
354
+
355
+ assert_raise(RuntimeError) { Matrix.empty(0, 0).laplace_expansion(row: 0) }
356
+ end
357
+
358
+ def test_regular?
359
+ assert(Matrix[[1, 0], [0, 1]].regular?)
360
+ assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].regular?)
361
+ assert(!Matrix[[1, 0, 0], [0, 0, 1], [0, 0, 1]].regular?)
362
+ end
363
+
364
+ def test_singular?
365
+ assert(!Matrix[[1, 0], [0, 1]].singular?)
366
+ assert(!Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].singular?)
367
+ assert(Matrix[[1, 0, 0], [0, 0, 1], [0, 0, 1]].singular?)
368
+ end
369
+
370
+ def test_square?
371
+ assert(Matrix[[1, 0], [0, 1]].square?)
372
+ assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].square?)
373
+ assert(Matrix[[1, 0, 0], [0, 0, 1], [0, 0, 1]].square?)
374
+ assert(!Matrix[[1, 0, 0], [0, 1, 0]].square?)
375
+ end
376
+
377
+ def test_mul
378
+ assert_equal(Matrix[[2,4],[6,8]], Matrix[[2,4],[6,8]] * Matrix.I(2))
379
+ assert_equal(Matrix[[4,8],[12,16]], Matrix[[2,4],[6,8]] * 2)
380
+ assert_equal(Matrix[[4,8],[12,16]], 2 * Matrix[[2,4],[6,8]])
381
+ assert_equal(Matrix[[14,32],[32,77]], @m1 * @m1.transpose)
382
+ assert_equal(Matrix[[17,22,27],[22,29,36],[27,36,45]], @m1.transpose * @m1)
383
+ assert_equal(Vector[14,32], @m1 * Vector[1,2,3])
384
+ o = Object.new
385
+ def o.coerce(m)
386
+ [m, m.transpose]
387
+ end
388
+ assert_equal(Matrix[[14,32],[32,77]], @m1 * o)
389
+ end
390
+
391
+ def test_add
392
+ assert_equal(Matrix[[6,0],[-4,12]], Matrix.scalar(2,5) + Matrix[[1,0],[-4,7]])
393
+ assert_equal(Matrix[[3,5,7],[9,11,13]], @m1 + @n1)
394
+ assert_equal(Matrix[[3,5,7],[9,11,13]], @n1 + @m1)
395
+ assert_equal(Matrix[[2],[4],[6]], Matrix[[1],[2],[3]] + Vector[1,2,3])
396
+ assert_raise(Matrix::ErrOperationNotDefined) { @m1 + 1 }
397
+ o = Object.new
398
+ def o.coerce(m)
399
+ [m, m]
400
+ end
401
+ assert_equal(Matrix[[2,4,6],[8,10,12]], @m1 + o)
402
+ end
403
+
404
+ def test_sub
405
+ assert_equal(Matrix[[4,0],[4,-2]], Matrix.scalar(2,5) - Matrix[[1,0],[-4,7]])
406
+ assert_equal(Matrix[[-1,-1,-1],[-1,-1,-1]], @m1 - @n1)
407
+ assert_equal(Matrix[[1,1,1],[1,1,1]], @n1 - @m1)
408
+ assert_equal(Matrix[[0],[0],[0]], Matrix[[1],[2],[3]] - Vector[1,2,3])
409
+ assert_raise(Matrix::ErrOperationNotDefined) { @m1 - 1 }
410
+ o = Object.new
411
+ def o.coerce(m)
412
+ [m, m]
413
+ end
414
+ assert_equal(Matrix[[0,0,0],[0,0,0]], @m1 - o)
415
+ end
416
+
417
+ def test_div
418
+ # Fails because in Opal 1 / 2 = 0.5
419
+ # assert_equal(Matrix[[0,1,1],[2,2,3]], @m1 / 2)
420
+ assert_equal(Matrix[[1,1],[1,1]], Matrix[[2,2],[2,2]] / Matrix.scalar(2,2))
421
+ o = Object.new
422
+ def o.coerce(m)
423
+ [m, Matrix.scalar(2,2)]
424
+ end
425
+ assert_equal(Matrix[[1,1],[1,1]], Matrix[[2,2],[2,2]] / o)
426
+ end
427
+
428
+ def test_hadamard_product
429
+ assert_equal(Matrix[[1,4], [9,16]], Matrix[[1,2], [3,4]].hadamard_product(Matrix[[1,2], [3,4]]))
430
+ assert_equal(Matrix[[2, 6, 12], [20, 30, 42]], @m1.hadamard_product(@n1))
431
+ o = Object.new
432
+ def o.to_matrix
433
+ Matrix[[1, 2, 3], [-1, 0, 1]]
434
+ end
435
+ assert_equal(Matrix[[1, 4, 9], [-4, 0, 6]], @m1.hadamard_product(o))
436
+ e = Matrix.empty(3, 0)
437
+ assert_equal(e, e.hadamard_product(e))
438
+ e = Matrix.empty(0, 3)
439
+ assert_equal(e, e.hadamard_product(e))
440
+ end
441
+
442
+ def test_exp
443
+ assert_equal(Matrix[[67,96],[48,99]], Matrix[[7,6],[3,9]] ** 2)
444
+ assert_equal(Matrix.I(5), Matrix.I(5) ** -1)
445
+ assert_raise(Matrix::ErrOperationNotDefined) { Matrix.I(5) ** Object.new }
446
+ end
447
+
448
+ # Fails because of Opal copies a method on aliasing
449
+ # def test_det
450
+ # assert_equal(Matrix.instance_method(:determinant), Matrix.instance_method(:det))
451
+ # end
452
+
453
+ def test_rank2
454
+ assert_equal(2, Matrix[[7,6],[3,9]].rank)
455
+ assert_equal(0, Matrix[[0,0],[0,0]].rank)
456
+ assert_equal(3, Matrix[[0,0,1],[0,7,6],[1,3,9]].rank)
457
+ assert_equal(1, Matrix[[0,1],[0,1],[0,1]].rank)
458
+ assert_equal(2, @m1.rank)
459
+ end
460
+
461
+ def test_trace
462
+ assert_equal(1+5+9, Matrix[[1,2,3],[4,5,6],[7,8,9]].trace)
463
+ end
464
+
465
+ def test_transpose
466
+ assert_equal(Matrix[[1,4],[2,5],[3,6]], @m1.transpose)
467
+ end
468
+
469
+ def test_conjugate
470
+ assert_equal(Matrix[[Complex(1,-2), Complex(0,-1), 0], [1, 2, 3]], @c1.conjugate)
471
+ end
472
+
473
+ def test_eigensystem
474
+ m = Matrix[[1, 2], [3, 4]]
475
+ v, d, v_inv = m.eigensystem
476
+ assert(d.diagonal?)
477
+ assert_equal(v.inv, v_inv)
478
+ # Fails because of the difference in rounding
479
+ # assert_equal((v * d * v_inv).round(5), m)
480
+ end
481
+
482
+ def test_imaginary
483
+ assert_equal(Matrix[[2, 1, 0], [0, 0, 0]], @c1.imaginary)
484
+ end
485
+
486
+ def test_lup
487
+ m = Matrix[[1, 2], [3, 4]]
488
+ l, u, p = m.lup
489
+ assert(l.lower_triangular?)
490
+ assert(u.upper_triangular?)
491
+ assert(p.permutation?)
492
+ assert(l * u == p * m)
493
+ assert_equal(m.lup.solve([2, 5]), Vector[1, Rational(1,2)])
494
+ end
495
+
496
+ def test_real
497
+ assert_equal(Matrix[[1, 0, 0], [1, 2, 3]], @c1.real)
498
+ end
499
+
500
+ def test_rect
501
+ assert_equal([Matrix[[1, 0, 0], [1, 2, 3]], Matrix[[2, 1, 0], [0, 0, 0]]], @c1.rect)
502
+ end
503
+
504
+ def test_row_vectors
505
+ assert_equal([Vector[1,2,3], Vector[4,5,6]], @m1.row_vectors)
506
+ end
507
+
508
+ def test_column_vectors
509
+ assert_equal([Vector[1,4], Vector[2,5], Vector[3,6]], @m1.column_vectors)
510
+ end
511
+
512
+ def test_to_s
513
+ assert_equal("Matrix[[1, 2, 3], [4, 5, 6]]", @m1.to_s)
514
+ assert_equal("Matrix.empty(0, 0)", Matrix[].to_s)
515
+ assert_equal("Matrix.empty(1, 0)", Matrix[[]].to_s)
516
+ end
517
+
518
+ def test_inspect
519
+ assert_equal("Matrix[[1, 2, 3], [4, 5, 6]]", @m1.inspect)
520
+ assert_equal("Matrix.empty(0, 0)", Matrix[].inspect)
521
+ assert_equal("Matrix.empty(1, 0)", Matrix[[]].inspect)
522
+ end
523
+
524
+ def test_scalar_add
525
+ s1 = @m1.coerce(1).first
526
+ assert_equal(Matrix[[1]], (s1 + 0) * Matrix[[1]])
527
+ assert_raise(Matrix::ErrOperationNotDefined) { s1 + Vector[0] }
528
+ assert_raise(Matrix::ErrOperationNotDefined) { s1 + Matrix[[0]] }
529
+ o = Object.new
530
+ def o.coerce(x)
531
+ [1, 1]
532
+ end
533
+ assert_equal(2, s1 + o)
534
+ end
535
+
536
+ def test_scalar_sub
537
+ s1 = @m1.coerce(1).first
538
+ assert_equal(Matrix[[1]], (s1 - 0) * Matrix[[1]])
539
+ assert_raise(Matrix::ErrOperationNotDefined) { s1 - Vector[0] }
540
+ assert_raise(Matrix::ErrOperationNotDefined) { s1 - Matrix[[0]] }
541
+ o = Object.new
542
+ def o.coerce(x)
543
+ [1, 1]
544
+ end
545
+ assert_equal(0, s1 - o)
546
+ end
547
+
548
+ def test_scalar_mul
549
+ s1 = @m1.coerce(1).first
550
+ assert_equal(Matrix[[1]], (s1 * 1) * Matrix[[1]])
551
+ assert_equal(Vector[2], s1 * Vector[2])
552
+ assert_equal(Matrix[[2]], s1 * Matrix[[2]])
553
+ o = Object.new
554
+ def o.coerce(x)
555
+ [1, 1]
556
+ end
557
+ assert_equal(1, s1 * o)
558
+ end
559
+
560
+ def test_scalar_div
561
+ s1 = @m1.coerce(1).first
562
+ assert_equal(Matrix[[1]], (s1 / 1) * Matrix[[1]])
563
+ assert_raise(Matrix::ErrOperationNotDefined) { s1 / Vector[0] }
564
+ assert_equal(Matrix[[Rational(1,2)]], s1 / Matrix[[2]])
565
+ o = Object.new
566
+ def o.coerce(x)
567
+ [1, 1]
568
+ end
569
+ assert_equal(1, s1 / o)
570
+ end
571
+
572
+ def test_scalar_pow
573
+ s1 = @m1.coerce(1).first
574
+ assert_equal(Matrix[[1]], (s1 ** 1) * Matrix[[1]])
575
+ assert_raise(Matrix::ErrOperationNotDefined) { s1 ** Vector[0] }
576
+ assert_raise(Matrix::ErrOperationNotImplemented) { s1 ** Matrix[[1]] }
577
+ o = Object.new
578
+ def o.coerce(x)
579
+ [1, 1]
580
+ end
581
+ assert_equal(1, s1 ** o)
582
+ end
583
+
584
+ def test_hstack
585
+ assert_equal Matrix[[1,2,3,2,3,4,1,2,3], [4,5,6,5,6,7,4,5,6]],
586
+ @m1.hstack(@n1, @m1)
587
+ # Error checking:
588
+ assert_raise(TypeError) { @m1.hstack(42) }
589
+ assert_raise(TypeError) { Matrix.hstack(42, @m1) }
590
+ assert_raise(Matrix::ErrDimensionMismatch) { @m1.hstack(Matrix.identity(3)) }
591
+ assert_raise(Matrix::ErrDimensionMismatch) { @e1.hstack(@e2) }
592
+ # Corner cases:
593
+ assert_equal @m1, @m1.hstack
594
+ assert_equal @e1, @e1.hstack(@e1)
595
+ assert_equal Matrix.empty(0,6), @e2.hstack(@e2)
596
+ assert_equal SubMatrix, SubMatrix.hstack(@e1).class
597
+ # From Vectors:
598
+ assert_equal Matrix[[1, 3],[2, 4]], Matrix.hstack(Vector[1,2], Vector[3, 4])
599
+ end
600
+
601
+ def test_vstack
602
+ assert_equal Matrix[[1,2,3], [4,5,6], [2,3,4], [5,6,7], [1,2,3], [4,5,6]],
603
+ @m1.vstack(@n1, @m1)
604
+ # Error checking:
605
+ assert_raise(TypeError) { @m1.vstack(42) }
606
+ assert_raise(TypeError) { Matrix.vstack(42, @m1) }
607
+ assert_raise(Matrix::ErrDimensionMismatch) { @m1.vstack(Matrix.identity(2)) }
608
+ assert_raise(Matrix::ErrDimensionMismatch) { @e1.vstack(@e2) }
609
+ # Corner cases:
610
+ assert_equal @m1, @m1.vstack
611
+ assert_equal Matrix.empty(4,0), @e1.vstack(@e1)
612
+ assert_equal @e2, @e2.vstack(@e2)
613
+ assert_equal SubMatrix, SubMatrix.vstack(@e1).class
614
+ # From Vectors:
615
+ assert_equal Matrix[[1],[2],[3]], Matrix.vstack(Vector[1,2], Vector[3])
616
+ end
617
+
618
+ def test_combine
619
+ x = Matrix[[6, 6], [4, 4]]
620
+ y = Matrix[[1, 2], [3, 4]]
621
+ assert_equal Matrix[[5, 4], [1, 0]], Matrix.combine(x, y) {|a, b| a - b}
622
+ assert_equal Matrix[[5, 4], [1, 0]], x.combine(y) {|a, b| a - b}
623
+ # Without block
624
+ assert_equal Matrix[[5, 4], [1, 0]], Matrix.combine(x, y).each {|a, b| a - b}
625
+ # With vectors
626
+ assert_equal Matrix[[111], [222]], Matrix.combine(Matrix[[1], [2]], Vector[10,20], Vector[100,200], &:sum)
627
+ # Basic checks
628
+ assert_raise(Matrix::ErrDimensionMismatch) { @m1.combine(x) { raise } }
629
+ # Edge cases
630
+ assert_equal Matrix.empty, Matrix.combine{ raise }
631
+ assert_equal Matrix.empty(3,0), Matrix.combine(Matrix.empty(3,0), Matrix.empty(3,0)) { raise }
632
+ assert_equal Matrix.empty(0,3), Matrix.combine(Matrix.empty(0,3), Matrix.empty(0,3)) { raise }
633
+ end
634
+
635
+ def test_eigenvalues_and_eigenvectors_symmetric
636
+ m = Matrix[
637
+ [8, 1],
638
+ [1, 8]
639
+ ]
640
+ values = m.eigensystem.eigenvalues
641
+ assert_in_epsilon(7.0, values[0])
642
+ assert_in_epsilon(9.0, values[1])
643
+ vectors = m.eigensystem.eigenvectors
644
+ assert_in_epsilon(-vectors[0][0], vectors[0][1])
645
+ assert_in_epsilon(vectors[1][0], vectors[1][1])
646
+ end
647
+
648
+ # Fails because of the difference in math
649
+ # def test_eigenvalues_and_eigenvectors_nonsymmetric
650
+ # m = Matrix[
651
+ # [8, 1],
652
+ # [4, 5]
653
+ # ]
654
+ # values = m.eigensystem.eigenvalues
655
+ # assert_in_epsilon(9.0, values[0])
656
+ # assert_in_epsilon(4.0, values[1])
657
+ # vectors = m.eigensystem.eigenvectors
658
+ # assert_in_epsilon(vectors[0][0], vectors[0][1])
659
+ # assert_in_epsilon(-4 * vectors[1][0], vectors[1][1])
660
+ # end
661
+ end