Skip to content

Commit 7639c5c

Browse files
committed
Adding test code for 'max'
1 parent e35d969 commit 7639c5c

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ set(TEST_TARGET ${CMAKE_PROJECT_NAME})
2424
##########################################################################
2525

2626
set(TEST_SRCS
27+
src/Common/test_max.cpp
2728
src/WCharacter/test_isHexadecimalDigit.cpp
2829
src/WCharacter/test_isSpace.cpp
2930
src/WCharacter/test_isUpperCase.cpp
3031
)
3132

3233
set(TEST_DUT_SRCS
34+
../api/Common.cpp
3335
)
3436

3537
##########################################################################

test/src/Common/test_max.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 ("Calling 'max(a,b)' with a < b", "[max-01]")
18+
{
19+
WHEN("a > 0 and b > 0") REQUIRE(max(1,5) == 5);
20+
WHEN("a < 0 and b > 0") REQUIRE(max(-1,5) == 5);
21+
WHEN("a < 0 and b < 0") REQUIRE(max(-5,-1) == -1);
22+
}
23+
24+
TEST_CASE ("Calling 'max(a,b)' with a > b", "[max-02]")
25+
{
26+
WHEN("a > 0 and b > 0") REQUIRE(max(5,1) == 5);
27+
WHEN("a > 0 and b < 0") REQUIRE(max(5,-1) == 5);
28+
WHEN("a < 0 and b < 0") REQUIRE(max(-1,-5) == -1);
29+
}
30+
31+
TEST_CASE ("Calling 'max(a,b)' with a == b", "[max-03]")
32+
{
33+
WHEN("a = b > 0") REQUIRE(max(5,5) == 5);
34+
WHEN("a = b < 0") REQUIRE(max(-5,-5) == -5);
35+
WHEN("a = b = 0") REQUIRE(max(0,0) == 0);
36+
}
37+
38+
TEST_CASE ("Calling 'max(a,b)' with type(a) != type(b)", "[max-04]")
39+
{
40+
WHEN("type(A) = uint8_t, type(b) = uint16_t")
41+
{
42+
uint8_t const a = 32;
43+
uint16_t const b = 10;
44+
REQUIRE(typeid(max(a,b)) == typeid(int));
45+
}
46+
WHEN("type(A) = uint16_t, type(b) = uint32_t")
47+
{
48+
uint16_t const a = 32;
49+
uint32_t const b = 10;
50+
REQUIRE(typeid(max(a,b)) == typeid(unsigned int));
51+
}
52+
WHEN("type(A) = uint32_t, type(b) = uint64_t")
53+
{
54+
uint32_t const a = 32;
55+
uint64_t const b = 10;
56+
REQUIRE(typeid(max(a,b)) == typeid(unsigned long));
57+
}
58+
WHEN("type(A) = int8_t, type(b) = int16_t")
59+
{
60+
int8_t const a = -32;
61+
int16_t const b = -10;
62+
REQUIRE(typeid(max(a,b)) == typeid(int));
63+
}
64+
WHEN("type(A) = int16_t, type(b) = int32_t")
65+
{
66+
int16_t const a = -32;
67+
int32_t const b = -10;
68+
REQUIRE(typeid(max(a,b)) == typeid(int));
69+
}
70+
WHEN("type(A) = int32_t, type(b) = int64_t")
71+
{
72+
int32_t const a = -32;
73+
int64_t const b = -10;
74+
REQUIRE(typeid(max(a,b)) == typeid(long));
75+
}
76+
}

0 commit comments

Comments
 (0)