Skip to content

Commit 252c24c

Browse files
author
thk123
committed
Migrate old string utils unit tests
1 parent e87edbf commit 252c24c

File tree

7 files changed

+121
-68
lines changed

7 files changed

+121
-68
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ set_target_properties(
7575
mmcc
7676
pointer-analysis
7777
solvers
78-
string_utils
7978
test-bigint
8079
testing-utils
8180
unit

unit/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Unit test binaries
22
miniBDD
33
sharing_node
4-
string_utils
54
unit_tests

unit/CMakeLists.txt

-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ file(GLOB_RECURSE testing_utils "testing-utils/*.cpp" "testing-utils/*.h")
55
list(REMOVE_ITEM sources
66
# Used in executables
77
${CMAKE_CURRENT_SOURCE_DIR}/miniBDD.cpp
8-
${CMAKE_CURRENT_SOURCE_DIR}/string_utils.cpp
98

109
# Don't build
1110
${CMAKE_CURRENT_SOURCE_DIR}/sharing_map.cpp
@@ -63,14 +62,3 @@ target_include_directories(miniBDD
6362
target_link_libraries(miniBDD solvers ansi-c)
6463
add_test(NAME miniBDD COMMAND $<TARGET_FILE:miniBDD>)
6564
set_tests_properties(miniBDD PROPERTIES LABELS "CORE;CBMC")
66-
67-
add_executable(string_utils string_utils.cpp)
68-
target_include_directories(string_utils
69-
PUBLIC
70-
${CBMC_BINARY_DIR}
71-
${CBMC_SOURCE_DIR}
72-
${CMAKE_CURRENT_SOURCE_DIR}
73-
)
74-
target_link_libraries(string_utils solvers ansi-c)
75-
add_test(NAME string_utils COMMAND $<TARGET_FILE:string_utils>)
76-
set_tests_properties(string_utils PROPERTIES LABELS "CORE;CBMC")

unit/Makefile

+2-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ SRC += unit_tests.cpp \
4747
util/optional.cpp \
4848
util/parameter_indices.cpp \
4949
util/simplify_expr.cpp \
50+
util/string_utils/split_string.cpp \
51+
util/string_utils/strip_string.cpp \
5052
util/symbol_table.cpp \
5153
catch_example.cpp \
5254
java_bytecode/java_virtual_functions/virtual_functions.cpp \
@@ -85,7 +87,6 @@ OBJ += $(CPROVER_LIBS) testing-utils/testing-utils$(LIBEXT)
8587

8688
TESTS = unit_tests$(EXEEXT) \
8789
miniBDD$(EXEEXT) \
88-
string_utils$(EXEEXT) \
8990
# Empty last line
9091

9192
CLEANFILES = $(TESTS)
@@ -104,6 +105,3 @@ unit_tests$(EXEEXT): $(OBJ)
104105

105106
miniBDD$(EXEEXT): miniBDD$(OBJEXT) $(CPROVER_LIBS)
106107
$(LINKBIN)
107-
108-
string_utils$(EXEEXT): string_utils$(OBJEXT) $(CPROVER_LIBS)
109-
$(LINKBIN)

unit/string_utils.cpp

-50
This file was deleted.
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Author: Diffblue Ltd.
3+
*/
4+
5+
/// \file
6+
/// split_string Unit Tests
7+
8+
#include <testing-utils/catch.hpp>
9+
#include <util/string_utils.h>
10+
11+
SCENARIO("split_string", "[core][utils][string_utils][split_string]")
12+
{
13+
GIVEN("A non whitespace delimited string with two elements")
14+
{
15+
const char delimiter = ',';
16+
const std::string string = " a,, x , ,";
17+
18+
WHEN("Not stripping, not removing empty")
19+
{
20+
std::vector<std::string> result;
21+
split_string(string, delimiter, result, false, false);
22+
23+
THEN("All the elements should remain")
24+
{
25+
std::vector<std::string> expected_result = {" a", "", " x ", " ", ""};
26+
REQUIRE_THAT(
27+
result,
28+
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
29+
}
30+
}
31+
WHEN("Stripping, not removing empty")
32+
{
33+
std::vector<std::string> result;
34+
split_string(string, delimiter, result, true, false);
35+
36+
THEN("All whitespace borders should be removed but all elements remain")
37+
{
38+
std::vector<std::string> expected_result = {"a", "", "x", "", ""};
39+
REQUIRE_THAT(
40+
result,
41+
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
42+
}
43+
}
44+
WHEN("Not stripping, removing empty")
45+
{
46+
std::vector<std::string> result;
47+
split_string(string, delimiter, result, false, true);
48+
49+
THEN("All empty elements should be removed")
50+
{
51+
std::vector<std::string> expected_result = {" a", " x ", " "};
52+
REQUIRE_THAT(
53+
result,
54+
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
55+
}
56+
}
57+
WHEN("Stripping and removing empty")
58+
{
59+
std::vector<std::string> result;
60+
split_string(string, delimiter, result, true, true);
61+
62+
THEN("Should get the two parts in the vector")
63+
{
64+
std::vector<std::string> expected_result = {"a", "x"};
65+
REQUIRE_THAT(
66+
result,
67+
Catch::Matchers::Vector::EqualsMatcher<std::string>{expected_result});
68+
}
69+
}
70+
}
71+
}
72+
73+
SCENARIO("split_string into two", "[core][utils][string_utils][split_string]")
74+
{
75+
GIVEN("A string with one separator")
76+
{
77+
const char separator = ':';
78+
std::string string = "a:b";
79+
80+
WHEN("Splitting into two strings")
81+
{
82+
std::string s1;
83+
std::string s2;
84+
split_string(string, separator, s1, s2, false);
85+
THEN("The string should be split")
86+
{
87+
REQUIRE(s1 == "a");
88+
REQUIRE(s2 == "b");
89+
}
90+
}
91+
}
92+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Author: Diffblue Ltd.
3+
*/
4+
5+
/// \file
6+
/// strip_string Unit Tests
7+
8+
#include <testing-utils/catch.hpp>
9+
#include <util/string_utils.h>
10+
11+
SCENARIO("strip_string", "[core][utils][string_utils][strip_string]")
12+
{
13+
GIVEN("A string with some whitespace in it")
14+
{
15+
std::string string = " x y ";
16+
WHEN("Using strip_string")
17+
{
18+
std::string result = strip_string(string);
19+
THEN(
20+
"Whitespace at either end should be removed, but not internal "
21+
"whitespace")
22+
{
23+
REQUIRE(result == "x y");
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)