Skip to content

Portable savetxt and loadtxt #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ace7812
Add testing module to allow better structuring of test suites
awvwgk Aug 21, 2021
d038530
Rewrite stdlib math tests
awvwgk Aug 22, 2021
8b93e8c
Merge branch 'master' into test
awvwgk Aug 22, 2021
6cd3851
Use to_string routines provided by stdlib
awvwgk Aug 22, 2021
3f186d2
Rewrite tests for stdlib_bitset_64
awvwgk Aug 22, 2021
24387b0
Moved test mean
jvdp1 Aug 23, 2021
789b912
Rewrite test fir stdlib_bitset_large
awvwgk Aug 24, 2021
a941a9a
Rewrite linear algebra tests
awvwgk Aug 24, 2021
d591157
Rewrite testsuite for optval
awvwgk Aug 24, 2021
cbb8b37
Rewrite string testsuites
awvwgk Aug 24, 2021
0185e3a
Merge branch 'master' into test
awvwgk Aug 24, 2021
dec1f12
Fix test conditions for stdlib_bitset_large tests
awvwgk Aug 24, 2021
5a0a008
Add test_mean_f03 + update makefile
jvdp1 Aug 26, 2021
a211e6c
Rewrite test_median.fypp
jvdp1 Aug 26, 2021
7aa6fab
Merge branch 'master' into test
awvwgk Aug 26, 2021
280f451
Rewrite tests for quadrature modules
awvwgk Aug 26, 2021
c525c96
Rewrite rawmoment tests
awvwgk Aug 26, 2021
83d321c
Rewrite variance tests
awvwgk Aug 27, 2021
0e88261
Merge remote-tracking branch 'upstream/test' into test
milancurcic Aug 29, 2021
c193450
Port test_open.f90 to test-drive
milancurcic Aug 29, 2021
5824208
Port test_parse_mode.f90 to test-drive
milancurcic Aug 29, 2021
c7ae718
Add private attribute
milancurcic Aug 30, 2021
917da6e
Port test_savetxt.f90 to test-drive
milancurcic Aug 30, 2021
b92b60b
Port test_savetxt_qp.f90 to test-drive
milancurcic Aug 30, 2021
f0f3478
Port test_loadtxt.f90 to test-drive
milancurcic Aug 30, 2021
08f10c3
Add complex data test to test_loadtxt.f90
milancurcic Aug 30, 2021
2ef8724
Port test_loadtxt_qp.f90 to test-drive
milancurcic Aug 30, 2021
ff8bf75
Fix indent in Makefile
milancurcic Aug 30, 2021
b0aedd3
Clean all test artifacts in Makefile.manual
milancurcic Aug 30, 2021
4e8894d
Fix error handling in test_loadtxt
milancurcic Aug 31, 2021
c2acb91
Don't reuse output file names between tests
milancurcic Aug 31, 2021
85d6e58
Use portable formats in savetxt and loadtxt
milancurcic Sep 1, 2021
3890ea9
Update tests for savetxt and loadtxt; compare exact values, not appro…
milancurcic Sep 1, 2021
693b885
Remove unused variable
milancurcic Sep 1, 2021
87d6dc3
Port stdlib_io tests to test-drive
milancurcic Sep 3, 2021
843a7cc
Update edit descriptors for savetxt and loadtxt; Small fix in number_…
milancurcic Sep 4, 2021
e026dc4
Tests loadtxt with tiny and huge numbers
milancurcic Sep 4, 2021
8dbfcc8
Resolve conflicts against awvwgk:test
milancurcic Sep 4, 2021
18b0cd2
Compare arrays exactly in test_savetxt_qp.f90
milancurcic Sep 4, 2021
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
57 changes: 36 additions & 21 deletions src/stdlib_io.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ module stdlib_io
! Private API that is exposed so that we can test it in tests
public :: parse_mode

! Format strings with edit descriptors for each type and kind
character(*), parameter :: &
FMT_INT = '(*(i0,1x))', &
FMT_REAL_SP = '(*(es15.8e2,1x))', &
FMT_REAL_DP = '(*(es24.16e3,1x))', &
FMT_REAL_QP = '(*(es44.35e4,1x))', &
FMT_COMPLEX_SP = '(*(es15.8e2,1x,es15.8e2))', &
FMT_COMPLEX_DP = '(*(es24.16e3,1x,es24.16e3))', &
FMT_COMPLEX_QP = '(*(es44.35e4,1x,es44.35e4))'

interface loadtxt
!! version: experimental
!!
Expand Down Expand Up @@ -78,13 +88,22 @@ contains

! determine number of columns
ncol = number_of_columns(s)
#:if 'complex' in t1
ncol = ncol / 2
#:endif

! determine number or rows
nrow = number_of_rows_numeric(s)
nrow = number_of_rows(s)

allocate(d(nrow, ncol))
do i = 1, nrow
read(s, *) d(i, :)
#:if 'real' in t1
read(s, FMT_REAL_${k1}$) d(i, :)
#:elif 'complex' in t1
read(s, FMT_COMPLEX_${k1}$) d(i, :)
#:else
read(s, *) d(i, :)
#:endif
end do
close(s)

Expand Down Expand Up @@ -116,7 +135,15 @@ contains
integer :: s, i
s = open(filename, "w")
do i = 1, size(d, 1)
write(s, *) d(i, :)
#:if 'real' in t1
write(s, FMT_REAL_${k1}$) d(i, :)
#:elif 'complex' in t1
write(s, FMT_COMPLEX_${k1}$) d(i, :)
#:elif 'integer' in t1
write(s, FMT_INT) d(i, :)
#:else
write(s, *) d(i, :)
#:endif
end do
close(s)
end subroutine savetxt_${t1[0]}$${k1}$
Expand Down Expand Up @@ -147,36 +174,24 @@ contains
end function number_of_columns


integer function number_of_rows_numeric(s) result(nrows)
integer function number_of_rows(s) result(nrows)
!! version: experimental
!!
!! determine number or rows
integer,intent(in)::s
!! Determine the number or rows in a file
integer, intent(in)::s
integer :: ios

real :: r
complex :: z

rewind(s)
nrows = 0
do
read(s, *, iostat=ios) r
read(s, *, iostat=ios)
if (ios /= 0) exit
nrows = nrows + 1
end do

rewind(s)

! If there are no rows of real numbers, it may be that they are complex
if( nrows == 0) then
do
read(s, *, iostat=ios) z
if (ios /= 0) exit
nrows = nrows + 1
end do
rewind(s)
end if
end function number_of_rows_numeric
end function number_of_rows


integer function open(filename, mode, iostat) result(u)
Expand Down Expand Up @@ -314,4 +329,4 @@ contains

end function parse_mode

end module
end module stdlib_io
15 changes: 14 additions & 1 deletion src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
fypp_f90("${fyppFlags};-I${CMAKE_CURRENT_SOURCE_DIR}/.." "stdlib_test.fypp" outFiles)
add_library("${PROJECT_NAME}-testing" "${outFiles}")
target_link_libraries("${PROJECT_NAME}-testing" PUBLIC "${PROJECT_NAME}")
set_target_properties(
"${PROJECT_NAME}-testing" PROPERTIES
Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
target_include_directories(
"${PROJECT_NAME}-testing" PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)

macro(ADDTEST name)
add_executable(test_${name} test_${name}.f90)
target_link_libraries(test_${name} ${PROJECT_NAME})
target_link_libraries(test_${name} "${PROJECT_NAME}-testing")
add_test(NAME ${name}
COMMAND $<TARGET_FILE:test_${name}> ${CMAKE_CURRENT_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Expand All @@ -16,6 +28,7 @@ add_subdirectory(sorting)
add_subdirectory(stats)
add_subdirectory(string)
add_subdirectory(system)
add_subdirectory(test)
add_subdirectory(quadrature)
add_subdirectory(math)
add_subdirectory(stringlist)
Expand Down
24 changes: 23 additions & 1 deletion src/tests/Makefile.manual
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
.PHONY: all clean test

all test clean:
LIB = libstdlib-testing.a
SRCFYPP = stdlib_test.fypp
SRCGEN = $(SRCFYPP:.fypp=.f90)
SRC = $(SRCGEN)
OBJS = $(SRC:.f90=.o)
MODS = $(OBJS:.o=.mod)

all test:: $(LIB)

all test clean::
$(MAKE) -f Makefile.manual --directory=ascii $@
$(MAKE) -f Makefile.manual --directory=bitsets $@
$(MAKE) -f Makefile.manual --directory=io $@
Expand All @@ -10,6 +19,19 @@ all test clean:
$(MAKE) -f Makefile.manual --directory=quadrature $@
$(MAKE) -f Makefile.manual --directory=stats $@
$(MAKE) -f Makefile.manual --directory=string $@
$(MAKE) -f Makefile.manual --directory=test $@
$(MAKE) -f Makefile.manual --directory=math $@
$(MAKE) -f Makefile.manual --directory=stringlist $@
$(MAKE) -f Makefile.manual --directory=linalg $@

$(LIB): $(OBJS)
ar rcs $@ $^

clean::
$(RM) $(LIB) $(OBJS) $(MODS) $(SRCGEN)

%.o: %.f90
$(FC) $(FFLAGS) -I.. -c $<

$(SRCGEN): %.f90: %.fypp ../common.fypp
fypp $(FYPPFLAGS) -I.. $< $@
4 changes: 2 additions & 2 deletions src/tests/Makefile.manual.test.mk
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Common Makefile rules that are included from each test subdirectory's
# Makefile

CPPFLAGS += -I../..
LDFLAGS += -L../.. -lstdlib
CPPFLAGS += -I../.. -I..
LDFLAGS += -L../.. -L.. -lstdlib-testing -lstdlib

OBJS = $(PROGS_SRC:.f90=.o)
PROGS = $(OBJS:.o=)
Expand Down
Loading