GY-63_MS5611/libraries/SparseMatrix/test/unit_test_001.cpp

146 lines
2.7 KiB
C++
Raw Normal View History

2022-07-12 14:20:20 -04:00
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2022-07-12
// PURPOSE: unit tests for SparseMatrix library
// https://github.com/RobTillaart/SparseMatrix
// 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 "Arduino.h"
#include "SparseMatrix.h"
unittest_setup()
{
fprintf(stderr, "SPARSEMATRIX_LIB_VERSION: %s\n", (char*) SPARSEMATRIX_LIB_VERSION);
}
unittest_teardown()
{
}
2022-07-14 11:28:24 -04:00
unittest(test_constants)
{
assertEqual(1000, SPARSEMATRIX_MAX_SIZE);
}
2022-07-12 14:20:20 -04:00
unittest(test_constructor)
{
SparseMatrix sm(10);
assertEqual(10, sm.size());
assertEqual(0, sm.count());
2022-07-14 11:28:24 -04:00
SparseMatrix sm2(1100);
assertEqual(1000, sm2.size());
2022-07-12 14:20:20 -04:00
}
unittest(test_set)
{
SparseMatrix sm(10);
for (int i = 0; i < 10; i++)
{
assertTrue(sm.set(i, i, 1.0 * i * i));
2022-07-13 11:47:55 -04:00
assertEqual(i, sm.count());
2022-07-12 14:20:20 -04:00
}
2022-07-13 11:47:55 -04:00
assertTrue(sm.set(3, 4, 5));
assertFalse(sm.set(5, 4, 5)); // don't fit any more...
2022-07-14 11:28:24 -04:00
// do not set new element to zero
2022-07-13 11:47:55 -04:00
sm.clear();
assertEqual(0, sm.count());
sm.set(1, 2, 0);
assertEqual(0, sm.count());
2022-07-12 14:20:20 -04:00
}
unittest(test_get)
{
SparseMatrix sm(10);
for (int i = 0; i < 10; i++)
{
assertTrue(sm.set(i, i, 1.0 * i * i));
assertEqualFloat(1.0 * i * i, sm.get(i, i), 0.001);
}
}
unittest(test_sum)
{
SparseMatrix sm(10);
for (int i = 0; i < 10; i++)
{
assertTrue(sm.set(i, i, 10));
}
assertEqualFloat(100, sm.sum(), 0.0001);
}
2022-07-13 11:47:55 -04:00
unittest(test_add)
{
SparseMatrix sm(10);
for (int i = 0; i < 10; i++)
{
assertTrue(sm.add(i, i, 1.0 * i * i));
assertEqualFloat(1.0 * i * i, sm.get(i, i), 0.001);
}
for (int i = 0; i < 10; i++)
{
assertTrue(sm.add(i, i, 1.0 * i * i));
assertEqualFloat(2.0 * i * i, sm.get(i, i), 0.001);
}
for (int i = 0; i < 10; i++)
{
assertTrue(sm.add(i, i, -2.0 * i * i));
assertEqualFloat(0, sm.get(i, i), 0.001);
}
assertEqual(0, sm.count());
}
2022-11-25 08:22:25 -05:00
unittest(test_bounding_box)
2022-07-16 07:42:58 -04:00
{
SparseMatrix sm(10);
// 10 x 10 matrix - 6 random elements in the middle
for (int i = 0; i < 6; i++)
{
uint8_t x = random(5) + 3;
uint8_t y = random(5) + 3;
sm.set(x, y, random(37));
}
// random generator does not work
// assertEqual(6, sm.count());
assertEqual(1, sm.count());
uint8_t minX, maxX, minY, maxY;
sm.boundingBox(minX, maxX, minY, maxY);
fprintf(stderr, "%d\t%d\t%d\t%d\n", minX, maxX, minY, maxY);
}
2022-07-12 14:20:20 -04:00
unittest_main()
// -- END OF FILE --