Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 56cd38c

Browse files
committed
Implement expected test run functionality.
1 parent 407ed05 commit 56cd38c

File tree

7 files changed

+161
-16
lines changed

7 files changed

+161
-16
lines changed

‎data/languages/cpp/CMakeLists.txt

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,54 @@ FetchContent_MakeAvailable(json)
3131
# Set the build type explicitly
3232
set(CMAKE_BUILD_TYPE Release)
3333

34+
set(CMAKE_BUILD_TYPE Release)
35+
3436
add_executable(solution_cpp)
37+
add_executable(solution_expected_cpp)
3538

36-
target_sources(solution_cpp PRIVATE
37-
# headers
38-
binder.h
39+
target_compile_definitions(solution_expected_cpp PRIVATE EXPECTED)
40+
41+
set(HEADER_FILES
42+
binder.h
3943
comparator.h
4044
parser.h
4145
printer.h
4246
problemtest.h
4347
solutionwrapper.h
4448
stlincludes.h
4549
typetraits.h
50+
)
4651

47-
# sources
52+
set(SOURCE_FILES
4853
main.cpp
4954
problemtest.cpp
5055
)
5156

57+
target_sources(solution_cpp PRIVATE
58+
# headers
59+
${HEADER_FILES}
60+
# sources
61+
${SOURCE_FILES}
62+
)
63+
64+
target_sources(solution_expected_cpp PRIVATE
65+
# headers
66+
${HEADER_FILES}
67+
# sources
68+
${SOURCE_FILES}
69+
)
70+
5271
target_link_libraries(solution_cpp PRIVATE nlohmann_json::nlohmann_json)
72+
target_link_libraries(solution_expected_cpp PRIVATE nlohmann_json::nlohmann_json)
5373

5474
set(CMAKE_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bin)
5575

5676
install(TARGETS solution_cpp
5777
CONFIGURATIONS Release
5878
RUNTIME DESTINATION ${CMAKE_INSTALL_DIR}
5979
)
80+
81+
install(TARGETS solution_expected_cpp
82+
CONFIGURATIONS Release
83+
RUNTIME DESTINATION ${CMAKE_INSTALL_DIR}
84+
)

‎data/languages/cpp/solutionwrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
using namespace std;
77

8+
#ifdef EXPECTED
9+
#include "solution_expected.cpp"
10+
#else
811
#include "solution.cpp"
12+
#endif
913

1014
#endif // SOLUTIONWRAPPER_H
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
int size = s.length();
5+
int maxLength = 0;
6+
unordered_map<char, int> lastVisited;
7+
8+
for (int j = 0, i = 0; j < size; j++){
9+
if(lastVisited[s[j]] > 0) {
10+
i = max(lastVisited[s[j]], i);
11+
}
12+
maxLength = max(maxLength, j - i + 1);
13+
lastVisited[s[j]] = j + 1;
14+
}
15+
return maxLength;
16+
}
17+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
void sinkIsland(vector<vector<char>>& grid, int startI, int startJ) {
3+
deque<pair<int, int>> pairs;
4+
pairs.push_back(make_pair(startI, startJ));
5+
6+
const auto tryAdd = [&grid, &pairs] (int i, int j) {
7+
if (i < 0 || j < 0) return;
8+
if (i >= grid.size() || j >= grid.front().size()) return;
9+
10+
pairs.push_back({i, j});
11+
};
12+
13+
while (!pairs.empty()) {
14+
const auto [i, j] = pairs.front();
15+
pairs.pop_front();
16+
17+
if (grid[i][j] == '0') continue;
18+
grid[i][j] = '0';
19+
20+
tryAdd(i - 1, j);
21+
tryAdd(i + 1, j);
22+
tryAdd(i, j - 1);
23+
tryAdd(i, j + 1);
24+
}
25+
}
26+
27+
public:
28+
int numIslands(vector<vector<char>>& grid) {
29+
int nIslands = 0;
30+
for (int i = 0; i < grid.size(); ++i) {
31+
for (int j = 0; j < grid.front().size(); ++j) {
32+
if (grid[i][j] == '1') {
33+
++nIslands;
34+
sinkIsland(grid, i, j);
35+
}
36+
}
37+
}
38+
return nIslands;
39+
}
40+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int,int> visited;
5+
6+
for (int i = 0; i < nums.size(); ++i) {
7+
const int curElement = nums[i];
8+
const int delta = target - curElement;
9+
10+
const auto it = visited.find(delta);
11+
if (it != visited.end()) {
12+
return {it->second, i};
13+
}
14+
15+
visited.insert({curElement, i});
16+
continue;
17+
18+
}
19+
return {};
20+
}
21+
};

‎src/app/openleetcode.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ def main():
5959
"--list-testcases",
6060
action="store_true",
6161
default=False,
62-
help="List testcases for a problem specified with '--problem' option."
63-
)
62+
help="List testcases for a problem specified with '--problem' option.")
6463
parser.add_argument(
6564
"--problem", "-p",
6665
metavar='problem_name',
@@ -75,6 +74,11 @@ def main():
7574
type=str,
7675
help=("Path to a directory with the problems. Usually "
7776
"./problem_builds/ directory. Default: problem_builds."))
77+
parser.add_argument(
78+
"--run-expected-tests", "-r",
79+
action="store_true",
80+
default=False,
81+
help="Run the expected solution. Default: False.")
7882
parser.add_argument(
7983
"--testcase", "-t",
8084
metavar='testcase_name',
@@ -223,9 +227,15 @@ def main():
223227
print(logger.red(f"The bin directory {bin_dir} does not exist. Check "
224228
"the problem_builds_dir and problem arguments."))
225229
sys.exit(1)
230+
231+
exe_file_name = (
232+
"solution_expected_"
233+
if args.run_expected_tests
234+
else "solution_"
235+
)
226236

227237
exe_file = os.path.abspath(os.path.join(
228-
bin_dir, f"solution_{args.language}" + getExeExtension()))
238+
bin_dir, f"{exe_file_name}{args.language}" + getExeExtension()))
229239

230240
if not os.path.isfile(exe_file):
231241
print(logger.red(f"The file {exe_file} does not exist. Check the "
@@ -244,10 +254,11 @@ def main():
244254
output_file_dir = os.path.abspath(os.path.join(TESTCAST_OUTPUT_DIR))
245255

246256
# Run the tests
247-
ret, error_message = testrunner.runTests(exe_file, testcases_dir,
248-
output_file_dir,
249-
args.problem,
250-
args.testcase)
257+
ret, error_message = testrunner.runTests(exe_file,
258+
testcases_dir,
259+
output_file_dir,
260+
args.problem,
261+
args.testcase)
251262

252263
if ret != 0:
253264
print(logger.red(f"Tests failed! Error: {error_message}"))

‎src/ui/index.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function setTestResults(results) {
9595
if (!validateResults(results)) {
9696
return;
9797
}
98-
98+
console.log("Setting test results: "+JSON.stringify(results));
9999
const div = document.getElementById('test-results-content');
100100

101101
let html = `
@@ -127,7 +127,7 @@ function setTestResults(results) {
127127
document.getElementById('tab-test-results-button').click();
128128
}
129129

130-
function run(callback, testcase = 'All') {
130+
function run(callback, testcase = 'All',expected=false) {
131131
saveSolution('cpp', editor.getValue());
132132
const pathsFile = DirectoryManager.getPathsFile();
133133
if (!file.existsSync(pathsFile)) {
@@ -143,15 +143,18 @@ function run(callback, testcase = 'All') {
143143
`--language cpp ` +
144144
`--problem ${activeProblem} ` +
145145
`--testcase ${testcase} ` +
146+
`${expected ? '--run-expected-tests ' : ''}` +
146147
`--verbose`;
147148

148149
console.log("Running command: " + command);
149150

150151
var resultsFilename;
151152
exec(command, (error, stdout, stderr) => {
152153
if (error) {
154+
console.log("Error running the command, error: " + error +
155+
", stderr: " + stderr + ", stdout: " + stdout);
153156
var element = document.getElementById("compilation-content");
154-
element.textContent = parseBuildError(stdout);
157+
element.textContent = parseBuildError(stdout+"\n"+error);
155158
document.getElementById('tab-compilation-button').click();
156159
return;
157160
}
@@ -187,13 +190,38 @@ function setCustomTestcaseResults(results) {
187190
}
188191

189192
if (results.tests[0].status !== "Skipped") {
190-
console.error("Expected custom test status to be skipped, got " +
193+
console.error("Expected custom test status to be 'skipped', got " +
191194
results.tests[0].status);
192195
}
193196

197+
console.log("Setting custom testcase results: " + JSON.stringify(results));
198+
194199
document.getElementById('testcase-stdout').textContent = results.stdout;
195200
document.getElementById('testcase-output').textContent =
196201
results.tests[0].actual;
202+
203+
run(setExpectedTestcaseResults, directoryManager.getCustomTestcaseName(),
204+
true);
205+
}
206+
207+
function setExpectedTestcaseResults(expected) {
208+
if (!validateResults(expected)) {
209+
return;
210+
}
211+
212+
if (expected.tests.length !== 1) {
213+
console.error("Expected 1 test results, got " +
214+
expected.tests.length);
215+
return;
216+
}
217+
218+
if (expected.tests[0].status !== "Skipped") {
219+
console.error("Expected test status to be 'skipped', got " +
220+
expected.tests[0].status);
221+
}
222+
223+
document.getElementById('expected-output').textContent =
224+
expected.tests[0].actual;
197225
}
198226

199227
function runCustomTestcase() {
@@ -214,7 +242,6 @@ function runCustomTestcase() {
214242
}
215243

216244
console.log('Custom testcase written to ' + customTestcaseFilename);
217-
console.log('Testcase input: ' + input);
218245

219246
run(setCustomTestcaseResults, directoryManager.getCustomTestcaseName());
220247
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /