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

Commit

Permalink
dslings: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisepeak committed Mar 22, 2024
1 parent 348828a commit 72ef7b4
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 18 deletions.
2 changes: 1 addition & 1 deletion exercises/array/Array.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __ARRAY_HPP__D2DS
#define __ARRAY_HPP__D2DS

#include <DStruct/dstruct.hpp>
//#include <DStruct/dstruct.hpp>

namespace d2ds {

Expand Down
36 changes: 36 additions & 0 deletions exercises/dslings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __DSLINGS_HPP__D2DS
#define __DSLINGS_HPP__D2DS

namespace d2ds {
// show your code






/*
class MaxValue {
public:
MaxValue(int val) {
__mVal = val;
}
int get() {
return __mVal;
}
void set(int val) {
if (val > __mVal)
__mVal = val;
}
private:
int __mVal;
};
*/


}

#endif
2 changes: 2 additions & 0 deletions tests/array/array.0.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <tests/common.hpp>

#include <exercises/array/Array.hpp>

int main() {
Expand Down
2 changes: 2 additions & 0 deletions tests/array/array.1.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <tests/common.hpp>

#include <exercises/array/Array.hpp>

int main() {
Expand Down
2 changes: 2 additions & 0 deletions tests/array/array.2.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <tests/common.hpp>

#include <exercises/array/Array.hpp>

int main() {
Expand Down
10 changes: 10 additions & 0 deletions tests/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __COMMON_HPP__D2DS
#define __COMMON_HPP__D2DS

#include <iostream>
#include <random>
#include <cassert>

#define DeleteTheAssertToContinue() assert(false)

#endif
15 changes: 0 additions & 15 deletions tests/d2ds.cpp

This file was deleted.

25 changes: 25 additions & 0 deletions tests/dslings.0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// dslings.0.cpp - readonly
//
// 描述:
// 通过实现一个MaxVal类型(保存最大值), 来介绍dslings的"编译器驱动开发"
// 即根据编译器的错误提示来完成这个训练流程的演示Demo, 并且通常为了降低难度会把一个'数据结构'的实现分成多个检测模块.
// 如: dslings.0.cpp dslings.1.cpp dslings.2.cpp
//
// 目标/要求:
// - 不修改该代码检测文件
// - 在exercises/dslings.hpp中完成你的代码设计
// - 通过所有编译器检测 和 断言
//

#include <tests/common.hpp>

#include <exercises/dslings.hpp>

int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);

return 0;
}
31 changes: 31 additions & 0 deletions tests/dslings.1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// dslings.1.cpp - readonly
//
// 描述:
// 通过实现一个MaxVal类型(保存最大值), 来介绍dslings的"编译器驱动开发"
// 即根据编译器的错误提示来完成这个训练流程的演示Demo, 并且通常为了降低难度会把一个'数据结构'的实现分成多个检测模块.
// 如: dslings.0.cpp dslings.1.cpp dslings.2.cpp
//
// 目标/要求:
// - 不修改该代码检测文件
// - 在exercises/dslings.hpp中完成你的代码设计
// - 通过所有编译器检测 和 断言
//

#include <tests/common.hpp>

#include <exercises/dslings.hpp>

int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);

mVal.set(-1);
assert(mVal.get() == 2);

mVal.set(100);
assert(mVal.get() == 100);

return 0;
}
49 changes: 49 additions & 0 deletions tests/dslings.2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// dslings.2.cpp - readonly
//
// 描述:
// 通过实现一个MaxVal类型(保存最大值), 来介绍dslings的"编译器驱动开发"
// 即根据编译器的错误提示来完成这个训练流程的演示Demo, 并且通常为了降低难度会把一个'数据结构'的实现分成多个检测模块.
// 如: dslings.0.cpp dslings.1.cpp dslings.2.cpp
//
// 目标/要求:
// - 不修改该代码检测文件
// - 在exercises/dslings.hpp中完成你的代码设计
// - 通过所有编译器检测 和 断言
//

#include <tests/common.hpp>

#include <exercises/dslings.hpp>

int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);

mVal.set(-1);
assert(mVal.get() == 2);

mVal.set(100);
assert(mVal.get() == 100);

// random test
std::random_device rd;
std::mt19937 gen(rd()); // 使用随机设备作为种子
std::uniform_int_distribution<int> dis(0, 200); // 定义范围为 [0, 200] 的均匀分布

int currMaxVal = mVal.get();
for (int i = 0; i < 10; i++) {
int randomNum = dis(gen);
int newMaxVal = currMaxVal > randomNum ? currMaxVal : randomNum;
mVal.set(randomNum);
assert(mVal.get() == newMaxVal);
currMaxVal = mVal.get();
}

std::cout << "hello, dslings..." << std::endl;

DeleteTheAssertToContinue();

return 0;
}
12 changes: 10 additions & 2 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ add_includedirs(".")
-- verify lib
add_includedirs("DStruct")

target("0.d2ds")
target("0.dslings-0")
set_kind("binary")
add_files("tests/d2ds.cpp")
add_files("tests/dslings.0.cpp")

target("0.dslings-1")
set_kind("binary")
add_files("tests/dslings.1.cpp")

target("0.dslings-2")
set_kind("binary")
add_files("tests/dslings.2.cpp")

target("1.array-0")
set_kind("binary")
Expand Down

0 comments on commit 72ef7b4

Please sign in to comment.