Module: Funit::Assertions

Included in:
Funit
Defined in:
lib/funit/assertions.rb

Overview

Fortran assertion macro definitions

Constant Summary collapse

ASSERTION_PATTERN =
/^\s*?(assert_(array_equal|real_equal|false|true|equal_within|equal))\(.*\)/i

Instance Method Summary collapse

Instance Method Details

#assert_array_equal(line) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/funit/assertions.rb', line 59

def assert_array_equal(line)
  line.match(/\((\w+),(\w+)\)/)
  @type = 'Assert_Array_Equal'
  @condition = ".not. all(#$1==#$2)"
  @message = "\"array #$1 is not #$2\""
  syntax_error("invalid body for #@type",@suite_name) unless $&
  write_assert
end

#assert_equal(line) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/funit/assertions.rb', line 50

def assert_equal(line)
  line.match(/\((\w+\(.*\)|[^,]+),(.+)\)/)
  @type = 'Assert_Equal'
  @condition = ".not.(#$1==#$2)"
  @message = "\"#$1 (\",#$1,\") is not\", #$2"
  syntax_error("invalid body for #@type",@suite_name) unless $&
  write_assert
end

#assert_equal_within(line) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/funit/assertions.rb', line 40

def assert_equal_within(line)
  line.match(/\((.*)\)/)
  expected, actual, tolerance = *(get_args($1))
  @type = 'Assert_Equal_Within'
  @condition = ".not.((#{actual} &\n     +#{tolerance}) &\n     .ge. &\n     (#{expected}) &\n             .and. &\n     (#{actual} &\n     -#{tolerance}) &\n     .le. &\n     (#{expected}) )"
  @message = "\"#{expected} (\",#{expected},\") is not\", &\n #{actual},\"within\",#{tolerance}"
  syntax_error("invalid body for #@type",@suite_name) unless $&
  write_assert
end

#assert_false(line) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/funit/assertions.rb', line 21

def assert_false(line)
  line.match(/\((.+)\)/)
  @type = 'Assert_False'
  @condition = "#$1"
  @message = "\"#$1 is not false\""
  syntax_error("invalid body for #@type",@suite_name) unless $1=~/\S+/
  write_assert
end

#assert_real_equal(line) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/funit/assertions.rb', line 30

def assert_real_equal(line)
  line.match(/\((.*)\)/)
  expected, actual = *(get_args($1))
  @type = 'Assert_Real_Equal'
  @condition = ".not.( (#{expected} &\n        +2*spacing(real(#{expected})) ) &\n        .ge. &\n        (#{actual}) &\n            .and. &\n     (#{expected} &\n      -2*spacing(real(#{expected})) ) &\n      .le. &\n       (#{actual}) )"
  @message = "\"#{actual} (\", &\n #{actual}, &\n  \") is not\", &\n #{expected},\&\n \"within\", &\n  2*spacing(real(#{expected}))"
  syntax_error("invalid body for #@type",@suite_name) unless $&
  write_assert
end

#assert_true(line) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/funit/assertions.rb', line 12

def assert_true(line)
  line.match(/\((.+)\)/)
  @type = 'Assert_True'
  @condition = ".not.(#$1)"
  @message = "\"#$1 is not true\""
  syntax_error("invalid body for #@type",@suite_name) unless $1=~/\S+/
  write_assert
end

#get_args(string) ⇒ Object

An argument scanner thanks to James Edward Gray II by way of ruby-talk mailing list.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/funit/assertions.rb', line 72

def get_args(string)
  scanner = ::StringScanner.new(string)
  result  = scanner.eos? ? [] : ['']
  paren_depth = 0
  until scanner.eos?
    if scanner.scan(/[^(),]+/)
      # do nothing--we found the part of the argument we need to add
    elsif scanner.scan(/\(/)
      paren_depth += 1
    elsif scanner.scan(/\)/)
      paren_depth -= 1
    elsif scanner.scan(/,\s*/) and paren_depth.zero?
      result << ''
      next
    end
    result.last << scanner.matched
  end
  result
end

#write_assertObject

Translate the assertion to Fortran.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/funit/assertions.rb', line 95

def write_assert
  <<-OUTPUT
  ! #@type assertion
  numAsserts = numAsserts + 1
  if (noAssertFailed) then
if (#@condition) then
  print *, " *#@type failed* in test #@test_name &
          &[#{@suite_name}.fun:#{@line_number.to_s}]"
  print *, "  ", #@message
  print *, ""
  noAssertFailed = .false.
  numFailures    = numFailures + 1
else
  numAssertsTested = numAssertsTested + 1
endif
  endif
  OUTPUT
end