-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
59 lines (50 loc) · 1.45 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <sstream>
#include "CustomAssert.hh"
#define NDEBUG
typedef unsigned int uint_32;
struct Err {
};
std::string intToBinary(int i){
std::string answer = "";
if (i > 1){
while (i != 0){
int q = i%2;
answer += std::to_string(q);
i /= 2;
}
}
return answer;
}
// lambda function 🧠🧠
auto testCases = [](auto func, auto expected) {
uint_32 passedTestcases = 0;
// map <int, int>
for (auto i: expected){
try {
Assert<Err>(func(i.first) == i.second);
} catch(Err err){
std::stringstream ss;
std::string hi;
std::string hi2;
ss << func(i.first);
hi = ss.str();
ss.str(std::string());
ss << i.second;
hi2 = ss.str();
return "Assertion failed at testcase #" + std::to_string(passedTestcases) + " (Expected " + hi2 + " but got " + hi + ") Passed " + std::to_string(passedTestcases) + "/" + std::to_string(expected.size()) + " testcases.";
}
passedTestcases++;
}
return "Passed " + std::to_string(passedTestcases) + "/" + std::to_string(expected.size()) + " testcases.";
};
int main() {
// try to make it typename iterator_traits<Iter>::value_type for more flexibility
std::vector<std::pair<int,std::string>> testcasesExpect {{55,"110111"}};
// give it a input and expected val in the std::pair
std::cout << testCases(intToBinary,testcasesExpect);
return 0;
}