Skip to content

Commit 85df108

Browse files
authored
Merge branch '111550064' into lab6
2 parents 6e9f6a3 + cb9a14f commit 85df108

5 files changed

Lines changed: 158 additions & 31 deletions

File tree

lab0/lab0.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello world!");

lab1/main_test.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,43 @@ const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
6+
const myClass = new MyClass();
7+
const student1 = new Student();
8+
student1.setName("Alice");
9+
10+
const studentId = myClass.addStudent(student1);
11+
assert.strictEqual(studentId, 0, "First student ID should be 0");
12+
13+
14+
const invalidStudent = myClass.addStudent({});
15+
assert.strictEqual(invalidStudent, -1, "Adding a non-Student object should return -1");
816
});
917

1018
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
19+
const myClass = new MyClass();
20+
const student1 = new Student();
21+
student1.setName("Alice");
22+
myClass.addStudent(student1);
23+
24+
assert.strictEqual(myClass.getStudentById(0).getName(), "Alice", "Should return Alice");
25+
assert.strictEqual(myClass.getStudentById(2), null, "ID out of range should return null");
1326
});
1427

1528
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
29+
const student = new Student();
30+
31+
student.setName("Bob");
32+
assert.strictEqual(student.getName(), "Bob", "Name should be Bob");
33+
34+
student.setName(12345);
35+
assert.strictEqual(student.getName(), "Bob", "Setting name to non-string should not change it");
1836
});
1937

2038
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
39+
const student = new Student();
40+
41+
assert.strictEqual(student.getName(), "", "Default name should be an empty string");
42+
43+
student.setName("David");
44+
assert.strictEqual(student.getName(), "David", "getName() should return 'David'");
45+
});

lab3/main_test.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,63 @@ const {describe, it} = require('node:test');
22
const assert = require('assert');
33
const { Calculator } = require('./main');
44

5-
// TODO: write your tests here
5+
6+
const calc = new Calculator();
7+
8+
describe('Calculator', () => {
9+
// exp() 正確結果
10+
it('exp() should return correct results', () => {
11+
const cases = [
12+
{ param: 0, expected: Math.exp(0) },
13+
{ param: 1, expected: Math.exp(1) },
14+
{ param: -1, expected: Math.exp(-1) },
15+
];
16+
for (const c of cases) {
17+
assert.strictEqual(calc.exp(c.param), c.expected);
18+
}
19+
});
20+
21+
// exp() 錯誤處理 unsupported operand
22+
it('exp() should throw unsupported operand type', () => {
23+
const inputs = [Infinity, -Infinity, NaN];
24+
for (const x of inputs) {
25+
assert.throws(() => calc.exp(x), { message: 'unsupported operand type' });
26+
}
27+
});
28+
29+
// exp() overflow
30+
it('exp() should throw overflow for large number', () => {
31+
assert.throws(() => calc.exp(1000), { message: 'overflow' });
32+
});
33+
34+
// log() 正確結果
35+
it('log() should return correct results', () => {
36+
const cases = [
37+
{ param: 1, expected: Math.log(1) },
38+
{ param: Math.E, expected: Math.log(Math.E) },
39+
{ param: 10, expected: Math.log(10) },
40+
];
41+
for (const c of cases) {
42+
assert.strictEqual(calc.log(c.param), c.expected);
43+
}
44+
});
45+
46+
// log() 錯誤處理 unsupported operand
47+
it('log() should throw unsupported operand type', () => {
48+
const inputs = [Infinity, -Infinity, NaN];
49+
for (const x of inputs) {
50+
assert.throws(() => calc.log(x), { message: 'unsupported operand type' });
51+
}
52+
});
53+
54+
// log() math domain error (1)
55+
it('log() should throw math domain error (1) for 0', () => {
56+
assert.throws(() => calc.log(0), { message: 'math domain error (1)' });
57+
});
58+
59+
// log() math domain error (2)
60+
it('log() should throw math domain error (2) for negative input', () => {
61+
assert.throws(() => calc.log(-3), { message: 'math domain error (2)' });
62+
});
63+
});
64+

lab4/main_test.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
11
const puppeteer = require('puppeteer');
22

33
(async () => {
4-
// Launch the browser and open a new blank page
5-
const browser = await puppeteer.launch();
6-
const page = await browser.newPage();
7-
8-
// Navigate the page to a URL
9-
await page.goto('https://pptr.dev/');
10-
11-
// Hints:
12-
// Click search button
13-
// Type into search box
14-
// Wait for search result
15-
// Get the `Docs` result section
16-
// Click on first result in `Docs` section
17-
// Locate the title
18-
// Print the title
19-
20-
// Close the browser
21-
await browser.close();
22-
})();
4+
5+
const browser = await puppeteer.launch({
6+
headless: true, // ← 顯示瀏覽器畫面
7+
slowMo: 100 // ← 每步操作加 100ms 延遲,方便觀察
8+
});
9+
10+
11+
const page = await browser.newPage();
12+
await page.goto('https://pptr.dev/');
13+
await page.setViewport({ width: 1280, height: 800 });
14+
15+
await page.waitForSelector('button.DocSearch-Button');
16+
await page.click('button.DocSearch-Button');
17+
18+
await page.waitForSelector('input.DocSearch-Input');
19+
await page.type('input.DocSearch-Input', 'andy popoo');
20+
21+
const sections = await page.$$('section.DocSearch-Hits');
22+
let clicked = false;
23+
24+
for (const section of sections) {
25+
// 抓出分類名稱
26+
const label = await section.$('.DocSearch-Hit-source');
27+
const labelText = label
28+
? await label.evaluate(el => el.textContent.trim())
29+
: '';
30+
31+
// 如果是 Documentation 區塊
32+
if (labelText === 'ElementHandle') {
33+
const firstLink = await section.$('li.DocSearch-Hit a');
34+
if (firstLink) {
35+
await firstLink.click();
36+
37+
// 給點時間等待跳轉或內容更新(因為可能是錨點 #)
38+
await new Promise(resolve => setTimeout(resolve, 1000));
39+
40+
const title = await page.$eval('h1', el => el.textContent.trim());
41+
console.log(title);
42+
43+
44+
clicked = true;
45+
break;
46+
} else {
47+
console.warn("⚠️ Documentation 區有資料,但沒有可點擊連結");
48+
}
49+
}
50+
}
51+
await browser.close();
52+
})();
53+

lab5/antiasan.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
2+
#include <stdint.h>
13
#include <string.h>
4+
#include <stdio.h>
5+
6+
void antiasan(unsigned long addr) {
7+
const unsigned long kShadowOffset = 0x7fff8000;
8+
9+
unsigned long shadow_start = (addr + 0x87 >> 3) + kShadowOffset;
10+
unsigned long shadow_end = ((addr + 0x87 + 0x58) >> 3) + kShadowOffset;
11+
12+
13+
if (1) {
14+
*(char *)shadow_end = 0x00;
15+
shadow_end = ((addr + 0x87 + 0x60) >> 3) + kShadowOffset;
16+
*(char *)shadow_end = 0x00;
17+
}
218

3-
void antiasan(unsigned long addr)
4-
{
519

620
}

0 commit comments

Comments
 (0)