10 Bài Tập C# – Basis Path Testing
Bài 1: Binary Search
using System;
public class BinarySearch {
public static int Search(int[] arr, int key) {
int left = 0, right = arr.Length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
}
Bài 2: Linear Search
using System;
public class LinearSearch {
public static int Search(int[] arr, int key) {
if (arr == null) return -1;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
}
Bài 3: Factorial
using System;
public class Factorial {
public static int Calc(int n) {
if (n < 0) return -1;
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
Bài 4: Prime Check
using System;
public class PrimeCheck {
public static bool IsPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.Sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Bài 5: Find Max in Array
using System;
public class FindMax {
public static int Max(int[] arr) {
if (arr == null || arr.Length == 0) return -1;
int max = arr[0];
for (int i = 1; i < arr.Length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
Bài 6: GCD (Euclid)
using System;
public class GCD {
public static int Calc(int a, int b) {
if (a == 0) return b;
if (b == 0) return a;
while (a != b) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a;
}
}
Bài 7: Password Validator
using System;
public class PasswordValidator {
public static bool Validate(string password) {
if (password == null || password.Length < 6) return false;
bool hasDigit = false, hasUpper = false;
foreach (char c in password) {
if (Char.IsDigit(c)) hasDigit = true;
if (Char.IsUpper(c)) hasUpper = true;
}
return hasDigit && hasUpper;
}
}
Bài 8: ATM Withdrawal
using System;
public class ATM {
public static string Withdraw(int balance, int amount) {
if (amount <= 0) return "Invalid";
if (amount > balance) return "Insuicient";
if (amount % 50 != 0) return "Must be multiple of 50";
balance -= amount;
return "Success";
}
}
Bài 9: Grade Classication
using System;
public class Grade {
public static string Classify(int score) {
if (score < 0 || score > 100) return "Invalid";
else if (score >= 90) return "A";
else if (score >= 75) return "B";
else if (score >= 50) return "C";
else return "F";
}
}
Bài 10: Fibonacci
using System;
public class Fibonacci {
public static int Calc(int n) {
if (n < 0) return -1;
if (n == 0) return 0;
if (n == 1) return 1;
int a = 0, b = 1, c = 0;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}
Với Control Flow Coverage Testing (Statement, Branch, Condition, Path Coverage)
10 Bài tập C# – Control Flow Coverage Testing
Bài 1: User Login System
using System;
public class UserLogin {
public static string Login(string username, string password, bool isLocked) {
if (isLocked) {
return "Account Locked";
}
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) {
return "Missing Credentials";
}
if (username == "admin" && password == "12345") {
return "Welcome Admin";
} else if (username == "user" && password == "user123") {
return "Welcome User";
} else {
return "Invalid Credentials";
}
}
}
Bài 2: Online Shopping Discount
using System;
public class ShoppingDiscount {
public static double Calculate(double total, bool isMember, string coupon) {
double discount = 0;
if (total <= 0) return 0;
if (isMember) {
discount += 0.1;
if (total > 100) discount += 0.05;
} else {
if (total > 200) discount += 0.05;
}
if (!string.IsNullOrEmpty(coupon)) {
if (coupon == "SALE20") discount += 0.2;
else if (coupon == "SALE10") discount += 0.1;
}
return total * (1 - discount);
}
}
Bài 3: Banking Transaction
using System;
public class BankTransaction {
public static string Transfer(int balance, int amount, bool vip) {
if (amount <= 0) return "Invalid";
if (amount > balance) {
if (vip && balance > 0) {
return "Overdraft Approved";
} else {
return "Insuicient Funds";
}
}
if (amount > 10000) {
return "Requires Manager Approval";
}
balance -= amount;
return "Transfer Successful";
}
}
Bài 4: Student Result Evaluation
using System;
public class StudentResult {
public static string Evaluate(int attendance, int midterm, int nalExam) {
if (attendance < 50) return "Fail - Low Attendance";
if (nalExam < 40) return "Fail - Final Too Low";
int total = midterm + nalExam;
if (total >= 85) return "Excellent";
else if (total >= 70) return "Good";
else if (total >= 50) return "Pass";
else return "Fail";
}
}
Bài 5: Online Exam Authentication
using System;
public class ExamAuth {
public static string Authenticate(string studentId, string password, bool faceMatch, bool
otpValid) {
if (string.IsNullOrEmpty(studentId) || string.IsNullOrEmpty(password)) {
return "Missing Info";
}
if (studentId == "S123" && password == "pass123") {
if (!faceMatch) return "Face ID Failed";
if (!otpValid) return "OTP Failed";
return "Access Granted";
}
return "Invalid Credentials";
}
}
Bài 6: E-commerce Payment System
using System;
public class Payment {
public static string ProcessPayment(double amount, string method, bool
balanceSuicient, bool cardValid) {
if (amount <= 0) return "Invalid Amount";
if (method == "CASH") {
return "Payment Success";
} else if (method == "CARD") {
if (!cardValid) return "Card Invalid";
if (amount > 5000) return "Manager Approval Needed";
return "Card Payment Success";
} else if (method == "WALLET") {
if (!balanceSuicient) return "Wallet Insuicient";
return "Wallet Payment Success";
}
return "Unknown Method";
}
}
Bài 7: Loan Approval System
using System;
public class LoanApproval {
public static string Approve(int salary, int creditScore, int loanAmount) {
if (salary < 2000) return "Rejected - Low Income";
if (creditScore < 500) return "Rejected - Bad Credit";
if (loanAmount > salary * 20) {
return "Rejected - Too High Loan";
} else if (loanAmount > salary * 10) {
return "Manual Review";
}
return "Approved";
}
}
Bài 8: Insurance Premium Calculator
using System;
public class Insurance {
public static double CalculatePremium(int age, bool smoker, string health) {
double premium = 500;
if (age < 18) return 0;
if (age > 60) premium += 300;
if (smoker) {
premium += 200;
}
if (health == "Poor") premium += 400;
else if (health == "Average") premium += 200;
else if (health == "Good") premium -= 100;
return premium;
}
}
Bài 9: Online Ticket Booking
using System;
public class TicketBooking {
public static string Book(int seats, bool paymentSuccess, bool vip) {
if (seats <= 0) return "Invalid Seats";
if (!paymentSuccess) return "Payment Failed";
if (seats > 5) {
if (vip) return "Group Booking Approved";
else return "Too Many Seats";
}
return "Booking Successful";
}
}
Bài 10: Shopping Cart Checkout
using System;
public class Checkout {
public static string Complete(int items, double total, bool stockAvailable, bool
addressValid) {
if (items <= 0) return "Empty Cart";
if (!stockAvailable) return "Out of Stock";
if (!addressValid) return "Invalid Address";
if (total > 1000) {
return "Apply Free Shipping";
} else if (total < 50) {
return "Small Order Fee Applied";
}
return "Checkout Success";
}
}
10 Bài tập C# – Data Flow Testing
Bài 1: Student Grade Calculation
using System;
public class StudentGrade {
public static string Calculate(int midterm, int nal) {
int total = 0;
double avg = 0;
total = midterm + nal; // DEF total
avg = total / 2.0; // DEF avg
if (avg >= 8.5) {
return "Excellent"; // USE avg
} else if (avg >= 7.0) {
return "Good"; // USE avg
} else if (avg >= 5.0) {
return "Pass"; // USE avg
} else {
return "Fail"; // USE avg
}
}
}
Bài 2: Simple Interest Calculator
using System;
public class InterestCalc {
public static double Calculate(double principal, double rate, int time) {
double interest = 0;
double amount = 0;
interest = (principal * rate * time) / 100; // DEF interest
amount = principal + interest; // DEF amount
if (amount > 10000) {
return amount * 0.95; // USE amount
}
return amount; // USE amount
}
}
Bài 3: Tax Calculator
using System;
public class TaxCalc {
public static double Compute(double salary) {
double tax = 0;
if (salary <= 5000) {
tax = 0;
} else if (salary <= 10000) {
tax = salary * 0.1;
} else {
tax = salary * 0.2;
}
double netIncome = salary - tax; // DEF netIncome
if (netIncome < 4000) return netIncome; // USE netIncome
return netIncome + 500; // USE netIncome
}
}
Bài 4: Array Sum and Average
using System;
public class ArrayOps {
public static double Average(int[] arr) {
int sum = 0; // DEF sum
double avg = 0; // DEF avg
for (int i = 0; i < arr.Length; i++) {
sum += arr[i]; // USE sum
}
avg = (double)sum / arr.Length; // DEF avg, USE sum
return avg; // USE avg
}
}
Bài 5: Password Strength
using System;
public class PasswordCheck {
public static string Strength(string pwd) {
int score = 0; // DEF score
if (pwd.Length >= 8) score += 2; // DEF score
if (pwd.Any(char.IsDigit)) score += 2;
if (pwd.Any(char.IsUpper)) score += 2;
if (pwd.Any(ch => "!@#$%".Contains(ch))) score += 2;
if (score >= 6) return "Strong"; // USE score
else if (score >= 4) return "Medium";
return "Weak";
}
}
Bài 6: Shopping Cart
using System;
public class ShoppingCart {
public static double Checkout(int items, double price) {
double total = 0;
double discount = 0;
total = items * price; // DEF total
if (total > 500) {
discount = total * 0.1; // DEF discount
} else {
discount = total * 0.05; // DEF discount
}
return total - discount; // USE total, discount
}
}
Bài 7: Temperature Converter
using System;
public class TempConvert {
public static double Convert(double temp, string unit) {
double result = 0;
if (unit == "CtoF") {
result = (temp * 9 / 5) + 32; // DEF result
} else if (unit == "FtoC") {
result = (temp - 32) * 5 / 9; // DEF result
} else {
result = temp; // DEF result
}
return result; // USE result
}
}
Bài 8: ATM Withdrawal
using System;
public class ATM {
public static string Withdraw(int balance, int amount) {
int remaining = balance; // DEF remaining
if (amount <= 0) return "Invalid";
if (amount > balance) return "Insuicient";
remaining = balance - amount; // DEF remaining, USE balance, amount
if (remaining < 100) return "Low Balance"; // USE remaining
return "Success"; // USE remaining
}
}
Bài 9: Loan Eligibility
using System;
public class LoanEligibility {
public static string Check(int income, int credit, int loan) {
double ratio = 0; // DEF ratio
if (income <= 0) return "Invalid";
ratio = (double)loan / income; // DEF ratio
if (credit < 600) return "Bad Credit"; // USE credit
if (ratio > 5) return "Loan Too High"; // USE ratio

Preview text:

10 Bài Tập C# – Basis Path Testing Bài 1: Binary Search using System; public class BinarySearch {
public static int Search(int[] arr, int key) {
int left = 0, right = arr.Length - 1; while (left <= right) {
int mid = (left + right) / 2; if (arr[mid] == key) { return mid;
} else if (arr[mid] < key) { left = mid + 1; } else { right = mid - 1; } } return -1; } } Bài 2: Linear Search using System; public class LinearSearch {
public static int Search(int[] arr, int key) { if (arr == null) return -1;
for (int i = 0; i < arr.Length; i++) { if (arr[i] == key) { return i; } } return -1; } } Bài 3: Factorial using System; public class Factorial {
public static int Calc(int n) { if (n < 0) return -1; int result = 1;
for (int i = 1; i <= n; i++) { result *= i; } return result; } } Bài 4: Prime Check using System; public class PrimeCheck {
public static bool IsPrime(int n) { if (n <= 1) return false;
for (int i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } }
Bài 5: Find Max in Array using System; public class FindMax {
public static int Max(int[] arr) {
if (arr == null || arr.Length == 0) return -1; int max = arr[0];
for (int i = 1; i < arr.Length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } } Bài 6: GCD (Euclid) using System; public class GCD {
public static int Calc(int a, int b) { if (a == 0) return b; if (b == 0) return a; while (a != b) { if (a > b) { a = a - b; } else { b = b - a; } } return a; } }
Bài 7: Password Validator using System;
public class PasswordValidator {
public static bool Validate(string password) {
if (password == null || password.Length < 6) return false;
bool hasDigit = false, hasUpper = false;
foreach (char c in password) {
if (Char.IsDigit(c)) hasDigit = true;
if (Char.IsUpper(c)) hasUpper = true; }
return hasDigit && hasUpper; } } Bài 8: ATM Withdrawal using System; public class ATM {
public static string Withdraw(int balance, int amount) {
if (amount <= 0) return "Invalid";
if (amount > balance) return "Insufficient";
if (amount % 50 != 0) return "Must be multiple of 50"; balance -= amount; return "Success"; } }
Bài 9: Grade Classification using System; public class Grade {
public static string Classify(int score) {
if (score < 0 || score > 100) return "Invalid";
else if (score >= 90) return "A";
else if (score >= 75) return "B";
else if (score >= 50) return "C"; else return "F"; } } Bài 10: Fibonacci using System; public class Fibonacci {
public static int Calc(int n) { if (n < 0) return -1; if (n == 0) return 0; if (n == 1) return 1; int a = 0, b = 1, c = 0;
for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return c; } }
Với Control Flow Coverage Testing (Statement, Branch, Condition, Path Coverage)
10 Bài tập C# – Control Flow Coverage Testing
Bài 1: User Login System using System; public class UserLogin {
public static string Login(string username, string password, bool isLocked) { if (isLocked) { return "Account Locked"; }
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) {
return "Missing Credentials"; }
if (username == "admin" && password == "12345") { return "Welcome Admin";
} else if (username == "user" && password == "user123") { return "Welcome User"; } else {
return "Invalid Credentials"; } } }
Bài 2: Online Shopping Discount using System;
public class ShoppingDiscount {
public static double Calculate(double total, bool isMember, string coupon) { double discount = 0; if (total <= 0) return 0; if (isMember) { discount += 0.1;
if (total > 100) discount += 0.05; } else {
if (total > 200) discount += 0.05; }
if (!string.IsNullOrEmpty(coupon)) {
if (coupon == "SALE20") discount += 0.2;
else if (coupon == "SALE10") discount += 0.1; }
return total * (1 - discount); } }
Bài 3: Banking Transaction using System;
public class BankTransaction {
public static string Transfer(int balance, int amount, bool vip) {
if (amount <= 0) return "Invalid"; if (amount > balance) {
if (vip && balance > 0) { return "Overdraft Approved"; } else { return "Insufficient Funds"; } } if (amount > 10000) {
return "Requires Manager Approval"; } balance -= amount;
return "Transfer Successful"; } }
Bài 4: Student Result Evaluation using System; public class StudentResult {
public static string Evaluate(int attendance, int midterm, int finalExam) {
if (attendance < 50) return "Fail - Low Attendance";
if (finalExam < 40) return "Fail - Final Too Low";
int total = midterm + finalExam;
if (total >= 85) return "Excellent";
else if (total >= 70) return "Good";
else if (total >= 50) return "Pass"; else return "Fail"; } }
Bài 5: Online Exam Authentication using System; public class ExamAuth {
public static string Authenticate(string studentId, string password, bool faceMatch, bool otpValid) {
if (string.IsNullOrEmpty(studentId) || string.IsNullOrEmpty(password)) { return "Missing Info"; }
if (studentId == "S123" && password == "pass123") {
if (!faceMatch) return "Face ID Failed";
if (!otpValid) return "OTP Failed"; return "Access Granted"; }
return "Invalid Credentials"; } }
Bài 6: E-commerce Payment System using System; public class Payment {
public static string ProcessPayment(double amount, string method, bool
balanceSufficient, bool cardValid) {
if (amount <= 0) return "Invalid Amount"; if (method == "CASH") { return "Payment Success";
} else if (method == "CARD") {
if (!cardValid) return "Card Invalid";
if (amount > 5000) return "Manager Approval Needed";
return "Card Payment Success";
} else if (method == "WALLET") {
if (!balanceSufficient) return "Wallet Insufficient";
return "Wallet Payment Success"; } return "Unknown Method"; } }
Bài 7: Loan Approval System using System; public class LoanApproval {
public static string Approve(int salary, int creditScore, int loanAmount) {
if (salary < 2000) return "Rejected - Low Income";
if (creditScore < 500) return "Rejected - Bad Credit";
if (loanAmount > salary * 20) {
return "Rejected - Too High Loan";
} else if (loanAmount > salary * 10) { return "Manual Review"; } return "Approved"; } }
Bài 8: Insurance Premium Calculator using System; public class Insurance {
public static double CalculatePremium(int age, bool smoker, string health) { double premium = 500; if (age < 18) return 0;
if (age > 60) premium += 300; if (smoker) { premium += 200; }
if (health == "Poor") premium += 400;
else if (health == "Average") premium += 200;
else if (health == "Good") premium -= 100; return premium; } }
Bài 9: Online Ticket Booking using System; public class TicketBooking {
public static string Book(int seats, bool paymentSuccess, bool vip) {
if (seats <= 0) return "Invalid Seats";
if (!paymentSuccess) return "Payment Failed"; if (seats > 5) {
if (vip) return "Group Booking Approved";
else return "Too Many Seats"; } return "Booking Successful"; } }
Bài 10: Shopping Cart Checkout using System; public class Checkout {
public static string Complete(int items, double total, bool stockAvailable, bool addressValid) {
if (items <= 0) return "Empty Cart";
if (!stockAvailable) return "Out of Stock";
if (!addressValid) return "Invalid Address"; if (total > 1000) {
return "Apply Free Shipping"; } else if (total < 50) {
return "Small Order Fee Applied"; } return "Checkout Success"; } }
10 Bài tập C# – Data Flow Testing
Bài 1: Student Grade Calculation using System; public class StudentGrade {
public static string Calculate(int midterm, int final) { int total = 0; double avg = 0;
total = midterm + final; // DEF total
avg = total / 2.0; // DEF avg if (avg >= 8.5) {
return "Excellent"; // USE avg } else if (avg >= 7.0) { return "Good"; // USE avg } else if (avg >= 5.0) { return "Pass"; // USE avg } else { return "Fail"; // USE avg } } }
Bài 2: Simple Interest Calculator using System; public class InterestCalc {
public static double Calculate(double principal, double rate, int time) { double interest = 0; double amount = 0;
interest = (principal * rate * time) / 100; // DEF interest
amount = principal + interest; // DEF amount if (amount > 10000) {
return amount * 0.95; // USE amount } return amount; // USE amount } } Bài 3: Tax Calculator using System; public class TaxCalc {
public static double Compute(double salary) { double tax = 0; if (salary <= 5000) { tax = 0;
} else if (salary <= 10000) { tax = salary * 0.1; } else { tax = salary * 0.2; }
double netIncome = salary - tax; // DEF netIncome
if (netIncome < 4000) return netIncome; // USE netIncome
return netIncome + 500; // USE netIncome } }
Bài 4: Array Sum and Average using System; public class ArrayOps {
public static double Average(int[] arr) { int sum = 0; // DEF sum double avg = 0; // DEF avg
for (int i = 0; i < arr.Length; i++) { sum += arr[i]; // USE sum }
avg = (double)sum / arr.Length; // DEF avg, USE sum return avg; // USE avg } }
Bài 5: Password Strength using System; public class PasswordCheck {
public static string Strength(string pwd) { int score = 0; // DEF score
if (pwd.Length >= 8) score += 2; // DEF score
if (pwd.Any(char.IsDigit)) score += 2;
if (pwd.Any(char.IsUpper)) score += 2;
if (pwd.Any(ch => "!@#$%".Contains(ch))) score += 2;
if (score >= 6) return "Strong"; // USE score
else if (score >= 4) return "Medium"; return "Weak"; } } Bài 6: Shopping Cart using System; public class ShoppingCart {
public static double Checkout(int items, double price) { double total = 0; double discount = 0;
total = items * price; // DEF total if (total > 500) {
discount = total * 0.1; // DEF discount } else {
discount = total * 0.05; // DEF discount }
return total - discount; // USE total, discount } }
Bài 7: Temperature Converter using System; public class TempConvert {
public static double Convert(double temp, string unit) { double result = 0; if (unit == "CtoF") {
result = (temp * 9 / 5) + 32; // DEF result } else if (unit == "FtoC") {
result = (temp - 32) * 5 / 9; // DEF result } else { result = temp; // DEF result } return result; // USE result } } Bài 8: ATM Withdrawal using System; public class ATM {
public static string Withdraw(int balance, int amount) {
int remaining = balance; // DEF remaining
if (amount <= 0) return "Invalid";
if (amount > balance) return "Insufficient";
remaining = balance - amount; // DEF remaining, USE balance, amount
if (remaining < 100) return "Low Balance"; // USE remaining
return "Success"; // USE remaining } }
Bài 9: Loan Eligibility using System;
public class LoanEligibility {
public static string Check(int income, int credit, int loan) {
double ratio = 0; // DEF ratio
if (income <= 0) return "Invalid";
ratio = (double)loan / income; // DEF ratio
if (credit < 600) return "Bad Credit"; // USE credit
if (ratio > 5) return "Loan Too High"; // USE ratio