---
# System prepended metadata

title: 作業三

---

# 作業三
## 作業概述
- 本次作業完成 Stress test coverage
    - 包含
        - 12 行程式碼
        - 6 個 test case

## 程式結構分析
### 測試特別結構分析
- 1 到 4 行為條件判斷
    - 檢查輸入是否在上下限
- 5 行爲初始化
    - 初始化需要使用的變數
- 6 到 11 行為迴圈與條件判斷
    - 計算總和與奇偶數
- 12 行爲回傳
    - 回傳計算結果

### 程式碼
```python
# 計算數字範圍內的總和與奇偶數：
MIN = 1
MAX = 100

def sum_numbers(number):
    # 檢查輸入是否在上下限
    if not isinstance(number, int):         # 第一行
        return                              # 第二行
    if number < MIN or number > MAX:        # 第三行
        return                              # 第四行

    # 初始化
    total = count_even = count_odd = 0      # 第五行

    # 計算總和與奇偶數
    for i in range(1, number + 1):          # 第六行
        total += i                          # 第七行
        if i % 2 == 0:                      # 第八行
            count_even += 1                 # 第九行
        else:                               # 第十行
            count_odd += 1                  # 第十一行

    # 回傳計算結果
    return {total, count_even, count_odd}   # 第十二行
```

## 範圍分析
- 上下限
    - 下限：1
    - 上限：100
- 型態
    - 整數

## 測試案例
| Test Case 1 | Result |
| -------- | -------- |
| Input values          | number=0 |
| Expected result       | None |
| Test program's result | None |
| Criteria analysis (Stress Coverage)     | 超出下限 |

| Test Case 2 | Result |
| -------- | -------- |
| Input values          | number=1 |
| Expected result       | 1 |
| Test program's result | 1 |
| Criteria analysis (Stress Coverage)     | 正好下限 |

| Test Case 3 | Result |
| -------- | -------- |
| Input values          | number=101 |
| Expected result       | None |
| Test program's result | None |
| Criteria analysis (Stress Coverage)     | 超出上限 |

| Test Case 4 | Result |
| -------- | -------- |
| Input values          | number=100 |
| Expected result       | 5050 |
| Test program's result | 5050 |
| Criteria analysis (Stress Coverage)     | 正好下限 |

| Test Case 5 | Result |
| -------- | -------- |
| Input values          | number=5.5 |
| Expected result       | None |
| Test program's result | None |
| Criteria analysis (Stress Coverage)     | 浮點數 |

| Test Case 6 | Result |
| -------- | -------- |
| Input values          | number="String" |
| Expected result       | None |
| Test program's result | None |
| Criteria analysis (Stress Coverage)     | 字串 |


## 測試案例詳述
- 測試案例一
    - 測試輸入為超出下限的狀況
- 測試案例二
    - 測試輸入為正好下限的狀況
- 測試案例三
    - 測試輸入為超出上限的狀況
- 測試案例四
    - 測試輸入為正好上限的狀況
- 測試案例五
    - 測試輸入為浮點數的狀況
- 測試案例六
    - 測試輸入為字串的狀況
- 總結
    - 6 個測試案例
    - 測試了超出上下限、正好上下限及非整數的狀況

## 測試工具展示
- 測試案例一
```python
def test_case_1_below_min(self):
    # 測試案例 1: 超出下限
    number = 0
    expected_result = None
    actual_result = sum_numbers(number)
        
    print(f"測試值: {number}，預期結果: {expected_result}，實際結果: {actual_result}")
    assert actual_result is expected_result
```
![image](https://hackmd.io/_uploads/r1Q29jJeZg.png)

- 測試案例二
```python
def test_case_2_at_min(self):
    # 測試案例 2: 正好下限
    number = 1
    expected_result = {1, 0, 1}
    actual_result = sum_numbers(number)
        
    print(f"測試值: {number}，預期結果: {expected_result}，實際結果: {actual_result}")
    assert actual_result == expected_result
```
![image](https://hackmd.io/_uploads/SyJT5sJgZx.png)

- 測試案例三
```python
def test_case_3_above_max(self):
    # 測試案例 3: 超出上限
    number = 101
    expected_result = None
    actual_result = sum_numbers(number)
        
    print(f"測試值: {number}，預期結果: {expected_result}，實際結果: {actual_result}")
    assert actual_result is expected_result
```
![image](https://hackmd.io/_uploads/SyKpqjJxWg.png)

- 測試案例四
```python
def test_case_4_at_max(self):
    # 測試案例 4: 正好上限
    number = 100
    expected_result = {5050, 50, 50}
    actual_result = sum_numbers(number)
        
    print(f"測試值: {number}，預期結果: {expected_result}，實際結果: {actual_result}")
    assert actual_result == expected_result
```
![image](https://hackmd.io/_uploads/HJIAqikgbe.png)

- 測試案例五
```python
def test_case_5_float_input(self):
    # 測試案例 5: 浮點數輸入
    number = 5.5
    expected_result = None
    actual_result = sum_numbers(number)
        
    print(f"測試值: {number}，預期結果: {expected_result}，實際結果: {actual_result}")
    assert actual_result is expected_result
```
![image](https://hackmd.io/_uploads/HkOWsj1lWl.png)

- 測試案例六
```python
def test_case_6_non_integer(self):
    # 測試案例 6: 字串輸入
    number = "String"
    expected_result = None
    actual_result = sum_numbers(number)
        
    print(f"測試值: {number}，預期結果: {expected_result}，實際結果: {actual_result}")
    assert actual_result is expected_result
```
![image](https://hackmd.io/_uploads/H1rfio1gZe.png)

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

collected 6 items
=== 6 passed in 0.01s ===
```
![image](https://hackmd.io/_uploads/Byd7ijkxZl.png)