Files
CCLite/tests/unit/test_data_block.f90
T
2026-07-05 16:54:31 +08:00

156 lines
7.8 KiB
Fortran

program test_data_block
use iso_c_binding, only: c_int32_t, c_int64_t
use ccl_data_block, only: CCL_BUFFER_CONSUMED, CCL_BUFFER_EMPTY, CCL_BUFFER_ERROR, &
CCL_BUFFER_READY, CCL_BUFFER_READING, CCL_BUFFER_WRITING, CCL_DATA_MAGIC, &
CCL_Data_Block_Header_Type, CCL_LAYOUT_SOA_V2, CCL_ROLE_CCL_DIAGNOSTIC, &
CCL_ROLE_CCL_TO_FEM_LOAD, CCL_ROLE_CCL_TO_SPH_FORCE, CCL_ROLE_FEM_TO_CCL_FACET, &
CCL_ROLE_SPH_TO_CCL_PARTICLE, ccl_data_header_init, ccl_data_header_pack, &
ccl_data_header_set_count, ccl_data_header_set_state, ccl_data_header_size, &
ccl_data_header_unpack, ccl_data_header_validate, ccl_data_payload_capacity_bytes, &
ccl_data_record_size_for_role, ccl_data_role_is_valid, ccl_data_state_transition_is_valid, &
ccl_diagnostic_record_size, ccl_fem_facet_record_size, ccl_fem_load_record_size, &
ccl_sph_force_record_size, ccl_sph_particle_record_size
use ccl_errors, only: CCL_ERROR_IPC_BAD_HEADER, CCL_SUCCESS
implicit none
integer :: ierr
integer(c_int32_t) :: particle_stride
integer(c_int32_t) :: fem_stride
integer(c_int32_t) :: force_stride
integer(c_int32_t) :: load_stride
type(CCL_Data_Block_Header_Type) :: header
type(CCL_Data_Block_Header_Type) :: packed
type(CCL_Data_Block_Header_Type) :: unpacked
particle_stride = ccl_sph_particle_record_size()
fem_stride = ccl_fem_facet_record_size()
force_stride = ccl_sph_force_record_size()
load_stride = ccl_fem_load_record_size()
call require_true(ccl_data_header_size() > 0_c_int32_t, "header has positive byte size")
call require_true(particle_stride > 0_c_int32_t, "particle record has positive byte size")
call require_true(fem_stride > 0_c_int32_t, "fem facet record has positive byte size")
call require_true(force_stride > 0_c_int32_t, "sph force record has positive byte size")
call require_true(load_stride > 0_c_int32_t, "fem load record has positive byte size")
call require_true(ccl_diagnostic_record_size() > 0_c_int32_t, "diagnostic record has positive byte size")
call require_true(ccl_data_role_is_valid(CCL_ROLE_CCL_DIAGNOSTIC), "diagnostic role valid")
call require_equal(ccl_data_record_size_for_role(CCL_ROLE_SPH_TO_CCL_PARTICLE), particle_stride, &
"particle role stride")
call require_equal(ccl_data_record_size_for_role(CCL_ROLE_FEM_TO_CCL_FACET), fem_stride, &
"fem role stride")
call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t)
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_SUCCESS, "fresh header validates")
call require_equal(header%magic, CCL_DATA_MAGIC, "magic initialized")
call require_equal(header%item_stride, particle_stride, "stride initialized")
call require_equal_int64(header%payload_bytes, 0_c_int64_t, "empty payload initialized")
call require_equal_int64(ccl_data_payload_capacity_bytes(header), int(4 * particle_stride, c_int64_t), &
"capacity bytes computed")
call ccl_data_header_set_state(header, CCL_BUFFER_WRITING, ierr)
call require_equal(ierr, CCL_SUCCESS, "set writing state")
call ccl_data_header_set_count(header, 3_c_int32_t, ierr)
call require_equal(ierr, CCL_SUCCESS, "set fixed payload count")
call ccl_data_header_set_state(header, CCL_BUFFER_READY, ierr)
call require_equal(ierr, CCL_SUCCESS, "set ready state")
call require_equal_int64(header%payload_bytes, int(3 * particle_stride, c_int64_t), &
"payload bytes match count and stride")
call ccl_data_header_pack(header, packed, ierr)
call require_equal(ierr, CCL_SUCCESS, "pack header")
call ccl_data_header_unpack(packed, unpacked, ierr)
call require_equal(ierr, CCL_SUCCESS, "unpack header")
call require_equal(unpacked%item_count, 3_c_int32_t, "unpacked count preserved")
call require_equal_int64(unpacked%payload_bytes, header%payload_bytes, "unpacked payload preserved")
call ccl_data_header_set_count(header, 4_c_int32_t, ierr)
call require_equal(ierr, CCL_SUCCESS, "max capacity accepted")
call ccl_data_header_set_count(header, 5_c_int32_t, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "count over capacity rejected")
call ccl_data_header_init(header, CCL_ROLE_CCL_TO_SPH_FORCE, 3_c_int32_t, 1_c_int32_t, 0_c_int32_t)
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_SUCCESS, "empty sph force block validates")
call require_equal(header%item_stride, force_stride, "empty sph force stride reserved")
call ccl_data_header_init(header, CCL_ROLE_CCL_TO_FEM_LOAD, 3_c_int32_t, 2_c_int32_t, 0_c_int32_t)
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_SUCCESS, "empty fem load block validates")
call require_equal(header%item_stride, load_stride, "empty fem load stride reserved")
call ccl_data_header_init(header, CCL_ROLE_FEM_TO_CCL_FACET, 2_c_int32_t, 3_c_int32_t, 1_c_int32_t)
call ccl_data_header_set_count(header, 1_c_int32_t, ierr)
call require_equal(ierr, CCL_SUCCESS, "fem facet payload accepted")
header%item_stride = header%item_stride + 8_c_int32_t
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad record stride rejected")
call ccl_data_header_init(header, 999_c_int32_t, 1_c_int32_t, 3_c_int32_t, 0_c_int32_t)
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad role rejected")
call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t)
header%layout_id = CCL_LAYOUT_SOA_V2
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "reserved soa layout rejected in 0.1.x")
call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t)
header%payload_bytes = 8_c_int64_t
call ccl_data_header_validate(header, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad payload bytes rejected")
call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t)
call ccl_data_header_set_state(header, 999_c_int32_t, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad buffer state rejected")
call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_EMPTY, CCL_BUFFER_WRITING), &
"empty to writing valid")
call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_WRITING, CCL_BUFFER_READY), &
"writing to ready valid")
call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_READY, CCL_BUFFER_READING), &
"ready to reading valid")
call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_READING, CCL_BUFFER_CONSUMED), &
"reading to consumed valid")
call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_ERROR, CCL_BUFFER_EMPTY), &
"error to empty valid")
call require_true(.not. ccl_data_state_transition_is_valid(CCL_BUFFER_READY, CCL_BUFFER_EMPTY), &
"ready to empty invalid")
contains
subroutine require_equal(actual, expected, message)
integer, intent(in) :: actual
integer, intent(in) :: expected
character(len=*), intent(in) :: message
if (actual /= expected) then
print '(a,": expected ",i0," got ",i0)', trim(message), expected, actual
stop 1
end if
end subroutine require_equal
subroutine require_equal_int64(actual, expected, message)
integer(c_int64_t), intent(in) :: actual
integer(c_int64_t), intent(in) :: expected
character(len=*), intent(in) :: message
if (actual /= expected) then
print '(a,": int64 mismatch")', trim(message)
stop 1
end if
end subroutine require_equal_int64
subroutine require_true(condition, message)
logical, intent(in) :: condition
character(len=*), intent(in) :: message
if (.not. condition) then
print '(a)', trim(message)
stop 1
end if
end subroutine require_true
end program test_data_block