mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-05-04 11:14:15 +02:00
Start improving tests for H5 writer
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "JFH5Writer.hpp"
|
||||
#include "gtest/gtest.h"
|
||||
#include "bitshuffle/bitshuffle.h"
|
||||
#include "mock/data.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace buffer_config;
|
||||
@@ -31,44 +32,19 @@ TEST(JFH5Writer, test_writing)
|
||||
uint64_t stop_pulse_id = 10;
|
||||
auto n_images = stop_pulse_id - start_pulse_id + 1;
|
||||
|
||||
auto metadata = make_shared<ImageMetadataBlock>();
|
||||
metadata->block_start_pulse_id = 0;
|
||||
metadata->block_stop_pulse_id = BUFFER_BLOCK_SIZE - 1;
|
||||
|
||||
for (uint64_t pulse_id=start_pulse_id;
|
||||
pulse_id<=stop_pulse_id;
|
||||
pulse_id++) {
|
||||
|
||||
metadata->pulse_id[pulse_id] = pulse_id;
|
||||
metadata->frame_index[pulse_id] = pulse_id + 10;
|
||||
metadata->daq_rec[pulse_id] = pulse_id + 100;
|
||||
metadata->is_good_image[pulse_id] = 1;
|
||||
}
|
||||
|
||||
auto image_buffer = make_unique<uint16_t[]>(
|
||||
MODULE_N_PIXELS * n_modules * BUFFER_BLOCK_SIZE);
|
||||
|
||||
for (int i_block=0; i_block<=BUFFER_BLOCK_SIZE; i_block++) {
|
||||
for (int i_module=0; i_module<n_modules; i_module++) {
|
||||
auto offset = i_block * MODULE_N_PIXELS;
|
||||
offset += i_module * MODULE_N_PIXELS;
|
||||
|
||||
for (int i_pixel=0; i_pixel<MODULE_N_PIXELS; i_pixel++) {
|
||||
image_buffer[offset + i_pixel] = i_pixel % 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto meta = get_test_block_metadata(start_pulse_id, stop_pulse_id, 1);
|
||||
auto data = get_test_block_data(n_modules);
|
||||
|
||||
// The writer closes the file on destruction.
|
||||
{
|
||||
JFH5Writer writer(
|
||||
"ignore.h5", n_modules, start_pulse_id, stop_pulse_id, 1);
|
||||
writer.write(metadata.get(), (char*)(&image_buffer[0]));
|
||||
writer.write(meta.get(), (char*)(&data[0]));
|
||||
}
|
||||
|
||||
H5::H5File reader("ignore.h5", H5F_ACC_RDONLY);
|
||||
auto image_dataset = reader.openDataSet("image");
|
||||
image_dataset.read(&image_buffer[0], H5::PredType::NATIVE_UINT16);
|
||||
image_dataset.read(&data[0], H5::PredType::NATIVE_UINT16);
|
||||
|
||||
for (int i_image=0; i_image < n_images; i_image++) {
|
||||
for (int i_module=0; i_module<n_modules; i_module++) {
|
||||
@@ -77,7 +53,7 @@ TEST(JFH5Writer, test_writing)
|
||||
offset += i_module * MODULE_N_PIXELS;
|
||||
|
||||
for (int i_pixel=0; i_pixel<MODULE_N_PIXELS; i_pixel++) {
|
||||
ASSERT_EQ(image_buffer[offset + i_pixel], i_pixel % 100);
|
||||
ASSERT_EQ(data[offset + i_pixel], i_pixel % 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,20 +101,12 @@ TEST(JFH5Writer, test_step_pulse_id)
|
||||
// No pulses in given range with step = 10
|
||||
ASSERT_THROW(JFH5Writer writer("ignore.h5", 1, 1, 9, 10), runtime_error);
|
||||
|
||||
// Stop pulse id is divisible by step.
|
||||
ASSERT_NO_THROW(JFH5Writer writer("ignore.h5", 1, 5, 10, 10));
|
||||
// Stop pulse id is divisible by step, but start is not.
|
||||
ASSERT_THROW(JFH5Writer writer("ignore.h5", 1, 5, 10, 10), runtime_error);
|
||||
|
||||
// Start pulse id is divisible by step.
|
||||
ASSERT_NO_THROW(JFH5Writer writer("ignore.h5", 1, 10, 19, 10));
|
||||
}
|
||||
// Start pulse id is divisible by step, but stop is not.
|
||||
ASSERT_THROW(JFH5Writer writer("ignore.h5", 1, 10, 19, 10), runtime_error);
|
||||
|
||||
TEST(JFH5Writer, test_writing_with_step)
|
||||
{
|
||||
// TODO: Implement this test.
|
||||
}
|
||||
|
||||
TEST(JFH5Writer, test_exceptions)
|
||||
{
|
||||
// Should be ok.
|
||||
ASSERT_NO_THROW(JFH5Writer("ignore.h5", 1, 1234, 1234, 1));
|
||||
// Should be ok.
|
||||
@@ -154,4 +122,67 @@ TEST(JFH5Writer, test_exceptions)
|
||||
ASSERT_THROW(JFH5Writer("ignore.h5", 1, 10, 10, 4), runtime_error);
|
||||
// stop not divisible by step
|
||||
ASSERT_THROW(JFH5Writer("ignore.h5", 1, 8, 10, 4), runtime_error);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(JFH5Writer, test_writing_with_step)
|
||||
{
|
||||
size_t n_modules = 2;
|
||||
uint64_t start_pulse_id = 500;
|
||||
uint64_t stop_pulse_id = 599;
|
||||
auto n_images = stop_pulse_id - start_pulse_id + 1;
|
||||
// 50Hz test.
|
||||
int step = 2;
|
||||
|
||||
auto meta = get_test_block_metadata(start_pulse_id, stop_pulse_id, step);
|
||||
auto data = get_test_block_data(n_modules);
|
||||
|
||||
// The writer closes the file on destruction.
|
||||
{
|
||||
JFH5Writer writer(
|
||||
"ignore.h5", n_modules, start_pulse_id, stop_pulse_id, 1);
|
||||
writer.write(meta.get(), (char*)(&data[0]));
|
||||
}
|
||||
|
||||
H5::H5File reader("ignore.h5", H5F_ACC_RDONLY);
|
||||
auto image_dataset = reader.openDataSet("image");
|
||||
image_dataset.read(&data[0], H5::PredType::NATIVE_UINT16);
|
||||
|
||||
for (int i_image=0; i_image < n_images; i_image++) {
|
||||
for (int i_module=0; i_module<n_modules; i_module++) {
|
||||
|
||||
auto offset = i_image * MODULE_N_PIXELS;
|
||||
offset += i_module * MODULE_N_PIXELS;
|
||||
|
||||
for (int i_pixel=0; i_pixel<MODULE_N_PIXELS; i_pixel++) {
|
||||
ASSERT_EQ(data[offset + i_pixel], i_pixel % 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto pulse_id_data = make_unique<uint64_t[]>(n_images);
|
||||
auto pulse_id_dataset = reader.openDataSet("pulse_id");
|
||||
pulse_id_dataset.read(&pulse_id_data[0], H5::PredType::NATIVE_UINT64);
|
||||
|
||||
auto frame_index_data = make_unique<uint64_t[]>(n_images);
|
||||
auto frame_index_dataset = reader.openDataSet("frame_index");
|
||||
frame_index_dataset.read(&frame_index_data[0], H5::PredType::NATIVE_UINT64);
|
||||
|
||||
auto daq_rec_data = make_unique<uint32_t[]>(n_images);
|
||||
auto daq_rec_dataset = reader.openDataSet("daq_rec");
|
||||
daq_rec_dataset.read(&daq_rec_data[0], H5::PredType::NATIVE_UINT32);
|
||||
|
||||
auto is_good_frame_data = make_unique<uint8_t[]>(n_images);
|
||||
auto is_good_frame_dataset = reader.openDataSet("is_good_frame");
|
||||
is_good_frame_dataset.read(
|
||||
&is_good_frame_data[0], H5::PredType::NATIVE_UINT8);
|
||||
|
||||
for (uint64_t pulse_id=start_pulse_id;
|
||||
pulse_id<=stop_pulse_id;
|
||||
pulse_id++) {
|
||||
|
||||
ASSERT_EQ(pulse_id_data[pulse_id - start_pulse_id], pulse_id);
|
||||
ASSERT_EQ(frame_index_data[pulse_id - start_pulse_id], pulse_id + 10);
|
||||
ASSERT_EQ(daq_rec_data[pulse_id - start_pulse_id], pulse_id + 100);
|
||||
ASSERT_EQ(is_good_frame_data[pulse_id - start_pulse_id], 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user