diff options
author | Vladimir Dementyev <dementiev.vm@gmail.com> | 2020-03-03 18:42:48 -0500 |
---|---|---|
committer | Kazuki Tsujimoto <kazuki@callcc.net> | 2020-06-27 13:51:03 +0900 |
commit | 5320375732a400ef915c8acbf810aed4ac5261b7 (patch) | |
tree | f41ad623d24db53e26c43dba611f897dee87c558 /test/ruby/test_pattern_matching.rb | |
parent | 6770d8f1b066908e5fb7401860fd6455df961281 (diff) |
Optimize array pattern matching by caching #deconstruct value
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3104
Diffstat (limited to 'test/ruby/test_pattern_matching.rb')
-rw-r--r-- | test/ruby/test_pattern_matching.rb | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb index de8515b397..78e3a4e8f3 100644 --- a/test/ruby/test_pattern_matching.rb +++ b/test/ruby/test_pattern_matching.rb @@ -1265,6 +1265,85 @@ END ################################################################ + class CDeconstructCache + def initialize(v) + @v = v + end + + def deconstruct + @v.shift + end + end + + def test_deconstruct_cache + assert_block do + case CDeconstructCache.new([[0]]) + in [1] + in [0] + true + end + end + + assert_block do + case CDeconstructCache.new([[0, 1]]) + in [1,] + in [0,] + true + end + end + + assert_block do + case CDeconstructCache.new([[[0]]]) + in [[1]] + in [[*a]] + true + end + end + + assert_block do + case CDeconstructCache.new([[0]]) + in [x] if x > 0 + in [0] + true + end + end + + assert_block do + case CDeconstructCache.new([[0]]) + in [] + in [1] | [0] + true + end + end + + assert_block do + case CDeconstructCache.new([[0]]) + in [1] => one + in [0] => zero + true + end + end + + assert_block do + case CDeconstructCache.new([[0]]) + in C[0] + in CDeconstructCache[0] + true + end + end + + assert_block do + case [CDeconstructCache.new([[0], [1]])] + in [[1]] + false + in [[1]] + true + end + end + end + + ################################################################ + class TestPatternMatchingRefinements < Test::Unit::TestCase class C1 def deconstruct |