File: logical_testing.f90

package info (click to toggle)
lfortran 0.61.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 61,892 kB
  • sloc: cpp: 181,767; f90: 92,175; python: 17,616; ansic: 10,170; yacc: 2,377; sh: 1,444; fortran: 892; makefile: 38; javascript: 15
file content (50 lines) | stat: -rw-r--r-- 1,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
program logicalStringInput
    implicit none
    integer, parameter :: n = 6
    logical :: x
    character(len=1) :: actual(n), expected(n)
    integer :: ios, i, unit_no
    character(len=20), dimension(n) :: inputs = [ &
        ".true.         ", ".false.        ", "true           ", "false          ", &
        "True           ", "False          " ]

    ! as suggested, creating array for the expected tests 
    expected = ['T', 'F', 'T', 'F', 'T', 'F']

    ! as suggested, using the scratch file over .txt file
    open(newunit=unit_no, status="scratch")

    ! writing in the scratch file
    do i = 1, n
        write(unit_no, '(A)') trim(inputs(i))
    end do

    rewind(unit_no)

    ! reading
    do i = 1, n
        read(unit_no, *, iostat=ios) x
        if (ios /= 0) then
            print *, "Error reading logical at index", i
            stop 1
        end if
        if (x) then
            actual(i) = 'T'
        else
            actual(i) = 'F'
        end if
    end do

    close(unit_no)

    ! array se check validation
    do i = 1, n
        if (actual(i) /= expected(i)) then
            print *, "Test failed at index", i
            print *, "Expected:", expected(i), " but got:", actual(i)
            stop 1
        end if
    end do

    print *, "Tests Working Correctly!"
end program logicalStringInput