Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/julienne/julienne_file_s.F90
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@

module procedure write_to_character_file_name
integer file_unit, l
logical file_open
logical file_open, i_opened

call_assert(allocated(self%lines_))

inquire(file=file_name, opened=file_open, number=file_unit)
if (.not. file_open) open(newunit=file_unit, file=file_name, form='formatted', status='unknown', action='write')

if (.not. file_open) then
open(newunit=file_unit, file=file_name, form='formatted', status='unknown', action='write')
i_opened = .true.
else
i_opened = .false.
end if

do l = 1, size(self%lines_)
write(file_unit, '(a)') self%lines_(l)%string()
end do

if (i_opened) close(file_unit)
end procedure

module procedure write_to_string_file_name
Expand Down Expand Up @@ -70,12 +78,22 @@

allocate(file_object%lines_(num_lines))

read_and_store_lines: &
do line_num = 1, num_lines
#ifdef __LFORTRAN__
if (lengths(line_num)==0) then
file_object%lines_(line_num) = string_t("")
if (allocated(line)) deallocate(line)
allocate(character(len=1) :: line)
read(file_unit, '(a)') line
cycle read_and_store_lines
end if
#endif
if (allocated(line)) deallocate(line)
allocate(character(len=lengths(line_num)) :: line)
read(file_unit, '(a)') line
file_object%lines_(line_num) = string_t(line)
deallocate(line)
end do
end do read_and_store_lines

end associate

Expand Down
2 changes: 2 additions & 0 deletions test/driver.F90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program test_suite_driver
use character_stop_code_test_m ,only : character_stop_code_test_t
#endif
use command_line_test_m ,only : command_line_test_t
use file_test_m ,only : file_test_t
use formats_test_m ,only : formats_test_t
use multi_image_test_m ,only : multi_image_test_t, multi_image_setup
use string_test_m ,only : string_test_t
Expand All @@ -40,6 +41,7 @@ program test_suite_driver
,test_fixture_t( test_description_test_t()) &
,test_fixture_t( test_diagnosis_test_t()) &
,test_fixture_t( test_result_test_t()) &
,test_fixture_t( file_test_t()) &
,test_fixture_t( command_line_test_t()) &
]))
call test_harness%report_results
Expand Down
68 changes: 68 additions & 0 deletions test/modules/file_test_m.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
! Copyright (c) 2024-2025, The Regents of the University of California and Sourcery Institute
! Terms of use are as specified in LICENSE.txt

#include "language-support.F90"

module file_test_m
!! Check data partitioning across files
use julienne_m, only : &
file_t &
,operator(.all.) &
,operator(.also.) &
,operator(.equalsExpected.) &
,passing_test &
,string_t &
,test_description_t &
,test_diagnosis_t &
,test_result_t &
,test_t &
,usher
use assert_m, only : assert
implicit none

private
public :: file_test_t

type, extends(test_t) :: file_test_t
contains
procedure, nopass :: subject
procedure, nopass :: results
end type

contains

pure function subject() result(specimen)
character(len=:), allocatable :: specimen
specimen = "A file_t object"
end function

function results() result(test_results)
type(test_result_t), allocatable :: test_results(:)
type(test_description_t), allocatable :: test_descriptions(:)
type(file_test_t) file_test

test_descriptions = [ &
test_description_t(string_t("reading a written file"), usher(check_write_then_read)) &
]
test_results = file_test%run(test_descriptions)
end function

function check_write_then_read() result(test_diagnosis)
!! Check that a written file can be read correctly
type(test_diagnosis_t) test_diagnosis
character(len=*), parameter :: file_name = "build/file_t-unit-test-data.txt"

test_diagnosis = passing_test()

associate(output_lines => [string_t("foo"), string_t(""), string_t("bar ")])
associate(output_file => file_t(output_lines))
call output_file%write_lines(file_name)
associate(input_file => file_t(file_name))
test_diagnosis = test_diagnosis .also. (.all. (input_file%lines() .equalsExpected. output_lines))
end associate
end associate
end associate

end function

end module file_test_m
Loading