package comp1110.exam;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
Copyright By PowCoder代写 加微信 powcoder
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runners.MethodSorters;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Q5ActorTest {
public Timeout globalTimeout = Timeout.millis(2000);
public static final int HASH_ITERATIONS = 100;
static Q5Actor[] actors = new Q5Actor[200 * HASH_ITERATIONS];
public void testHashCodeDeterministic() {
checkNotObjectHashCode();
for (Q5Actor actor : actors) {
int hash = actor.hashCode();
for (int i = 0; i < 10; i++) {
int hash2 = actor.hashCode();
assertTrue("actor " + actor + " returned different values for hashCode(): " + hash + ", " + hash2, hash == hash2);
public void testAllFields() {
checkNotObjectHashCode();
Q5Actor actor1 = new Q5Actor("Michelle", "Williams", "19800909");
Q5Actor actor2 = new Q5Actor("Michelle", "Williams", "19790723");
testDifferent(actor1, actor2);
testDifferent(actor1, actor2);
actor2 = new Q5Actor("Michelle", "Pfeiffer", "19800909");
testDifferent(actor1, actor2);
testDifferent(actor1, actor2);
actor2 = new Q5Actor("Michelle", "Rodriguez", "19800909");
testDifferent(actor1, actor2);
testDifferent(actor1, actor2);
actor2 = new Q5Actor("Robin", "Williams", "19800909");
testDifferent(actor1, actor2);
testDifferent(actor1, actor2);
actor2 = new Q5Actor("Cara", "Williams", "19800909");
testDifferent(actor1, actor2);
testDifferent(actor1, actor2);
public void testEquals() {
Q5Actor actor1 = new Q5Actor("Michelle", "Williams", "19800909");
assertTrue("actor1.equals(actor1) returned false", actor1.equals(actor1));
Q5Actor actor2 = new Q5Actor("Michelle", "Williams", "19790723");
assertFalse("actors " + actor1 + " and " + actor2 + " are not equal, but actor1.equals(actor2) returned true", actor1.equals(actor2));
actor2.given = "Robin";
actor2.dob = "19800909";
assertFalse("actors " + actor1 + " and " + actor2 + " are not equal, but actor1.equals(actor2) returned true", actor1.equals(actor2));
actor2.given = "Michelle";
actor2.family = "Bridges";
assertFalse("actors " + actor1 + " and " + actor2 + " are not equal, but actor1.equals(actor2) returned true", actor1.equals(actor2));
actor2.family = null;
assertFalse("actors " + actor1 + " and " + actor2 + " are not equal, but actor1.equals(actor2) returned true", actor1.equals(actor2));
actor1.family = null;
assertTrue("actors " + actor1 + " and " + actor2 + " are equal, but actor1.equals(actor2) returned false", actor1.equals(actor2));
actor2.family = "Williams";
assertFalse("actors " + actor1 + " and " + actor2 + " are not equal, but actor1.equals(actor2) returned true", actor1.equals(actor2));
actor1.family = "Williams";
assertTrue("actors " + actor1 + " and " + actor2 + " are equal, but actor1.equals(actor2) returned false", actor1.equals(actor2));
public void testUniformA() {
checkNotObjectHashCode();
// chiSquared critical value (DOF=10-1, prob=0.999) ~= 27.88
testUniformity(10, 45);
public void testUniformB() {
checkNotObjectHashCode();
// chiSquared critical value (DOF=50-1, prob=0.999) ~= 85.35
testUniformity(50, 135);
private void testUniformity(int buckets, double chiSqCriticalValue) {
Random r = new Random(7);
int[] count = new int[buckets];
int samples = buckets * HASH_ITERATIONS;
for (int i = 0; i < samples; i++) {
Q5Actor actor = actors[r.nextInt(actors.length)];
int h = Math.abs(actor.hashCode()) % buckets;
count[h]++;
double chiSq = chiSquaredUniform(samples, count);
assertTrue("Distribution of hash function doesn't appear to be uniform over " + buckets + " buckets (chi squared value of " + chiSq + ").\nExpected " + samples / buckets + " elements per bucket, but got " + Arrays.toString(count), chiSq < chiSqCriticalValue);
private void checkNotObjectHashCode() {
Random r = new Random(9);
int range = 39;
Consumer
Set
Set
for (int i = 0; i < range; i++) {
myBuckets[i] = new HashSet<>();
defaultBuckets[i] = new HashSet<>();
for (int i = 0; i < 98 * HASH_ITERATIONS; i++) {
Q5Actor actor = actors[r.nextInt(actors.length)];
int m = Math.abs(actor.hashCode()) % range;
myBuckets[m].add(actor);
int n = Math.abs(hashFunction.apply(actor)) % range;
defaultBuckets[n].add(actor);
for (Set
for (Set
assertFalse(“It looks like you’re using Object.hashCode() or Objects.hash()!”, myBucket.equals(defaultBucket));
checkHash.accept((Q5Actor actor) -> actor.passThroughHash());
checkHash.accept((Q5Actor actor) -> Objects.hash(actor.given, actor.family, actor.dob));
checkHash.accept((Q5Actor actor) -> Objects.hash(actor.given, actor.dob, actor.family));
checkHash.accept((Q5Actor actor) -> Objects.hash(actor.family, actor.dob, actor.given));
checkHash.accept((Q5Actor actor) -> Objects.hash(actor.family, actor.given, actor.dob));
checkHash.accept((Q5Actor actor) -> Objects.hash(actor.dob, actor.given, actor.family));
checkHash.accept((Q5Actor actor) -> Objects.hash(actor.dob, actor.family, actor.given));
private void testDifferent(Q5Actor actor1, Q5Actor actor2) {
int hash1 = actor1.hashCode();
int hash2 = actor2.hashCode();
assertTrue(“actors ” + actor1 + ” and ” + actor2 + ” returned same hashCode(): ” + hash1 + “, ” + hash2, hash1 != hash2);
@BeforeClass
public static void generateActors() {
Random r = new Random(11);
for (int i = 0; i < actors.length; i++) {
String[] base = actorlist[r.nextInt(actorlist.length)];
actors[i] = new Q5Actor(base[0], base[1], String.format("%s%02d%02d", base[2], r.nextInt(12), r.nextInt(28)));
private static double chiSquaredUniform(int samples, int[] counts) {
double uniformProb = 1.0 / counts.length;
double total = 0;
for (int count : counts) {
double mi = ((double) samples) * uniformProb;
total += ((double) count - mi) * ((double) count - mi) / mi;
return total;
static String[][] actorlist = {
{"Barkhad", "Abdi", "1985"},
{"F. Murray", "Abraham", "1939"},
{"Amy", "Adams", "1974"},
{"Nick", "Adams", "1931"},
{"Isabelle", "Adjani", "1955"},
{"Casey", "Affleck", "1975"},
{"Shohreh", "Aghdashloo", "1952"},
{"Brian", "Aherne", "1902"},
{"Danny", "Aiello", "1933"},
{"Anouk", "Aimée", "1932"},
{"Eddie", "Albert", "1906"},
{"Jack", "Albertson", "1907"},
{"Alan", "Alda", "1936"},
{"Norma", "Aleandro", "1936"},
{"Jane", "Alexander", "1939"},
{"Mahershala", "Ali", "1974"},
{"Joan", "Allen", "1956"},
{"Woody", "Allen", "1935"},
{"Sara", "Allgood", "1880"},
{"Don", "Ameche", "1908"},
{"Judith", "Anderson", "1897"},
{"Julie", "Andrews", "1935"},
{"Ann-Margret", "", "1941"},
{"Yalitza", "Aparicio", "1993"},
{"Anne", "Archer", "1947"},
{"Eve", "Arden", "1908"},
{"Alan", "Arkin", "1934"},
{"George", "Arliss", "1868"},
{"Patricia", "Arquette", "1968"},
{"Jean", "Arthur", "1900"},
{"Peggy", "Ashcroft", "1907"},
{"Fred", "Astaire", "1899"},
{"Mary", "Astor", "1906"},
{"Mischa", "Auer", "1905"},
{"Margaret", "Avery", "1944"},
{"Dan", "Aykroyd", "1952"},
{"Lew", "Ayres", "1908"},
{"Lauren", "Bacall", "1924"},
{"Hermione", "Baddeley", "1906"},
{"Mary", "Badham", "1952"},
{"Fay", "Bainter", "1893"},
{"Carroll", "Baker", "1931"},
{"Alec", "Baldwin", "1958"},
{"Christian", "Bale", "1974"},
{"Martin", "Balsam", "1919"},
{"Anne", "Bancroft", "1931"},
{"George", "Bancroft", "1882"},
{"Antonio", "Banderas", "1960"},
{"Ian", "Bannen", "1928"},
{"Javier", "Bardem", "1969"},
{"Marie-Christine", "Barrault", "1944"},
{"Adriana", "Barraza", "1956"},
{"Barbara", "Barrie", "1931"},
{"Ethel", "Barrymore", "1879"},
{"Lionel", "Barrymore", "1878"},
{"Richard", "Barthelmess", "1895"},
{"Mikhail", "Baryshnikov", "1948"},
{"Kim", "Basinger", "1953"},
{"Albert", "Bassermann", "1867"},
{"Angela", "Bassett", "1958"},
{"Alan", "Bates", "1934"},
{"Kathy", "Bates", "1948"},
{"Anne", "Baxter", "1923"},
{"Warner", "Baxter", "1889"},
{"Ned", "Beatty", "1937"},
{"Warren", "Beatty", "1937"},
{"Wallace", "Beery", "1885"},
{"Ed", "Begley", "1901"},
{"Bérénice", "Bejo", "1976"},
{" ", "Geddes", "1922"},
{"Ralph", "Bellamy", "1904"},
{"William", "Bendix", "1906"},
{"Roberto", "Benigni", "1952"},
{"Annette", "Bening", "1958"},
{"Tom", "Berenger", "1949"},
{"Candice", "Bergen", "1946"},
{"Ingrid", "Bergman", "1915"},
{"Elisabeth", "Bergner", "1897"},
{"Jeannie", "Berlin", "1949"},
{"Halle", "Berry", "1966"},
{"Demián", "Bichir", "1963"},
{"Charles", "Bickford", "1891"},
{"Theodore", "Bikel", "1924"},
{"Juliette", "Binoche", "1964"},
{"Karen", "Black", "1939"},
{"Betsy", "Blair", "1923"},
{"Linda", "Blair", "1959"},
{"Ronee", "Blakley", "1945"},
{"Cate", "Blanchett", "1969"},
{"Brenda", "Blethyn", "1946"},
{" .", "Blige", "1971"},
{"Joan", "Blondell", "1906"},
{"Ann", "Blyth", "1928"},
{"Humphrey", "Bogart", "1899"},
{"Beulah", "Bondi", "1889"},
{" ", "Carter", "1966"},
{"Shirley", "Booth", "1898"},
{"Ernest", "Borgnine", "1917"},
{"Charles", "Boyer", "1899"},
{"Lorraine", "Bracco", "1954"},
{"Alice", "Brady", "1892"},
{"Kenneth", "Branagh", "1960"},
{" ", "Brandauer", "1943"},
{" ", "§", "1924"},
{"Eileen", "Brennan", "1932"},
{"Walter", "Brennan", "1894"},
{"Abigail", "Breslin", "1996"},
{"Jeff", "Bridges", "1949"},
{"Jim", "Broadbent", "1949"},
{"Adrien", "Brody", "1973"},
{"Josh", "Brolin", "1968"},
{"Albert", "Brooks", "1947"},
{"Leslie", "Browne", "1957"},
{"Yul", "Brynner", "1920"},
{"Geneviève", "Bujold", "1942"},
{"Sandra", "Bullock", "1964"},
{"Victor", "Buono", "1938"},
{"Billie", "Burke", "1884"},
{"Catherine", "Burns", "1945"},
{"George", "Burns", "1896"},
{"Ellen", "Burstyn", "1932"},
{"Richard", "Burton", "1925"},
{"Gary", "Busey", "1944"},
{"Red", "Buttons", "1919"},
{"Spring", "Byington", "1886"},
{"James", "Caan", "1940"},
{"Adolph", "Caesar", "1933"},
{"Nicolas", "Cage", "1964"},
{"James", "Cagney", "1899"},
{"Michael", "Caine", "1933"},
{"Louis", "Calhern", "1895"},
{"Dyan", "Cannon", "1937"},
{"Steve", "Carell", "1962"},
{"Harry", "Carey", "1878"},
{"Lynn", "Carlin", "1938"},
{"Art", "Carney", "1918"},
{"Leslie", "Caron", "1931"},
{"Diahann", "Carroll", "1935"},
{"Nancy", "Carroll", "1903"},
{"Peggy", "Cass", "1924"},
{"John", "Cassavetes", "1929"},
{"Seymour", "Cassel", "1935"},
{" .", "Castellano", "1933"},
{"Keisha", "Castle-Hughes", "1990"},
{"George", "Chakiris", "1934"},
{"Timothée", "Chalamet", "1995"},
{"Jeff", "Chandler", "1918"},
{"Carol", "Channing", "1921"},
{"Stockard", "Channing", "1944"},
{"Charlie", "Chaplin", "1889"},
{"Jessica", "Chastain", "1977"},
{"Ruth", "Chatterton", "1892"},
{"Don", "Cheadle", "1964"},
{"Michael", "Chekhov", "1891"},
{"", "Cher", "1946"},
{"Maurice", "Chevalier", "1888"},
{"Julie", "Christie", "1940"},
{" ", "Church", "1960"},
{"Diane", "Cilento", "1933"},
{"Candy", "Clark", "1947"},
{"Patricia", "Clarkson", "1959"},
{"Jill", "Clayburgh", "1944"},
{"Montgomery", "Clift", "1920"},
{"George", "Clooney", "1961"},
{"Glenn", "Close", "1947"},
{" .", "Cobb", "1911"},
{"Charles", "Coburn", "1877"},
{"James", "Coburn", "1928"},
{"James", "Coco", "1930"},
{"Claudette", "Colbert", "1903"},
{"Toni", "Collette", "1972"},
{"Patricia", "Collinge", "1892"},
{"Pauline", "Collins", "1940"},
{"Olivia", "Colman", "1974"},
{"Ronald", "Colman", "1891"},
{"Betty", "Compson", "1897"},
{"Jennifer", "Connelly", "1970"},
{"Sean", "Connery", "1930"},
{"Tom", "Conti", "1941"},
{"Bradley", "Cooper", "1975"},
{"Chris", "Cooper", "1951"},
{"Gary", "Cooper", "1901"},
{"Gladys", "Cooper", "1888"},
{"Jackie", "Cooper", "1922"},
{"Ellen", "Corby", "1911"},
{"Valentina", "Cortese", "1923"},
{"Kevin", "Costner", "1955"},
{"Marion", "Cotillard", "1975"},
{"Tom", "Courtenay", "1937"},
{"Jeanne", "Crain", "1925"},
{"Bryan", "Cranston", "1956"},
{"Broderick", "Crawford", "1911"},
{"Joan", "Crawford", "1904"},
{"Donald", "Crisp", "1882"},
{"James", "Cromwell", "1940"},
{"Hume", "Cronyn", "1911"},
{"Bing", "Crosby", "1903"},
{"Rupert", "Crosse", "1927"},
{"Lindsay", "Crouse", "1948"},
{"Russell", "Crowe", "1964"},
{"Tom", "Cruise", "1962"},
{"Penélope", "Cruz", "1974"},
{"Benedict", "Cumberbatch", "1976"},
{"Quinn", "Cummings", "1967"},
{"Tony", "Curtis", "1925"},
{"Joan", "Cusack", "1962"},
{"Willem", "Dafoe", "1955"},
{"Dan", "Dailey", "1915"},
{"John", "Dall", "1920"},
{"Matt", "Damon", "1970"},
{"Dorothy", "Dandridge", "1922"},
{"Bobby", "Darin", "1936"},
{"Jane", "Darwell", "1879"},
{"Jaye", "Davidson", "1968"},
{"Bette", "Davis", "1908"},
{"Geena", "Davis", "1956"},
{"Judy", "Davis", "1955"},
{"Viola", "Davis", "1965"},
{"Bruce", "Davison", "1946"},
{"Doris", "Day", "1922"},
{"Daniel", "Day-Lewis", "1957"},
{"Olivia de", "Havilland", "1916"},
{" ", "Niro", "1943"},
{" ", "Sica", "1901"},
{"Marina de", "Tavira", "1974"},
{"James", "Dean", "1931"},
{"Ruby", "Dee", "1922"},
{"Benicio del", "Toro", "1967"},
{"William", "Demarest", "1892"},
{"Judi", "Dench", "1934"},
{"Catherine", "Deneuve", "1943"},
{"Sandy", "Dennis", "1937"},
{"Gérard", "Depardieu", "1948"},
{"Johnny", "Depp", "1963"},
{"Bruce", "Dern", "1936"},
{"Laura", "Dern", "1967"},
{"Brandon", "deWilde", "1942"},
{"Leonardo", "DiCaprio", "1974"},
{"Marlene", "Dietrich", "1901"},
{"Matt", "Dillon", "1964"},
{"Melinda", "Dillon", "1939"},
{"Richard", "Dix", "1893"},
{"Robert", "Donat", "1905"},
{"Brian", "Donlevy", "1901"},
{"Kirk", "Douglas", "1916"},
{"Melvyn", "Douglas", "1901"},
{"Michael", "Douglas", "1944"},
{"Brad", "Dourif", "1950"},
{" ", "Jr.", "1965"},
{"Louise", "Dresser", "1878"},
{"Marie", "Dressler", "1868"},
{"Richard", "Dreyfuss", "1947"},
{"Adam", "Driver", "1983"},
{"Minnie", "Driver", "1970"},
{"Jean", "Dujardin", "1972"},
{"Olympia", "Dukakis", "1931"},
{"Patty", "Duke", "1946"},
{"Faye", "Dunaway", "1941"},
{" ", "Duncan", "1957"},
{"James", "Dunn", "1901"},
{"Michael", "Dunn", "1934"},
{"Irene", "Dunne", "1898"},
{"Mildred", "Dunnock", "1901"},
{"Charles", "Durning", "1923"},
{"Robert", "Duvall", "1931"},
{"Jeanne", "Eagels", "1890"},
{"Clint", "Eastwood", "1930"},
{"Samantha", "Eggar", "1939"},
{"Jesse", "Eisenberg", "1983"},
{"Chiwetel", "Ejiofor", "1977"},
{"Denholm", "Elliott", "1922"},
{"Sam", "Elliott", "1944"},
{"Hope", "Emerson", "1897"},
{"Cynthia", "Erivo", "1987"},
{"Stuart", "Erwin", "1903"},
{"Edith", "Evans", "1888"},
{"Peter", "Falk", "1927"},
{"Vera", "Farmiga", "1973"},
{"Richard", "Farnsworth", "1920"},
{"Michael", "Fassbender", "1977"},
{"José", "Ferrer", "1912"},
{"Sally", "Field", "1946"},
{"Ralph", "Fiennes", "1962"},
{"Peter", "Finch", "1916"},
{"Frank", "Finlay", "1926"},
{"Albert", "Finney", "1936"},
{"Colin", "Firth", "1960"},
{"Peter", "Firth", "1953"},
{"Laurence", "Fishburne", "1961"},
{"Barry", "Fitzgerald", "1888"},
{"Geraldine", "Fitzgerald", "1913"},
{"Louise", "Fletcher", "1934"},
{"Nina", "Foch", "1924"},
{"Henry", "Fonda", "1905"},
{"Jane", "Fonda", "1937"},
{"Peter", "Fonda", "1940"},
{"Joan", "Fontaine", "1917"},
{"Lynn", "Fontanne", "1887"},
{"Harrison", "Ford", "1942"},
{"Frederic", "Forrest", "1936"},
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com