Skip to content

Commit 0b57a80

Browse files
committed
Fixed #13 Added unit-tests for memory leaks
1 parent 7f9d90d commit 0b57a80

File tree

6 files changed

+184
-19
lines changed

6 files changed

+184
-19
lines changed

src.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Automaticly generated by wsjcpp@v0.1.7
1+
# Automaticly generated by wsjcpp@v0.2.0
22
cmake_minimum_required(VERSION 3.0)
33

44
add_definitions(-DWSJCPP_APP_VERSION="v0.1.3")
@@ -22,7 +22,4 @@ list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/")
2222
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
2323
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h")
2424

25-
# required-libraries
26-
list (APPEND WSJCPP_LIBRARIES "-lpthread")
27-
2825

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Automaticly generated by wsjcpp@v0.1.7
1+
# Automaticly generated by wsjcpp@v0.2.0
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
@@ -44,9 +44,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_all.
4444
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp")
4545
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp")
4646
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp")
47-
48-
# required-libraries
49-
list (APPEND WSJCPP_LIBRARIES "-lpthread")
47+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp")
5048

5149
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5250

@@ -57,11 +55,3 @@ add_executable ("unit-tests" ${WSJCPP_SOURCES})
5755

5856
target_link_libraries("unit-tests" -lpthread ${WSJCPP_LIBRARIES} )
5957

60-
install(
61-
TARGETS
62-
"unit-tests"
63-
RUNTIME DESTINATION
64-
/usr/bin
65-
)
66-
67-
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# example yaml for test memeoty leak
2+
doe: "a deer, a female deer"
3+
ray: "a drop of golden sun"
4+
pi: 3.14159
5+
xmas: true
6+
french-hens: 3
7+
calling-birds:
8+
- huey
9+
- dewey
10+
- louie
11+
- fred
12+
xmas-fifth-day:
13+
calling-birds: four
14+
french-hens: 3
15+
golden-rings: 5
16+
partridges:
17+
count: 1
18+
location: "a pear tree"
19+
turtle-doves: two
20+
21+
# example yaml again
22+
again0:
23+
doe: "a deer, a female deer"
24+
ray: "a drop of golden sun"
25+
pi: 3.14159
26+
xmas: true
27+
french-hens: 3
28+
calling-birds:
29+
- huey
30+
- dewey
31+
- louie
32+
- fred
33+
xmas-fifth-day:
34+
calling-birds: four
35+
french-hens: 3
36+
golden-rings: 5
37+
partridges:
38+
count: 1
39+
location: "a pear tree"
40+
turtle-doves: two
41+
42+
# example yaml again2
43+
again2:
44+
doe: "a deer, a female deer"
45+
ray: "a drop of golden sun"
46+
pi: 3.14159
47+
xmas: true
48+
french-hens: 3
49+
calling-birds:
50+
- huey
51+
- dewey
52+
- louie
53+
- fred
54+
xmas-fifth-day:
55+
calling-birds: four
56+
french-hens: 3
57+
golden-rings: 5
58+
partridges:
59+
count: 1
60+
location: "a pear tree"
61+
turtle-doves: two
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <unistd.h>
5+
#include <ios>
6+
#include <iostream>
7+
#include <fstream>
8+
#include <string>
9+
#include <wsjcpp_yaml.h>
10+
11+
//////////////////////////////////////////////////////////////////////////////
12+
//
13+
// process_mem_usage(double &, double &) - takes two doubles by reference,
14+
// attempts to read the system-dependent data for a process' virtual memory
15+
// size and resident set size, and return the results in KB.
16+
//
17+
// On failure, returns 0.0, 0.0
18+
19+
void process_mem_usage(double& vm_usage, double& resident_set)
20+
{
21+
using std::ios_base;
22+
using std::ifstream;
23+
using std::string;
24+
25+
vm_usage = 0.0;
26+
resident_set = 0.0;
27+
28+
// 'file' stat seems to give the most reliable results
29+
//
30+
ifstream stat_stream("/proc/self/stat",ios_base::in);
31+
32+
// dummy vars for leading entries in stat that we don't care about
33+
//
34+
string pid, comm, state, ppid, pgrp, session, tty_nr;
35+
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
36+
string utime, stime, cutime, cstime, priority, nice;
37+
string O, itrealvalue, starttime;
38+
39+
// the two fields we want
40+
//
41+
unsigned long vsize;
42+
long rss;
43+
44+
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
45+
>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
46+
>> utime >> stime >> cutime >> cstime >> priority >> nice
47+
>> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
48+
49+
stat_stream.close();
50+
51+
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
52+
vm_usage = vsize / 1024.0;
53+
resident_set = rss * page_size_kb;
54+
}
55+
56+
// ---------------------------------------------------------------------
57+
// UnitTestMemoryLeaks
58+
59+
class UnitTestMemoryLeaks : public WsjcppUnitTestBase {
60+
public:
61+
UnitTestMemoryLeaks();
62+
virtual bool doBeforeTest() override;
63+
virtual void executeTest() override;
64+
virtual bool doAfterTest() override;
65+
};
66+
67+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestMemoryLeaks)
68+
69+
UnitTestMemoryLeaks::UnitTestMemoryLeaks()
70+
: WsjcppUnitTestBase("UnitTestMemoryLeaks") {
71+
}
72+
73+
// ---------------------------------------------------------------------
74+
75+
bool UnitTestMemoryLeaks::doBeforeTest() {
76+
// do something before test
77+
return true;
78+
}
79+
80+
// ---------------------------------------------------------------------
81+
82+
void UnitTestMemoryLeaks::executeTest() {
83+
double nBeforeVm, nBeforeRss;
84+
process_mem_usage(nBeforeVm, nBeforeRss);
85+
// std::cout << "nBeforeVm: " << nBeforeVm << std::endl;
86+
// std::cout << "nBeforeRss: " << nBeforeRss << std::endl;
87+
compare("memory vm not null", (int)nBeforeVm > 0, true);
88+
compare("memory vm not null", (int)nBeforeRss > 0, true);
89+
90+
for (int i = 0; i < 1000; i++) {
91+
WsjcppYaml *pYaml = new WsjcppYaml();
92+
std::string sError;
93+
if (!compare("Error parsing", pYaml->loadFromFile("./data-tests/for-memory-leak/some.yml", sError), true)) {
94+
WsjcppLog::err(TAG, sError);
95+
return;
96+
}
97+
delete pYaml;
98+
}
99+
100+
double nAfterVm, nAfterRss;
101+
process_mem_usage(nAfterVm, nAfterRss);
102+
// std::cout << "nAfterVm: " << nAfterVm << std::endl;
103+
// std::cout << "nAfterRss: " << nAfterRss << std::endl;
104+
compare("memory vm", (int)nAfterVm, (int)nBeforeVm);
105+
compare("memory rss", (int)nAfterRss, (int)nBeforeRss);
106+
}
107+
108+
// ---------------------------------------------------------------------
109+
110+
bool UnitTestMemoryLeaks::doAfterTest() {
111+
// do somethig after test
112+
return true;
113+
}
114+
115+

unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ bool UnitTestRemoveElementForMap::doBeforeTest() {
3030
// ---------------------------------------------------------------------
3131

3232
void UnitTestRemoveElementForMap::executeTest() {
33-
3433
std::string sTestYaml =
3534
"# Some comment 1\n"
3635
"map1: \n"

wsjcpp.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ keywords:
1717
authors:
1818
- name: Evgenii Sopov
1919
email: mrseakg@gmail.com
20-
required-libraries:
21-
- pthread
20+
2221
dependencies:
2322
- name: "wsjcpp-core"
2423
version: "v0.2.1"
2524
url: "https://github.com/wsjcpp/wsjcpp-core:master"
2625
origin: "https://github.com/"
2726
installation-dir: "./src.wsjcpp/wsjcpp_core"
27+
2828
distribution:
2929
- source-file: src/wsjcpp_yaml.cpp
3030
target-file: wsjcpp_yaml.cpp
3131
type: "source-code"
3232
- source-file: src/wsjcpp_yaml.h
3333
target-file: wsjcpp_yaml.h
3434
type: "source-code"
35+
3536
unit-tests:
3637
cases:
3738
- name: LineParser
@@ -54,3 +55,5 @@ unit-tests:
5455
description: ""
5556
- name: "RemoveElementInArray"
5657
description: ""
58+
- name: "MemoryLeaks"
59+
description: ""

0 commit comments

Comments
 (0)