ubuntu

Ubuntu如何进行Fortran单元测试

小樊
41
2025-10-16 05:51:16
栏目: 智能运维

Installing Required Tools
To perform unit testing on Fortran code in Ubuntu, you need a Fortran compiler and a dedicated testing framework. The most commonly used compiler is gfortran (available via Ubuntu’s default repositories), and popular frameworks include pFUnit (a modern, feature-rich option) and FRUIT (a simpler, assertion-based toolkit).

Writing Test Cases with FRUIT
FRUIT simplifies unit testing by providing built-in assertion functions and a structured workflow. Below is an example for testing a simple Fortran module that adds two numbers.

  1. Create the Source Module: Save the following code as functions.f90. This module contains the function add that you want to test.

    module functions
      implicit none
    contains
      function add(a, b) result(c)
        real, intent(in) :: a, b
        real :: c
        c = a + b
      end function add
    end module functions
    
  2. Write the Test Program: Create a file named test_functions.f90. This program uses FRUIT to define test cases and verify the output of add.

    program test_functions
      use functions
      use fruit
      implicit none
      
      ! Initialize the test suite
      call init_unit_tests('Add Function Tests')
      
      ! Test case 1: Positive numbers
      call assert_equals(5.0, add(2.0, 3.0), '2.0 + 3.0 should equal 5.0')
      
      ! Test case 2: Negative numbers
      call assert_equals(-2.0, add(-1.0, -1.0), '-1.0 + (-1.0) should equal -2.0')
      
      ! Test case 3: Mixed numbers
      call assert_equals(0.0, add(1.5, -1.5), '1.5 + (-1.5) should equal 0.0')
      
      ! Finalize and run the tests
      call finalize_unit_tests()
    end program test_functions
    

    Key components:

    • init_unit_tests: Initializes the test suite with a descriptive name.
    • assert_equals: Compares the actual result (from add) with the expected result. If they don’t match, it prints an error message.
    • finalize_unit_tests: Runs all registered tests and reports the results.

Writing Test Cases with pFUnit
pFUnit offers more advanced features (e.g., test fixtures, parallel testing) and integrates better with modern Fortran projects. Here’s how to test the same add function using pFUnit.

  1. Create the Test Program: Save the following code as test_functions.pf90. pFUnit uses special directives (like @test) to mark test cases.
    program test_functions
      use functions
      use pfunit
      implicit none
      
      ! Mark this program as a test suite
      @testSuite
      
      ! Test case 1: Positive numbers
      @test
      subroutine test_add_positive()
        real :: result
        result = add(2.0, 3.0)
        @assertEqual(5.0, result, '2.0 + 3.0 should equal 5.0')
      end subroutine test_add_positive
      
      ! Test case 2: Negative numbers
      @test
      subroutine test_add_negative()
        real :: result
        result = add(-1.0, -1.0)
        @assertEqual(-2.0, result, '-1.0 + (-1.0) should equal -2.0')
      end subroutine test_add_negative
      
    end program test_functions
    
    Key components:
    • @testSuite: Marks the program as a test suite.
    • @test: Labels a subroutine as a test case.
    • @assertEqual: Asserts that two values are equal (similar to FRUIT’s assert_equals).

Compiling and Running Tests
The compilation process for Fortran unit tests requires linking the source module, the test program, and the testing framework’s libraries.

Best Practices for Fortran Unit Testing

0
看了该问题的人还看了