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

179 lines
4.0 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-12-30
// PURPOSE: unit tests for the LineFormatter library
// https://github.com/RobTillaart/LineFormatter
// 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)
2021-11-06 12:49:23 -04:00
2021-01-29 06:31:58 -05:00
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "LineFormatter.h"
unittest_setup()
{
2021-12-20 14:09:13 -05:00
fprintf(stderr, "LINEFORMATTER_LIB_VERSION: %s\n", (char*) LINEFORMATTER_LIB_VERSION);
2021-01-29 06:31:58 -05:00
}
2023-01-31 07:39:06 -05:00
2021-01-29 06:31:58 -05:00
unittest_teardown()
{
}
unittest(test_constructor)
{
2021-11-06 12:49:23 -04:00
LineFormatter Line;
2021-01-29 06:31:58 -05:00
2021-11-06 12:49:23 -04:00
assertEqual(0, Line.getMaxLength());
Line.setMaxLength(80);
assertEqual(80, Line.getMaxLength());
assertEqual(0, Line.getAutoNewLine());
Line.setAutoNewLine(5);
assertEqual(5, Line.getAutoNewLine());
2021-01-29 06:31:58 -05:00
}
unittest(test_position)
{
2021-11-06 12:49:23 -04:00
LineFormatter Line;
2021-01-29 06:31:58 -05:00
2021-11-06 12:49:23 -04:00
assertEqual(0, (int)Line.getPos());
assertEqual(20, (int)Line.gotoPos(20));
assertEqual(20, (int)Line.gotoPos(15));
2021-01-29 06:31:58 -05:00
2021-11-06 12:49:23 -04:00
Line.repeat(10, '*');
assertEqual(30, (int)Line.getPos());
Line.repeat(10, "--");
assertEqual(50, (int)Line.getPos());
2021-01-29 06:31:58 -05:00
}
2021-11-06 12:49:23 -04:00
2021-01-29 06:31:58 -05:00
unittest(test_tab)
{
2021-11-06 12:49:23 -04:00
LineFormatter Line;
2021-01-29 06:31:58 -05:00
fprintf(stderr, "tab setting\n");
for (int i = 8; i <= 80; i += 8)
{
2021-11-06 12:49:23 -04:00
Line.addTab(i);
2021-01-29 06:31:58 -05:00
}
2021-11-06 12:49:23 -04:00
assertEqual(10, Line.getTabCount());
for (int i = 0; i < Line.getTabCount(); i++)
2021-01-29 06:31:58 -05:00
{
fprintf(stderr, "%d\t", 8 + i*8);
2023-01-31 07:39:06 -05:00
assertEqual(8 + i*8, (int)Line.getTabStop(i));
2021-01-29 06:31:58 -05:00
}
2021-11-06 12:49:23 -04:00
fprintf(stderr, "tab test - !! cur position is one before tab position\n");
for (int i = 0; i < Line.getTabCount(); i++)
2021-01-29 06:31:58 -05:00
{
2022-11-14 14:50:36 -05:00
fprintf(stderr, "%d\t", 8 + i*8); // tab positions
2021-11-06 12:49:23 -04:00
Line.write('\t');
2022-11-14 14:50:36 -05:00
assertEqual(8 + i*8, (int)Line.getPos() + 1 ); // current position is just before tab
2021-01-29 06:31:58 -05:00
}
}
2023-01-31 07:39:06 -05:00
unittest(test_removeTab)
{
LineFormatter Line;
fprintf(stderr, "tab setting\n");
for (int i = 8; i <= 80; i += 8)
{
Line.addTab(i);
}
assertEqual(10, Line.getTabCount());
for (int i = 0; i < Line.getTabCount(); i++)
{
fprintf(stderr, "%d\t", 8 + i*8);
assertEqual(8 + i*8, (int)Line.getTabStop(i));
}
fprintf(stderr, "remove tabs\n");
for (int i = 8; i <= 80; i += 16)
{
Line.removeTab(i);
}
assertEqual(5, Line.getTabCount());
for (int i = 0; i < Line.getTabCount(); i++)
{
fprintf(stderr, "%d\t", 16 + i*16);
assertEqual(16 + i*16, (int)Line.getTabStop(i));
}
}
unittest(test_existTab)
{
LineFormatter Line;
for (int i = 8; i <= 80; i += 8)
{
assertFalse(Line.existTab(i));
Line.addTab(i);
assertTrue(Line.existTab(i));
}
Line.clearTabs();
for (int i = 8; i <= 80; i += 8)
{
assertFalse(Line.existTab(i));
}
}
unittest(test_addTab)
{
LineFormatter Line;
// cannot add position 0.
assertFalse(Line.addTab(0));
// cannot add twice
for (int i = 8; i <= 80; i += 8)
{
assertTrue(Line.addTab(i));
assertFalse(Line.addTab(i));
}
assertEqual(10, Line.getTabCount());
}
2021-01-29 06:31:58 -05:00
unittest_main()
2023-01-31 07:39:06 -05:00
// -- END OF FILE --