テストケース練習:シンプル純粋関数②
code: case2.js
/**
* ルール:
* - 10個以上購入で追加5%オフ
* - 最終的な割引率は最大50%まで
*/
function calculateDiscountedPrice(price, discountPercent, quantity) {
if (price < 0 || discountPercent < 0 || discountPercent > 100 || quantity < 1) {
throw new Error('Invalid input');
}
let totalDiscount = discountPercent;
// 数量割引
if (quantity >= 10) {
totalDiscount += 5;
}
// 最大割引率の制限
totalDiscount = Math.min(totalDiscount, 50);
const discountedPrice = price * (1 - totalDiscount / 100);
const total = discountedPrice * quantity;
return Math.floor(total);
}
機械的グルーピング
price = -1, 0, 1000
discount = -1, 0, 30, 49/50/51, 101
quantity = -1, 0, 9/10/11
テストケース
外形
異常
price = -1
discount = -1
discount = 101
quantity = 0
quantity = -1
正常
table: price以外固定
3ケース
table: discount以外固定
price discount quantity 期待
1000 0 5 5000
1000 1 5 4950
1000 30 5 3500
1000 49 5 2550
1000 50 5 2500
1000 51 5 2500
table: quantity以外固定
4ケース
table: 結果が端数になり得て、切り捨てするパターン
price discount quantity 期待
333 10 1 299
table: 組み合わせでdiscountが50%を超えそうなパターン
price discount quantity 期待
1000 45 10 5000
1000 46 10 5000
内部
なし
気づき