Skip to content

Commit f62f9bf

Browse files
committed
Added some new functions and new tests
1 parent 5b3a3d2 commit f62f9bf

File tree

7 files changed

+221
-3
lines changed

7 files changed

+221
-3
lines changed

src/wsjcpp_core.cpp

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <iostream>
55
#include <sstream>
66
#include <fstream>
7-
#include <sys/stat.h>
87
#include <sys/time.h>
98
#include <time.h>
109
#include <ctime>
@@ -14,6 +13,95 @@
1413
#include <algorithm>
1514
#include <cstdlib>
1615
#include <thread>
16+
#include <cstdint>
17+
#include <unistd.h>
18+
19+
// ---------------------------------------------------------------------
20+
21+
bool WSJCppCore::init(
22+
int argc, char** argv,
23+
const std::string &sApplicationName,
24+
const std::string &sApplicationVersion,
25+
const std::string &sApplicationAuthor,
26+
const std::string &sLibraryNameForExports
27+
) {
28+
// init random
29+
std::srand(std::rand() + std::time(0));
30+
// WSJCppCore::initRandom();
31+
return true;
32+
}
33+
34+
// ---------------------------------------------------------------------
35+
36+
std::string WSJCppCore::doNormalizePath(const std::string & sPath) {
37+
// split path by /
38+
std::vector<std::string> vNames;
39+
std::string s = "";
40+
int nStrLen = sPath.length();
41+
for (int i = 0; i < sPath.length(); i++) {
42+
if (sPath[i] == '/') {
43+
vNames.push_back(s);
44+
s = "";
45+
if (i == nStrLen-1) {
46+
vNames.push_back("");
47+
}
48+
} else {
49+
s += sPath[i];
50+
}
51+
}
52+
if (s != "") {
53+
vNames.push_back(s);
54+
}
55+
56+
// fildered
57+
int nLen = vNames.size();
58+
std::vector<std::string> vNewNames;
59+
for (int i = 0; i < nLen; i++) {
60+
std::string sCurrent = vNames[i];
61+
if (sCurrent == "" && i == nLen-1) {
62+
vNewNames.push_back(sCurrent);
63+
continue;
64+
}
65+
66+
if ((sCurrent == "" || sCurrent == ".") && i != 0) {
67+
continue;
68+
}
69+
70+
if (sCurrent == ".." && vNewNames.size() > 0) {
71+
std::string sPrev = vNewNames[vNewNames.size()-1];
72+
if (sPrev == "") {
73+
vNewNames.pop_back();
74+
vNewNames.push_back(sCurrent);
75+
} else if (sPrev != "." && sPrev != "..") {
76+
vNewNames.pop_back();
77+
} else {
78+
vNewNames.push_back(sCurrent);
79+
}
80+
} else {
81+
vNewNames.push_back(sCurrent);
82+
}
83+
}
84+
std::string sRet = "";
85+
int nNewLen = vNewNames.size();
86+
int nLastNew = nNewLen-1;
87+
for (int i = 0; i < nNewLen; i++) {
88+
sRet += vNewNames[i];
89+
if (i != nLastNew) {
90+
sRet += "/";
91+
}
92+
}
93+
return sRet;
94+
}
95+
96+
// ---------------------------------------------------------------------
97+
98+
std::string WSJCppCore::getCurrentDirectory() {
99+
char cwd[PATH_MAX];
100+
if (getcwd(cwd, sizeof(cwd)) == NULL) {
101+
WSJCppLog::throw_err("getCurrentDirectory", "Could not get current directory");
102+
}
103+
return std::string(cwd) + "/";
104+
}
17105

18106
// ---------------------------------------------------------------------
19107

src/wsjcpp_core.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010

1111
class WSJCppCore {
1212
public:
13+
static bool init(
14+
int argc, char** argv,
15+
const std::string &sApplicationName,
16+
const std::string &sApplicationVersion,
17+
const std::string &sApplicationAuthor,
18+
const std::string &sLibraryNameForExports
19+
);
20+
21+
static std::string doNormalizePath(const std::string & sPath);
22+
static std::string getCurrentDirectory();
23+
1324
static long currentTime_milliseconds();
1425
static long currentTime_seconds();
1526
static std::string currentTime_forFilename();

unit-tests/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ list (APPEND WSJCPP_INCLUDE_DIRS "../src")
2020
list (APPEND WSJCPP_INCLUDE_DIRS "src")
2121

2222

23+
list (APPEND WSJCPP_SOURCES "./src/main.cpp")
2324
list (APPEND WSJCPP_SOURCES "./src/unit_tests.h")
2425
list (APPEND WSJCPP_SOURCES "./src/unit_tests.cpp")
25-
26-
list (APPEND WSJCPP_SOURCES "./src/main.cpp")
26+
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_normalize_path.h")
27+
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_normalize_path.cpp")
28+
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_uuid.h")
29+
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_uuid.cpp")
2730

2831
include_directories(${WSJCPP_INCLUDE_DIRS})
2932

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "unit_test_core_normalize_path.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <wsjcpp_core.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestCoreNormalizePath)
7+
8+
UnitTestCoreNormalizePath::UnitTestCoreNormalizePath()
9+
: UnitTestBase("UnitTestFallenNormalizePath") {
10+
//
11+
}
12+
13+
// ---------------------------------------------------------------------
14+
15+
void UnitTestCoreNormalizePath::init() {
16+
// nothing
17+
}
18+
19+
// ---------------------------------------------------------------------
20+
21+
bool UnitTestCoreNormalizePath::run() {
22+
std::map<std::string, std::string> mapPaths;
23+
mapPaths.insert(std::pair<std::string, std::string>("",""));
24+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/bin/some","/usr/local/bin/some"));
25+
mapPaths.insert(std::pair<std::string, std::string>("./bin/some","./bin/some"));
26+
mapPaths.insert(std::pair<std::string, std::string>(".//../bin/some","./../bin/some"));
27+
mapPaths.insert(std::pair<std::string, std::string>(".//../bin/some/../","./../bin/"));
28+
mapPaths.insert(std::pair<std::string, std::string>(".//../bin/some/..","./../bin"));
29+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/./bin/some","/usr/local/bin/some"));
30+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../bin/some","/usr/bin/some"));
31+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/..//bin/some","/usr/bin/some"));
32+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/..//bin/some/","/usr/bin/some/"));
33+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/..//bin/./some/","/usr/bin/some/"));
34+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../../bin/./some/","/bin/some/"));
35+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../../../bin/./some/","../bin/some/"));
36+
mapPaths.insert(std::pair<std::string, std::string>(".//","./"));
37+
std::map<std::string, std::string>::iterator it;
38+
39+
int nCounter = 0;
40+
for ( it = mapPaths.begin(); it != mapPaths.end(); it++ ) {
41+
std::string sSource = it->first;
42+
std::string sExpected = it->second;
43+
std::string sGot = WSJCppCore::doNormalizePath(sSource);
44+
if (sGot != sExpected) {
45+
nCounter++;
46+
WSJCppLog::err(TAG, "Wrong normalize path for: '" + sSource + "'.\n\t Got: '" + sGot + "', but expected: '" + sExpected + "'");
47+
}
48+
49+
}
50+
return nCounter == 0;
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_CORE_NORMALIZE_PATH_H
2+
#define UNIT_TEST_CORE_NORMALIZE_PATH_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestCoreNormalizePath : public UnitTestBase {
7+
public:
8+
UnitTestCoreNormalizePath();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_CORE_NORMALIZE_PATH_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "unit_test_core_uuid.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <wsjcpp_core.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestCoreUuid)
7+
8+
9+
UnitTestCoreUuid::UnitTestCoreUuid()
10+
: UnitTestBase("UnitTestCoreUuid") {
11+
//
12+
}
13+
14+
void UnitTestCoreUuid::init() {
15+
// nothing
16+
}
17+
18+
bool UnitTestCoreUuid::run() {
19+
std::vector<std::string> vUuids;
20+
for (int i = 0; i < 100; i++) {
21+
std::string sUuid = WSJCppCore::createUuid();
22+
/*if (i < 3) {
23+
Log::info(TAG, "sUuid: " + sUuid);
24+
}*/
25+
vUuids.push_back(sUuid);
26+
}
27+
28+
int nCounter = 0;
29+
for (int x = 0; x < 100; x++) {
30+
for (int y = 0; y < 100; y++) {
31+
if (vUuids[x] == vUuids[y] && x != y) {
32+
nCounter++;
33+
WSJCppLog::err(TAG, "Wrong generation random uuids: \nvUuids[" + std::to_string(x)+ "] == vUuids[" + std::to_string(y) + "] == '" + vUuids[x] + "'");
34+
}
35+
}
36+
}
37+
38+
return nCounter == 0;
39+
}

unit-tests/src/unit_test_core_uuid.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_CORE_UUID_H
2+
#define UNIT_TEST_CORE_UUID_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestCoreUuid : public UnitTestBase {
7+
public:
8+
UnitTestCoreUuid();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_CORE_UUID_H

0 commit comments

Comments
 (0)