"""Kiểm thử biến hình cho bộ kiểm chứng lịch đồ chơi.""" from dataclasses import dataclass, replace from itertools import combinations, permutations @dataclass(frozen=True) class Buoi: ma: str phong: str gv: str ca: int thu: int def danh_gia(lich, gv_uu_tien): trung_phong = sum(a.ca == b.ca and a.thu == b.thu and a.phong == b.phong for a, b in combinations(lich, 2)) trung_gv = sum(a.ca == b.ca and a.thu == b.thu and a.gv == b.gv for a, b in combinations(lich, 2)) if trung_phong or trung_gv: return (trung_phong, trung_gv, None) ngay = {b.thu for b in lich if b.gv == gv_uu_tien} return (0, 0, 5 * {2, 6}.issubset(ngay)) if __name__ == "__main__": sample = [ Buoi("L1", "P101", "A", 1, 2), Buoi("L2", "P102", "B", 1, 2), Buoi("L3", "P101", "A", 1, 6), ] conflict = [sample[0], replace(sample[1], phong="P101", gv="A"), sample[2]] # Neo kỳ vọng để tránh một validator luôn trả cùng một kết quả. assert danh_gia(sample, "A") == (0, 0, 5) assert danh_gia(conflict, "A") == (1, 1, None) for lich in (sample, conflict): expected = danh_gia(lich, "A") for permutation in permutations(lich): assert danh_gia(permutation, "A") == expected renamed = [replace(b, ma="x_" + b.ma, phong="x_" + b.phong, gv="x_" + b.gv) for b in lich] assert danh_gia(renamed, "x_A") == expected # Minh họa lỗi thiết kế test: đổi ID giảng viên mà quên đổi ưu tiên. renamed = [replace(b, gv="x_" + b.gv) for b in sample] assert danh_gia(renamed, "A") == (0, 0, 0) print("Đạt kiểm thử hoán vị và đổi mã đồng bộ trên hai lịch.")