Add unit tests for maths functions

This commit is contained in:
Josh Stewart 2022-01-05 13:13:41 +11:00
parent 464933b38f
commit 65bcf5b1a7
8 changed files with 228 additions and 6 deletions

View File

@ -1,7 +1,7 @@
#include <globals.h>
#include <speeduino.h>
#include <unity.h>
#include "tests_PW.h"
#include "test_PW.h"
#define PW_ALLOWED_ERROR 30

View File

@ -1,7 +1,7 @@
#include <globals.h>
#include <corrections.h>
#include <unity.h>
#include "tests_corrections.h"
#include "test_corrections.h"
void testCorrections()

View File

@ -0,0 +1,35 @@
#include <globals.h>
#include <init.h>
#include <Arduino.h>
#include <unity.h>
#include "test_corrections.h"
#include "test_PW.h"
#define UNITY_EXCLUDE_DETAILS
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
// NOTE!!! Wait for >2 secs
// if board doesn't support software reset via Serial.DTR/RTS
delay(2000);
UNITY_BEGIN(); // IMPORTANT LINE!
initialiseAll(); //Run the main initialise function
testCorrections();
testPW();
UNITY_END(); // stop unit testing
}
void loop()
{
// Blink to indicate end of test
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}

View File

@ -0,0 +1,171 @@
//#include <Arduino.h>
#include <string.h> // memcpy
#include <unity.h>
#include <stdio.h>
#include "tests_maths.h"
#include "maths.h"
void testMaths()
{
RUN_TEST(test_maths_percent_U8);
RUN_TEST(test_maths_percent_U16);
RUN_TEST(test_maths_percent_U32);
RUN_TEST(test_maths_halfpercent_U8);
RUN_TEST(test_maths_halfpercent_U16);
RUN_TEST(test_maths_halfpercent_U32);
RUN_TEST(test_maths_div100_U8);
RUN_TEST(test_maths_div100_U16);
RUN_TEST(test_maths_div100_U32);
RUN_TEST(test_maths_div100_S8);
RUN_TEST(test_maths_div100_S16);
RUN_TEST(test_maths_div100_S32);
RUN_TEST(test_maths_div10_U8);
RUN_TEST(test_maths_div10_U16);
RUN_TEST(test_maths_div10_U32);
}
void test_maths_percent_U8(void)
{
uint8_t percentOf = 200;
TEST_ASSERT_EQUAL(percentage(50, percentOf), 100);
TEST_ASSERT_EQUAL(percentage(75, percentOf), 150);
TEST_ASSERT_EQUAL(percentage(0, percentOf), 0);
TEST_ASSERT_EQUAL(percentage(100, percentOf), 200);
TEST_ASSERT_EQUAL(percentage(125, percentOf), 250);
}
void test_maths_percent_U16(void)
{
uint16_t percentOf = 20000;
TEST_ASSERT_EQUAL(percentage(50, percentOf), 10000);
TEST_ASSERT_EQUAL(percentage(75, percentOf), 15000);
TEST_ASSERT_EQUAL(percentage(0, percentOf), 0);
TEST_ASSERT_EQUAL(percentage(100, percentOf), 20000);
TEST_ASSERT_EQUAL(percentage(125, percentOf), 25000);
}
void test_maths_percent_U32(void)
{
uint32_t percentOf = 20000000UL;
TEST_ASSERT_EQUAL(percentage(50, percentOf), 10000000UL);
TEST_ASSERT_EQUAL(percentage(75, percentOf), 15000000UL);
TEST_ASSERT_EQUAL(percentage(0, percentOf), 0);
TEST_ASSERT_EQUAL(percentage(100, percentOf), 20000000UL);
TEST_ASSERT_EQUAL(percentage(125, percentOf), 25000000UL);
}
void test_maths_halfpercent_U8(void)
{
uint8_t percentOf = 200;
TEST_ASSERT_EQUAL(halfPercentage(50, percentOf), 50);
TEST_ASSERT_EQUAL(halfPercentage(75, percentOf), 75);
TEST_ASSERT_EQUAL(halfPercentage(0, percentOf), 0);
TEST_ASSERT_EQUAL(halfPercentage(100, percentOf), 100);
TEST_ASSERT_EQUAL(halfPercentage(125, percentOf), 125);
}
void test_maths_halfpercent_U16(void)
{
uint16_t percentOf = 20000;
TEST_ASSERT_EQUAL(halfPercentage(50, percentOf), 5000);
TEST_ASSERT_EQUAL(halfPercentage(75, percentOf), 7500);
TEST_ASSERT_EQUAL(halfPercentage(0, percentOf), 0);
TEST_ASSERT_EQUAL(halfPercentage(100, percentOf), 10000);
TEST_ASSERT_EQUAL(halfPercentage(125, percentOf), 12500);
}
void test_maths_halfpercent_U32(void)
{
uint32_t percentOf = 20000000UL;
TEST_ASSERT_EQUAL(halfPercentage(50, percentOf), 5000000UL);
TEST_ASSERT_EQUAL(halfPercentage(75, percentOf), 7500000UL);
TEST_ASSERT_EQUAL(halfPercentage(0, percentOf), 0);
TEST_ASSERT_EQUAL(halfPercentage(100, percentOf), 10000000UL);
TEST_ASSERT_EQUAL(halfPercentage(125, percentOf), 12500000UL);
}
void test_maths_div100_U8(void)
{
TEST_ASSERT_EQUAL(divu100(100), 1);
TEST_ASSERT_EQUAL(divu100(200), 2);
TEST_ASSERT_EQUAL(divu100(0), 0);
TEST_ASSERT_EQUAL(divu100(50), 0);
TEST_ASSERT_EQUAL(divu100(250), 2);
}
void test_maths_div100_U16(void)
{
TEST_ASSERT_EQUAL(divu100(10000), 100);
TEST_ASSERT_EQUAL(divu100(40000), 400);
}
void test_maths_div100_U32(void)
{
TEST_ASSERT_EQUAL(divu100(100000000UL), 1000000UL);
TEST_ASSERT_EQUAL(divu100(200000000UL), 2000000UL);
}
void test_maths_div100_S8(void)
{
//Check both the signed and unsigned results
TEST_ASSERT_EQUAL(divs100(100), 1);
TEST_ASSERT_EQUAL(divs100(0), 0);
TEST_ASSERT_EQUAL(divs100(50), 0);
TEST_ASSERT_EQUAL(divs100(-100), -1);
TEST_ASSERT_EQUAL(divs100(-50), 0);
TEST_ASSERT_EQUAL(divs100(-120), -1);
}
void test_maths_div100_S16(void)
{
//Check both the signed and unsigned results
TEST_ASSERT_EQUAL(divs100(10000), 100);
TEST_ASSERT_EQUAL(divs100(0), 0);
TEST_ASSERT_EQUAL(divs100(50), 0);
TEST_ASSERT_EQUAL(divs100(-10000), -100);
TEST_ASSERT_EQUAL(divs100(-50), 0);
TEST_ASSERT_EQUAL(divs100(-120), -1);
}
void test_maths_div100_S32(void)
{
//Check both the signed and unsigned results
TEST_ASSERT_EQUAL(divs100(100000000L), 1000000L);
TEST_ASSERT_EQUAL(divs100(0), 0);
TEST_ASSERT_EQUAL(divs100(50), 0);
TEST_ASSERT_EQUAL(divs100(-100000000L), -1000000L);
TEST_ASSERT_EQUAL(divs100(-50), 0);
TEST_ASSERT_EQUAL(divs100(-120), -1);
}
void test_maths_div10_U8(void)
{
TEST_ASSERT_EQUAL(divu10(100), 10);
TEST_ASSERT_EQUAL(divu10(200), 20);
TEST_ASSERT_EQUAL(divu10(0), 0);
TEST_ASSERT_EQUAL(divu10(50), 5);
TEST_ASSERT_EQUAL(divu10(250), 25);
}
void test_maths_div10_U16(void)
{
TEST_ASSERT_EQUAL(divu10(10000), 1000);
TEST_ASSERT_EQUAL(divu10(40000), 4000);
TEST_ASSERT_EQUAL(divu10(0), 0);
TEST_ASSERT_EQUAL(divu10(50), 5);
TEST_ASSERT_EQUAL(divu10(250), 25);
}
void test_maths_div10_U32(void)
{
TEST_ASSERT_EQUAL(divu10(100000000UL), 10000000UL);
TEST_ASSERT_EQUAL(divu10(400000000UL), 40000000UL);
TEST_ASSERT_EQUAL(divu10(0), 0);
TEST_ASSERT_EQUAL(divu10(50), 5);
TEST_ASSERT_EQUAL(divu10(250), 25);
}

View File

@ -0,0 +1,18 @@
extern void testMaths();
void test_maths_percent_U8(void);
void test_maths_percent_U16(void);
void test_maths_percent_U32(void);
void test_maths_halfpercent_U8(void);
void test_maths_halfpercent_U16(void);
void test_maths_halfpercent_U32(void);
void test_maths_div100_U8(void);
void test_maths_div100_U16(void);
void test_maths_div100_U32(void);
void test_maths_div100_S8(void);
void test_maths_div100_S16(void);
void test_maths_div100_S32(void);
void test_maths_div10_U8(void);
void test_maths_div10_U16(void);
void test_maths_div10_U32(void);

View File

@ -1,11 +1,10 @@
#include <Arduino.h>
#include <unity.h>
#include "tests_corrections.h"
#include "tests_init.h"
#include "tests_tables.h"
#include "tests_PW.h"
#include "test_table2d.h"
#include "tests_maths.h"
#define UNITY_EXCLUDE_DETAILS
@ -20,10 +19,9 @@ void setup()
UNITY_BEGIN(); // IMPORTANT LINE!
testInitialisation();
testCorrections();
testPW();
testTables();
testTable2d();
testMaths();
UNITY_END(); // stop unit testing
}