Files
CCLite/tests/unit/test_command_block.f90
2026-07-05 16:49:28 +08:00

103 lines
4.3 KiB
Fortran

program test_command_block
use iso_c_binding, only: c_double, c_int32_t, c_int64_t
use ccl_command_block, only: CCL_CMD_ADVANCE, CCL_CMD_INIT, CCL_COMMAND_MAGIC, &
CCL_Command_Block_Type, CCL_SOLVER_ID_SPH, CCL_WORKER_DONE, CCL_WORKER_READY, &
ccl_command_ack, ccl_command_block_size, ccl_command_check_ack, &
ccl_command_check_timeout, ccl_command_dispatch, ccl_command_heartbeat, &
ccl_command_init, ccl_command_pack, ccl_command_unpack, ccl_command_validate
use ccl_errors, only: CCL_ERROR_IPC_BAD_HEADER, CCL_ERROR_IPC_TIMEOUT, &
CCL_ERROR_WORKER_BAD_STATE, CCL_SUCCESS
implicit none
integer :: ierr
integer(c_int64_t) :: heartbeat
type(CCL_Command_Block_Type) :: block
type(CCL_Command_Block_Type) :: packed
type(CCL_Command_Block_Type) :: unpacked
call ccl_command_init(block, CCL_SOLVER_ID_SPH)
call ccl_command_validate(block, ierr)
call require_equal(ierr, CCL_SUCCESS, "fresh command block validates")
call require_true(ccl_command_block_size() > 0_c_int64_t, "command block has positive byte size")
call ccl_command_dispatch(block, CCL_CMD_INIT, 1_c_int64_t, 1.0d-6, ierr)
call require_equal(ierr, CCL_SUCCESS, "dispatch INIT")
call require_equal_int64(block%command_seq, 1_c_int64_t, "command seq increments")
call require_equal(block%command_id, CCL_CMD_INIT, "command id set")
call require_equal_int64(block%substep_id, 0_c_int64_t, "substep id reserved as zero")
call require_equal_int64(block%iteration_id, 0_c_int64_t, "iteration id reserved as zero")
call ccl_command_check_ack(block, ierr)
call require_equal(ierr, CCL_ERROR_WORKER_BAD_STATE, "unacked command is not complete")
call ccl_command_heartbeat(block)
heartbeat = block%heartbeat_seq
call ccl_command_check_timeout(block, heartbeat, 10, 5, ierr)
call require_equal(ierr, CCL_ERROR_IPC_TIMEOUT, "unchanged heartbeat times out")
call ccl_command_heartbeat(block)
call ccl_command_check_timeout(block, heartbeat, 10, 5, ierr)
call require_equal(ierr, CCL_SUCCESS, "changed heartbeat avoids timeout")
call ccl_command_ack(block, CCL_WORKER_DONE, 0_c_int32_t, ierr)
call require_equal(ierr, CCL_SUCCESS, "ack DONE")
call ccl_command_check_ack(block, ierr)
call require_equal(ierr, CCL_SUCCESS, "ack seq matches command seq")
call ccl_command_dispatch(block, CCL_CMD_ADVANCE, 2_c_int64_t, 2.0d-6, ierr)
call require_equal(ierr, CCL_SUCCESS, "dispatch ADVANCE")
call ccl_command_ack(block, CCL_WORKER_READY, 0_c_int32_t, ierr)
call require_equal(ierr, CCL_SUCCESS, "ack READY")
call ccl_command_pack(block, packed, ierr)
call require_equal(ierr, CCL_SUCCESS, "pack command block")
call ccl_command_unpack(packed, unpacked, ierr)
call require_equal(ierr, CCL_SUCCESS, "unpack command block")
call require_equal_int64(unpacked%command_seq, block%command_seq, "unpacked seq matches")
call require_equal_int64(unpacked%ack_seq, block%ack_seq, "unpacked ack matches")
call require_true(abs(unpacked%macro_dt - 2.0_c_double * 1.0d-6) < 1.0d-14, "macro dt preserved")
block%magic = CCL_COMMAND_MAGIC + 1_c_int64_t
call ccl_command_validate(block, ierr)
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad magic rejected")
call ccl_command_init(block, CCL_SOLVER_ID_SPH)
block%worker_state = 999_c_int32_t
call ccl_command_validate(block, ierr)
call require_equal(ierr, CCL_ERROR_WORKER_BAD_STATE, "bad worker state rejected")
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_command_block