Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit 4adef22

Browse files
committed
hw1: update cases, manual
1 parent 5a8b6d5 commit 4adef22

File tree

102 files changed

+838
-64
lines changed

Some content is hidden

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

102 files changed

+838
-64
lines changed
Binary file not shown.

ConWaysGame/manual.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

ConWaysGame/manuals/manual.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Manual
2+
3+
This is the manual of Conway Game of Life simulator usage.
4+
5+
## Inputs
6+
7+
In this section, program usage will be presented, as well as input file format.
8+
9+
### Command line
10+
11+
Please refer to `readme.md`, looking for commands for your OS. Here we will explain each argument and its usage.
12+
13+
#### `<input>`
14+
15+
The path to the seed file. The file format is defined below.
16+
17+
##### Input File format
18+
19+
- The first line contains, in order, the number of rows, and the number of columns in the grid, separated by a comma and a space. E.g., 5, 7 means that there are 5 rows and 7 columns in the grid. There are no spaces after the number of columns.
20+
- All lines starting from the second one contain the state of one row in the grid, starting with row #0. The state of each cell is designated with either 0 (cell is dead) or 1 (cell is alive). The states of individual cells are separated by a comma followed by a space (there is no comma and/or space after the last 0 or 1 in the line). For example, if the third line of the file (the second line of the grid part of the file) is 0, 0, 0, 1, 0, 0, 0 it corresponds to the following cell states in row[1]: row[1][0], row[1][1], row[1][2], row[1][4], row[1][5], and row[1][6] are dead, and row[1][3] is alive.
21+
- All lines end with the new line character.
22+
23+
###### Example
24+
25+
```
26+
5, 7
27+
0, 0, 0, 0, 0, 0, 0
28+
0, 0, 0, 1, 0, 0, 0
29+
0, 0, 0, 1, 1, 1, 1
30+
1, 0, 0, 0, 1, 1, 1
31+
1, 1, 1, 1, 1, 1, 1
32+
```
33+
34+
You may find more examples under `tests/` folder.
35+
36+
#### `<output>`
37+
38+
The path to the directory where results should be stored. If the directory does not exist, program will generate all necessary parent directories. Each file inside the directory will follow the following format:
39+
40+
```
41+
<seedFilename>.<currentStep>.txt
42+
```
43+
44+
##### Output File Format
45+
46+
The output file example may be found at `tests/result/`. The format of the file is the same as the input file.
47+
48+
#### `<step>`
49+
50+
The number of step the simulation should run. This should be a positive integer.
51+
52+
## Errors
53+
54+
Here we have listed some possible errors when using the program and what could you do when you have encountered them.
55+
We have also provided some good and bad examples in next section.
56+
57+
### `Err: [io] Output path must be a directory`
58+
59+
The specified `<output>` exists and is not a directory. Please specify another available location.
60+
61+
### `Err: [io] Failed to create output directory`
62+
63+
The specified `<output>` does not exist, but program failed to create corresponded parents directories. Sometimes it may be system error, but please ensure the path enter is valid for your current OS.
64+
65+
### `Err: [input] step x is invalid`
66+
67+
This happened when step is less than 0. Please specify a number equal to or larger than 0.
68+
69+
### `An error occur while processing files: `
70+
71+
An `IOException` occurs. Possibly due to non-existence of a specified file, or other system issue. Please read stack for more info.
72+
73+
### `Err: [input] unable to parse "x" as integer`
74+
75+
Either seed file row/col or step is not a valid integer representation. Please double check your input.
76+
77+
### `Err: [seedFile] unable to identify row/col of the map`
78+
79+
The first line of the seed file doesn't follow the `row, col` format and can't be parsed.
80+
81+
### `Err: [seedFile] Invalid row/col, must be positive integer larger than 2`
82+
83+
Row or column integer representation in the seedFile is invalid.
84+
85+
### `Err: [seedFile] Unexpected col length at row index x`
86+
87+
When reading seed file, a row is missing some cell representation. Please check if your file is corrupted.
88+
89+
### `Err: [seedFile] Unexpected cell type x`
90+
91+
When reading a specific cell in the seed file, program cannot recognize its status. Please refer to file format section for the correct types.
92+
93+
### `Cannot invoke "String.split(String)" because "<local1>" is null`
94+
95+
Seed file is possibly empty. Please check if correct seed file is given.
96+
97+
## Examples Args
98+
99+
Please append the following with your OS command specify in `readme.md`
100+
101+
```bash
102+
tests/example1.txt tests/result/example1 21
103+
tests/example2.txt tests/result/example2 5
104+
tests/example3.txt tests/result/example3 50
105+
```
106+
107+
### Bad Examples
108+
109+
```bash
110+
... tests/invalid1.txt tests/result/example1 1
111+
Err: [seedFile] Invalid row/col, must be positive integer larger than 2
112+
... tests/invalid2.txt tests/result/example1 1
113+
Err: [seedFile] Invalid row/col, must be positive integer larger than 2
114+
... tests/invalid3.txt tests/result/example1 1
115+
Err: [seedFile] Unexpected cell type 2
116+
... tests/invalid4.txt tests/result/example1 1
117+
Err: [seedFile] Unexpected col length at row index 1
118+
... tests/invalid5.txt tests/result/example1 1
119+
Cannot invoke "String.split(String)" because "<local1>" is null
120+
... tests/example3.txt tests/result/example1 -1
121+
Err: [input] step -1 is invalid
122+
```

ConWaysGame/manuals/tests.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Tests
2+
3+
The following methods are used when testing the program.
4+
5+
## United test
6+
7+
A Java framework, JUnit5, has been used within the process. You may consult to `com.luox6.conway.test` for the test cases written.
8+
9+
### Included sections
10+
11+
- getters/setters
12+
- class constructor
13+
- `getCell` with wrapping
14+
- `neighbour` live cell counts with wrapping
15+
- `toString` format test
16+
17+
The above have tested class's construction, mutation, and expected string representation.
18+
19+
### Run Unit Test
20+
21+
You may use your favourite IDE integration with JUnit. Please consult [JUnit manual](https://junit.org/junit5/docs/current/user-guide/#running-tests)
22+
However, if you would like to use `ConsoleLauncher`, after compiling the program following the instruction in `readme.md`, try the following command for unit test result.
23+
24+
```bash
25+
# The following bash command should run at project root directory
26+
$ java -jar deps/junit-platform-console-standalone-1.7.0.jar -cp ./src --scan-class-path
27+
```
28+
29+
Success Output:
30+
31+
```
32+
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
33+
34+
35+
├─ JUnit Jupiter ✔
36+
│ └─ ConwayMapTest ✔
37+
│ ├─ getMapRowLength() ✔
38+
│ ├─ isValidCoordinate() ✔
39+
│ ├─ getMapColLength() ✔
40+
│ ├─ testToString() ✔
41+
│ ├─ getCell() ✔
42+
│ └─ neighbours() ✔
43+
└─ JUnit Vintage ✔
44+
45+
Test run finished after 89 ms
46+
[ 3 containers found ]
47+
[ 0 containers skipped ]
48+
[ 3 containers started ]
49+
[ 0 containers aborted ]
50+
[ 3 containers successful ]
51+
[ 0 containers failed ]
52+
[ 6 tests found ]
53+
[ 0 tests skipped ]
54+
[ 6 tests started ]
55+
[ 0 tests aborted ]
56+
[ 6 tests successful ]
57+
[ 0 tests failed ]
58+
```
59+
60+
## Manual Test
61+
62+
However, the unit test cannot cover some corner cases, e.g.:
63+
64+
- Seed file parsing
65+
- IO/Input validation
66+
- ...
67+
68+
In order to test these cases, here we have some crafted files, which are also listed in `manual->Example Args` section.
69+
These cases have covered the following topics:
70+
71+
- example1.txt -> Given test file from homework description
72+
- example2.txt -> Wrapping, neighbour counts, simple, mostly static case
73+
- example3.txt -> Wrapping, neighbour counts, complex moving case
74+
- invalid1.txt -> Seed file with < 2 rows
75+
- invalid2.txt -> Seed file with < 2 columns
76+
- invalid3.txt -> Seed file with undefined char
77+
- invalid4.txt -> Seed file with inconsistent rows
78+
- invalid5.txt -> empty seed file
79+
80+
Commands to run these examples have also listed in the manual. It is believed that these cases cover most of the common mistakes that users would make.
81+
82+
Game rules are also tested at `example2.txt` and `example3.txt` by stepping through the generated files.
83+
84+
Some examples inspired by [wikipedia](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life).

ConWaysGame/readme.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
1+
# Conway Game of Life
2+
3+
## Environment
4+
5+
Code and scripts are tested under with following settings.
6+
7+
### Linux
8+
9+
#### Distribution
10+
11+
NixOS https://nixos.org
12+
13+
```bash
14+
$ uname -a
15+
Linux home-thinkcenter 5.4.90 #1-NixOS SMP Sun Jan 17 13:05:38 UTC 2021 x86_64 GNU/Linux
16+
```
17+
18+
#### JDK
19+
20+
```bash
21+
$ javac --version
22+
javac 15.0.1
23+
$ java --version
24+
openjdk 15.0.1 2020-10-20
25+
OpenJDK Runtime Environment (build 15.0.1+0-adhoc..jdk15u-jdk-15.0.1-ga)
26+
OpenJDK 64-Bit Server VM (build 15.0.1+0-adhoc..jdk15u-jdk-15.0.1-ga, mixed mode, sharing)
27+
```
28+
29+
### Windows
30+
31+
Virtualbox (Oracle VM VirtualBox VM Runner v6.1.16) used.
32+
33+
https://modern.ie Image from https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/
34+
35+
#### Distribution
36+
37+
![](./windows.png)
38+
39+
#### JDK
40+
41+
```powershell
42+
PS C:\Users\IEUser> javac --version
43+
javac 15.0.2
44+
PS C:\Users\IEUser> java --version
45+
java 15.0.2 2021-01-19
46+
Java(TM) SE Runtime Environment (build 15.0.2+7-27)
47+
Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)
48+
```
49+
50+
#### Note for javadoc
51+
52+
By default, installation from Java won't add javadoc to path. Please add Java bin path to `Control Panel -> System and Security -> System -> Advance System Setting -> Environment Variables -> Path` with your install path so the script can work properly.
53+
54+
![](./windows_path.png)
55+
56+
57+
###
58+
159
## Usage
260

361
The following command are happened under project root directory
@@ -18,14 +76,21 @@ sh scripts/run.sh <input> <output> <step>
1876

1977
### Windows
2078

79+
Open the powershell and nevigate to the project root directory
80+
2181
#### Compile
2282

2383
```cmd
24-
scripts/compile.cmd
84+
.\scripts\compile.cmd
2585
```
2686

2787
#### Run
2888

2989
```cmd
30-
scripts/run.cmd <input> <output> <step>
90+
.\scripts\run.cmd <input> <output> <step>
3191
```
92+
93+
94+
## Others
95+
96+
Please consult `manuals/` for more info

ConWaysGame/scripts/compile.cmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@ECHO OFF
22
echo "====== GameOfLife Script on Windows ======"
33
echo "Compiling..."
4-
javac -cp '.\deps\*' .\src\**\*.java
5-
echo "Compilaton success. Generating Javadoc..."
4+
javac -cp '.\deps\*' -sourcepath src src/com/luox6/conway/Main.java
5+
echo "Compilaton complete. Generating Javadoc..."
66
cd .\src
7-
javadoc -encoding UTF-8 com.luox6.conway -d ..\doc
7+
javadoc -encoding UTF-8 com.luox6.conway -d ..\docs

ConWaysGame/scripts/compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
echo "====== GameOfLife Script on Linux ======"
22
echo "[MSG] Compiling..."
3-
javac -cp './deps/*' ./src/**/*.java
3+
javac -cp './deps/*' -sourcepath src src/com/luox6/conway/Main.java
44
echo "[MSG] Compilaton success. Generating Javadoc..."
55
cd ./src
66
javadoc -encoding UTF-8 com.luox6.conway -d ../docs

ConWaysGame/scripts/run.cmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
@ECHO OFF
2-
echo "Start running..."
32
java -classpath .\src com.luox6.conway.Main %1 %2 %3

ConWaysGame/scripts/run.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
echo "Start running..."
21
java -classpath ./src com.luox6.conway.Main "$@"

ConWaysGame/src/com/luox6/conway/ConwayMap.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Integer getCell(int row, int col) {
117117
// However, if it is less than 0, we need to have the reminder of our length and then
118118
// add the length back. Not the index.
119119
final BiFunction<Integer, Integer, Integer> wrapper =
120-
(k, v) -> k < 0 ? k % v + v : k % (v - 1);
120+
(k, v) -> k < 0 ? k % v + v : k % v;
121121
int realRow = wrapper.apply(row, this.row);
122122
int realCol = wrapper.apply(col, this.col);
123123

@@ -177,7 +177,11 @@ private void step(int row, int col, int[][] changed) {
177177
* Move game forward by calculating each coordinates
178178
*/
179179
public void tick() {
180-
int[][] changed = map.clone();
180+
// Clone the array for record
181+
int[][] changed = new int[row][col];
182+
for (int i = 0; i < row; i++) {
183+
changed[i] = map[i].clone();
184+
}
181185

182186
for (int i = 0; i < row; i++) {
183187
for (int j = 0; j < col; j++) {
@@ -208,13 +212,17 @@ public static int charToInt(char cell) {
208212
@Override
209213
public String toString() {
210214
StringBuilder x = new StringBuilder();
211-
x.append("ConwayGameOfLife with row " + row + ", col " + col + ", current tick " + tick + ".\n");
212-
x.append("Map: \n");
215+
x.append(row).append(", ").append(col).append("\n");
213216
for (int i = 0; i < row; i++) {
214-
x.append(Arrays.toString(map[i]));
215-
x.append("\n");
217+
for (int j = 0; j < col - 1; j++) {
218+
x.append(map[i][j]).append(", ");
219+
}
220+
x.append(map[i][col - 1]).append("\n");
216221
}
217222

223+
// Remove last newline
224+
x.setLength(x.length() - 1);
225+
218226
return x.toString();
219227
}
220228
}

0 commit comments

Comments
 (0)