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

          Line data    Source code
       1             : #include <gtest/gtest.h>
       2             : #include <rusefi/rusefi_time_types.h>
       3             : #include <rusefi/rusefi_time_wraparound.h>
       4             : 
       5           4 : TEST(util, WrapAround62) {
       6             :         // Random test
       7             :         {
       8           1 :                 WrapAround62 t;
       9           1 :                 uint32_t source = 0;
      10           1 :                 uint64_t actual = 0;
      11             : 
      12             :                 // Test random progression, positive and negative.
      13           1 :                 uint32_t seed = time(NULL);
      14           1 :                 printf("Testing with seed 0x%08x\n", seed);
      15           1 :                 srand(seed);
      16       10001 :                 for (unsigned i = 0; i < 10000; i++) {
      17       10000 :                         int32_t delta = rand();
      18       10000 :                         if (delta < 0) {
      19           0 :                                 delta = ~delta;
      20             :                         }
      21       10000 :                         delta -= RAND_MAX >> 1;
      22             : 
      23             :                         // Cap negative test
      24       10000 :                         if (delta < 0 && -delta > actual) {
      25         202 :                                 delta = -actual;
      26             :                         }
      27             : 
      28       10000 :                         source += delta;
      29       10000 :                         actual += delta;
      30             : 
      31       10000 :                         uint64_t next = t.update(source);
      32       10000 :                         EXPECT_EQ(actual, next);
      33             :                 }
      34             :         }
      35             : 
      36             :         // More pointed test for expected edge conditions
      37             :         {
      38           1 :                 WrapAround62 t;
      39             : 
      40           1 :                 EXPECT_EQ(t.update(0x03453455), 0x003453455LL);
      41           1 :                 EXPECT_EQ(t.update(0x42342323), 0x042342323LL);
      42           1 :                 EXPECT_EQ(t.update(0x84356345), 0x084356345LL);
      43           1 :                 EXPECT_EQ(t.update(0x42342323), 0x042342323LL);
      44           1 :                 EXPECT_EQ(t.update(0x84356345), 0x084356345LL);
      45           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x0C5656565LL);
      46           1 :                 EXPECT_EQ(t.update(0x01122112), 0x101122112LL); // Wrap around!
      47           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x0C5656565LL);
      48           1 :                 EXPECT_EQ(t.update(0x84356345), 0x084356345LL);
      49           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x0C5656565LL);
      50           1 :                 EXPECT_EQ(t.update(0x01122112), 0x101122112LL); // Wrap around!
      51           1 :                 EXPECT_EQ(t.update(0x42342323), 0x142342323LL);
      52           1 :                 EXPECT_EQ(t.update(0x84356345), 0x184356345LL);
      53           1 :                 EXPECT_EQ(t.update(0x42342323), 0x142342323LL);
      54           1 :                 EXPECT_EQ(t.update(0x84356345), 0x184356345LL);
      55           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x1C5656565LL);
      56           1 :                 EXPECT_EQ(t.update(0x01122112), 0x201122112LL); // Wrap around!
      57           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x1C5656565LL);
      58           1 :                 EXPECT_EQ(t.update(0x84356345), 0x184356345LL);
      59           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x1C5656565LL);
      60           1 :                 EXPECT_EQ(t.update(0x01122112), 0x201122112LL); // Wrap around!
      61           1 :                 EXPECT_EQ(t.update(0xC5656565), 0x1C5656565LL);
      62           1 :                 EXPECT_EQ(t.update(0x84356345), 0x184356345LL);
      63           1 :                 EXPECT_EQ(t.update(0x42342323), 0x142342323LL);
      64           1 :                 EXPECT_EQ(t.update(0x01122112), 0x101122112LL);
      65           1 :                 EXPECT_EQ(t.update(0x84356345), 0x084356345LL);
      66           1 :                 EXPECT_EQ(t.update(0x42342323), 0x042342323LL);
      67           1 :                 EXPECT_EQ(t.update(0x03453455), 0x003453455LL);
      68             :         }
      69           1 : }

Generated by: LCOV version 1.14