LCOV - code coverage report
Current view: top level - unit_tests/tests - test_gpiochip.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 47 93.6 %
Date: 2024-04-22 23:59:02 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /**
       2             :  * @file        test_gpiochip.cpp
       3             :  *
       4             :  * @date Mar 12, 2019
       5             :  */
       6             : 
       7             : #include "pch.h"
       8             : 
       9             : #include "gpio/gpio_ext.h"
      10             : 
      11             : using ::testing::_;
      12             : 
      13             : static int io_state = 0;
      14             : 
      15             : static int initcalls = 0;
      16             : struct GoodChip : public GpioChip {
      17           2 :         int init() override {
      18           2 :                 initcalls++;
      19           2 :                 return 0;
      20             :         }
      21             : };
      22             : 
      23             : class TestChip1 : public GoodChip {
      24           2 :         int readPad(size_t pin) override {
      25           2 :                 if (pin & 0x01)
      26           1 :                         return 1;
      27           1 :                 return 0;
      28             :         }
      29             : };
      30             : 
      31             : static TestChip1 testchip1;
      32             : 
      33             : class TestChip2 : public GoodChip {
      34           2 :         int writePad(size_t pin, int value) override {
      35           2 :                 if (value)
      36           1 :                         io_state |=  (1 << value);
      37             :                 else
      38           1 :                         io_state &= ~(1 << value);
      39             : 
      40           2 :                 return 0;
      41             :         }
      42             : };
      43             : 
      44             : static TestChip2 testchip2;
      45             : 
      46             : static int calls_to_failed_chip = 0;
      47             : // This chip fails to start
      48             : class TestChip3 : public GpioChip {
      49           0 :         int writePad(size_t pin, int value) override {
      50           0 :                 calls_to_failed_chip++;
      51           0 :                 return 0;
      52             :         }
      53             : 
      54           1 :         int init() override {
      55           1 :                 return -1;
      56             :         }
      57             : };
      58             : 
      59             : static TestChip3 testchip3;
      60             : 
      61           4 : TEST(gpioext, testGpioExt) {
      62           1 :         int ret;
      63             :         int chip1_base, chip2_base, chip3_base;
      64             : 
      65           1 :         printf("====================================================================================== testGpioExt\r\n");
      66             : 
      67             :         /* should fail to register chip with zero gpios */
      68           1 :         EXPECT_FALSE(gpiochip_register((brain_pin_e)(BRAIN_PIN_ONCHIP_LAST + 1), "invalid", testchip1, 0) > 0);
      69             : 
      70             :         /* should fail to register chip with base overlapig on-chip gpios */
      71           1 :         EXPECT_FALSE(gpiochip_register((brain_pin_e)(BRAIN_PIN_ONCHIP_LAST - 1), "invalid", testchip1, 0) > 0);
      72             : 
      73           1 :         chip1_base = gpiochip_register((brain_pin_e)(BRAIN_PIN_ONCHIP_LAST + 1), "input only", testchip1, 16);
      74           1 :         EXPECT_TRUE(chip1_base > 0);
      75             : 
      76           1 :         EXPECT_EQ(16, gpiochips_get_total_pins());
      77             : 
      78             :         /* should fail to register chip overlapping other one */
      79           1 :         EXPECT_FALSE(gpiochip_register((brain_pin_e)(BRAIN_PIN_ONCHIP_LAST + 1 + 15), "output only", testchip2, 16) > 0);
      80             : 
      81           1 :         chip2_base = gpiochip_register((brain_pin_e)(BRAIN_PIN_ONCHIP_LAST + 1 + 16), "output only", testchip2, 16);
      82           1 :         EXPECT_TRUE(chip2_base > 0);
      83             : 
      84             :         /* this chip will fail to init, but should be registered without errors */
      85           1 :         chip3_base = gpiochip_register((brain_pin_e)(BRAIN_PIN_ONCHIP_LAST + 1 + 16 + 16), "failed chip", testchip3, 16);
      86           1 :         EXPECT_TRUE(chip2_base > 0);
      87             : 
      88           1 :         EXPECT_EQ(48, gpiochips_get_total_pins());
      89             : 
      90             :         /* init 3 chips, one will fail */
      91           1 :         ret = gpiochips_init();
      92           1 :         EXPECT_EQ(32, ret);
      93             : 
      94             :         /* two drivers should be inited */
      95           1 :         EXPECT_EQ(2, initcalls);
      96             : 
      97             :         /* gpio reads */
      98           1 :         EXPECT_TRUE(gpiochips_readPad((Gpio)(chip1_base + 0)) == 0);
      99           1 :         EXPECT_TRUE(gpiochips_readPad((Gpio)(chip1_base + 1)) != 0);
     100             : 
     101             :         /* gpio write */
     102           1 :         gpiochips_writePad((Gpio)(chip2_base + 0), 0);
     103           1 :         gpiochips_writePad((Gpio)(chip2_base + 1), 1);
     104           1 :         EXPECT_EQ(0x02, io_state);
     105             : 
     106             :         /* try to access failed chip */
     107           1 :         EXPECT_ANY_THROW(gpiochips_writePad((Gpio)(chip3_base + 0), 0));
     108           1 :         EXPECT_ANY_THROW(gpiochips_writePad((Gpio)(chip3_base + 1), 1));
     109           1 :         EXPECT_EQ(0, calls_to_failed_chip);
     110             : 
     111             :   // todo: make readPad fail in a similar way?
     112             : 
     113             :         /* read/write outside range */
     114           1 :         EXPECT_TRUE(gpiochips_readPad((Gpio)(chip1_base - 1)) < 0);
     115           1 :         EXPECT_ANY_THROW(gpiochips_writePad((Gpio)(chip1_base - 1), 1));
     116             : 
     117           1 :   EXPECT_TRUE(gpiochips_readPad((Gpio)(chip3_base + 16)) < 0);
     118           1 :         EXPECT_ANY_THROW(gpiochips_writePad((Gpio)(chip3_base + 16), 1));
     119             : 
     120           1 : }

Generated by: LCOV version 1.14