Skip to content

Commit bb5fb3d

Browse files
committed
feat: add hw4
1 parent 7a945c9 commit bb5fb3d

File tree

7 files changed

+2766
-0
lines changed

7 files changed

+2766
-0
lines changed

hw4/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
reports/
2+
.stryker-tmp/

hw4/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# HW4
2+
3+
You can install package through:
4+
5+
```shell
6+
npm i
7+
```
8+
9+
After finish your test code in `tests/calculator_test.js`, you can run `Stryker` by:
10+
11+
```shell
12+
npm run mutate
13+
```
14+
15+
to get your mutation testing result.

hw4/package-lock.json

Lines changed: 2661 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hw4/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "hw4",
3+
"version": "1.0.0",
4+
"description": "software testing hw4",
5+
"main": "src/calculator.js",
6+
"scripts": {
7+
"test": "node --test",
8+
"mutate": "npx stryker run"
9+
},
10+
"dependencies": {},
11+
"devDependencies": {
12+
"@stryker-mutator/core": "^8.2.6"
13+
},
14+
"license": "MIT"
15+
}

hw4/src/calculator.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Calculator {
2+
static main(month1, day1, month2, day2, year) {
3+
if (month1 < 1 || month1 > 12) {
4+
throw new Error("invalid month1");
5+
}
6+
if (month2 < 1 || month2 > 12) {
7+
throw new Error("invalid month2");
8+
}
9+
if (day1 < 1 || day1 > 31) {
10+
throw new Error("invalid day1");
11+
}
12+
if (day2 < 1 || day2 > 31) {
13+
throw new Error("invalid day2");
14+
}
15+
if (year < 1 || year > 10000) {
16+
throw new Error("invalid year");
17+
}
18+
if (month1 === month2 && day1 > day2) {
19+
throw new Error("day1 must be less than day2 if month1 is equal to month2");
20+
}
21+
if (month1 > month2) {
22+
throw new Error("month1 must be less than month2");
23+
}
24+
25+
return this.#calculate(month1, day1, month2, day2, year);
26+
}
27+
28+
static #calculate(month1, day1, month2, day2, year) {
29+
let numDays;
30+
31+
if (month2 === month1) {
32+
numDays = day2 - day1;
33+
} else {
34+
// ignore 0 index
35+
let daysIn = [0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
36+
if (this.#isLeapYear(year))
37+
daysIn[2] = 28;
38+
else
39+
daysIn[2] = 29;
40+
41+
numDays = day2 + (daysIn[month1] - day1);
42+
43+
for (let i = month1 + 1; i <= month2 - 1; i++)
44+
numDays += daysIn[i];
45+
}
46+
return numDays;
47+
}
48+
49+
static #isLeapYear(year) {
50+
return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
51+
}
52+
}
53+
54+
module.exports = Calculator;

hw4/stryker.config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
3+
"_comment": "This config was generated using 'stryker init'. Please take a look at: https://stryker-mutator.io/docs/stryker-js/configuration/ for more information.",
4+
"packageManager": "npm",
5+
"reporters": [
6+
"html",
7+
"clear-text",
8+
"progress"
9+
],
10+
"testRunner": "command",
11+
"testRunner_comment": "Take a look at (missing 'homepage' URL in package.json) for information about the command plugin.",
12+
"coverageAnalysis": "off"
13+
}

hw4/tests/calculator_test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const assert = require('assert');
2+
const { test } = require('node:test');
3+
4+
const Calculator = require('../src/calculator');
5+
6+
// TODO: write your test cases here to kill mutants

0 commit comments

Comments
 (0)