Module Funit::Assertions
In: lib/funit/assertions.rb

Fortran assertion macro definitions

Methods

Constants

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

Public Instance methods

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# 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

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

[Source]

# 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

Translate the assertion to Fortran.

[Source]

# File lib/funit/assertions.rb, line 95
    def write_assert
      "! \#@type assertion\nnumAsserts = numAsserts + 1\nif (noAssertFailed) then\nif (\#@condition) then\nprint *, \" *\#@type failed* in test \#@test_name &\n&[\#{@suite_name}.fun:\#{@line_number.to_s}]\"\nprint *, \"  \", \#@message\nprint *, \"\"\nnoAssertFailed = .false.\nnumFailures    = numFailures + 1\nelse\nnumAssertsTested = numAssertsTested + 1\nendif\nendif\n"
    end

[Validate]