Skip to content
This repository was archived by the owner on Aug 16, 2026. It is now read-only.

Commit dfb5915

Browse files
committed
Redesign unit-tests for ctest
1 parent 346f6b8 commit dfb5915

61 files changed

Lines changed: 1037 additions & 1515 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ install(
3131
RUNTIME DESTINATION
3232
/usr/bin
3333
)
34+
35+
# enable testing functionality
36+
enable_testing()
37+
add_subdirectory(src/tests)

src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ distribution:
2121
- source-file: src/wsjcpp_core/wsjcpp_core.cpp
2222
target-file: wsjcpp_core.cpp
2323
type: "source-code"
24-
sha1: "791a6b5dfd94d411494a989ea366b3bbe5f776aa"
24+
sha1: "25926b911a674cf969b94f29e4480f242b31094a"
2525
- source-file: src/wsjcpp_core/wsjcpp_core.h
2626
target-file: wsjcpp_core.h
2727
type: "source-code" # todo must be header-file
28-
sha1: "d8a139d0a171059d7d12763b043bc6d33b3f08dc"
28+
sha1: "01aa9c5363db7523efb2dad5b32f90bdba1c16e1"
2929
- source-file: "src/wsjcpp_core/wsjcpp_unit_tests.cpp"
3030
target-file: "wsjcpp_unit_tests.cpp"
3131
type: "unit-tests"

src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,4 +1357,49 @@ std::string padding_left(const std::string &source, char pad_char, size_t length
13571357
return ret + source;
13581358
}
13591359

1360+
1361+
bool is_valid_ip4(const std::string &value, std::string &error) {
1362+
int n = 0;
1363+
std::string s[4] = {"", "", "", ""};
1364+
for (int i = 0; i < value.length(); i++) {
1365+
char c = value[i];
1366+
if (n > 3) {
1367+
error = "Groups number must be less than 5 (like '0.0.0.0')";
1368+
return false;
1369+
}
1370+
if (c >= '0' && c <= '9') {
1371+
s[n] += c;
1372+
} else if (c == '.') {
1373+
n++;
1374+
} else {
1375+
error = "Unexpected character '";
1376+
error += c;
1377+
error += "'";
1378+
return false;
1379+
}
1380+
}
1381+
for (int i = 0; i < 4; i++) {
1382+
if (s[i].length() > 3) {
1383+
error = "Value '" + s[i] + "' could not contains more than 3 digits in a row";
1384+
return false;
1385+
}
1386+
int p = std::stoi(s[i]);
1387+
if (p > 255 || p < 0) {
1388+
error = "Value '" + std::to_string(p) + "' must be 0..255";
1389+
return false;
1390+
}
1391+
}
1392+
return true;
1393+
}
1394+
1395+
bool is_valid_ip6(const std::string &value, std::string &error) {
1396+
// TODO redesign without arpa
1397+
unsigned char buf[sizeof(struct in6_addr)];
1398+
bool isValid = inet_pton(AF_INET6, value.c_str(), buf);
1399+
if (!isValid) {
1400+
error = "Format of ipv6 invalid";
1401+
}
1402+
return isValid;
1403+
}
1404+
13601405
} // namespace wsjcpp

src.wsjcpp/wsjcpp_core/wsjcpp_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ bool is_windows();
269269
std::string padding_right(const std::string &source, char pad_char, size_t length);
270270
std::string padding_left(const std::string &source, char pad_char, size_t length);
271271

272+
bool is_valid_ip4(const std::string &value, std::string &error);
273+
bool is_valid_ip6(const std::string &value, std::string &error);
274+
272275
} // namespace wsjcpp
273276

274277
#endif // WSJCPP_CORE_H

src/tests/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
file(GLOB ALL_TESTS
2+
"test_*.cpp"
3+
)
4+
5+
list (APPEND TEST_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../")
6+
list (APPEND TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../../src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
7+
list (APPEND TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../wsjcpp_validators/wsjcpp_validators.cpp")
8+
9+
10+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY tests)
11+
12+
foreach(_TEST ${ALL_TESTS})
13+
get_filename_component(TEST_NAME ${_TEST} NAME_WE)
14+
add_executable(${TEST_NAME}
15+
${_TEST}
16+
${TEST_SOURCES}
17+
)
18+
target_link_libraries(${TEST_NAME} -lpthread ${WSJCPP_LIBRARIES})
19+
20+
add_test(
21+
NAME ${TEST_NAME}
22+
COMMAND $<TARGET_FILE:${TEST_NAME}>
23+
)
24+
message(${CMAKE_CURRENT_BINARY_DIR}/tests)
25+
set_target_properties (${TEST_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY tests)
26+
endforeach()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
#include "wsjcpp_validators.h"
3+
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <vector>
7+
8+
int main() {
9+
int found_errors = 0;
10+
11+
struct local {
12+
local(const std::string &value, bool expected)
13+
: value(value), expected(expected) {
14+
15+
};
16+
std::string value;
17+
bool expected;
18+
};
19+
std::vector<local> tests;
20+
tests.push_back(local("O2Rrc2Y7YXNsa2RmMQ==", true));
21+
tests.push_back(local("O2Rrc2Y7YXNsa2Rm", true));
22+
tests.push_back(local("O2Rrc2Y7YXNsa2Rm==", false));
23+
tests.push_back(local("1898-01-01", false));
24+
tests.push_back(local("+/11", true));
25+
tests.push_back(local("%$", false));
26+
27+
WsjcppValidatorBase64 *pValidator = new WsjcppValidatorBase64();
28+
29+
for (int i = 0; i < tests.size(); i++) {
30+
local t = tests[i];
31+
std::string error;
32+
bool got = pValidator->isValid(t.value, error);
33+
if (got != t.expected) {
34+
found_errors++;
35+
std::cerr << "Expected " << (t.expected ? "true" : "false") << ", but got " << (got ? "true" : "false") << " for "
36+
<< t.value << "error: " << error << std::endl;
37+
}
38+
}
39+
40+
if (found_errors > 0) {
41+
std::cerr << "FAILED" << std::endl;
42+
return 1;
43+
}
44+
45+
std::cout << "OK" << std::endl;
46+
return 0;
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#include "wsjcpp_validators.h"
3+
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <vector>
7+
8+
int main() {
9+
int found_errors = 0;
10+
11+
struct local {
12+
local(const std::string &value, bool expected)
13+
: value(value), expected(expected) {
14+
15+
};
16+
std::string value;
17+
bool expected;
18+
};
19+
std::vector<local> tests;
20+
tests.push_back(local("some", false));
21+
tests.push_back(local("2020-01-01", true));
22+
tests.push_back(local("2020-12-00", false));
23+
tests.push_back(local("0000-00-00", false));
24+
tests.push_back(local("0000-01-01", true));
25+
tests.push_back(local("2020-12-31", true));
26+
tests.push_back(local("2020-02-29", true));
27+
tests.push_back(local("2021-02-29", false));
28+
tests.push_back(local("1898-01-01", true));
29+
tests.push_back(local("1898-15-01", false));
30+
tests.push_back(local("1898-01-32", false));
31+
32+
WsjcppValidatorDate *pValidator = new WsjcppValidatorDate();
33+
34+
for (int i = 0; i < tests.size(); i++) {
35+
local t = tests[i];
36+
std::string error;
37+
bool got = pValidator->isValid(t.value, error);
38+
if (got != t.expected) {
39+
found_errors++;
40+
std::cerr << "Expected " << (t.expected ? "true" : "false") << ", but got " << (got ? "true" : "false") << " for "
41+
<< t.value << "error: " << error << std::endl;
42+
}
43+
}
44+
45+
if (found_errors > 0) {
46+
std::cerr << "FAILED" << std::endl;
47+
return 1;
48+
}
49+
50+
std::cout << "OK" << std::endl;
51+
return 0;
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
#include "wsjcpp_validators.h"
3+
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <vector>
7+
8+
int main() {
9+
int found_errors = 0;
10+
11+
struct local {
12+
local(const std::string &value, bool expected)
13+
: value(value), expected(expected) {
14+
15+
};
16+
std::string value;
17+
bool expected;
18+
};
19+
std::vector<local> tests;
20+
tests.push_back(local("some", false));
21+
tests.push_back(local("2020-01-01T00:00:00", true));
22+
tests.push_back(local("2020-12-01T00:00:00", true));
23+
tests.push_back(local("1898-01-01T00:00:00", true));
24+
tests.push_back(local("1898-15-01T00:00:00", false));
25+
tests.push_back(local("1898-01-32T00:00:00", false));
26+
tests.push_back(local("1898-01-01X00:00:00", false));
27+
tests.push_back(local("18a8-01-01T00:00:00", false));
28+
tests.push_back(local("1838-01-01T25:00:00", false));
29+
30+
WsjcppValidatorDateTime *pValidator = new WsjcppValidatorDateTime();
31+
32+
for (int i = 0; i < tests.size(); i++) {
33+
local t = tests[i];
34+
std::string error;
35+
bool got = pValidator->isValid(t.value, error);
36+
if (got != t.expected) {
37+
found_errors++;
38+
std::cerr << "Expected " << (t.expected ? "true" : "false") << ", but got " << (got ? "true" : "false") << " for "
39+
<< t.value << "error: " << error << std::endl;
40+
}
41+
}
42+
43+
if (found_errors > 0) {
44+
std::cerr << "FAILED" << std::endl;
45+
return 1;
46+
}
47+
48+
std::cout << "OK" << std::endl;
49+
return 0;
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
#include "wsjcpp_validators.h"
3+
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <vector>
7+
8+
int main() {
9+
int found_errors = 0;
10+
11+
struct local {
12+
local(const std::string &value, bool expected)
13+
: value(value), expected(expected) {
14+
15+
};
16+
std::string value;
17+
bool expected;
18+
};
19+
std::vector<local> tests;
20+
tests.push_back(local("some", false));
21+
tests.push_back(local("some@some", false));
22+
tests.push_back(local("some@some.rr", true));
23+
tests.push_back(local("01@some.com", true));
24+
tests.push_back(local("s_s-some@test.com", true));
25+
tests.push_back(local("s_s-some@test.c", false));
26+
27+
WsjcppValidatorEmail *pValidator = new WsjcppValidatorEmail();
28+
29+
for (int i = 0; i < tests.size(); i++) {
30+
local t = tests[i];
31+
std::string error;
32+
bool got = pValidator->isValid(t.value, error);
33+
if (got != t.expected) {
34+
found_errors++;
35+
std::cerr << "Expected " << (t.expected ? "true" : "false") << ", but got " << (got ? "true" : "false") << " for "
36+
<< t.value << "error: " << error << std::endl;
37+
}
38+
}
39+
40+
if (found_errors > 0) {
41+
std::cerr << "FAILED" << std::endl;
42+
return 1;
43+
}
44+
45+
std::cout << "OK" << std::endl;
46+
return 0;
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
#include "wsjcpp_validators.h"
3+
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <vector>
7+
8+
int main() {
9+
int found_errors = 0;
10+
11+
struct local {
12+
local(const std::string &value, bool expected)
13+
: value(value), expected(expected) {
14+
15+
};
16+
std::string value;
17+
bool expected;
18+
};
19+
std::vector<local> tests;
20+
tests.push_back(local("some", false));
21+
tests.push_back(local("2020", true));
22+
tests.push_back(local("abcdef0123456789ABCDEF", true));
23+
tests.push_back(local("0123J", false));
24+
25+
WsjcppValidatorHex *pValidator = new WsjcppValidatorHex();
26+
27+
for (int i = 0; i < tests.size(); i++) {
28+
local t = tests[i];
29+
std::string error;
30+
bool got = pValidator->isValid(t.value, error);
31+
if (got != t.expected) {
32+
found_errors++;
33+
std::cerr << "Expected " << (t.expected ? "true" : "false") << ", but got " << (got ? "true" : "false") << " for "
34+
<< t.value << "error: " << error << std::endl;
35+
}
36+
}
37+
38+
if (found_errors > 0) {
39+
std::cerr << "FAILED" << std::endl;
40+
return 1;
41+
}
42+
43+
std::cout << "OK" << std::endl;
44+
return 0;
45+
}

0 commit comments

Comments
 (0)