Skip to content

Commit 5bd28b7

Browse files
committed
Adding test code for Common::map
1 parent 23ef4ce commit 5bd28b7

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(TEST_TARGET ${CMAKE_PROJECT_NAME})
2626

2727
set(TEST_SRCS
2828
src/Common/test_makeWord.cpp
29+
src/Common/test_map.cpp
2930
src/Common/test_max.cpp
3031
src/Common/test_min.cpp
3132
src/IPAddress/test_fromString.cpp

test/src/Common/test_map.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <Common.h>
12+
13+
/**************************************************************************************
14+
* TEST CODE
15+
**************************************************************************************/
16+
17+
TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' usage with reduction of output", "[map-01]")
18+
{
19+
REQUIRE(map(50, 0, 100, 0, 10) == 5);
20+
}
21+
22+
TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' usage with increase of output", "[map-02]")
23+
{
24+
REQUIRE(map(5, 0, 10, 0, 100) == 50);
25+
}
26+
27+
TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' where output is reduced to a value < 0", "[map-03]")
28+
{
29+
REQUIRE(map(1, 0, 100, 0, 10) == 0); /* Would be 0.1 if we'd be using floating point. */
30+
}
31+
32+
TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' default usage with negative values", "[map-04]")
33+
{
34+
WHEN ("Negative 'from' values")
35+
{
36+
REQUIRE(map(0, 0, -256, 0, 1024) == 0);
37+
REQUIRE(map(-256, 0, -256, 0, 1024) == 1024);
38+
REQUIRE(map(-128, 0, -256, 0, 1024) == 512);
39+
}
40+
WHEN ("Negative 'to' values")
41+
{
42+
REQUIRE(map(0, 0, 256, 0, -1024) == 0);
43+
REQUIRE(map(256, 0, 256, 0, -1024) == -1024);
44+
REQUIRE(map(128, 0, 256, 0, -1024) == -512);
45+
}
46+
}

0 commit comments

Comments
 (0)