---
# System prepended metadata

title: 作業二

---

# 作業二
## 作業概述
- 本次作業完成以下測試
    - Edge Coverage
    - Edge-Pair Coverage
    - Path Coverage
    - 包含
        - 12 行程式碼
        - 3 個 test case

## 程式結構分析
### 主要函式流程圖
#### 主流程
![Main.drawio](https://hackmd.io/_uploads/S1owBilk-e.png)
#### 條件判斷（錯誤檢查）
![Integer.drawio](https://hackmd.io/_uploads/Hyovrigk-g.png)
#### 條件判斷（檢查邊界條件）
![0.drawio](https://hackmd.io/_uploads/Byiwrix1-g.png)
#### 迴圈
![While.drawio](https://hackmd.io/_uploads/SJowHjg1Wg.png)

### 測試特別結構分析
- 1 到 4 行為條件判斷
    - 判斷是否為整數且為非負整數
- 5 到 6 行為條件判斷
    - 判斷輸入值是否是特殊值
- 9 到 11 行為迴圈
    - 計算非特殊輸入值之階乘結果
- 12 行爲回傳
    - 回傳計算結果

### 程式碼
```python
def calculate_factorial(n):
    # 條件判斷 - 錯誤檢查
    is_valid_type = isinstance(n, int)              #第一行
    is_non_negative = n >= 0                        #第二行
    
    if not is_valid_type or not is_non_negative:    #第三行
        return None                                 #第四行

    # 條件判斷 - 檢查邊界條件
    if n == 0:                                      #第五行
        return 1                                    #第六行

    # 初始化變數
    result = 1                                      #第七行
    i = 1                                           #第八行
    
    # 迴圈 - 計算階層
    while i <= n:                                   #第九行
        result = result * i                         #第十行 
        i = i + 1                                   #第十一行
        
    # 返回結果
    return result                                   #第十二行
```

### 

| 邊編號 (Edge) | 邊描述 |
| -------- | -------- |
| E1 | 錯誤發生 |
| E2 | 錯誤未發生 |
| E3 | 邊界條件滿足 |
| E4 | 邊界條件未滿足 |
| E5 | 初始化到迴圈判斷 (第一次檢查)|
| E6 | 迴圈條件滿足 (進入迴圈體) |
| E7 | 迴圈體結束 (返回迴圈判斷) |
| E8 | 迴圈條件不滿足 (跳出迴圈) |

| 區塊編號 (Node) | 區塊描述 |
| -------- | -------- |
| N1 | 錯誤檢查 |
| N2 | 檢查邊界條件並回傳 |
| N3 | 初始化變數 |
| N4 | 迴圈條件|
| N5 | 迴圈體 |
| N6 | 返回結果 |

### 測試案例
| Test Case 1 | Result |
| -------- | -------- |
| Input values          | `n = -5` |
| Expected result       | `None` |
| Test program's result | `None` |
| Criteria analysis (Edge)     | E1 |
| Criteria analysis (Edge-Pair)    | 無 |
| Criteria analysis (Path)    | N1 |

| Test Case 2 | Result |
| -------- | -------- |
| Input values          | `n = 0` |
| Expected result       | `1` |
| Test program's result | `1` |
| Criteria analysis (Edge)     | E2, E3 |
| Criteria analysis (Edge-Pair)    | (E2,E3) |
| Criteria analysis (Path)    | N1 → N2 → N6 |

| Test Case 3 | Result |
| -------- | -------- |
| **Input values**          | `n = 5` |
| **Expected result**       | `120` |
| **Test program's result** | `120` |
| Criteria analysis (Edge)     | E2, E4, E5, E6, E7, E8 |
| Criteria analysis (Edge-Pair)  | (E2,E4),(E4,E5),(E5,E6),(E6,E7),(E7,E6),(E7,E8) |
| Criteria analysis (Path)    | N1 → N2 → N3 → N4 → (N5 → N4) 循環 → N6 |

## 測試案例詳述
- 測試案例一
    - 測試輸入為負數的狀況
    - Edge Coverage：E1
    - Path Coverage：N1
- 測試案例二
    - 測試輸入為特殊值的狀況
    - Edge Coverage：E2, E3
    - Edge-Pair Coverage：(E2,E3) 
    - Path Coverage：N1 → N2 → N6
- 測試案例三
    - 測試非特殊值有效輸入的狀況
    - Edge Coverage：E2, E4, E5, E6, E7, E8
    - Edge-Pair Coverage：(E2,E4),(E4,E5),(E5,E6),(E6,E7),(E7,E6),(E7,E8)
    - Path Coverage：N1 → N2 → N3 → N4 → (N5 → N4) 循環 → N6

## 測試工具展示
- 測試案例一
```python
def test_error_input_negative():
    # 測試案例 1 - 錯誤輸入：負數
    n = -5
    expected = None
    result = calculate_factorial(n)
    print(f"Test case 1 - Negative input: n = {n}, expected = {expected}, result = {result}")
    assert result == expected
```

![CleanShot 2025-11-28 at 01.04.25@2x](https://hackmd.io/_uploads/SJgVn4Z8WZl.png)

- 測試案例二
```python
def test_boundary_input_zero():
    # 測試案例 2 - 特殊值：0
    n = 0
    expected = 1
    result = calculate_factorial(n)
    print(f"Test case 3 - Zero input: n = {n}, expected = {expected}, result = {result}")
    assert result == expected
```

![CleanShot 2025-11-28 at 01.05.41@2x](https://hackmd.io/_uploads/BJXZBbU-bg.png)

- 測試案例三
```python
def test_valid_input_integer():
    # 測試案例 3 - 非特殊值有效輸入：正整數
    n = 5
    expected = 120
    result = calculate_factorial(n)
    print(f"Test case 4 - Positive integer: n = {n}, expected = {expected}, result = {result}")
    assert result == expected
```

![CleanShot 2025-11-28 at 01.06.20@2x](https://hackmd.io/_uploads/S1RGH-U-We.png)

## 測試結果
```
python3 -m pytest

collected 3 items [100%]
=== 3 passed in 0.01s ===
```
![CleanShot 2025-12-12 at 11.03.12@2x](https://hackmd.io/_uploads/rk-zL-Kz-l.png)
