Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
all: update dslings + book
Browse files Browse the repository at this point in the history
book:
    update vector chapter

dslings:
    1.update vector's exercise code - split big task to small task
    2.add D2DS_WAIT for every exercise-code
    3.optimize:  add compile bypass mechanism by target file's mtime

Signed-off-by: SPeak <[email protected]>
  • Loading branch information
Sunrisepeak committed May 13, 2024
1 parent 6f7a26e commit 9dcf4da
Show file tree
Hide file tree
Showing 27 changed files with 445 additions and 106 deletions.
38 changes: 28 additions & 10 deletions common/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <cassert>
#include <thread>

#include <dstruct.hpp>

#define HONLY_LOGGER_TAG "D2DS"
#include "common/honly_logger.hpp"
#include "common/dslings_config.hpp"
Expand All @@ -32,28 +34,38 @@
} \
}

#define D2DS_WAIT HONLY_LOGW("Delete the D2DS_WAIT to continue...");
#define D2DS_RETURN HONLY_LOGW("Delete the D2DS_RETURN to continue..."); return 0;
#define D2DS_WAIT HONLY_LOGW("🥳 Delete the D2DS_WAIT to continue...");
#define D2DS_RETURN HONLY_LOGW("🥳 Delete the D2DS_RETURN to continue..."); return 0;

#define D2DS_SELF_ASSIGNMENT_CHECKER if (this == &dsObj) return *this;

namespace d2ds {

struct DefaultAllocator {

using Address = unsigned long long;

static void * allocate(int bytes) {
allocate_counter()++;
if (debug())
HONLY_LOGI("DefaultAllocator: try to allocate %d bytes", bytes);
return malloc(bytes);
void *memPtr = malloc(bytes);
memory_addr_flag_d()[(Address)memPtr] = true;
return memPtr;
}

static void deallocate(void *addr, int bytes) {
deallocate_counter()++;
if (debug())
HONLY_LOGI("DefaultAllocator: free addr %p, bytes %d", addr, bytes);
assert(addr != nullptr);
free(addr);
if (addr == nullptr) {
HONLY_LOGE("free null pointer");
} else if (memory_addr_flag_d()[(Address)addr] == false) {
HONLY_LOGE("double free - %p", addr);
} else {
memory_addr_flag_d()[(Address)addr] = false;
free(addr);
}
}

public: // config & status
Expand All @@ -77,6 +89,12 @@ struct DefaultAllocator {
deallocate_counter() = 0;
}

protected:
static dstruct::Map<Address, bool> & memory_addr_flag_d() {
static dstruct::Map<Address, bool> memAddrFlag;
return memAddrFlag;
}

};

class BigFiveTest {
Expand Down Expand Up @@ -146,23 +164,23 @@ class BigFiveTest {
}

public:
static bool copy_constructor_pass() {
static void copy_constructor_pass() {
get_test_data_e().mCopyConstructor = true;
}

static bool copy_assignment_pass() {
static void copy_assignment_pass() {
get_test_data_e().mCopyAssignment = true;
}

static bool move_constructor_pass() {
static void move_constructor_pass() {
get_test_data_e().mMoveConstructor = true;
}

static bool move_assignment_pass() {
static void move_assignment_pass() {
get_test_data_e().mMoveAssignment = true;
}

static bool self_assignment_pass() {
static void self_assignment_pass() {
get_test_data_e().mSelfAssignment = true;
}

Expand Down
2 changes: 1 addition & 1 deletion common/honly_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
#define HONLY_LOGI(...) { fprintf (stdout, "\033[32m[%s LOGI]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGD(...) { fprintf (stdout, "\033[37m[%s LOGD]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGW(...) { fprintf (stdout, "\033[33m[%s LOGW]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGE(...) { fprintf (stderr, "\033[31m[%s LOGE]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stderr, __VA_ARGS__); }
#define HONLY_LOGE(...) { fprintf (stdout, "\033[31m[%s LOGE]: %s: %s:%d - ❌ | ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }

#endif
4 changes: 2 additions & 2 deletions src/chapter_02_vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,9 @@ namespace d2ds {

template <typename T>
bool operator==(const Vector<T> &v1, const Vector<T> &v2) {
bool equal = v1.size() == v2.size;
bool equal = v1.size() == v2.size();
if (equal) {
for (int i = 0; i < v1.size()) {
for (int i = 0; i < v1.size(); i++) {
if (v1[i] != v2[i]) {
equal = false;
break;
Expand Down
3 changes: 3 additions & 0 deletions tests/array/array.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
int main() {
d2ds::Array<int, 5> intArr;
d2ds::Array<double, 10> doubleArr;

D2DS_WAIT

return 0;
}
3 changes: 3 additions & 0 deletions tests/array/array.1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@

int main() {
d2ds::Array<int, 5> intArr { 5, 4, 3, 2 /*, 1*/ };

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/array/array.3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ int main() {
intArr[4] = intArr[0];
d2ds_assert_eq(intArr[0], intArr[4]);

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/array/array.4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ int main() {

d2ds_assert_eq(4, intArr.back());

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/array/array.5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ int main() {
val++;
}

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/array/array.6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ int main() {
d2ds_assert_eq(intArr[2], intArr[-2]);
d2ds_assert_eq(intArr[3], intArr[-1]);

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/dslings.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ int main() {

d2ds_assert_eq(mVal.get(), 2);

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/dslings.1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ int main() {
mVal.set(100);
d2ds_assert_eq(mVal.get(), 100);

D2DS_WAIT

return 0;
}
3 changes: 3 additions & 0 deletions tests/other/cpp-base/range_for.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
int main() {
d2ds::PyRange(0, 10);
d2ds::PyRange(0, 5, 200);

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/other/cpp-base/range_for.1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ int main() {
auto begin = range.begin();
auto end = range.end();

D2DS_WAIT

return 0;
}
2 changes: 2 additions & 0 deletions tests/other/cpp-base/range_for.2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ int main() {
++begin;
d2ds_assert_eq(*begin, 1);

D2DS_WAIT

return 0;
}
4 changes: 3 additions & 1 deletion tests/other/cpp-base/template.1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@


int main() {

d2ds::Box<int> intBox;
d2ds::Box<dstruct::String> stringBox;

D2DS_WAIT

return 0;
}
29 changes: 29 additions & 0 deletions tests/vector/vector.0.0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// vector.0.cpp - readonly
//
// 描述:
// 定义Vector
//
// 目标/要求:
// - 实现类模板和数据初成员定义
// - 在exercises/array/Vector.hpp中完成你的代码设计
// - 通过编译器检测
//

#include "common/common.hpp"

#include <exercises/array/Vector.hpp>

struct DataMember {
int size;
void *ptr;
};

int main() {

d2ds::Vector<int> intVec;
d2ds_assert(sizeof(intVec) >= sizeof(DataMember));

D2DS_WAIT

return 0;
}
66 changes: 66 additions & 0 deletions tests/vector/vector.0.1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// vector.1.cpp - readonly
//
// 描述:
// Vector自定义分配器支持
//
// struct AllocatorInterface {
// static void * allocate(int bytes);
// static void deallocate(void *addr, int bytes);
// };
//
// 目标/要求:
// - 开发者能使用默认分配器, 也可以通过模板的第二个参数配置自定义的分配器
// - 在exercises/array/Vector.hpp中完成你的代码设计
// - 通过编译器检测
//

#include "common/common.hpp"

#include <exercises/array/Vector.hpp>

struct StackMemAllocator {

static void * allocate(int bytes) {
HONLY_LOGI("StackMemAllocator: try to allocate %d bytes", bytes);
mTop = mTop - bytes;
assert(mTop >= mMemory);
return mTop;
}

static void deallocate(void *addr, int bytes) {
HONLY_LOGI("StackMemAllocator: free addr %p, bytes %d", addr, bytes);
// nothing
}

static void config_and_init(char *memory, int size) {
mMemory = memory;
mTop = memory + size;
}

static char *mTop;
static char *mMemory;
};

char * StackMemAllocator::mTop = nullptr;
char * StackMemAllocator::mMemory = nullptr;

int main() {

char stackMemory[1024];
StackMemAllocator::config_and_init(stackMemory, 1024);

{
int *intPtr = (int *) StackMemAllocator::allocate(sizeof(4));
*intPtr = 1010;
std::cout << intPtr << ": " << *intPtr << std::endl;
StackMemAllocator::deallocate(intPtr, sizeof(intPtr));
}

d2ds::Vector<int, d2ds::DefaultAllocator> intVec;
d2ds::Vector<char, StackMemAllocator> charVecByStack;
d2ds::Vector<double> doubleVec;

D2DS_WAIT

return 0;
}
13 changes: 5 additions & 8 deletions tests/vector/vector.0.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
// vector.0.cpp - readonly
//
// 描述:
// 定义数据结构Vector, 完成数据初始化
//
// struct AllocatorInterface {
// static void * allocate(int bytes);
// static void deallocate(void *addr, int bytes);
// };
// Vector的不同初始化方式
//
// 目标/要求:
// - 实现类模板定义完成数据初始化, 并支持自定义分配器
// - 实现 默认初始化、指定长度初始化、列表初始化 对应的构造函数
// - 在exercises/array/Vector.hpp中完成你的代码设计
// - 通过编译器检测
//
Expand Down Expand Up @@ -56,7 +51,9 @@ int main() {
d2ds::Vector<char, StackMemAllocator> charVecByStack(10);
d2ds::Vector<double> doubleVec = { 1.1, 2.2, 3.3 };

//D2DS_WAIT
d2ds_assert_eq(d2ds::DefaultAllocator::allocate_counter(), 1);

D2DS_WAIT

return 0;
}
32 changes: 32 additions & 0 deletions tests/vector/vector.1.0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// vector.1.cpp - readonly
//
// 描述:
// 显示定义数据结构Vector的bigfive - 析构
//
// 目标/要求:
// - 实现Vector的析构函数
// - 在exercises/array/Vector.hpp中完成你的代码设计
// - 通过编译器检测
//

#include "common/common.hpp"

#include <exercises/array/Vector.hpp>

using d2ds::BigFiveTest;

int main() {

d2ds::DefaultAllocator::debug() = true;

{
d2ds::Vector<BigFiveTest::Obj> objArr(1);
}

d2ds_assert(BigFiveTest::destructor());
d2ds_assert_eq(1,d2ds::DefaultAllocator::deallocate_counter());

D2DS_WAIT

return 0;
}
Loading

0 comments on commit 9dcf4da

Please sign in to comment.