LCOV - code coverage report
Current view: top level - tpglibs/unittest - binary_signal_reader_test.cxx (source / functions) Coverage Total Hit
Test: code.result Lines: 100.0 % 80 80
Test Date: 2025-12-21 13:07:08 Functions: 100.0 % 10 10

            Line data    Source code
       1              : /**
       2              :  * @file binary_signal_reader_test.cxx
       3              :  *
       4              :  * @copyright This is part of the DUNE DAQ Software Suite, copyright 2020.
       5              :  * Licensing/copyright details are in the COPYING file that you should have
       6              :  * received with this code.
       7              :  */
       8              : 
       9              : #define BOOST_TEST_MODULE binary_signal_reader_test
      10              : #define FMT_HEADER_ONLY
      11              : 
      12              : #include "tpglibs/testapp/reader/BinarySignalReader.hpp"
      13              : 
      14              : #include <boost/test/unit_test.hpp>
      15              : #include <sstream>
      16              : #include <vector>
      17              : #include <cstdint>
      18              : 
      19              : BOOST_AUTO_TEST_SUITE(BinarySignalReaderTest)
      20              : 
      21            2 : BOOST_AUTO_TEST_CASE(TestInt16Read)
      22              : {
      23              :   // Create test data: 5 int16 values
      24            1 :   std::vector<int16_t> test_data = {100, -200, 300, -400, 500};
      25              :   
      26              :   // Write to stringstream as binary data
      27            1 :   std::stringstream ss(std::ios::binary | std::ios::in | std::ios::out);
      28            6 :   for (const auto& value : test_data) {
      29            5 :     ss.write(reinterpret_cast<const char*>(&value), sizeof(int16_t));
      30              :   }
      31              :   
      32              :   // Reset to beginning
      33            1 :   ss.seekg(0);
      34              :   
      35              :   // Create reader from stringstream (we need to modify BinarySignalReader to accept istream)
      36              :   // For now, let's create a temporary file approach
      37            1 :   std::string temp_filename = "/tmp/test_binary_data.bin";
      38            1 :   std::ofstream temp_file(temp_filename, std::ios::binary);
      39            1 :   temp_file.write(ss.str().c_str(), ss.str().size());
      40            1 :   temp_file.close();
      41              :   
      42              :   // Test the reader
      43            1 :   tpglibs::testapp::BinarySignalReader<int16_t> reader(temp_filename);
      44              :   
      45              :   // Read all data
      46            1 :   auto result = reader.next(5);
      47              :   
      48            1 :   BOOST_CHECK_EQUAL(result.size(), 5);
      49            6 :   for (size_t i = 0; i < test_data.size(); ++i) {
      50            5 :     BOOST_CHECK_EQUAL(result[i], test_data[i]);
      51              :   }
      52              :   
      53              :   // Should be at EOF
      54            1 :   BOOST_CHECK(reader.eof());
      55              :   
      56              :   // Clean up
      57            1 :   std::remove(temp_filename.c_str());
      58            1 : }
      59              : 
      60            2 : BOOST_AUTO_TEST_CASE(TestPartialRead)
      61              : {
      62              :   // Create test data: 10 int16 values
      63            1 :   std::vector<int16_t> test_data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      64              :   
      65            1 :   std::string temp_filename = "/tmp/test_partial_read.bin";
      66            1 :   std::ofstream temp_file(temp_filename, std::ios::binary);
      67           11 :   for (const auto& value : test_data) {
      68           10 :     temp_file.write(reinterpret_cast<const char*>(&value), sizeof(int16_t));
      69              :   }
      70            1 :   temp_file.close();
      71              :   
      72            1 :   tpglibs::testapp::BinarySignalReader<int16_t> reader(temp_filename);
      73              :   
      74              :   // Read first 3 elements
      75            1 :   auto result1 = reader.next(3);
      76            1 :   BOOST_CHECK_EQUAL(result1.size(), 3);
      77            1 :   BOOST_CHECK_EQUAL(result1[0], 1);
      78            1 :   BOOST_CHECK_EQUAL(result1[1], 2);
      79            1 :   BOOST_CHECK_EQUAL(result1[2], 3);
      80            1 :   BOOST_CHECK(!reader.eof());
      81              :   
      82              :   // Read next 4 elements
      83            1 :   auto result2 = reader.next(4);
      84            1 :   BOOST_CHECK_EQUAL(result2.size(), 4);
      85            1 :   BOOST_CHECK_EQUAL(result2[0], 4);
      86            1 :   BOOST_CHECK_EQUAL(result2[1], 5);
      87            1 :   BOOST_CHECK_EQUAL(result2[2], 6);
      88            1 :   BOOST_CHECK_EQUAL(result2[3], 7);
      89            1 :   BOOST_CHECK(!reader.eof());
      90              :   
      91              :   // Read remaining elements
      92            1 :   auto result3 = reader.next(5);
      93            1 :   BOOST_CHECK_EQUAL(result3.size(), 3); // Only 3 left
      94            1 :   BOOST_CHECK_EQUAL(result3[0], 8);
      95            1 :   BOOST_CHECK_EQUAL(result3[1], 9);
      96            1 :   BOOST_CHECK_EQUAL(result3[2], 10);
      97            1 :   BOOST_CHECK(reader.eof());
      98              :   
      99              :   // Clean up
     100            1 :   std::remove(temp_filename.c_str());
     101            1 : }
     102              : 
     103            2 : BOOST_AUTO_TEST_CASE(TestEmptyFile)
     104              : {
     105            1 :   std::string temp_filename = "/tmp/test_empty.bin";
     106            1 :   std::ofstream temp_file(temp_filename, std::ios::binary);
     107            1 :   temp_file.close();
     108              :   
     109            1 :   tpglibs::testapp::BinarySignalReader<int16_t> reader(temp_filename);
     110              :   
     111            1 :   auto result = reader.next(5);
     112            1 :   BOOST_CHECK_EQUAL(result.size(), 0);
     113            1 :   BOOST_CHECK(reader.eof());
     114              :   
     115              :   // Clean up
     116            1 :   std::remove(temp_filename.c_str());
     117            1 : }
     118              : 
     119            2 : BOOST_AUTO_TEST_CASE(TestSeekAndTell)
     120              : {
     121              :   // Create test data: 5 int16 values
     122            1 :   std::vector<int16_t> test_data = {10, 20, 30, 40, 50};
     123              :   
     124            1 :   std::string temp_filename = "/tmp/test_seek.bin";
     125            1 :   std::ofstream temp_file(temp_filename, std::ios::binary);
     126            6 :   for (const auto& value : test_data) {
     127            5 :     temp_file.write(reinterpret_cast<const char*>(&value), sizeof(int16_t));
     128              :   }
     129            1 :   temp_file.close();
     130              :   
     131            1 :   tpglibs::testapp::BinarySignalReader<int16_t> reader(temp_filename);
     132              :   
     133              :   // Read first element
     134            1 :   auto result1 = reader.next(1);
     135            1 :   BOOST_CHECK_EQUAL(result1.size(), 1);
     136            1 :   BOOST_CHECK_EQUAL(result1[0], 10);
     137              :   
     138              :   // Check position
     139            1 :   auto pos = reader.tellg();
     140            1 :   BOOST_CHECK_EQUAL(pos, sizeof(int16_t));
     141              :   
     142              :   // Seek to beginning
     143            1 :   reader.seekg(0);
     144              :   
     145              :   // Read all elements
     146            1 :   auto result2 = reader.next(5);
     147            1 :   BOOST_CHECK_EQUAL(result2.size(), 5);
     148            6 :   for (size_t i = 0; i < test_data.size(); ++i) {
     149            5 :     BOOST_CHECK_EQUAL(result2[i], test_data[i]);
     150              :   }
     151              :   
     152              :   // Clean up
     153            1 :   std::remove(temp_filename.c_str());
     154            1 : }
     155              : 
     156            2 : BOOST_AUTO_TEST_CASE(TestInvalidFile)
     157              : {
     158              :   // Try to open non-existent file
     159            3 :   BOOST_CHECK_THROW(tpglibs::testapp::BinarySignalReader<int16_t>("/tmp/non_existent_file.bin"), 
     160              :                     std::runtime_error);
     161            1 : }
     162              : 
     163              : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1