Skip to content

Commit

Permalink
Correct advance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmrodriguezr committed Jun 17, 2024
1 parent e6bfd64 commit 5cc5596
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/miga/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def say(*par)
# The report goes to $stderr iff --verbose
def advance(step, n = 0, total = nil, bin = true)
# Initialize advance timing
@_advance_time ||= { last: nil, n: 0, avg: nil }
if @_advance_time[:n] > n
@_advance_time ||= { last: nil, n: 0, avg: nil, total: total }
if @_advance_time[:n] > n || total != @_advance_time[:total]
@_advance_time[:last] = nil
@_advance_time[:n] = 0
@_advance_time[:avg] = nil
@_advance_time[:total] = total
end

# Estimate timing
Expand Down
13 changes: 8 additions & 5 deletions test/common_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,26 @@ def test_miga_name
def test_advance
m = MiGA::MiGA.new

# Check advance when missing total
# Check advance when missing total and n
o = capture_stderr { m.advance('x', 0) }.string
assert_match(%r{\] x *\r}, o)

# Check advance when missing total
o = capture_stderr { m.advance('x', 10) }.string
assert_match(%r{\] x 10 *\r}, o)

# Initialize advance
o = capture_stderr { m.advance('x', 0, 1001) }.string
assert_match(%r{\] x 0\.0% \(0/1001\) *\r}, o)

# Insufficient data for prediction
sleep(1)
o = capture_stderr { m.advance('x', 1, 1000) }.string
assert_match(%r{\] x 0\.1% \(1/1000\) *\r}, o)

# Predict time
sleep(1)
o = capture_stderr { m.advance('x', 2, 1000) }.string
assert_match(%r{\] x 0\.2% \(2/1000\) 1\d\.\dm left *\r}, o)
sleep(1.01)
o = capture_stderr { m.advance('x', 4, 1000) }.string
assert_match(%r{\] x 0\.4% \(4/1000\) \d\.\dm left *\r}, o)
end

def test_num_suffix
Expand Down

0 comments on commit 5cc5596

Please sign in to comment.