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 368023d

Browse files
Add files via upload
1 parent 316bf66 commit 368023d

File tree

67 files changed

+2339
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2339
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part02_15.CountingToHundred</artifactId>
6+
<name>Part02_15.CountingToHundred</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>fi.helsinki.cs.tmc</groupId>
25+
<artifactId>edu-test-utils</artifactId>
26+
<version>0.4.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.0</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.6.0</version>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>tmc</id>
49+
<name>TMC repo</name>
50+
<url>https://maven.mooc.fi/releases</url>
51+
<releases>
52+
<enabled>true</enabled>
53+
</releases>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>tmc</id>
63+
<name>TMC repo</name>
64+
<url>https://maven.mooc.fi/releases</url>
65+
<releases>
66+
<enabled>true</enabled>
67+
</releases>
68+
<snapshots>
69+
<enabled>true</enabled>
70+
</snapshots>
71+
</pluginRepository>
72+
</pluginRepositories>
73+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import java.util.Scanner;
3+
4+
public class CountingToHundred {
5+
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
9+
int number = scanner.nextInt();
10+
11+
for (int i = number; i <= 100; i++) {
12+
System.out.println(i);
13+
}
14+
}
15+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
5+
import java.lang.reflect.Method;
6+
import org.junit.*;
7+
import static org.junit.Assert.*;
8+
9+
@Points("02-15")
10+
public class CountingToHundredTest {
11+
12+
@Rule
13+
public MockStdio io = new MockStdio();
14+
15+
@Test(timeout = 1000)
16+
public void test1() {
17+
verifyOrderOfOutput("99\n", "99\n100\n", "\\s*99\\s*100\\s*", "98", "101");
18+
}
19+
20+
@Test(timeout = 1000)
21+
public void test2() {
22+
verifyOrderOfOutput("-3\n", "3-\n-2\n...many numbers...\n98\n99\n100\n", "\\s*-3\\s*-2\\s*-1\\s*[0-98\\s]*\\s*99\\s*100\\s*", "-4", "101");
23+
}
24+
25+
public void test(String input, String expected, String... notExpected) {
26+
27+
int oldOut = io.getSysOut().length();
28+
io.setSysIn(input);
29+
callMain(CountingToHundredTest.class);
30+
String out = io.getSysOut().substring(oldOut);
31+
32+
assertTrue("When input was:\n" + input + ", the following output was expected:\n" + expected + "\nNow the output was:\n" + out, out.contains(expected));
33+
for (String unexpected : notExpected) {
34+
assertFalse("When input was:\n" + input + ", the output wasn't expected to contain:\n" + unexpected + "", out.contains(unexpected));
35+
}
36+
}
37+
38+
public void verifyOrderOfOutput(String input, String expectationExplanation, String expectedRegex, String... notExpected) {
39+
40+
int oldOut = io.getSysOut().length();
41+
io.setSysIn(input);
42+
callMain(CountingToHundred.class);
43+
String out = io.getSysOut().substring(oldOut);
44+
45+
assertTrue("When input was:\n" + input + ", the expected output was:\n" + expectationExplanation + "\nNow the output was:\n" + out, out.matches(expectedRegex));
46+
for (String unexpected : notExpected) {
47+
assertFalse("When input was:\n" + input + ", the output wasn't expected to contain:\n" + unexpected + "", out.contains(unexpected));
48+
}
49+
}
50+
51+
private void callMain(Class kl) {
52+
try {
53+
kl = ReflectionUtils.newInstanceOfClass(kl);
54+
String[] t = null;
55+
String x[] = new String[0];
56+
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
57+
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
58+
} catch (Throwable e) {
59+
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
60+
+ "or your program crashed because of an exception. More info: " + e);
61+
}
62+
}
63+
64+
}
802 Bytes
Binary file not shown.
3.77 KB
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part02_16.FromWhereToWhere</artifactId>
6+
<name>Part02_16.FromWhereToWhere</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>fi.helsinki.cs.tmc</groupId>
25+
<artifactId>edu-test-utils</artifactId>
26+
<version>0.4.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.0</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.6.0</version>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>tmc</id>
49+
<name>TMC repo</name>
50+
<url>https://maven.mooc.fi/releases</url>
51+
<releases>
52+
<enabled>true</enabled>
53+
</releases>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>tmc</id>
63+
<name>TMC repo</name>
64+
<url>https://maven.mooc.fi/releases</url>
65+
<releases>
66+
<enabled>true</enabled>
67+
</releases>
68+
<snapshots>
69+
<enabled>true</enabled>
70+
</snapshots>
71+
</pluginRepository>
72+
</pluginRepositories>
73+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import java.util.Scanner;
3+
4+
public class FromWhereToWhere {
5+
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
9+
// Write your program here
10+
System.out.println("Where to?");
11+
int numberTo = scanner.nextInt();
12+
13+
System.out.println("Where from?");
14+
int numberFrom = scanner.nextInt();
15+
16+
for (int i = numberFrom; i <= numberTo; i++) {
17+
System.out.println(i);
18+
}
19+
}
20+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
import org.junit.*;
8+
import static org.junit.Assert.*;
9+
10+
@Points("02-16.2")
11+
public class WhereFromTest {
12+
13+
@Rule
14+
public MockStdio io = new MockStdio();
15+
16+
@Test
17+
public void test() {
18+
int[][] pairs = {{1, 1}, {12, 8}, {50, 100}, {-2,2}};
19+
for (int[] pair : pairs) {
20+
test(pair);
21+
}
22+
}
23+
24+
private void test(int[] pair) {
25+
io.setSysIn(pair[0] + "\n" + pair[1] + "\n");
26+
int len = io.getSysOut().length();
27+
28+
ReflectionUtils.newInstanceOfClass(FromWhereToWhere.class);
29+
FromWhereToWhere.main(new String[0]);
30+
String output = io.getSysOut().substring(len);
31+
32+
output = output.replaceAll("[^-\\d]+", " ").trim();
33+
String[] lines = output.split("\\s+");
34+
int linesInOutput = (lines.length == 1 && lines[0].isEmpty()) ? 0 : lines.length;
35+
36+
int linesCount;
37+
if(pair[0] < pair[1]) {
38+
linesCount = 0;
39+
} else {
40+
linesCount = pair[0] - pair[1] + 1;
41+
}
42+
43+
if (linesCount != linesInOutput) {
44+
String numbersCount = (linesCount == 1) ? "number": "numbers";
45+
fail("With the input " + pair[0] + ", " + pair[1] + " output should contain " + linesCount + " " + numbersCount + ", now it contained " + linesInOutput);
46+
}
47+
48+
if(linesCount == 0) {
49+
return;
50+
}
51+
52+
int firstNumber = Integer.valueOf(lines[0]);
53+
if(firstNumber != pair[1]) {
54+
fail("With the input " + pair[0] + ", " + pair[1] + " the first printed number should be " + pair[1] + ", now it was " + firstNumber);
55+
}
56+
57+
int lastNumber = getLastNumber(output);
58+
if(lastNumber != pair[0]) {
59+
fail("With the input " + pair[0] + ", " + pair[1] + " the last printed number should be " + pair[0] + ", now it was " + lastNumber);
60+
}
61+
}
62+
63+
private static int getLastNumber(String inputStr) {
64+
String patternStr = "(?s).*?(-?\\d+)\\s*$";
65+
Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);
66+
assertTrue("The output should be a number.", matcher.find());
67+
68+
int number = Integer.valueOf(matcher.group(1));
69+
return number;
70+
}
71+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
2+
import fi.helsinki.cs.tmc.edutestutils.Points;
3+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
4+
import java.util.NoSuchElementException;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
import static org.junit.Assert.assertTrue;
8+
import static org.junit.Assert.fail;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
12+
@Points("02-16.1")
13+
public class WhereToTest {
14+
15+
@Rule
16+
public MockStdio io = new MockStdio();
17+
18+
@Test
19+
public void verifyOutput() {
20+
io.setSysIn("3\n");
21+
22+
ReflectionUtils.newInstanceOfClass(FromWhereToWhere.class);
23+
try {
24+
FromWhereToWhere.main(new String[0]);
25+
} catch (NoSuchElementException e) {
26+
return;
27+
}
28+
29+
String output = io.getSysOut();
30+
output = output.replaceAll("[^\\d]", " ");
31+
output = output.trim();
32+
output = output.replace("1", "");
33+
output = output.replace("2", "");
34+
output = output.replace("3", "");
35+
36+
output = output.trim();
37+
if (!output.isEmpty()) {
38+
fail("When you're printing numbers until 3, you should only print numbers 1, 2, and 3. Now you printed: " + output);
39+
}
40+
}
41+
42+
@Test
43+
public void testi() {
44+
int[] numbers = {1, 50, 100};
45+
for (int number : numbers) {
46+
testaa(number);
47+
}
48+
}
49+
50+
private void testaa(int whereTo) {
51+
io.setSysIn(whereTo + "\n");
52+
53+
ReflectionUtils.newInstanceOfClass(FromWhereToWhere.class);
54+
try {
55+
FromWhereToWhere.main(new String[0]);
56+
} catch (NoSuchElementException e) {
57+
return;
58+
}
59+
60+
int lastNumber = getLastNumber(io.getSysOut(), whereTo);
61+
62+
if (whereTo != lastNumber) {
63+
fail("There should be " + whereTo + " on the last line, now there was " + lastNumber);
64+
}
65+
}
66+
67+
private static int getLastNumber(String inputStr, int last) {
68+
String patternStr = "(?s).*?(\\d+)\\s*$";
69+
Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);
70+
assertTrue("You should print numbers. With the user input "+last+ ", \""+inputStr+"\"" + "was printed", matcher.find());
71+
72+
int number = Integer.valueOf(matcher.group(1));
73+
return number;
74+
}
75+
}
936 Bytes
Binary file not shown.

0 commit comments

Comments
(0)

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