Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8c4c390

Browse files
committed
Add cpp.run.js
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 2e5c914 commit 8c4c390

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
|-|-|
66
|company.js|Filter question by company tag.|
77
|cpp.lint.js|Run cpplint to check syntax before running test.|
8+
|cpp.run.js|Run cpp code locally for debugging purpose.|
89

910
## HOWTO
1011

cpp.run.js

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
var cp = require('child_process');
2+
var fs = require('fs');
3+
4+
var h = require('../helper');
5+
var log = require('../log');
6+
var Plugin = require('../plugin.js');
7+
var session = require('../session');
8+
9+
// Please note that we DON'T want implement a lightweight judge engine
10+
// here, thus we are NOT going to support all the problems!!!
11+
//
12+
// Only works for those problems could be easily tested.
13+
//
14+
// [prerequisite]
15+
//
16+
// - g++ (support c++11)
17+
var plugin = new Plugin(100, 'cpp.run', '2017.07.29',
18+
'Plugin to run cpp code locally for debugging.');
19+
20+
var FILE_SRC = '.tmp.cpp.run.cpp';
21+
var FILE_EXEC = '.tmp.cpp.run.exe';
22+
23+
plugin.testProblem = function(problem, cb) {
24+
if (!session.argv.local || h.extToLang(problem.file) !== 'cpp')
25+
return plugin.next.testProblem(problem, cb);
26+
27+
log.info('\nTesting locally ...\n');
28+
29+
// generate full cpp source code that runnable
30+
var meta = problem.templateMeta;
31+
var args = problem.testcase.split('\n').map(function(x, i) {
32+
// TODO: handle more special types??
33+
// array, list, tree, etc
34+
var t = meta.params[i].type;
35+
if (t.indexOf('[]') >= 0 || t === 'ListNode' || t === 'TreeNode')
36+
x = x.replace('[', '{').replace(']', '}');
37+
if (t === 'ListNode') x = 'make_listnode(' + x + ')';
38+
if (t === 'TreeNode') x = 'make_treenode(' + x + ')';
39+
40+
return x;
41+
});
42+
43+
var data = DATA.replace('$code', fs.readFileSync(problem.file))
44+
.replace('$method', meta.name)
45+
.replace('$args', args.join(','));
46+
fs.writeFileSync(FILE_SRC, data);
47+
48+
// compile and run
49+
var cmd = [
50+
'g++',
51+
'-std=c++11',
52+
'-o',
53+
FILE_EXEC,
54+
FILE_SRC,
55+
'&&',
56+
'./' + FILE_EXEC
57+
].join(' ');
58+
cp.exec(cmd, function(e, stdout, stderr) {
59+
if (e) {
60+
stderr.split('\n').forEach(function(line) {
61+
if (line.length > 0) log.error(line);
62+
});
63+
} else {
64+
stdout.split('\n').forEach(function(line) {
65+
if (line.length > 0) log.info(line);
66+
});
67+
}
68+
});
69+
};
70+
71+
// FIXME: use file template!!
72+
var DATA = `
73+
#include <algorithm>
74+
#include <bitset>
75+
#include <complex>
76+
#include <deque>
77+
#include <exception>
78+
#include <fstream>
79+
#include <functional>
80+
#include <iomanip>
81+
#include <ios>
82+
#include <iosfwd>
83+
#include <iostream>
84+
#include <istream>
85+
#include <iterator>
86+
#include <limits>
87+
#include <list>
88+
#include <locale>
89+
#include <map>
90+
#include <memory>
91+
#include <new>
92+
#include <numeric>
93+
#include <ostream>
94+
#include <queue>
95+
#include <set>
96+
#include <sstream>
97+
#include <stack>
98+
#include <stdexcept>
99+
#include <streambuf>
100+
#include <string>
101+
#include <typeinfo>
102+
#include <utility>
103+
#include <valarray>
104+
#include <vector>
105+
106+
#if __cplusplus >= 201103L
107+
#include <array>
108+
#include <atomic>
109+
#include <chrono>
110+
#include <condition_variable>
111+
#include <forward_list>
112+
#include <future>
113+
#include <initializer_list>
114+
#include <mutex>
115+
#include <random>
116+
#include <ratio>
117+
#include <regex>
118+
#include <scoped_allocator>
119+
#include <system_error>
120+
#include <thread>
121+
#include <tuple>
122+
#include <typeindex>
123+
#include <type_traits>
124+
#include <unordered_map>
125+
#include <unordered_set>
126+
#endif
127+
128+
using namespace std;
129+
130+
/// leetcode defined data types ///
131+
struct ListNode {
132+
int val;
133+
ListNode *next;
134+
ListNode(int x) : val(x), next(NULL) {}
135+
};
136+
137+
struct TreeNode {
138+
int val;
139+
TreeNode *left, *right;
140+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
141+
};
142+
143+
ListNode* make_listnode(const vector<int> &v) {
144+
ListNode head(0), *p = &head, *cur;
145+
for (auto x: v) { cur = new ListNode(x); p->next = cur; p = cur; }
146+
return head.next;
147+
}
148+
149+
constexpr long long null = numeric_limits<long long>::min();
150+
151+
TreeNode* make_treenode(const vector<long long> &v) {
152+
vector<TreeNode*> cur, next;
153+
TreeNode root(0); cur.push_back(&root);
154+
long long i = 0, n = v.size(), x;
155+
while (i < n) {
156+
for (auto p: cur) {
157+
if ((x = v[i++]) != null) { p->left = new TreeNode(x); next.push_back(p->left); }
158+
if (i == n || p == &root) continue;
159+
if ((x = v[i++]) != null) { p->right = new TreeNode(x); next.push_back(p->right); }
160+
}
161+
cur.swap(next); next.clear();
162+
}
163+
return root.left;
164+
}
165+
166+
template<class T>
167+
ostream& operator<<(ostream &os, const vector<T> &v) {
168+
os << "[";
169+
for (int i = 0; i < v.size(); ++i) os << (i > 0 ? "," : "") << v[i];
170+
os << "]";
171+
return os;
172+
}
173+
174+
ostream& operator<<(ostream &os, const ListNode *p) {
175+
vector<int> v;
176+
while (p) { v.push_back(p->val); p = p->next; }
177+
return os << v;
178+
}
179+
180+
ostream& operator<<(ostream &os, const TreeNode *t) {
181+
vector<string> v;
182+
queue<const TreeNode*> cur, next;
183+
if (t) cur.push(t);
184+
185+
while (!cur.empty()) {
186+
t = cur.front(); cur.pop();
187+
v.push_back(t ? to_string(t->val) : "null");
188+
if (t && (t->left || t->right)) {
189+
next.push(t->left);
190+
if (t->right || !cur.empty()) next.push(t->right);
191+
}
192+
if (cur.empty()) cur.swap(next);
193+
}
194+
return os << v;
195+
}
196+
197+
$code
198+
int main() {
199+
Solution s;
200+
auto res = s.$method($args);
201+
cout << res << endl;
202+
return 0;
203+
}
204+
`;
205+
206+
module.exports = plugin;

0 commit comments

Comments
 (0)