Skip to content

Commit b582173

Browse files
committed
Start migration test to cmake tests
1 parent 7a35436 commit b582173

File tree

11 files changed

+215
-70
lines changed

11 files changed

+215
-70
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [v0.1.8] - 2022-01-12 (2022 Jan 12)
99

1010
- Fixed #35 Add valFloat / valDouble
11+
- Start migrating tests to cmake tests
12+
- Updated copyright
13+
- Fix for c++ builder 12.2
14+
- Make compatible code with compiler bcc32 C++ Builder
15+
1116

1217
## [v0.1.7] - 2022-01-12 (2022 Jan 12)
1318

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2019-2025 wsjcpp
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
#
23+
# Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
24+
125
cmake_minimum_required(VERSION 3.0)
226

327
project(wsjcpp-yaml)
@@ -34,3 +58,7 @@ install(
3458
RUNTIME DESTINATION
3559
/usr/bin
3660
)
61+
62+
# enable testing functionality
63+
enable_testing()
64+
add_subdirectory(src/tests)

build_simple.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ check_ret $? "configure"
2424

2525
cmake --build ./tmp/linux_release --config Release
2626
check_ret $? "build"
27+
28+
cd ./tmp/linux_release && ctest --output-on-failure
29+
check_ret $? "tests"

src/tests/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2019-2025 wsjcpp
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
#
23+
# Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
24+
25+
file(GLOB ALL_TESTS
26+
"test_*.cpp"
27+
)
28+
29+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY tests)
30+
31+
list (APPEND TEST_YAML_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../wsjcpp_yaml.cpp")
32+
33+
foreach(_TEST ${ALL_TESTS})
34+
get_filename_component(TESTNAME ${_TEST} NAME_WE)
35+
add_executable(${TESTNAME}
36+
${_TEST}
37+
${TEST_YAML_SOURCES}
38+
)
39+
target_link_libraries(${TESTNAME} -lpthread ${WSJCPP_LIBRARIES})
40+
41+
add_test(
42+
NAME ${TESTNAME}
43+
COMMAND $<TARGET_FILE:${TESTNAME}>
44+
)
45+
message(${CMAKE_CURRENT_BINARY_DIR}/tests)
46+
set_target_properties (${TESTNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY tests)
47+
endforeach()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
float: 1.0001
2+
double: 1.0002

src/tests/test_double_value.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <cmath>
4+
#include <wsjcpp_yaml.h>
5+
6+
#ifdef WINDOWS
7+
#include <direct.h>
8+
#define GetCurrentDir _getcwd
9+
#else
10+
#include <unistd.h>
11+
#define GetCurrentDir getcwd
12+
#endif
13+
14+
int main() {
15+
char buff[FILENAME_MAX];
16+
GetCurrentDir(buff, FILENAME_MAX );
17+
std::cout << "Current path: " << buff << std::endl;
18+
19+
WsjcppYaml yaml;
20+
std::string sFilepath = "../../../../src/tests/data-tests/float-double/example.yml";
21+
std::string sError;
22+
if (!yaml.loadFromFile(sFilepath, sError)) {
23+
std::cerr << "Error parsing: " << sError << std::endl;
24+
return -1;
25+
}
26+
27+
int ret = 0;
28+
if (!yaml.getRoot()->hasElement("double")) {
29+
std::cerr << "Parameter 'float' not found" << std::endl;
30+
ret = -1;
31+
}
32+
33+
if (!yaml.getRoot()->hasElement("double")) {
34+
std::cerr << "Parameter 'double' has not value" << std::endl;
35+
ret = -1;
36+
}
37+
38+
std::string expected_val_str = "1.0002";
39+
if (yaml.getRoot()->getElement("double")->getValue() != expected_val_str) {
40+
std::cerr << "Parameter 'double' has value string '" << yaml.getRoot()->getElement("double")->getValue() << "', but expected '" << expected_val_str << "'" << std::endl;
41+
ret = -1;
42+
}
43+
44+
double val_double = yaml["double"].valDouble();
45+
double expected_double = 1.0002f;
46+
// std::numeric_limits<double>::epsilon() => 2.22045e-16
47+
// std::numeric_limits<float>::epsilon() => 1.19209e-07
48+
// abs(val_double - expected_double) => 3.31879e-08
49+
if (abs(val_double - expected_double) > std::numeric_limits<float>::epsilon()) {
50+
std::cerr << "Parameter 'double' has value number '" << val_double << "', but expected '" << expected_double << "'" << std::endl;
51+
ret = -1;
52+
}
53+
54+
yaml["double"].val(1.0003f);
55+
56+
expected_val_str = "1.0003";
57+
if (yaml.getRoot()->getElement("double")->getValue() != expected_val_str) {
58+
std::cerr << "Parameter 'double' has value string '" << yaml.getRoot()->getElement("double")->getValue() << "', but expected '" << expected_val_str << "'" << std::endl;
59+
ret = -1;
60+
}
61+
62+
expected_double = 1.0003f;
63+
val_double = yaml["double"].valDouble();
64+
if (abs(val_double - expected_double) > std::numeric_limits<float>::epsilon()) {
65+
std::cerr << "Parameter 'double' has value number '" << val_double << "', but expected '" << expected_double << "'" << std::endl;
66+
ret = -1;
67+
}
68+
float val_double_like_float = yaml["double"].valFloat();
69+
float expected_float = 1.0003f;
70+
if (abs(val_double_like_float - expected_float) > std::numeric_limits<float>::epsilon()) {
71+
std::cerr << "Parameter 'double' has value number (like float) '" << val_double_like_float << "', but expected '" << expected_float << "'" << std::endl;
72+
ret = -1;
73+
}
74+
75+
// compareD("has double value is 1.0003 (double 2)", yaml["double"].valDouble(), 1.0003f);
76+
77+
return ret;
78+
}

src/tests/test_float_value.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <cmath>
4+
#include <wsjcpp_yaml.h>
5+
6+
#ifdef WINDOWS
7+
#include <direct.h>
8+
#define GetCurrentDir _getcwd
9+
#else
10+
#include <unistd.h>
11+
#define GetCurrentDir getcwd
12+
#endif
13+
14+
int main() {
15+
char buff[FILENAME_MAX];
16+
GetCurrentDir(buff, FILENAME_MAX);
17+
std::cout << "Current path: " << buff << std::endl;
18+
19+
WsjcppYaml yaml;
20+
std::string sFilepath = "../../../../src/tests/data-tests/float-double/example.yml";
21+
std::string sError;
22+
if (!yaml.loadFromFile(sFilepath, sError)) {
23+
std::cerr << "Error parsing: " << sError << std::endl;
24+
return -1;
25+
}
26+
27+
int ret = 0;
28+
if (!yaml.getRoot()->hasElement("float")) {
29+
std::cerr << "Parameter 'float' not found" << std::endl;
30+
ret = -1;
31+
}
32+
33+
if (!yaml.getRoot()->hasElement("float")) {
34+
std::cerr << "Parameter 'float' has not value" << std::endl;
35+
ret = -1;
36+
}
37+
38+
std::string expected_val_str = "1.0001";
39+
if (yaml.getRoot()->getElement("float")->getValue() != expected_val_str) {
40+
std::cerr << "Parameter 'float' has value '" << yaml.getRoot()->getElement("float")->getValue() << "', but expected '" << expected_val_str << "'" << std::endl;
41+
ret = -1;
42+
}
43+
44+
float val_float = yaml["float"].valFloat();
45+
float expected_float = 1.0001f;
46+
if (abs(val_float - expected_float) > std::numeric_limits<float>::epsilon()) {
47+
std::cerr << "Parameter 'float' has value '" << val_float << "', but expected '" << expected_float << "'" << std::endl;
48+
ret = -1;
49+
}
50+
51+
return ret;
52+
}

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_t
5252
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_cleanup.cpp")
5353
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_append_elements.cpp")
5454
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_keep_format.cpp")
55-
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_float_double.cpp")
5655

5756
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5857

unit-tests.wsjcpp/data-tests/read-file/float-double/example.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

unit-tests.wsjcpp/src/unit_test_float_double.cpp

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)