It’s been long time not writing a post, and I decide to write post in English.
In this term, I have a required course called Specilised Programming, and this class teaches algorithms. A reference textbook is Introduction to The Design and Analysis of Algorithm (3e.).
Well, this course needs use to submit a practise report each week. While the problems should be from TopCoder.
I found a complicated thing, we need to copy and paste all the test cases by hands. That’s unbearable! So write this program using JUnit to simulate online judge.
Scope of usage
- Clone the repo: https://github.com/MewX/MyPracticeInOne/tree/master/TopCoder
- Use IntelliJ IDEA to open the project
- Find a problem in
TopCoder Archive - Create a solution class of a problem, and download test case page file from detail page, to “testdata/”
- Create a JUnit test for the class and destination function
- Just use my utils library!
Structure of projects
org/mewx/topcoder/problems
This folder stores all the solution codes.
org/mewx/topcoder/test
This folder stores all the test codes.
All the test codes should be JUnit test entities. And tests are available to access common test utils functions.
testdata/
This folder stores all the web pages containing the whole set of test cases.
Sample test case:
public class TheLotteryBothDivsTest {
private final double DELTA = 1e-9;
@Test
public void find() throws Exception {
List<ParsedResultMeta> parsedResultMeta = TestUtils.parseTestData(TestUtils.getFileContentById(TheLotteryBothDivs.id));
for (int i = 0; i < parsedResultMeta.size(); i ++) {
assertEquals(TestUtils.getFailureMessage(i, parsedResultMeta.size(), BuiltinParser.parseToString(parsedResultMeta.get(i).getTestArgs())),
BuiltinParser.parseToDouble(parsedResultMeta.get(i).getExpectedResult()),
new TheLotteryBothDivs().find(BuiltinParser.parseToStringArray(parsedResultMeta.get(i).getTestArgs())), DELTA);
}
System.out.println(TestUtils.getSuccessMessage(parsedResultMeta.size()));
}
}
好久没写博客了,这次我决定试着用英文来写。
这学期我有一门必修课叫 Specilised Programming,主要讲算法。参考教材是《算法设计与分析基础(第 3 版)》(Introduction to The Design and Analysis of Algorithm (3e.))。
这门课每周都要求我们提交一份练习报告,而题目都来自 TopCoder。
不过我发现一件很麻烦的事:所有测试用例都得手动一个一个复制粘贴,简直不能忍!于是我用 JUnit 写了这个程序,用来模拟在线评测系统(OJ)。
使用方法
- 克隆仓库:https://github.com/MewX/MyPracticeInOne/tree/master/TopCoder
- 使用 IntelliJ IDEA 打开项目
- 在
TopCoder Archive中找一道题 - 为这道题创建一个解题类,并从题目详情页下载测试用例网页文件,保存到 “testdata/” 目录
- 为该类和目标方法创建一个 JUnit 测试
- 直接调用我写的工具库就行啦!
项目结构
org/mewx/topcoder/problems
这个目录存放所有的解题代码。
org/mewx/topcoder/test
这个目录存放所有的测试代码。
所有测试代码都应为 JUnit 测试类,并且可以直接调用通用的测试工具方法。
testdata/
这个目录存放包含完整测试用例集的网页文件。
测试用例示例:
public class TheLotteryBothDivsTest {
private final double DELTA = 1e-9;
@Test
public void find() throws Exception {
List<ParsedResultMeta> parsedResultMeta = TestUtils.parseTestData(TestUtils.getFileContentById(TheLotteryBothDivs.id));
for (int i = 0; i < parsedResultMeta.size(); i ++) {
assertEquals(TestUtils.getFailureMessage(i, parsedResultMeta.size(), BuiltinParser.parseToString(parsedResultMeta.get(i).getTestArgs())),
BuiltinParser.parseToDouble(parsedResultMeta.get(i).getExpectedResult()),
new TheLotteryBothDivs().find(BuiltinParser.parseToStringArray(parsedResultMeta.get(i).getTestArgs())), DELTA);
}
System.out.println(TestUtils.getSuccessMessage(parsedResultMeta.size()));
}
}