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

166 lines
2.9 KiB
C++
Raw Permalink Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-12-13
// PURPOSE: unit tests for the BitArray
2023-02-08 08:03:23 -05:00
// https://github.com/RobTillaart/BitArray
2021-01-29 06:31:58 -05:00
// 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)
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
#include <ArduinoUnitTests.h>
#include "BitArray.h"
unittest_setup()
{
2022-10-29 09:54:56 -04:00
fprintf(stderr, "BITARRAY_LIB_VERSION: %s\n", (char *) BITARRAY_LIB_VERSION);
2021-01-29 06:31:58 -05:00
}
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
unittest_teardown()
{
fprintf(stderr, "\n");
}
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
unittest(test_constructor)
{
BitArray ba;
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
assertEqual(BA_NO_MEMORY_ERR, ba.getError());
ba.begin(0, 1000);
assertEqual(BA_ELEMENT_SIZE_ERR, ba.getError());
ba.begin(33, 1000);
assertEqual(BA_ELEMENT_SIZE_ERR, ba.getError());
ba.begin(1, 1000);
assertEqual(BA_OK, ba.getError());
assertEqual(1, ba.bits());
assertEqual(1000, ba.capacity());
assertEqual(125, ba.memory());
fprintf(stderr, "\tSEGMENTS:\t %d\n", ba.segments());
}
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
unittest(test_set_get_toggle)
{
BitArray ba;
ba.begin(1, 1000);
assertEqual(BA_OK, ba.getError());
fprintf(stderr, "\t1000x set(i, 0) -> sum += get(i)\n");
int sum = 0;
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
ba.set(i, 0);
}
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
sum += ba.get(i);
}
assertEqual(0, sum);
fprintf(stderr, "\t1000x set(i, 1) -> sum += get(i)\n");
sum = 0;
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
ba.set(i, 1);
}
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
sum += ba.get(i);
}
assertEqual(1000, sum);
2022-10-29 09:54:56 -04:00
2021-01-29 06:31:58 -05:00
fprintf(stderr, "\t1000x toggle(i)\n");
sum = 0;
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
ba.toggle(i);
}
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
sum += ba.get(i);
}
assertEqual(0, sum);
}
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
unittest(test_clear)
{
BitArray ba;
ba.begin(1, 1000);
assertEqual(BA_OK, ba.getError());
fprintf(stderr, "\t1000x set(i, 1) -> clear() -> sum += get(i)\n");
int sum = 0;
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
ba.set(i, 1);
}
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
sum += ba.get(i);
}
assertEqual(1000, sum);
ba.clear();
sum = 0;
2021-10-19 10:11:27 -04:00
for (int i = 0; i < ba.capacity(); i++)
{
sum += ba.get(i);
}
assertEqual(0, sum);
}
unittest(test_setAll)
{
BitArray ba;
ba.begin(5, 200);
assertEqual(BA_OK, ba.getError());
fprintf(stderr, "\tsetAll(17) -> sum += get(i)\n");
ba.setAll(17);
uint32_t sum = 0;
for (int i = 0; i < ba.capacity(); i++)
{
sum += ba.get(i);
}
assertEqual(3400, sum);
ba.clear();
sum = 0;
for (int i = 0; i < ba.capacity(); i++)
2021-01-29 06:31:58 -05:00
{
sum += ba.get(i);
}
assertEqual(0, sum);
}
2021-10-19 10:11:27 -04:00
2021-01-29 06:31:58 -05:00
unittest_main()
2023-02-08 08:03:23 -05:00
// -- END OF FILE --