2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: unit_test_001.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 2020-12-03
|
|
|
|
// PURPOSE: unit tests for the DHT temperature and humidity sensor
|
|
|
|
// https://github.com/RobTillaart/DHTNew
|
|
|
|
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
|
|
|
//
|
|
|
|
|
|
|
|
// supported assertions
|
|
|
|
// ----------------------------
|
|
|
|
// assertEqual(expected, actual)
|
|
|
|
// assertNotEqual(expected, actual)
|
|
|
|
// assertLess(expected, actual)
|
|
|
|
// assertMore(expected, actual)
|
|
|
|
// assertLessOrEqual(expected, actual)
|
|
|
|
// assertMoreOrEqual(expected, actual)
|
|
|
|
// assertTrue(actual)
|
|
|
|
// assertFalse(actual)
|
|
|
|
// assertNull(actual)
|
|
|
|
|
|
|
|
#include <ArduinoUnitTests.h>
|
|
|
|
|
|
|
|
#include "dhtnew.h"
|
|
|
|
|
|
|
|
|
|
|
|
unittest_setup()
|
|
|
|
{
|
2021-12-16 11:24:05 -05:00
|
|
|
fprintf(stderr, "DHTNEW_LIB_VERSION: %s\n", (char *) DHTNEW_LIB_VERSION);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unittest_teardown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
|
|
|
|
unittest(test_constants)
|
|
|
|
{
|
|
|
|
assertEqual( 0, DHTLIB_OK );
|
|
|
|
assertEqual(-1, DHTLIB_ERROR_CHECKSUM );
|
|
|
|
assertEqual(-2, DHTLIB_ERROR_TIMEOUT_A );
|
|
|
|
assertEqual(-3, DHTLIB_ERROR_BIT_SHIFT );
|
|
|
|
assertEqual(-4, DHTLIB_ERROR_SENSOR_NOT_READY);
|
|
|
|
assertEqual(-5, DHTLIB_ERROR_TIMEOUT_C );
|
|
|
|
assertEqual(-6, DHTLIB_ERROR_TIMEOUT_D );
|
|
|
|
assertEqual(-7, DHTLIB_ERROR_TIMEOUT_B );
|
|
|
|
assertEqual(-8, DHTLIB_WAITING_FOR_READ );
|
2024-03-24 11:51:11 -04:00
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
assertEqual(-100, DHTLIB_HUMIDITY_OUT_OF_RANGE );
|
|
|
|
assertEqual(-101, DHTLIB_TEMPERATURE_OUT_OF_RANGE);
|
2024-03-24 11:51:11 -04:00
|
|
|
|
|
|
|
assertEqual(50, DHTLIB_BIT_THRESHOLD);
|
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
assertEqual(-999, DHTLIB_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
unittest(test_constructor)
|
|
|
|
{
|
|
|
|
DHTNEW dht(4);
|
|
|
|
|
2024-03-24 11:51:11 -04:00
|
|
|
// verify default flags
|
|
|
|
// assertEqual(0, dht.getType()); // calls read which blocks.
|
|
|
|
assertEqual(0.0, dht.getHumidity());
|
|
|
|
assertEqual(0.0, dht.getTemperature());
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqual(0, dht.getHumOffset());
|
|
|
|
assertEqual(0, dht.getTempOffset());
|
2024-03-24 11:51:11 -04:00
|
|
|
|
|
|
|
assertEqual(0, dht.lastRead());
|
|
|
|
|
|
|
|
#if defined(__AVR__)
|
2021-01-29 06:31:58 -05:00
|
|
|
fprintf(stderr, "__AVR__ defined.");
|
|
|
|
assertFalse(dht.getDisableIRQ());
|
2024-03-24 11:51:11 -04:00
|
|
|
#else
|
2021-01-29 06:31:58 -05:00
|
|
|
fprintf(stderr, "__AVR__ not defined.");
|
|
|
|
assertTrue(dht.getDisableIRQ());
|
2024-03-24 11:51:11 -04:00
|
|
|
#endif
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
assertFalse(dht.getWaitForReading());
|
|
|
|
assertEqual(0, dht.getReadDelay());
|
|
|
|
assertFalse(dht.getSuppressError());
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
unittest(test_hum_temp)
|
|
|
|
{
|
|
|
|
DHTNEW dht(4);
|
|
|
|
|
|
|
|
assertEqual(0, dht.getHumidity());
|
|
|
|
assertEqual(0, dht.getHumOffset());
|
|
|
|
dht.setHumOffset(1.5);
|
|
|
|
assertEqual(1.5, dht.getHumOffset());
|
2024-03-24 11:51:11 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqual(0, dht.getTemperature());
|
|
|
|
assertEqual(0, dht.getTempOffset());
|
|
|
|
dht.setTempOffset(-1.5);
|
|
|
|
assertEqual(-1.5, dht.getTempOffset());
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
|
2024-03-24 11:51:11 -04:00
|
|
|
unittest(test_setType)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
DHTNEW dht(4);
|
|
|
|
|
|
|
|
dht.setType(11);
|
|
|
|
assertEqual(11, dht.getType());
|
|
|
|
dht.setType(22);
|
|
|
|
assertEqual(22, dht.getType());
|
2024-03-24 11:51:11 -04:00
|
|
|
dht.setType(23);
|
|
|
|
assertEqual(23, dht.getType());
|
|
|
|
dht.setType(70);
|
|
|
|
assertEqual(70, dht.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_process_flags)
|
|
|
|
{
|
|
|
|
DHTNEW dht(4);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
dht.setDisableIRQ(true);
|
|
|
|
assertTrue(dht.getDisableIRQ());
|
|
|
|
dht.setDisableIRQ(false);
|
|
|
|
assertFalse(dht.getDisableIRQ());
|
2024-03-24 11:51:11 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
dht.setWaitForReading(true);
|
|
|
|
assertTrue(dht.getWaitForReading());
|
|
|
|
dht.setWaitForReading(false);
|
|
|
|
assertFalse(dht.getWaitForReading());
|
2024-03-24 11:51:11 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
dht.setReadDelay(1500);
|
|
|
|
assertEqual(1500, dht.getReadDelay());
|
|
|
|
dht.setType(11);
|
|
|
|
dht.setReadDelay();
|
|
|
|
assertEqual(0, dht.getReadDelay());
|
|
|
|
dht.setType(22);
|
|
|
|
dht.setReadDelay();
|
|
|
|
assertEqual(0, dht.getReadDelay());
|
2024-03-24 11:51:11 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
dht.setSuppressError(true);
|
|
|
|
assertTrue(dht.getSuppressError());
|
|
|
|
dht.setSuppressError(false);
|
|
|
|
assertFalse(dht.getSuppressError());
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
unittest(test_read)
|
|
|
|
{
|
|
|
|
DHTNEW dht(4);
|
|
|
|
|
2021-12-16 11:24:05 -05:00
|
|
|
fprintf(stderr, "\tread() cannot be tested GODMODE?\n");
|
2024-03-24 11:51:11 -04:00
|
|
|
// int rc = dht.read();
|
|
|
|
// fprintf(stderr, "%d\n", rc);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
long lr = dht.lastRead();
|
|
|
|
fprintf(stderr, "\ttime since lastRead %ld\n", lr);
|
|
|
|
}
|
|
|
|
|
2024-03-24 11:51:11 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
unittest_main()
|
|
|
|
|
2024-03-24 11:51:11 -04:00
|
|
|
|
|
|
|
// -- END OF FILE --
|