arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

unit_test_001.cpp (2673B)


      1 //
      2 //    FILE: unit_test_001.cpp
      3 //  AUTHOR: Rob Tillaart
      4 //    DATE: 2020-12-03
      5 // PURPOSE: unit tests for the DHT temperature and humidity sensor
      6 //          https://github.com/RobTillaart/DHTNew
      7 //          https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
      8 //
      9 
     10 // supported assertions
     11 // ----------------------------
     12 // assertEqual(expected, actual)
     13 // assertNotEqual(expected, actual)
     14 // assertLess(expected, actual)
     15 // assertMore(expected, actual)
     16 // assertLessOrEqual(expected, actual)
     17 // assertMoreOrEqual(expected, actual)
     18 // assertTrue(actual)
     19 // assertFalse(actual)
     20 // assertNull(actual)
     21 
     22 #include <ArduinoUnitTests.h>
     23 
     24 #include "Arduino.h"
     25 #include "dhtnew.h"
     26 
     27 
     28 unittest_setup()
     29 {
     30 }
     31 
     32 unittest_teardown()
     33 {
     34 }
     35 
     36 unittest(test_constructor)
     37 {
     38   DHTNEW dht(4);
     39 
     40   // verify default flags
     41   // assertEqual(0, dht.getType());     // calls read which blocks.
     42   assertEqual(0, dht.getHumOffset());
     43   assertEqual(0, dht.getTempOffset());
     44   #if defined(__AVR__)
     45   fprintf(stderr, "__AVR__ defined.");
     46   assertFalse(dht.getDisableIRQ());
     47   #else
     48   fprintf(stderr, "__AVR__ not defined.");
     49   assertTrue(dht.getDisableIRQ());
     50   #endif
     51   assertFalse(dht.getWaitForReading());
     52   assertEqual(0, dht.getReadDelay());
     53   assertFalse(dht.getSuppressError());
     54 }
     55 
     56 unittest(test_hum_temp)
     57 {
     58   DHTNEW dht(4);
     59 
     60   assertEqual(0, dht.getHumidity());
     61   assertEqual(0, dht.getHumOffset());
     62   dht.setHumOffset(1.5);
     63   assertEqual(1.5, dht.getHumOffset());
     64   
     65   assertEqual(0, dht.getTemperature());
     66   assertEqual(0, dht.getTempOffset());
     67   dht.setTempOffset(-1.5);
     68   assertEqual(-1.5, dht.getTempOffset());
     69 }
     70 
     71 unittest(test_process_flags)
     72 {
     73   DHTNEW dht(4);
     74 
     75   dht.setType(11);
     76   assertEqual(11, dht.getType());
     77   dht.setType(22);
     78   assertEqual(22, dht.getType());
     79   
     80   dht.setDisableIRQ(true);
     81   assertTrue(dht.getDisableIRQ());
     82   dht.setDisableIRQ(false);
     83   assertFalse(dht.getDisableIRQ());
     84   
     85   dht.setWaitForReading(true);
     86   assertTrue(dht.getWaitForReading());
     87   dht.setWaitForReading(false);
     88   assertFalse(dht.getWaitForReading());
     89   
     90   dht.setReadDelay(1500);
     91   assertEqual(1500, dht.getReadDelay());
     92   dht.setType(11);
     93   dht.setReadDelay();
     94   assertEqual(0, dht.getReadDelay());
     95   dht.setType(22);
     96   dht.setReadDelay();
     97   assertEqual(0, dht.getReadDelay());
     98   
     99   dht.setSuppressError(true);
    100   assertTrue(dht.getSuppressError());
    101   dht.setSuppressError(false);
    102   assertFalse(dht.getSuppressError());
    103 }
    104 
    105 unittest(test_read)
    106 {
    107   DHTNEW dht(4);
    108 
    109   fprintf(stderr, "\tread() cannot be tested\n");
    110   // int rc = dht.read();
    111   // fprintf(stderr, "%d\n", rc);
    112   
    113   long lr = dht.lastRead();
    114   fprintf(stderr, "\ttime since lastRead %ld\n", lr);
    115 }
    116 
    117 unittest_main()
    118 
    119 // --------