-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-new-problem
More file actions
executable file
·215 lines (178 loc) · 3.92 KB
/
setup-new-problem
File metadata and controls
executable file
·215 lines (178 loc) · 3.92 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
function usage_msg {
echo "Usage: $0 {option}"
echo
echo "option:"
echo " -n|--name The name of the problem"
echo " You can use namespace format here"
echo " -h|--help Print help message"
}
function error_arg {
usage_msg
exit 1
}
function set_name {
[[ ! "$1" =~ ^\- ]] && export problem_ns="$1"
}
function create_source_dir {
# create src dir
src_dir="src/${problem_ns//::/\/}"
mkdir -p "${src_dir}"
}
function write_cmake_hlp_file {
local p_ns=${problem_ns%::*}
(
for ns in ${p_ns//::/ }
do
cd $ns
if [ ! -e CMakeLists.txt ]; then
cat << EOF_CMAKELISTS > CMakeLists.txt
project_add_all_folders()
EOF_CMAKELISTS
fi
done
)
}
function write_cmake_prj_file {
if [ -e CMakeLists.txt ]; then
return 1;
fi
cat << EOF_CMAKELISTS > CMakeLists.txt
hackerrank_challenge_solution(${problem_name}.cpp ${problem_name}.h)
hackerrank_challenge_test(${problem_name}_test.cpp)
EOF_CMAKELISTS
}
function write_header_file {
local header_file="${problem_name}.h"
if [ -e $header_file ]; then
return 1;
fi
local p_ns=${problem_ns%::*}
local ns_begin=
local ns_end=
for ns in ${p_ns//::/ }
do
ns_begin="$ns_begin\nnamespace $ns {"
done
for ns in $(echo ${p_ns//::/ } | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }' )
do
ns_end="${ns_end}\n} // namespace $ns"
done
echo -ne "#pragma once
namespace hackerrank {
namespace bmgandre {${ns_begin}
class ${problem_name}
{
public:
static void solve();
};
${ns_end}
} // namespace bmgandre
} // namespace hackerrank
" > $header_file
}
function write_source_file {
local source_file="${problem_name}.cpp"
if [ -e $source_file ]; then
return 1;
fi
echo -ne "#include \"${problem_name}.h\"
#include <iostream>
#include <string>
using namespace hackerrank::bmgandre::${problem_ns%::*};
void ${problem_name}::solve()
{
}
" > $source_file
}
function write_test_file {
local test_file="${problem_name}_test.cpp"
if [ -e $test_file ]; then
return 1;
fi
local p_ns=${problem_ns%::*}
local ns_begin=
local ns_end=
for ns in ${p_ns//::/ }
do
ns_begin="$ns_begin\nnamespace $ns {"
done
for ns in $(echo ${p_ns//::/ } | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }' )
do
ns_end="${ns_end}\n} // namespace $ns"
done
echo -ne "#include <hackerrank_gtest.h>
#include \"${problem_name}.h\"
namespace hackerrank {
namespace bmgandre {${ns_begin}
using ${problem_name}_test = hackerrank::bmgandre::tests::hackerrank_gtest;
TEST_F(${problem_name}_test, test_case_1) {
input_stream << \"TEST INPUT\";
${problem_name}::solve();
std::string output = output_stream.str();
ASSERT_EQ(output, \"TEST OUTPUT\");
}
${ns_end}
} // namespace bmgandre
} // namespace hackerrank
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
" > $test_file
}
if [ ! -d "src" ] || [ ! -f CMakeLists.txt ]; then
echo "Error: You must run this script in root project directory"
usage_msg
exit 2
fi
if ! grep -iq "^project(" CMakeLists.txt; then
echo "Error: You must run this script in root project directory"
usage_msg
exit 2
fi
if [[ $# < 1 ]]; then
error_arg
fi
while [[ $# > 0 ]]
do
key="$1"
case $key in
-n|--name)
set_name "$2"
[ ! -z "${problem_ns}" ] && shift
;;
-h|--help)
usage_msg
exit 0
;;
*)
error_arg
;;
esac
shift # past argument or value
done
if [ -z "${problem_ns}" ]; then
echo "Error: name is required but missing"
usage_msg
exit 2
fi
ns_trim="$(echo -e "${problem_ns}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[[:space:]]\+//')"
ns_trim="${ns_trim,,}"
if [ "${problem_ns}" != "${ns_trim}" ]; then
echo "Warn: name will be fixed to: ${ns_trim}"
fi
# lowercase problem namespace without space
problem_ns="${ns_trim}"
# problem name from namespace
problem_name=${problem_ns##*::}
create_source_dir
(cd "${src_dir}"
write_cmake_prj_file
write_header_file
write_source_file
write_test_file
)
(cd "src"
write_cmake_hlp_file
)