Skip to content

Commit c66a568

Browse files
committed
Add unit tests
1 parent b9f76e6 commit c66a568

4 files changed

Lines changed: 321 additions & 3 deletions

File tree

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ foreach(execid
33
misc
44
misc_maths
55
misc_linalg
6+
cache
67
element_utils
78
geom_rw
89
geom_utils
@@ -29,4 +30,4 @@ foreach(execid
2930
endif()
3031

3132
add_test(NAME test_${execid} COMMAND test_${execid})
32-
endforeach()
33+
endforeach()

test/test_cache.f90

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
program test_cache
2+
use raffle__io_utils
3+
use raffle__cache
4+
use raffle__constants, only: real32
5+
implicit none
6+
7+
logical :: success = .true.
8+
9+
call test_retrieve_unallocated(success)
10+
call test_store_retrieve_probability_density(success)
11+
12+
!-----------------------------------------------------------------------------
13+
! check for any failed tests
14+
!-----------------------------------------------------------------------------
15+
write(*,*) "----------------------------------------"
16+
if(success)then
17+
write(*,*) 'test_cache passed all tests'
18+
else
19+
write(0,*) 'test_cache failed one or more tests'
20+
stop 1
21+
end if
22+
23+
contains
24+
25+
subroutine test_store_retrieve_probability_density(success)
26+
implicit none
27+
logical, intent(inout) :: success
28+
real(real32), allocatable :: test_data(:,:), retrieved_data(:,:)
29+
integer :: i, j
30+
31+
! Create test data - a 3x3 matrix with values
32+
allocate(test_data(3,3))
33+
do i = 1, 3
34+
do j = 1, 3
35+
test_data(i,j) = real(i*10 + j, real32)
36+
end do
37+
end do
38+
39+
! Store the test data in the cache
40+
call store_probability_density(test_data)
41+
42+
! Retrieve the data from the cache
43+
retrieved_data = retrieve_probability_density()
44+
45+
! Check dimensions match
46+
if (.not. all(shape(retrieved_data) == shape(test_data))) then
47+
write(0,*) 'test_store_retrieve_probability_density: array shapes do not match'
48+
success = .false.
49+
return
50+
end if
51+
52+
! Check all values match
53+
do i = 1, 3
54+
do j = 1, 3
55+
if (abs(retrieved_data(i,j) - test_data(i,j)) .gt. 1.e-6_real32) then
56+
write(0,*) &
57+
'test_store_retrieve_probability_density: values do not match at', &
58+
i, j, retrieved_data(i,j), test_data(i,j)
59+
success = .false.
60+
return
61+
end if
62+
end do
63+
end do
64+
65+
! Clean up
66+
if (allocated(test_data)) deallocate(test_data)
67+
if (allocated(retrieved_data)) deallocate(retrieved_data)
68+
69+
write(*,*) 'test_store_retrieve_probability_density: PASSED'
70+
end subroutine test_store_retrieve_probability_density
71+
72+
subroutine test_retrieve_unallocated(success)
73+
implicit none
74+
logical, intent(inout) :: success
75+
real(real32), allocatable :: retrieved_data(:,:)
76+
77+
! Try to retrieve data when cache is not allocated
78+
retrieved_data = retrieve_probability_density()
79+
80+
! Verify returned array is a scalar with value 0
81+
if (size(retrieved_data) .ne. 1) then
82+
write(0,*) &
83+
'test_retrieve_unallocated: returned array should be scalar but has size', &
84+
size(retrieved_data)
85+
success = .false.
86+
return
87+
end if
88+
89+
if (abs(retrieved_data(1,1)) .gt. 1.e-6_real32) then
90+
write(0,*) &
91+
'test_retrieve_unallocated: returned value should be 0 but is', &
92+
retrieved_data(1,1)
93+
success = .false.
94+
return
95+
end if
96+
97+
! Clean up
98+
if (allocated(retrieved_data)) deallocate(retrieved_data)
99+
100+
write(*,*) 'test_retrieve_unallocated: PASSED'
101+
end subroutine test_retrieve_unallocated
102+
103+
end program test_cache

test/test_distribs_container.f90

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ program test_distribs_container
8383
call test_set_radius_distance_tol(success)
8484
call test_create([basis_graphite], success)
8585
call test_update([basis_graphite, basis_diamond], success)
86+
call test_read_write_gdfs([basis_graphite, basis_diamond], success)
8687

8788
call test_create([basis_diamond, basis_mgo], success)
8889
call test_update([basis_diamond, basis_mgo], success)
90+
call test_read_write_gdfs([basis_diamond, basis_mgo], success)
8991
! call test_write_read(success)
9092
! call test_write_2body(success)
9193
! call test_write_3body(success)
@@ -232,6 +234,41 @@ subroutine test_set_history_len(success)
232234
success &
233235
)
234236

237+
! Call the subroutine to set the history length when already allocated
238+
history_len = 3
239+
call distribs_container%set_history_len(history_len)
240+
241+
! Check if the history length was set correctly
242+
call assert( &
243+
distribs_container%history_len .eq. history_len, &
244+
"History length was not set correctly after reallocation", &
245+
success &
246+
)
247+
248+
call assert( &
249+
allocated(distribs_container%history_deltas) .and. &
250+
size(distribs_container%history_deltas, 1) .eq. history_len, &
251+
"History delta list was not reallocated", &
252+
success &
253+
)
254+
255+
! Call the subroutine to set the history length to zero
256+
history_len = 0
257+
call distribs_container%set_history_len(history_len)
258+
259+
! Check if the history length was set correctly
260+
call assert( &
261+
distribs_container%history_len .eq. history_len, &
262+
"History length was not set to zero", &
263+
success &
264+
)
265+
266+
call assert( &
267+
.not.allocated(distribs_container%history_deltas), &
268+
"History deltas was not deallocated", &
269+
success &
270+
)
271+
235272
end subroutine test_set_history_len
236273

237274
subroutine test_is_converged(success)
@@ -392,6 +429,7 @@ subroutine test_create(basis, success)
392429
type(distribs_container_type) :: distribs_container
393430
type(basis_type), dimension(size(basis,1)) :: basis_list
394431
character(len=3), dimension(:), allocatable :: elements
432+
real(real32), dimension(:), allocatable :: energy_above_hull
395433

396434
! Initialise basis_list
397435
do i = 1, size(basis,1)
@@ -584,6 +622,29 @@ subroutine test_create(basis, success)
584622
success &
585623
)
586624

625+
allocate(energy_above_hull(10))
626+
energy_above_hull = 0._real32
627+
628+
write(*,*) "Testing energy_above_hull error handling"
629+
call distribs_container%create( &
630+
basis_list, deallocate_systems=.false., &
631+
energy_above_hull_list=energy_above_hull &
632+
)
633+
write(*,*) "Handled error: energy_above_hull provided but weight_by_hull is false"
634+
distribs_container%weight_by_hull = .true.
635+
call distribs_container%create( &
636+
basis_list, deallocate_systems=.false., &
637+
energy_above_hull_list=energy_above_hull &
638+
)
639+
write(*,*) "Handled error: energy_above_hull and basis_list sizes do not match"
640+
deallocate(energy_above_hull)
641+
allocate(energy_above_hull(size(basis_list,1)))
642+
energy_above_hull = 0._real32
643+
call distribs_container%create( &
644+
basis_list, deallocate_systems=.false., &
645+
energy_above_hull_list=energy_above_hull &
646+
)
647+
587648
end subroutine test_create
588649

589650
subroutine test_update(basis, success)
@@ -597,6 +658,7 @@ subroutine test_update(basis, success)
597658
type(distribs_container_type) :: distribs_container
598659
type(basis_type), dimension(size(basis,1)) :: basis_list
599660
character(len=3), dimension(:), allocatable :: elements
661+
real(real32), dimension(:), allocatable :: energy_above_hull
600662

601663
! Initialise basis_list
602664
do i = 1, size(basis,1)
@@ -652,6 +714,42 @@ subroutine test_update(basis, success)
652714
success &
653715
)
654716

717+
! Check history length
718+
call distribs_container%set_history_len(10)
719+
call distribs_container%update( &
720+
basis_list, deallocate_systems=.false. &
721+
)
722+
call assert( &
723+
distribs_container%history_deltas(1) .ne. huge(0._real32), &
724+
"History delta not set on update", &
725+
success &
726+
)
727+
728+
allocate(energy_above_hull(10))
729+
energy_above_hull = 0._real32
730+
731+
write(*,*) "Testing energy_above_hull error handling"
732+
call distribs_container%update( &
733+
basis_list, deallocate_systems=.false., &
734+
energy_above_hull_list=energy_above_hull &
735+
)
736+
write(*,*) "Handled error: energy_above_hull provided but weight_by_hull is false"
737+
distribs_container%weight_by_hull = .true.
738+
call distribs_container%update( &
739+
basis_list, deallocate_systems=.false., &
740+
energy_above_hull_list=energy_above_hull &
741+
)
742+
write(*,*) "Handled error: energy_above_hull and basis_list sizes do not match"
743+
deallocate(energy_above_hull)
744+
allocate(energy_above_hull(size(basis_list,1)))
745+
energy_above_hull = 0._real32
746+
call distribs_container%update( &
747+
basis_list, deallocate_systems=.false., &
748+
energy_above_hull_list=energy_above_hull, &
749+
from_host = .true. &
750+
)
751+
write(*,*) "Handled error: from_host and energy_above_hull provided"
752+
655753
end subroutine test_update
656754

657755
subroutine test_add(basis, success)
@@ -909,6 +1007,123 @@ subroutine test_get_bin(success)
9091007

9101008
end subroutine test_get_bin
9111009

1010+
subroutine test_read_write_gdfs(basis, success)
1011+
implicit none
1012+
type(basis_type), dimension(:), intent(in) :: basis
1013+
logical, intent(inout) :: success
1014+
1015+
type(distribs_container_type) :: distribs_container, distribs_container_read
1016+
character(len=100) :: filename
1017+
1018+
! Set up a test filename
1019+
filename = 'test_gdfs.dat'
1020+
1021+
! Create a dummy distribs_container
1022+
call distribs_container%set_history_len(10)
1023+
call distribs_container%set_width([0.1_real32, 0.1_real32, 0.1_real32])
1024+
call distribs_container%set_sigma([0.2_real32, 0.2_real32, 0.2_real32])
1025+
call distribs_container%create([basis], deallocate_systems=.false.)
1026+
1027+
! Write the GDFs to a file
1028+
call distribs_container%write_gdfs(filename)
1029+
1030+
! Check if the file exists
1031+
inquire(file=filename, exist=success)
1032+
call assert(success, "GDF file was not created", success)
1033+
1034+
! Read the GDFs from the file
1035+
call distribs_container_read%read_gdfs(filename)
1036+
1037+
! Check if the GDFs were read correctly
1038+
call assert( &
1039+
allocated(distribs_container_read%gdf%df_2body), &
1040+
"2-body GDF was not read correctly", &
1041+
success &
1042+
)
1043+
call assert( &
1044+
allocated(distribs_container_read%gdf%df_3body), &
1045+
"3-body GDF was not read correctly", &
1046+
success &
1047+
)
1048+
call assert( &
1049+
allocated(distribs_container_read%gdf%df_4body), &
1050+
"4-body GDF was not read correctly", &
1051+
success &
1052+
)
1053+
1054+
! Check if the read width, sigma, and history length match
1055+
call assert( &
1056+
all( &
1057+
abs( &
1058+
distribs_container_read%width - &
1059+
distribs_container%width &
1060+
) .lt. 1.E-6_real32 &
1061+
), &
1062+
"Width was not read correctly", &
1063+
success &
1064+
)
1065+
call assert( &
1066+
all( &
1067+
abs( &
1068+
distribs_container_read%sigma - &
1069+
distribs_container%sigma &
1070+
) .lt. 1.E-6_real32 &
1071+
), &
1072+
"Sigma was not read correctly", &
1073+
success &
1074+
)
1075+
call assert( &
1076+
distribs_container_read%history_len .eq. distribs_container%history_len, &
1077+
"History length was not read correctly", &
1078+
success &
1079+
)
1080+
call assert( &
1081+
all( &
1082+
abs( 1._real32 - &
1083+
distribs_container_read%history_deltas / &
1084+
distribs_container%history_deltas &
1085+
) .lt. 1.E-4_real32 &
1086+
), &
1087+
"History deltas were not read correctly", &
1088+
success &
1089+
)
1090+
call assert( &
1091+
all( &
1092+
abs( &
1093+
distribs_container_read%cutoff_min - &
1094+
distribs_container%cutoff_min &
1095+
) .lt. 1.E-6_real32 &
1096+
), &
1097+
"Cutoff min was not read correctly", &
1098+
success &
1099+
)
1100+
call assert( &
1101+
all( &
1102+
abs( &
1103+
distribs_container_read%cutoff_max - &
1104+
distribs_container%cutoff_max &
1105+
) .lt. 1.E-6_real32 &
1106+
), &
1107+
"Cutoff max was not read correctly", &
1108+
success &
1109+
)
1110+
call assert( &
1111+
all( &
1112+
abs( &
1113+
distribs_container_read%radius_distance_tol - &
1114+
distribs_container%radius_distance_tol &
1115+
) .lt. 1.E-6_real32 &
1116+
), &
1117+
"Radius distance tolerance was not read correctly", &
1118+
success &
1119+
)
1120+
call assert( &
1121+
all( distribs_container_read%nbins .eq. distribs_container%nbins ), &
1122+
"Number of bins was not read correctly", &
1123+
success &
1124+
)
1125+
end subroutine test_read_write_gdfs
1126+
9121127
!###############################################################################
9131128

9141129
subroutine assert(condition, message, success)

0 commit comments

Comments
 (0)