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 I2C_ASDX pressure sensor library
|
|
|
|
// https://github.com/RobTillaart/I2C_ASDX
|
|
|
|
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
|
|
|
//
|
|
|
|
|
|
|
|
// supported assertions
|
|
|
|
// ----------------------------
|
|
|
|
// assertEqual(expected, actual); // a == b
|
|
|
|
// assertNotEqual(unwanted, actual); // a != b
|
|
|
|
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
|
|
|
|
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
|
|
|
|
// assertLess(upperBound, actual); // a < b
|
|
|
|
// assertMore(lowerBound, actual); // a > b
|
|
|
|
// assertLessOrEqual(upperBound, actual); // a <= b
|
|
|
|
// assertMoreOrEqual(lowerBound, actual); // a >= b
|
|
|
|
// assertTrue(actual);
|
|
|
|
// assertFalse(actual);
|
|
|
|
// assertNull(actual);
|
|
|
|
|
|
|
|
// // special cases for floats
|
|
|
|
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
|
|
|
|
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
|
|
|
|
// assertInfinity(actual); // isinf(a)
|
|
|
|
// assertNotInfinity(actual); // !isinf(a)
|
|
|
|
// assertNAN(arg); // isnan(a)
|
|
|
|
// assertNotNAN(arg); // !isnan(a)
|
|
|
|
|
2022-11-12 09:43:08 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include <ArduinoUnitTests.h>
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "I2C_ASDX.h"
|
|
|
|
|
|
|
|
|
|
|
|
unittest_setup()
|
|
|
|
{
|
2021-12-19 11:54:58 -05:00
|
|
|
fprintf(stderr, "I2C_ASDX_VERSION: %s\n", (char *) I2C_ASDX_VERSION);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unittest_teardown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_constructor)
|
|
|
|
{
|
|
|
|
I2C_ASDX sensor(0x58, 100);
|
2021-12-19 11:54:58 -05:00
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
assertEqual(I2C_ASDX_INIT, sensor.state());
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
assertTrue(sensor.begin());
|
2022-11-12 09:43:08 -05:00
|
|
|
assertTrue(sensor.isConnected()); // incorrect, keep build happy
|
|
|
|
assertTrue(sensor.available()); // obsolete in the future
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
fprintf(stderr, "test state\n");
|
2021-01-29 06:31:58 -05:00
|
|
|
assertEqual(0, sensor.errorCount());
|
|
|
|
assertEqual(0, sensor.lastRead());
|
2021-06-07 05:49:28 -04:00
|
|
|
assertEqual(I2C_ASDX_OK, sensor.state());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_constants)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
fprintf(stderr, "Test conversion constants\n");
|
2021-06-07 05:49:28 -04:00
|
|
|
assertEqualFloat(68.9475729, PSI2MILLIBAR, 1e-4);
|
|
|
|
assertEqualFloat(0.01450377377, MILLIBAR2PSI, 1e-4);
|
|
|
|
assertEqualFloat(0.0689475729, PSI2BAR, 1e-4);
|
|
|
|
assertEqualFloat(14.503773773, BAR2PSI, 1e-4);
|
|
|
|
|
|
|
|
assertEqualFloat(0.001, MILLIBAR2BAR, 1e-7);
|
|
|
|
assertEqualFloat(9.872e-4, MILLIBAR2ATM, 1e-7);
|
|
|
|
assertEqualFloat(1000, MILLIBAR2DYNES, 1);
|
|
|
|
assertEqualFloat(2.9539e-2, MILLIBAR2INHG, 1e-5);
|
|
|
|
|
|
|
|
assertEqualFloat(0.4018, MILLIBAR2INH2O, 1e-4);
|
|
|
|
assertEqualFloat(100, MILLIBAR2PASCAL, 1e-3);
|
|
|
|
assertEqualFloat(0.75028, MILLIBAR2TORR, 1e-5);
|
|
|
|
assertEqualFloat(0.075028, MILLIBAR2CMHG, 1e-6);
|
|
|
|
|
|
|
|
assertEqualFloat(1.02056, MILLIBAR2CMH2O, 1e-5);
|
|
|
|
assertEqualFloat(100, MILLIBAR2MSW, 1);
|
2021-12-19 11:54:58 -05:00
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
fprintf(stderr, "Test state constants\n");
|
|
|
|
assertEqual(1, I2C_ASDX_OK);
|
|
|
|
assertEqual(0, I2C_ASDX_INIT);
|
|
|
|
assertEqual(-1, I2C_ASDX_READ_ERROR);
|
|
|
|
assertEqual(-2, I2C_ASDX_C000_ERROR);
|
|
|
|
assertEqual(-3, I2C_ASDX_CONNECT_ERROR);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
unittest(test_read_zero)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
I2C_ASDX sensor(0x58, 100);
|
2021-12-19 11:54:58 -05:00
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
assertTrue(sensor.begin());
|
2022-11-12 09:43:08 -05:00
|
|
|
assertTrue(sensor.isConnected()); // incorrect, keep build happy
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
fprintf(stderr, "Test default pressure\n");
|
|
|
|
assertEqual(0, sensor.getPressure());
|
|
|
|
assertEqual(0, sensor.getMilliBar());
|
|
|
|
assertEqual(0, sensor.getBar());
|
|
|
|
assertEqual(0, sensor.getPSI());
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
assertEqual(0, sensor.getATM());
|
|
|
|
assertEqual(0, sensor.getDynes());
|
|
|
|
assertEqual(0, sensor.getInchHg());
|
|
|
|
assertEqual(0, sensor.getInchH2O());
|
|
|
|
assertEqual(0, sensor.getPascal());
|
|
|
|
assertEqual(0, sensor.getTORR());
|
|
|
|
assertEqual(0, sensor.getCmHg());
|
|
|
|
assertEqual(0, sensor.getCmH2O());
|
|
|
|
assertEqual(0, sensor.getMSW());
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// assertEqual(I2C_ASDX_READ_ERROR, sensor.read());
|
|
|
|
|
|
|
|
// fprintf(stderr, "Test after read\n");
|
|
|
|
// assertEqual(0, sensor.getPressure());
|
|
|
|
// assertEqual(0, sensor.getMilliBar());
|
|
|
|
// assertEqual(0, sensor.getBar());
|
|
|
|
// assertEqual(0, sensor.getPSI());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest_main()
|
|
|
|
|
|
|
|
// --------
|