Skip to content

Commit a14055a

Browse files
committed
Update test libraries from ruby/ruby@b4e438d
1 parent 94fb921 commit a14055a

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

test/lib/core_assertions.rb

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def syntax_check(code, fname, line)
111111
end
112112

113113
def assert_no_memory_leak(args, prepare, code, message=nil, limit: 2.0, rss: false, **opt)
114-
# TODO: consider choosing some appropriate limit for MJIT and stop skipping this once it does not randomly fail
114+
# TODO: consider choosing some appropriate limit for RJIT and stop skipping this once it does not randomly fail
115+
pend 'assert_no_memory_leak may consider RJIT memory usage as leak' if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
116+
# For previous versions which implemented MJIT
115117
pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?
116118

117119
require_relative 'memory_status'
@@ -248,8 +250,11 @@ def separated_runner(token, out = nil)
248250
at_exit {
249251
out.puts "#{token}<error>", [Marshal.dump($!)].pack('m'), "#{token}</error>", "#{token}assertions=#{self._assertions}"
250252
}
251-
Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true) if defined?(Test::Unit::Runner)
252-
Test::Unit::AutoRunner.need_auto_run = false if defined?(Test::Unit::AutoRunner)
253+
if defined?(Test::Unit::Runner)
254+
Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true)
255+
elsif defined?(Test::Unit::AutoRunner)
256+
Test::Unit::AutoRunner.need_auto_run = false
257+
end
253258
end
254259

255260
def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **opt)
@@ -536,11 +541,11 @@ def assert_not_respond_to(obj, (meth, *priv), msg = nil)
536541
refute_respond_to(obj, meth, msg)
537542
end
538543

539-
# pattern_list is an array which contains regexp and :*.
544+
# pattern_list is an array which contains regexp, string and :*.
540545
# :* means any sequence.
541546
#
542547
# pattern_list is anchored.
543-
# Use [:*, regexp, :*] for non-anchored match.
548+
# Use [:*, regexp/string, :*] for non-anchored match.
544549
def assert_pattern_list(pattern_list, actual, message=nil)
545550
rest = actual
546551
anchored = true
@@ -698,7 +703,7 @@ def assert_join_threads(threads, message = nil)
698703
msg = "exceptions on #{errs.length} threads:\n" +
699704
errs.map {|t, err|
700705
"#{t.inspect}:\n" +
701-
err.full_message(highlight: false, order: :top)
706+
(err.respond_to?(:full_message) ? err.full_message(highlight: false, order: :top) : err.message)
702707
}.join("\n---\n")
703708
if message
704709
msg = "#{message}\n#{msg}"
@@ -733,6 +738,39 @@ def assert_all_assertions_foreach(msg = nil, *keys, &block)
733738
end
734739
alias all_assertions_foreach assert_all_assertions_foreach
735740

741+
# Expect +seq+ to respond to +first+ and +each+ methods, e.g.,
742+
# Array, Range, Enumerator::ArithmeticSequence and other
743+
# Enumerable-s, and each elements should be size factors.
744+
#
745+
# :yield: each elements of +seq+.
746+
def assert_linear_performance(seq, rehearsal: nil, pre: ->(n) {n})
747+
first = seq.first
748+
*arg = pre.call(first)
749+
times = (0..(rehearsal || (2 * first))).map do
750+
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
751+
yield(*arg)
752+
t = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
753+
assert_operator 0, :<=, t
754+
t.nonzero?
755+
end
756+
times.compact!
757+
tmin, tmax = times.minmax
758+
tmax *= tmax / tmin
759+
tmax = 10**Math.log10(tmax).ceil
760+
761+
seq.each do |i|
762+
next if i == first
763+
t = tmax * i.fdiv(first)
764+
*arg = pre.call(i)
765+
message = "[#{i}]: in #{t}s"
766+
Timeout.timeout(t, Timeout::Error, message) do
767+
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
768+
yield(*arg)
769+
assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
770+
end
771+
end
772+
end
773+
736774
def diff(exp, act)
737775
require 'pp'
738776
q = PP.new(+"")

0 commit comments

Comments
 (0)