Skip to content

Commit 560a67c

Browse files
committed
Add test code for String::compareTo(...)
1 parent 2c77098 commit 560a67c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set(TEST_SRCS
4343
src/Ringbuffer/test_read_char.cpp
4444
src/Ringbuffer/test_store_char.cpp
4545
src/String/test_concat.cpp
46+
src/String/test_compareTo.cpp
4647
src/String/test_indexOf.cpp
4748
src/String/test_lastIndexOf.cpp
4849
src/String/test_length.cpp

test/src/String/test_compareTo.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <String.h>
12+
13+
/**************************************************************************************
14+
* TEST CODE
15+
**************************************************************************************/
16+
17+
TEST_CASE ("Testing String::compareTo(const String &)", "[String-compareTo-01]")
18+
{
19+
WHEN ("Strings are equal")
20+
{
21+
arduino::String str1("Hello"), str2("Hello");
22+
REQUIRE(str1.compareTo(str2) == 0);
23+
}
24+
25+
WHEN ("str2 is empty")
26+
{
27+
arduino::String str1("Hello"), str2;
28+
REQUIRE(str1.compareTo(str2) == strcmp(str1.c_str(), str2.c_str()));
29+
}
30+
31+
WHEN ("str1 is empty")
32+
{
33+
arduino::String str1, str2("Hello");
34+
REQUIRE(str1.compareTo(str2) == strcmp(str1.c_str(), str2.c_str()));
35+
}
36+
}
37+
38+
TEST_CASE ("Testing String::compareTo(const char *)", "[String-compareTo-02]")
39+
{
40+
WHEN ("Strings are equal")
41+
{
42+
arduino::String str("Hello");
43+
REQUIRE(str.compareTo("Hello") == 0);
44+
}
45+
46+
WHEN ("Passed string is empty")
47+
{
48+
arduino::String str1("Hello"), str2("");
49+
REQUIRE(str1.compareTo("") == strcmp(str1.c_str(), str2.c_str()));
50+
}
51+
52+
WHEN ("Passed string is compared with empty string")
53+
{
54+
arduino::String str1, str2("Hello");
55+
REQUIRE(str1.compareTo("Hello") == strcmp(str1.c_str(), str2.c_str()));
56+
}
57+
}

0 commit comments

Comments
 (0)