LCOV - code coverage report
Current view: top level - firmware/libfirmware/util/test - test_fragments.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 105 105 100.0 %
Date: 2024-04-25 02:23:43 Functions: 7 7 100.0 %

          Line data    Source code
       1             : #include <rusefi/fragments.h>
       2             : #include <rusefi/arrays.h>
       3             : 
       4             : #include <gtest/gtest.h>
       5             : 
       6             : #include <cstring>
       7             : 
       8             : struct obj1 {
       9             :         const uint8_t x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      10             : };
      11             : 
      12             : static_assert(sizeof(obj1) == 10);
      13             : 
      14             : struct obj2 {
      15             :         const uint8_t x[5] = { 11, 12, 13, 14, 15 };
      16             : };
      17             : 
      18             : static_assert(sizeof(obj2) == 5);
      19             : 
      20             : obj1 buffer10;
      21             : obj2 buffer5;
      22             : 
      23             : template<>
      24          20 : const obj1* getLiveData(size_t) {
      25          20 :         return &buffer10;
      26             : }
      27             : 
      28             : template<>
      29          23 : const obj2* getLiveData(size_t idx) {
      30          23 :         if (idx == 1) {
      31           7 :                 return nullptr;
      32             :         }
      33             : 
      34          16 :         return &buffer5;
      35             : }
      36             : 
      37             : static FragmentEntry fragmentBuffer[] = {
      38             :         decl_frag<obj1>{},
      39             :         decl_frag<obj2, 0>{},
      40             :         decl_frag<obj2, 1>{},     // this one will be null
      41             :         decl_frag<obj1>{},
      42             : };
      43             : 
      44             : static uint8_t buffer[120];
      45           7 : static void resetBuffer() {
      46           7 :         memset(buffer, 0xFF, sizeof(buffer));
      47           7 : }
      48             : 
      49           4 : TEST(Util_Fragments, fragments) {
      50             :         static_assert(4 == efi::size(fragmentBuffer));
      51             : 
      52           1 :         FragmentList fragments{ fragmentBuffer, efi::size(fragmentBuffer) };
      53             : 
      54           1 :         resetBuffer();
      55             : 
      56             :         {
      57             :                 // Exactly copy both buffers
      58           1 :                 uint8_t expected[] = { 
      59             :                         1, 2, 3, 4, 5, 6, 7, 8, 9, 10,  // buffer10
      60             :                         11, 12, 13, 14, 15,                             // buffer5
      61             :                         0, 0, 0, 0, 0,                                  // null obj2
      62             :                         1, 2, 3, 4, 5, 6, 7, 8, 9, 10   // buffer10 again
      63             :                 };
      64           1 :                 copyRange(buffer, fragments, 0, 10 + 5 + 5 + 10);
      65           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
      66             : 
      67           1 :                 uint8_t *ptr;
      68           1 :                 size_t offset = 0;
      69             :                 size_t ret;
      70             :                 /* pointer to first fragment */
      71           1 :                 ret = getRangePtr(&ptr, fragments, offset, 10 + 5 + 5 + 10);
      72           1 :                 EXPECT_TRUE( 10 == ret);
      73           1 :                 EXPECT_TRUE( 0 == std::memcmp(ptr, expected, ret));
      74           1 :                 EXPECT_TRUE( fragmentBuffer[0].get() == ptr);
      75             : 
      76           1 :                 offset += ret;
      77             :                 /* pointer to second fragment */
      78           1 :                 ret = getRangePtr(&ptr, fragments, offset, 5 + 5 + 10);
      79           1 :                 EXPECT_TRUE( 5 == ret);
      80           1 :                 EXPECT_TRUE( 0 == std::memcmp(ptr, expected + 10, ret));
      81           1 :                 EXPECT_TRUE( fragmentBuffer[1].get() == ptr);
      82             : 
      83           1 :                 offset += ret;
      84             :                 /* pointer to null */
      85           1 :                 ret = getRangePtr(&ptr, fragments, offset, 5 + 10);
      86           1 :                 EXPECT_TRUE( 5 == ret);
      87           1 :                 EXPECT_TRUE( NULL == ptr);
      88           1 :                 EXPECT_TRUE( fragmentBuffer[2].get() == ptr);
      89             : 
      90           1 :                 offset += ret;
      91             :                 /* pointer to fourth fragment */
      92           1 :                 ret = getRangePtr(&ptr, fragments, offset, 10);
      93           1 :                 EXPECT_TRUE( 10 == ret);
      94           1 :                 EXPECT_TRUE( 0 == std::memcmp(ptr, expected + 10 + 5 + 5, ret));
      95           1 :                 EXPECT_TRUE( fragmentBuffer[3].get() == ptr);
      96             :         }
      97             : 
      98           1 :         resetBuffer();
      99             : 
     100             :         {
     101             :                 // Check overlap between first and second fragments
     102           1 :                 uint8_t expected[] = {9, 10, 11, 12, 13};
     103           1 :                 copyRange(buffer, fragments, 8, 5);
     104           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
     105             : 
     106           1 :                 uint8_t *ptr;
     107           1 :                 size_t offset = 8;
     108             :                 size_t ret;
     109           1 :                 ret = getRangePtr(&ptr, fragments, offset, 5);
     110           1 :                 EXPECT_TRUE( 2 == ret);
     111           1 :                 EXPECT_TRUE( fragmentBuffer[0].get() + 8 == ptr);
     112             : 
     113           1 :                 offset += ret;
     114           1 :                 ret = getRangePtr(&ptr, fragments, offset, 5 - 2);
     115           1 :                 EXPECT_TRUE( 3 == ret);
     116           1 :                 EXPECT_TRUE( fragmentBuffer[1].get() + 0 == ptr);
     117             :         }
     118             : 
     119           1 :         resetBuffer();
     120             : 
     121             :         {
     122             :                 // Check partial of only second fragment
     123           1 :                 uint8_t expected[] = {13, 14, 15};
     124           1 :                 copyRange(buffer, fragments, 12, 3);
     125           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
     126             : 
     127           1 :                 uint8_t *ptr;
     128             :                 size_t ret;
     129           1 :                 ret = getRangePtr(&ptr, fragments, 12, 3);
     130           1 :                 EXPECT_TRUE( 3 == ret);
     131           1 :                 EXPECT_TRUE( fragmentBuffer[1].get() + 2 == ptr);
     132             :         }
     133             : 
     134           1 :         resetBuffer();
     135             : 
     136             :         {
     137             :                 // Check overlap between second fragment and null fragment
     138           1 :                 uint8_t expected[] = {15, 0, 0};
     139           1 :                 copyRange(buffer, fragments, 14, 3);
     140           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
     141             : 
     142           1 :                 uint8_t *ptr;
     143           1 :                 size_t offset = 14;
     144             :                 size_t ret;
     145           1 :                 ret = getRangePtr(&ptr, fragments, offset, 5);
     146           1 :                 EXPECT_TRUE( 1 == ret);
     147           1 :                 EXPECT_TRUE( fragmentBuffer[1].get() + 4 == ptr);
     148             : 
     149           1 :                 offset += ret;
     150           1 :                 ret = getRangePtr(&ptr, fragments, offset, 5 - 1);
     151           1 :                 EXPECT_TRUE( 4 == ret);
     152           1 :                 EXPECT_TRUE( NULL == ptr);
     153             :         }
     154             : 
     155           1 :         resetBuffer();
     156             : 
     157             :         {
     158             :                 // Check overlap between null fragment and 4th fragment
     159           1 :                 uint8_t expected[] = {0, 1, 2};
     160           1 :                 copyRange(buffer, fragments, 19, 3);
     161           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
     162             : 
     163           1 :                 uint8_t *ptr;
     164           1 :                 size_t offset = 19;
     165             :                 size_t ret;
     166           1 :                 ret = getRangePtr(&ptr, fragments, offset, 3);
     167           1 :                 EXPECT_TRUE( 1 == ret);
     168           1 :                 EXPECT_TRUE( NULL == ptr);
     169             : 
     170           1 :                 offset += ret;
     171           1 :                 ret = getRangePtr(&ptr, fragments, offset, 3 - 1);
     172           1 :                 EXPECT_TRUE( 2 == ret);
     173           1 :                 EXPECT_TRUE( fragmentBuffer[3].get() == ptr);
     174             :         }
     175             : 
     176           1 :         resetBuffer();
     177             : 
     178             :         {
     179             :                 // Check overlap between last fragment and off-end of buffer (off-buffer should give 0s)
     180           1 :                 uint8_t expected[] = {9, 10, 0, 0};
     181           1 :                 copyRange(buffer, fragments, 28, 4);
     182           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
     183             : 
     184           1 :                 uint8_t *ptr;
     185           1 :                 size_t offset = 28;
     186             :                 size_t ret;
     187           1 :                 ret = getRangePtr(&ptr, fragments, offset, 4);
     188           1 :                 EXPECT_TRUE( 2 == ret);
     189           1 :                 EXPECT_TRUE( fragmentBuffer[3].get() + 8 == ptr);
     190             : 
     191           1 :                 offset += ret;
     192           1 :                 ret = getRangePtr(&ptr, fragments, offset, 4 - 2);
     193           1 :                 EXPECT_TRUE( 2 == ret);
     194           1 :                 EXPECT_TRUE( NULL == ptr);
     195             :         }
     196             : 
     197           1 :         resetBuffer();
     198             : 
     199             :         {
     200             :                 // Check way off the end of the buffer
     201           1 :                 uint8_t expected[] = {0, 0, 0};
     202           1 :                 copyRange(buffer, fragments, 1000, 3);
     203           1 :                 EXPECT_TRUE( 0 == std::memcmp(buffer, expected, sizeof(expected)));
     204             : 
     205           1 :                 uint8_t *ptr;
     206             :                 size_t ret;
     207           1 :                 ret = getRangePtr(&ptr, fragments, 1000, 3);
     208           1 :                 EXPECT_TRUE( 3 == ret);
     209           1 :                 EXPECT_TRUE( NULL == ptr);
     210             :         }
     211           1 : }

Generated by: LCOV version 1.14