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-07-12 14:20:20 -04:00
|
|
|
unittest_main()
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|