Skip to content

Commit 7ef199a

Browse files
committed
Fixed #8 added getHumanSizeBytes
1 parent afc679d commit 7ef199a

11 files changed

+206
-37
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,17 @@ Simular a js function `decodeURIComponent`
378378
```
379379
static std::string decodeUriComponent(const std::string& sValue);
380380
```
381+
382+
### getHumanSizeBytes
383+
384+
Human size for sizes in bytes
385+
386+
```
387+
std::string sResult = WsjcppCore::getHumanSizeBytes(12012);
388+
std::cout << "Size: " << sResult << std::endl;
389+
```
390+
391+
Example output:
392+
```
393+
Size: 12K
394+
```

src.wsjcpp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by [email protected]
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_VERSION="v0.0.8")
4+
add_definitions(-DWSJCPP_VERSION="v0.1.0")
55
add_definitions(-DWSJCPP_NAME="wsjcpp-core")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

src/wsjcpp_core.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,31 @@ std::string WsjcppCore::decodeUriComponent(const std::string& sValue) {
609609
return sRet;
610610
}
611611

612+
// ---------------------------------------------------------------------
613+
614+
std::string WsjcppCore::getHumanSizeBytes(long nBytes) {
615+
if (nValue == 0) {
616+
return "0B";
617+
}
618+
std::string arrPrefix[] = {"B", "KB", "MB", "GB", "TB", "PB"};
619+
long n0 = nValue;
620+
long n1 = 0;
621+
for (int i = 0; i < 6; i++) {
622+
if (n0 >= 1 && n0 < 1000) {
623+
return std::to_string(n0) + arrPrefix[i];
624+
}
625+
n0 = nValue / 1000;
626+
n1 = nValue - n0 * 1000;
627+
n0 += n1 >= 500 ? 1 : 0;
628+
629+
nValue = nValue / 1000;
630+
if (n0 == 0 && n1 == 0) {
631+
return "fuck";
632+
}
633+
}
634+
return std::to_string(nValue) + "PB";
635+
}
636+
612637
// ---------------------------------------------------------------------
613638
// WsjcppLog
614639

src/wsjcpp_core.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class WsjcppCore {
6363
static std::string encodeUriComponent(const std::string& sValue);
6464
static std::string decodeUriComponent(const std::string& sValue);
6565

66-
66+
static std::string getHumanSizeBytes(long nBytes);
6767
};
6868

6969

src/wsjcpp_unit_tests.cpp

+6-28
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ void WsjcppUnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint,
5555

5656
// ---------------------------------------------------------------------
5757

58-
std::vector<WsjcppUnitTestBase*> *g_pUnitTests = NULL;
58+
std::vector<WsjcppUnitTestBase*> *g_pWsjcppUnitTests = nullptr;
5959

6060
void WsjcppUnitTests::initGlobalVariables() {
61-
if (g_pUnitTests == NULL) {
61+
if (g_pWsjcppUnitTests == nullptr) {
6262
// WsjcppLog::info(std::string(), "Create handlers map");
63-
g_pUnitTests = new std::vector<WsjcppUnitTestBase*>();
63+
g_pWsjcppUnitTests = new std::vector<WsjcppUnitTestBase*>();
6464
}
6565
}
6666

@@ -69,8 +69,8 @@ void WsjcppUnitTests::initGlobalVariables() {
6969
void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBase* pUnitTest) {
7070
WsjcppUnitTests::initGlobalVariables();
7171
bool bFound = false;
72-
for (int i = 0; i < g_pUnitTests->size(); i++) {
73-
WsjcppUnitTestBase* p = g_pUnitTests->at(i);
72+
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
73+
WsjcppUnitTestBase* p = g_pWsjcppUnitTests->at(i);
7474
if (p->name() == sTestName) {
7575
bFound = true;
7676
}
@@ -79,31 +79,9 @@ void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBa
7979
if (bFound) {
8080
WsjcppLog::err(sTestName, "Already registered");
8181
} else {
82-
g_pUnitTests->push_back(pUnitTest);
82+
g_pWsjcppUnitTests->push_back(pUnitTest);
8383
// Log::info(sCmd, "Registered");
8484
}
8585
}
8686

87-
// ---------------------------------------------------------------------
88-
89-
bool WsjcppUnitTests::runUnitTests() {
90-
WsjcppUnitTests::initGlobalVariables();
91-
int nAll = g_pUnitTests->size();
92-
WsjcppLog::info("runUnitTests", "All tests count " + std::to_string(nAll));
93-
int nSuccess = 0;
94-
for (int i = 0; i < g_pUnitTests->size(); i++) {
95-
WsjcppUnitTestBase* pUnitTest = g_pUnitTests->at(i);
96-
std::string sTestName = pUnitTest->name();
97-
WsjcppLog::info("runUnitTests", "Run test " + sTestName);
98-
if (pUnitTest->run()) {
99-
WsjcppLog::ok(sTestName, "Test passed");
100-
nSuccess++;
101-
} else {
102-
WsjcppLog::err(sTestName, "Test failed");
103-
}
104-
}
105-
WsjcppLog::info("WsjcppUnitTests::runUnitTests", "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll));
106-
return nSuccess == nAll;
107-
}
108-
10987
// ---------------------------------------------------------------------

src/wsjcpp_unit_tests.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ class WsjcppUnitTestBase {
2323
std::string m_sTestName;
2424
};
2525

26-
extern std::vector<WsjcppUnitTestBase*> *g_pUnitTests;
26+
extern std::vector<WsjcppUnitTestBase*> *g_pWsjcppUnitTests;
2727

2828
class WsjcppUnitTests {
2929
public:
3030
static void initGlobalVariables();
31-
static void addUnitTest(const std::string &sTestName, WsjcppUnitTestBase* pCmdHandler);
32-
static bool runUnitTests();
31+
static void addUnitTest(const std::string &sTestName, WsjcppUnitTestBase* pUnitTest);
3332
};
3433

3534
// RegistryCmdHandler

src/wsjcpp_unit_tests_main.cpp

+72-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#include <wsjcpp_core.h>
33
#include <wsjcpp_unit_tests.h>
44

5+
void printHelp(const std::string &sProgramName) {
6+
std::string sOutput = "\nHelp:\n";
7+
sOutput +=
8+
" '" + sProgramName + "' - run all unit-tests\n"
9+
" '" + sProgramName + " help' - print this help\n"
10+
" '" + sProgramName + " list' - print list of unit-tests\n"
11+
" '" + sProgramName + " run <TestName>' - run single unit-test\n"
12+
;
13+
WsjcppLog::info("UnitTests", sOutput);
14+
}
15+
516
int main(int argc, char** argv) {
617
WsjcppCore::initRandom();
718
std::string TAG = "UnitTests";
@@ -16,9 +27,68 @@ int main(int argc, char** argv) {
1627
return -1;
1728
}
1829

19-
if (!WsjcppUnitTests::runUnitTests()) {
20-
WsjcppLog::err(TAG, "Some unit tests failed");
30+
WsjcppUnitTests::initGlobalVariables();
31+
std::string sProgramName(argv[0]);
32+
33+
if (argc == 1) {
34+
int nAll = g_pWsjcppUnitTests->size();
35+
WsjcppLog::info("runUnitTests", "All tests count " + std::to_string(nAll));
36+
int nSuccess = 0;
37+
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
38+
WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i);
39+
std::string sTestName = pUnitTest->name();
40+
WsjcppLog::info("runUnitTests", "Run test " + sTestName);
41+
if (pUnitTest->run()) {
42+
WsjcppLog::ok(sTestName, "Test passed");
43+
nSuccess++;
44+
} else {
45+
WsjcppLog::err(sTestName, "Test failed");
46+
}
47+
}
48+
WsjcppLog::info(TAG, "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll));
49+
bool bResult = nSuccess == nAll;
50+
return bResult ? 0 : -1;
51+
} else if (argc == 2) {
52+
std::string sArg2(argv[1]);
53+
if (sArg2 == "list") {
54+
std::string sOutput = "\nList of unit-tests:\n";
55+
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
56+
WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i);
57+
sOutput += " - " + pUnitTest->name() + "\n";
58+
}
59+
WsjcppLog::info(TAG, sOutput);
60+
return -1;
61+
} else if (sArg2 == "help") {
62+
printHelp(sProgramName);
63+
return -1;
64+
}
65+
} else if (argc == 3) {
66+
std::string sArg2(argv[1]);
67+
std::string sArg3(argv[2]);
68+
if (sArg2 == "run") {
69+
int nSuccess = 0;
70+
bool bTestFound = false;
71+
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
72+
WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i);
73+
if (pUnitTest->name() == sArg3) {
74+
bTestFound = true;
75+
if (pUnitTest->run()) {
76+
WsjcppLog::ok(TAG, pUnitTest->name() + " Test passed");
77+
nSuccess++;
78+
} else {
79+
WsjcppLog::err(TAG, pUnitTest->name() + " Test failed");
80+
}
81+
}
82+
}
83+
if (!bTestFound) {
84+
WsjcppLog::err(TAG, "Test not found try help");
85+
}
86+
return -1;
87+
}
88+
printHelp(sProgramName);
2189
return -1;
2290
}
91+
92+
printHelp(sProgramName);
2393
return 0;
2494
}

unit-tests.wsjcpp/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
5-
add_definitions(-DWSJCPP_VERSION="ut-v0.0.8")
5+
add_definitions(-DWSJCPP_VERSION="ut-v0.1.0")
66
add_definitions(-DWSJCPP_NAME="unit-tests-wsjcpp-core")
77

88
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -19,7 +19,7 @@ set (WSJCPP_SOURCES "")
1919
find_package(Threads REQUIRED)
2020
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
2121

22-
# wsjcpp-core:v0.0.8
22+
# wsjcpp-core:v0.1.0
2323
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
2424
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.cpp")
2525
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.h")
@@ -59,6 +59,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_file_to_buf
5959
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_file_to_buffer.cpp")
6060
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_join.h")
6161
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_join.cpp")
62+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.h")
63+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.cpp")
6264

6365
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
6466

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "unit_test_get_human_size_bytes.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
5+
REGISTRY_UNIT_TEST(UnitTestgetHumanSizeBytes)
6+
7+
UnitTestgetHumanSizeBytes::UnitTestgetHumanSizeBytes()
8+
: WsjcppUnitTestBase("UnitTestgetHumanSizeBytes") {
9+
}
10+
11+
// ---------------------------------------------------------------------
12+
13+
void UnitTestgetHumanSizeBytes::init() {
14+
// nothing
15+
}
16+
17+
// ---------------------------------------------------------------------
18+
19+
bool UnitTestgetHumanSizeBytes::run() {
20+
bool bTestSuccess = true;
21+
22+
struct LTest {
23+
LTest(
24+
long nValue,
25+
const std::string &sExpectedStr
26+
) {
27+
this->nValue = nValue;
28+
this->sExpectedStr = sExpectedStr;
29+
};
30+
long nValue;
31+
std::string sExpectedStr;
32+
};
33+
std::vector<LTest> tests;
34+
tests.push_back(LTest(0, "0B"));
35+
tests.push_back(LTest(30, "30B"));
36+
tests.push_back(LTest(999, "999B"));
37+
tests.push_back(LTest(1000, "1KB"));
38+
tests.push_back(LTest(1400, "1KB"));
39+
tests.push_back(LTest(1600, "2KB"));
40+
tests.push_back(LTest(100600, "101KB"));
41+
tests.push_back(LTest(10123, "10KB"));
42+
tests.push_back(LTest(10499, "10KB"));
43+
tests.push_back(LTest(10999, "11KB"));
44+
tests.push_back(LTest(100999, "101KB"));
45+
tests.push_back(LTest(1000999, "1MB"));
46+
tests.push_back(LTest(1001999, "1MB"));
47+
tests.push_back(LTest(1999999, "2MB"));
48+
tests.push_back(LTest(1401999, "1MB"));
49+
tests.push_back(LTest(1501999, "2MB"));
50+
tests.push_back(LTest(201001999, "201MB"));
51+
tests.push_back(LTest(2000501999, "2GB"));
52+
tests.push_back(LTest(2300501999, "2GB"));
53+
tests.push_back(LTest(2500501999, "3GB"));
54+
55+
for (int i = 0; i < tests.size(); i++) {
56+
long nValue = tests[i].nValue;
57+
std::string sExpectedStr = tests[i].sExpectedStr;
58+
std::string sGotStr = WsjcppCore::getHumanSizeBytes(nValue);
59+
compareS(bTestSuccess, "value=" + std::to_string(nValue), sGotStr, sExpectedStr);
60+
}
61+
62+
return bTestSuccess;
63+
}
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_GET_HUMAN_SIZE_BYTES_H
2+
#define UNIT_TEST_GET_HUMAN_SIZE_BYTES_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestgetHumanSizeBytes : public WsjcppUnitTestBase {
8+
public:
9+
UnitTestgetHumanSizeBytes();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_GET_HUMAN_SIZE_BYTES_H
15+

wsjcpp.yml

+2
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ unit-tests:
6666
description: "test for readFileToBuffer"
6767
- name: "Join"
6868
description: "Test join function"
69+
- name: "getHumanSizeBytes"
70+
description: "Test function get human size in bytes"

0 commit comments

Comments
 (0)