package comp1110.exam;
import org.junit.Before;
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.Arrays;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Q3LibraryTest {
public Timeout globalTimeout = Timeout.millis(1000);
Q3Library library;
static final int INITIAL = 50;
static final int LARGER = 200;
public void setup() {
library = new Q3Library();
addInitialBooks();
public void testGetBookCount() {
Q3Library empty = new Q3Library();
assertEquals(“getBookCount() returned incorrect number of Books for empty library”, 0, empty.getBookCount());
assertEquals(“getBookCount() returned incorrect number of Books”, INITIAL, library.getBookCount());
addMoreBooks();
assertEquals(“getBookCount() returned incorrect number of Books”, LARGER, library.getBookCount());
public void testGetBookCountDuplicate() {
addInitialBooks();
assertEquals(“getBookCount() returned incorrect number of Books”, INITIAL, library.getBookCount());
addMoreBooks();
addMoreBooks();
assertEquals(“getBookCount() returned incorrect number of Books”, LARGER, library.getBookCount());
public void testGetBookCountDelete() {
Q3Library empty = new Q3Library();
final int NON_EXISTENT_ARTICLE = 1234567;
library.deleteBook(NON_EXISTENT_ARTICLE);
assertEquals(“getBookCount() returned incorrect number of Books for empty library”, 0, empty.getBookCount());
int isbn = Integer.parseInt(books[0][0]);
assertTrue(library.deleteBook(isbn));
assertFalse(library.deleteBook(isbn));
assertEquals(“getBookCount() returned incorrect number of Books”, INITIAL-1, library.getBookCount());
private boolean checkStringSet(Set
boolean sound = false;
if (out.size() == expected.length) {
sound = true;
for (String s : expected) {
if (!out.contains(s))
sound = false;
return sound;
public void testGetTopBooks() {
Set
assertTrue(“Incorrect getTopBooks(0). Expected to get all “+INITIAL+” books but got “+out.size(), out.size() == INITIAL);
out = library.getTopBooks(5f); // all
assertTrue(“Incorrect getTopBooks(5). Expected to get no books but got “+out.size(), out.size() == 0);
String[] t45 = {” and the Deathly Hallows”, ” and the Half-Blood Prince”, ” and the Prisoner of Azkaban”, ” and the Goblet of Fire”};
out = library.getTopBooks(4.5f);
assertTrue(“Incorrect getTopBooks(4.5f). Expected “+Arrays.toString(t45)+” but got “+out, checkStringSet(out, t45));
addMoreBooks();
String[] t455 = {” and the Deathly Hallows”, “The Name of the Wind”};
out = library.getTopBooks(4.55f);
assertTrue(“Incorrect getTopBooks(4.55f). Expected “+Arrays.toString(t455)+” but got “+out, checkStringSet(out, t455));
public void testGetTopBooksDuplicate() {
addInitialBooks();
Set
assertTrue(“Incorrect getTopBooks(0). Expected to get all “+INITIAL+” books but got “+out.size(), out.size() == INITIAL);
out = library.getTopBooks(5f); // all
assertTrue(“Incorrect getTopBooks(5). Expected to get no books but got “+out.size(), out.size() == 0);
String[] t45 = {” and the Deathly Hallows”, ” and the Half-Blood Prince”, ” and the Prisoner of Azkaban”, ” and the Goblet of Fire”};
out = library.getTopBooks(4.5f);
assertTrue(“Incorrect getTopBooks(4.5f). Expected “+Arrays.toString(t45)+” but got “+out, checkStringSet(out, t45));
addMoreBooks();
addMoreBooks();
String[] t455 = {” and the Deathly Hallows”, “The Name of the Wind”};
out = library.getTopBooks(4.55f);
assertTrue(“Incorrect getTopBooks(4.55f). Expected “+Arrays.toString(t455)+” but got “+out, checkStringSet(out, t455));
public void testGetTopBooksDelete() {
library.deleteBook(545010225);
String[] t45 = {” and the Half-Blood Prince”, ” and the Prisoner of Azkaban”, ” and the Goblet of Fire”};
Set
assertTrue(“Incorrect getTopBooks(4.5f). Expected “+Arrays.toString(t45)+” but got “+out, checkStringSet(out, t45));
addMoreBooks();
String[] t455 = {“The Name of the Wind”};
out = library.getTopBooks(4.55f);
assertTrue(“Incorrect getTopBooks(4.55f). Expected “+Arrays.toString(t455)+” but got “+out, checkStringSet(out, t455));
private void checkAuthor(String author, Set
assertEquals(“getAuthorBooks(\”” + author + “\”) returned incorrect set”, expected, library.getAuthorBooks(author));
public void testGetAuthorBooks() {
checkAuthor(” “, Set.of());
checkAuthor(” “, Set.of(“Pride and Prejudice”));
checkAuthor(” “, Set.of());
checkAuthor(” Alcott”, Set.of(“Little Women”));
addMoreBooks();
checkAuthor(” “, Set.of(“The Firm”, “A Time to Kill”));
checkAuthor(” “, Set.of(“Pride and Prejudice”, “Sense and Sensibility”, “Emma”));
public void testGetAuthorBooksDuplicate() {
addMoreBooks();
addInitialBooks();
addMoreBooks();
checkAuthor(” “, Set.of(“Pride and Prejudice”, “Sense and Sensibility”, “Emma”));
public void testGetAuthorBooksDelete() {
addMoreBooks();
checkAuthor(” “, Set.of(“Pride and Prejudice”, “Sense and Sensibility”, “Emma”));
library.deleteBook(679783261);
checkAuthor(” “, Set.of(“Sense and Sensibility”, “Emma”));
library.deleteBook(141439661);
checkAuthor(” “, Set.of(“Emma”));
checkAuthor(” “, Set.of(“The Firm”, “A Time to Kill”));
library.deleteBook(385338600);
checkAuthor(” “, Set.of(“The Firm”));
public void testGetNumAuthors() {
assertEquals(“getNumAuthors() returned incorrect value”, 48, library.getNumAuthors());
addMoreBooks();
assertEquals(“getNumAuthors() returned incorrect value”, 199, library.getNumAuthors());
public void testGetNumAuthorsDuplicate() {
addInitialBooks();
assertEquals(“getNumAuthors() returned incorrect value”, 48, library.getNumAuthors());
addMoreBooks();
addMoreBooks();
assertEquals(“getNumAuthors() returned incorrect value”, 199, library.getNumAuthors());
public void testGetNumAuthorsDelete() {
library.deleteBook(739326228);
assertEquals(“getNumAuthors() returned incorrect value”, 47, library.getNumAuthors());
library.deleteBook(61120081);
assertEquals(“getNumAuthors() returned incorrect value”, 46, library.getNumAuthors());
addMoreBooks();
library.deleteBook(345391802);
assertEquals(“getNumAuthors() returned incorrect value”, 196, library.getNumAuthors());
private void checkCoAuthors(String person1, String person2, boolean expected) {
boolean result = library.areCoAuthors(person1, person2);
assertTrue(“areCoAuthors(\”” + person1 + “\”, \”” + person2 + “\”) returned ” + result + “, expected ” + expected, expected == result);
public void testAreCoAuthors() {
checkCoAuthors(” “, ” “, true);
checkCoAuthors(“J.K. Rowling”, ” Pré”, true);
checkCoAuthors(” “, ” “, false);
checkCoAuthors(” “, ” “, false);
checkCoAuthors(“Kami Garcia”, ” “, false);
addMoreBooks();
checkCoAuthors(” “, ” “, true);
checkCoAuthors(” “, ” “, true);
checkCoAuthors(“Kami Garcia”, ” “, true);
public void testAreCoAuthorsDelete() {
addMoreBooks();
checkCoAuthors(” “, ” “, true);
library.deleteBook(142403881);
checkCoAuthors(” “, ” “, true);
library.deleteBook(141301066);
checkCoAuthors(” “, ” “, false);
checkCoAuthors(” “, ” “, true);
library.deleteBook(345803922);
checkCoAuthors(” “, ” “, false);
checkCoAuthors(“Kami Garcia”, ” “, true);
library.deleteBook(316042676);
checkCoAuthors(“Kami Garcia”, ” “, false);
public void testGetFirstPublishYearNull() {
checkPublishYear(” “, -1);
checkPublishYear(“Jeanne DuPrau”, -1);
checkPublishYear(” “, -1);
addMoreBooks();
checkPublishYear(” “, 1977);
public void testGetFirstPublishYear() {
checkPublishYear(” “, 1813);
checkPublishYear(“J.K. Rowling”, 1997);
addMoreBooks();
checkPublishYear(” “, 1811);
checkPublishYear(“J.K. Rowling”, 1997);
public void testGetFirstPublishYearDelete() {
addMoreBooks();
checkPublishYear(” “, 1811);
checkPublishYear(“J.K. Rowling”, 1997);
library.deleteBook(141439661);
checkPublishYear(” “, 1813);
library.deleteBook(439554934);
checkPublishYear(“J.K. Rowling”, 1998);
private void checkPublishYear(String person, int expected) {
int result = library.getFirstPublishYear(person);
assertTrue(“getFirstBookYear(\”” + person + “\”) returned ” + result + “, expected ” + expected, expected == result);
public void testGetMaxBooks() {
assertEquals(“getMaxBooks() returned incorrect value”, 7, library.getMaxBooks());
addMoreBooks();
assertEquals(“getMaxBooks() returned incorrect value”, 7, library.getMaxBooks());
public void testGetMaxBooksDuplicate() {
addInitialBooks();
assertEquals(“getMaxBooks() returned incorrect value”, 7, library.getMaxBooks());
addMoreBooks();
addMoreBooks();
assertEquals(“getMaxBooks() returned incorrect value”, 7, library.getMaxBooks());
public void testGetMaxBooksDelete() {
library.deleteBook(439554934);
library.deleteBook(43965548);
library.deleteBook(439358078);
assertEquals(“getMaxBooks() returned incorrect value”, 4, library.getMaxBooks());
library.deleteBook(439064864);
addMoreBooks();
assertEquals(“getMaxBooks() returned incorrect value”, 5, library.getMaxBooks());
private void addBooks(int start, int end) {
for (int i = start; i < end; i++) {
int isbn = Integer.parseInt(books[i][0]);
int year = Integer.parseInt(books[i][1]);
float rating = Float.parseFloat(books[i][2]);
String title = books[i][3];
Set
library.addBook(isbn, year, authors, title, rating);
private void addInitialBooks() {
addBooks(0, INITIAL);
private void addMoreBooks() {
addBooks(50,LARGER);
String[][] books = {
{“439023483″,”2008″,”4.34″,”The Hunger Games”,” “},
{“439554934″,”1997″,”4.44″,” and the Philosopher’s Stone”,”J.K. Rowling, Pré”},
{“316015849″,”2005″,”3.57″,”Twilight”,”Stephenie Meyer”},
{“61120081″,”1960″,”4.25″,”To Kill a Mockingbird”,” “},
{“743273567″,”1925″,”3.89″,”The Great Gatsby”,”F. “},
{“525478817″,”2012″,”4.26″,”The Fault in Our Stars”,” “},
{“618260307″,”1937″,”4.25″,”The Hobbit or There and Back Again”,”J.R.R. Tolkien”},
{“316769177″,”1951″,”3.79″,”The Catcher in the Rye”,”J.D. Salinger”},
{“1416524797″,”2000″,”3.85”,”Angels & Demons “,” “},
{“679783261″,”1813″,”4.24″,”Pride and Prejudice”,” “},
{“1594480001″,”2003″,”4.26”,”The Kite Runner “,” “},
{“62024035″,”2011″,”4.24″,”Divergent”,” “},
{“451524934″,”1949″,”4.14″,”Nineteen Eighty-Four”,” , , Celâl Üster”},
{“452284244″,”1945″,”3.87″,”Animal Farm: A Fairy Story”,” “},
{“553296981″,”1947″,”4.1″,” : Dagboekbrieven 14 juni 1942 – 1 augustus 1944″,” , , B.M. Mooyaart-Doubleday”},
{“307269752″,”2005″,”4.11″,”Män som hatar kvinnor”,” , Reg Keeland”},
{“439023491″,”2009″,”4.3″,”Catching Fire”,” “},
{“043965548″,”1999″,”4.53″,” and the Prisoner of Azkaban”,”J.K. Rowling, Pré, “},
{“618346252″,”1954″,”4.34″,” The Fellowship of the Ring”,”J.R.R. Tolkien”},
{“439023513″,”2010″,”4.03″,”Mockingjay”,” “},
{“439358078″,”2003″,”4.46″,” and the Order of the Phoenix”,”J.K. Rowling, Pré”},
{“316166685″,”2002″,”3.77″,”The Lovely Bones”,” “},
{“439064864″,”1998″,”4.37″,” and the Chamber of Secrets”,”J.K. Rowling, Pré”},
{“439139600″,”2000″,”4.53″,” and the Goblet of Fire”,”J.K. Rowling, Pré”},
{“545010225″,”2007″,”4.61″,” and the Deathly Hallows”,”J.K. Rowling, Pré”},
{“307277674″,”2003″,”3.79”,”The Da “,” “},
{“439785960″,”2005″,”4.54″,” and the Half-Blood Prince”,”J.K. Rowling, Pré”},
{“140283331″,”1954″,”3.64”,”Lord of the Flies “,” “},
{“743477111″,”1595″,”3.73″,”An Excellent conceited Tragedie of Romeo and Juliet”,” , “},
{“297859382″,”2012″,”4.03″,”Gone Girl”,” “},
{“399155341″,”2009″,”4.45″,”The Help”,” “},
{“142000671″,”1937″,”3.84”,”Of Mice and Men “,” “},
{“739326228″,”1997″,”4.08″,”Memoirs of a Geisha”,” “},
{“1612130291″,”2011″,”3.67″,”Fifty Shades of Grey”,”E.L. James”},
{“61122416″,”1988″,”3.82″,”O Alquimista”,” , . Clarke”},
{“385732554″,”1993″,”4.12″,”The Giver”,” “},
{“60764899″,”1950″,”4.19″,”The Lion, the Witch and the Wardrobe”,”C.S. Lewis”},
{“965818675″,”2003″,”3.95″,”The Time Traveler’s Wife”,” “},
{“553588486″,”1996″,”4.45″,”A Game of Thrones”,” .R. Martin”},
{“143038419″,”2006″,”3.51″,”Eat, pray, love: one woman’s search for everything across Italy, India and Indonesia”,” “},
{“786838655″,”2005″,”4.23″,”The Lightning Thief”,” “},
{“451529308″,”1868″,”4.04″,”Little Women”,” Alcott”},
{“142437204″,”1847″,”4.1″,” “,”Charlotte Brontë, “},
{“553816713″,”1996″,”4.06″,”The Notebook”,” “},
{“770430074″,”2001″,”3.88″,”Life of Pi”,” “},
{“1565125606″,”2006″,”4.07″,”Water for Elephants”,” “},
{“375831002″,”2005″,”4.36″,”The Book Thief”,” “},
{“307347974″,”1953″,”3.97″,”Fahrenheit 451″,” “},
{“316160199″,”2006″,”3.52″,” (Twilight, #2)”,”Stephenie Meyer”},
{“60513039″,”1974″,”4.29”,”Where the Sidewalk Ends: The Poems and Drawings of “,” “},
{“1416914285″,”2007″,”4.12″,”City of Bones”,” “},
{“316160202″,”2007″,”3.69″,”Eclipse”,”Stephenie Meyer”},
{“375826696″,”2002″,”3.86″,”Eragon”,” “},
{“345391802″,”1979″,”4.2″,”The Hitchhiker’s Guide to the Galaxy”,” “},
{“60929871″,”1932″,”3.97″,”Brave New World”,” “},
{“031606792″,”2008″,”3.7″,”Breaking Dawn”,”Stephenie Meyer”},
{“142001740″,”2001″,”4.01″,”The Secret Life of Bees”,” Kidd”},
{“142437174″,”1884″,”3.8”,”The Adventures of “,” , , Guy Cardwell”},
{“64410935″,”1952″,”4.15″,”Charlotte’s Web”,”E.B. White, , Rosemary Wells”},
{“1400032717″,”2003″,”3.85″,”The Curious Incident of the Dog in the Night-Time”,” “},
{“1594633665″,”2015″,”3.88″,”The Girl on the Train”,” “},
{“679879242″,”1995″,”3.94″,”Northern Lights”,” “},
{“393978893″,”1847″,”3.82″,”Wuthering Heights”,” ë, . Dunn”},
{“743454537″,”2004″,”4.06″,”My Sister’s Keeper”,”Jodi Picoult”},
{“385333846″,”1969″,”4.06”,”Slaughterhouse-Five, or The Children’s Crusade: A Duty-Dance with Death “,” Jr.”},
{“446675539″,”1936″,”4.28″,”Gone with the Wind”,” “},
{“1594489505″,”2007″,”4.34″,”A Thousand Splendid Suns”,” “},
{“671027344″,”1999″,”4.21″,”The Perks of Being a Wallflower”,” “},
{“7442912″,”2012″,”4.07″,”Insurgent”,” “},
{“812550706″,”1985″,”4.3″,”Ender’s Game”,” Card”},
{“141439475″,”1818″,”3.75″,”Frankenstein; or, The Modern Prometheus”,” Shelley, Shelley, “},
{“450040186″,”1977″,”4.17″,”The Shining”,” “},
{“316068047″,”2008″,”3.84″,”The Host”,”Stephenie Meyer”},
{“142402516″,”2005″,”4.09″,”Looking for Alaska”,” “},
{“014028009″,”1996″,”3.75″,””,” “},
{“141439661″,”1811″,”4.06″,”Sense and Sensibility”,” , , “},
{“439244196″,”1998″,”3.93″,”Holes”,” , “},
{“307275558″,”2003″,”3.7″,”The Devil Wears Prada”,” “},
{“143039954″,”-720″,”3.73″,”Ὀδύσσεια”,”Homer, , E.V. Rieu, Frédéric Mugler, “},
{“156012197″,”1946″,”4.28″,”Le Petit Prince”,”Antoine de Saint-Exupéry, , Barbosa, “},
{“074324754″,”2005″,”4.24″,”The Glass Castle”,” “},
{“385486804″,”1996″,”3.94″,”Into the Wild”,” “},
{“141439602″,”1859″,”3.81″,”A Tale of Two Cities”,” , , Hablot Knight Browne”},
{“030734813″,”1990″,”3.96″,”Jurassic Park”,” “},
{“60256656″,”1964″,”4.38″,”The Giving Tree”,” “},
{“385338600″,”1989″,”4.03″,”A Time to Kill”,” “},
{“374500010″,”1958″,”4.3″,”Un di Velt Hot Geshvign”,” , “},
{“014241493″,”2008″,”3.88″,”Paper Towns”,” “},
{“345418263″,”1973″,”4.25″,”The Princess Bride”,” “},
{“014038572″,”1967″,”4.06″,”The Outsiders”,”S.E. Hinton”},
{“385737947″,”2009″,”4.02″,”The Maze Runner”,” “},
{“61234001″,”2005″,”3.93″,”Freakonomics: A Rogue Economist Explores the Hidden Side of Everything”,” . Levitt, . Dubner”},
{“517189607″,”1911″,”4.12″,”The Secret Garden”,” Burnett”},
{“60531045″,”1967″,”4.04″,”Cien años de soledad”,” ía Márquez, “},
{“375751513″,”1891″,”4.06”,”The Picture of “,” , “},
{“345803507″,”2012″,”3.88″,”Fifty Shades Freed”,”E.L. James”},
{“393970124″,”1897″,”3.98″,”Dracula”,” , , . Skal”},
{“307269981″,”2006″,”4.22″,”Flickan som lekte med elden”,” , Reg Keeland”},
{“1612130585″,”2011″,”3.87″,”Fifty Shades Darker”,”E.L. James”},
{“60786507″,”1998″,”4.02″,”The Poisonwood Bible”,” “},
{“349113912″,”2000″,”3.97″,”Me Talk Pretty One Day”,” “},
{“99408392″,”1963″,”4.22″,”Where the Wild Things Are”,” “},
{“140449264″,”1844″,”4.21″,”Le Comte de Monte-Cristo”,” , “},
{“307265439″,”2006″,”3.95″,”The Road”,” Carthy”},
{“7524277″,”2013″,”3.63″,”Allegiant”,” “},
{“446693804″,”1999″,”4.15″,”A Walk to Remember”,” “},
{“440241413″,”2000″,”3.61″,”Confessions of a Shopaholic”,” “},
{“451525264″,”1862″,”4.14″,”Les Misérables”,” , , Afee”},
{“553381695″,”1998″,”4.4″,”A Clash of Kings”,” .R. Martin”},
{“143037145″,”2005″,”3.64″,”The Memory Keeper’s Daughter”,” “},
{“670026603″,”2012″,”4.27″,”Me Before You”,” “},
{“684833395″,”1961″,”3.98″,”Catch-22″,” “},
{“751529818″,”1997″,”4.06″,”Tuesdays with Morrie”,” , “},
{“312422156″,”2002″,”3.98″,”Middlesex”,” “},
{“143039563″,”1876″,”3.89”,”The Adventures o
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com