Module: LC3Spec::Expectations

Extended by:
Helpers
Defined in:
lib/lc3spec/expectations.rb

Class Method Summary collapse

Methods included from Helpers

is_absolute_path?, normalize_to_i, normalize_to_s

Class Method Details

.diff(expected, actual) ⇒ Object



35
36
37
# File 'lib/lc3spec/expectations.rb', line 35

def self.diff(expected, actual)
  "expected: #{expected}, actual: #{actual}"
end

.expect_memory(lc3, reporter, addr, val) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/lc3spec/expectations.rb', line 17

def self.expect_memory(lc3, reporter, addr, val)
  expected = normalize_to_s(val)
  actual = lc3.get_memory(addr)

  unless expected == actual
    reporter.report "Incorrect mem[#{addr}]: #{diff(expected, actual)}"
  end
end

.expect_output(lc3, reporter, expected) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/lc3spec/expectations.rb', line 26

def self.expect_output(lc3, reporter, expected)
  actual = lc3.get_output

  unless ignore_whitespace_equal(expected, actual)
    reporter.report "Incorrect output:\n" +
      "  expected:\n#{strblock(expected)}\n  actual:\n#{strblock(actual)}"
  end
end

.expect_register(lc3, reporter, reg, val) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/lc3spec/expectations.rb', line 8

def self.expect_register(lc3, reporter, reg, val)
  expected = normalize_to_s(val)
  actual = lc3.get_register(reg)

  unless expected == actual
    reporter.report "Incorrect #{reg.to_s}: #{diff(expected, actual)}"
  end
end

.ignore_whitespace_equal(lhs, rhs) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lc3spec/expectations.rb', line 48

def self.ignore_whitespace_equal(lhs, rhs)
  lhs = lhs.each_line.map do |line|
    if line =~ /^\s*$/
      nil
    else
      line.gsub(/[ \t]+/, ' ')
    end
  end.compact.join('').strip

  rhs = rhs.each_line.map do |line|
    if line =~ /^\s*$/
      nil
    else
      line.gsub(/[ \t]+/, ' ')
    end
  end.compact.join('').strip

  lhs == rhs
end

.strblock(str, indent = ' ') ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/lc3spec/expectations.rb', line 39

def self.strblock(str, indent = '    ')
  return str if str.nil? or str.empty?
  block = ''
  str.each_line do |line|
    block << indent + line
  end
  block
end