Skip to content
Snippets Groups Projects
Commit 3c509eb5 authored by MASSY FERNANDEZ Neva Aracely's avatar MASSY FERNANDEZ Neva Aracely
Browse files

fix: ConsultITemFilmTest

parent 248c744e
No related branches found
No related tags found
1 merge request!9Merge code for iteration3 with main
package tests;
//package teacherTests;
import opinion.ISocialNetwork;
import opinion.SocialNetwork;
import exceptions.*;
/**
* Tools for testing a ISocialNetwork
* @author cousin
*
*/
public class Tools {
private static java.util.Random randomGenerator;
/*
* These counters allow specific members/books/films to be automatically generated, through String concatenate operations
* There are static, so as to guarantee that successive calls to populate() won't generate identical Strings
* They represent the number of members/books/films already generated via Tools
* but depending on other actions made on the ISocialNetwork, they may differ from its actual 'numbers' of members/books/films
*/
private static int nextMember=1; // next value for members (cf logins/passwords)
private static int nextBook=1; // next value for books (cf title)
private static int nextFilm=1; // next value for films (cf title)
/*
* These constants are String or prefix-String to be used when generating new members/books/films/reviews
*/
private static final String LOGIN ="member";
private static final String PASSWORD = "passwd";
private static final String PROFILE ="profile";
private static final String TITLE = "title";
private static final String KIND = "kind";
private static final String AUTHOR = "author";
private static final int NB_PAGES = 12;
private static final String DIRECTOR = "director";
private static final String SCENARIST = "scenarist";
private static final int DURATION = 15;
private static final String COMMENT = "comment";
/**
* Adds members, books, films and reviews to the ISocialNetwork, according to the following process :<br>
* 1. Adds the asked number of members, giving them pseudo-random logins/passwords<br>
* 2. Make one of the added members to add the total asked number of books, giving them pseudo-random characteristics<br>
* 3. Make one of the added members to add the total asked number of films, giving them pseudo-random characteristics<br>
* 4. Add the asked number of reviews : <br>
* - reviewed items (book/film) and reviews'authors are randomly chosen among those that were added by Tools<br>
* - depending on "chance", a member can add a review on an item that he already had reviewed, which will replace this previous review. It is therefore not guaranteed that the total number of reviews stored in the ISocialNetwork will have a nbReviews increase.<br>
* @param sn the ISocialNetwork to work with
* @param nbMembers the number of members to add to sn.
* @param nbBooks the number of books to add to sn
* @param nbFilms the number of films to add to sn
* @param nbReviews the number of reviews to add to sn
* @return meantime of each operation
* @throws java.lang.Exception whenever one of the calls to addMember(), addItemBook(), addItemFilm(), reviewItemBook() or reviewItemFilm() throws an exception, this exception is directly rethrown to the caller. That's why these methods should have been well tested before trying to use populate().
**/
public static long populate (ISocialNetwork sn, int nbMembers, int nbBooks, int nbFilms, int nbReviews)
throws BadEntryException, MemberAlreadyExistsException, NotMemberException, ItemBookAlreadyExistsException, ItemFilmAlreadyExistsException, NotItemException {
long elapsedTime=0;
long before, after; //timestamps
int totalNbOfOperations = nbMembers + nbBooks +nbFilms + nbReviews;
randomGenerator = new java.util.Random();
//
// Adds nbNumber members, giving them pseudo-random logins/passwords
//
for (int i=0; i<nbMembers;i++) {
before = System.nanoTime();
sn.addMember(LOGIN+nextMember, PASSWORD+nextMember, PROFILE);
after = System.nanoTime();
elapsedTime += after - before;
nextMember++;
}
//System.out.println("Temps en nano seconde écoulé pour addMember "+elapsedTime);
//
// Make the last added member to add the total asked number of books, giving them pseudo-random characteristics
//
if (nbBooks>0) {
//we don't care about WHO adds the books, so we only use last added member to do it
if (nextMember==1) throw new BadEntryException ("populate() : must add at least a member before being able to add books");
for (int i=0;i<nbBooks;i++) {
int lastMemberNumber = nextMember-1;
before = System.nanoTime();
sn.addItemBook(LOGIN+lastMemberNumber, PASSWORD+lastMemberNumber, TITLE+nextBook, KIND, AUTHOR, NB_PAGES);
after = System.nanoTime();
elapsedTime += after - before;
nextBook++;
}
//System.out.println("Temps en nano seconde écoulé pour addItembook :" + elapsedTime);
}
//
// Make one of the added members to add the total asked number of films, giving them pseudo-random characteristics
//
if (nbFilms>0) {
// we don't care about WHO adds the films, so we only use last added member to do it
if (nextMember==1) throw new BadEntryException ("populate(): must add at least 1 member before being able to add films");
for (int i=0;i<nbFilms;i++) {
int lastMemberNumber = nextMember-1;
before = System.nanoTime();
sn.addItemFilm(LOGIN+lastMemberNumber, PASSWORD+lastMemberNumber, TITLE+nextFilm, KIND, DIRECTOR, SCENARIST, DURATION);
after = System.nanoTime();
elapsedTime += after - before;
nextFilm++;
}
//System.out.println("Temps en nano seconde écoulé pour addItemFilm :" + elapsedTime);
}
//
// Add the asked number of reviews :
// - author of each review is randomly chosen among those that were added in step 1 ;
// - reviewed items (book/film) and reviews'authors are randomly chosen among those that were added in steps 1, 2 and 3
// - depending on "chance", a member can add a review on an item that he already had reviewed, which will replace this previous review.
// It is therefore not guaranteed that the total number of reviews stored in the ISocialNetwork will have a nbReviews increase.
//
if (nbReviews>0) {
// try to fairly distribute reviews over members that were generated this time and previously
// try to fairly distribute reviews among items
if (nextMember==1) throw new BadEntryException ("populate() : must add at least a member before being able to add reviews");
if ((nextBook==1)&&(nextFilm==1)) throw new BadEntryException ("populate() : must add at least an item before being able to add reviews");
int currentMemberNum; // number of the member that do the review
int itemNum; // number of the item that is reviewed
// reviews will be splited between books reviews and film reviews
int nbBookReviews, nbFilmReviews; // number of book reviews and film reviews
nbBookReviews = nextBook * (nbReviews / (nextBook + nextFilm)) ; // proportional distribution
nbFilmReviews = nbReviews - nbBookReviews;
currentMemberNum = nextMember-1; // work downward so as to have new members contribute first
for (int i = 0; i<nbBookReviews;i++) {// add a book review
itemNum = 1 + randomGenerator.nextInt(nextBook-2); // choose randomly a book to review - some tricks here, dealing with boundaries
before = System.nanoTime();
sn.reviewItemBook(LOGIN+currentMemberNum, PASSWORD+currentMemberNum, TITLE+itemNum, 5*randomGenerator.nextFloat(), COMMENT);
currentMemberNum = (currentMemberNum>1) ? currentMemberNum-1 : nextMember-1; // decrementing modulo nextMember
after = System.nanoTime();
elapsedTime += after - before;
nbReviews--;
}
//System.out.println("Temps en nano seconde écoulé pour reviewitembook :" + elapsedTime);
for (int i = 0; i<nbFilmReviews;i++) {// add a film review
itemNum = 1 + randomGenerator.nextInt(nextFilm-2); // choose randomly a film to review - some tricks here, dealing with boundaries
before = System.nanoTime();
sn.reviewItemFilm(LOGIN+currentMemberNum, PASSWORD+currentMemberNum, TITLE+itemNum, 5*randomGenerator.nextFloat(), COMMENT);
currentMemberNum = (currentMemberNum>1) ? currentMemberNum-1 : nextMember-1; // decrementing modulo nextMember
after = System.nanoTime();
elapsedTime += after - before;
nbReviews--;
}
//System.out.println("Temps en nano seconde écoulé pour reviewitemFIlm :" + elapsedTime);
}
return elapsedTime/totalNbOfOperations;
}
public static void main(String[] args) {
ISocialNetwork sn = new SocialNetwork();
// Test addMember()
System.out.println("Testing addMember()");
for (int i = 1; i <= 10; i++) {
testAddMember(sn, i * 1000);
}
// Test addItemBook()
System.out.println("Testing addItemBook()");
for (int i = 1; i <= 10; i++) {
testAddItemBook(sn, i * 1000);
}
// Test addItemFilm()
System.out.println("Testing addItemFilm()");
for (int i = 1; i <= 10; i++) {
testAddItemFilm(sn, i * 1000);
}
// Test reviewItemBook()
System.out.println("Testing reviewItemBook()");
for (int i = 1; i <= 10; i++) {
testReviewItemBook(sn, i * 1000);
}
// Test reviewItemFilm()
System.out.println("Testing reviewItemFilm()");
for (int i = 1; i <= 10; i++) {
testReviewItemFilm(sn, i * 1000);
}
}
public static void testAddMember(ISocialNetwork sn, int numMembers) {
try {
long averageTime = Tools.populate(sn, numMembers, 0, 0, 0);
System.out.println("Average time for adding " + numMembers + " members: " + averageTime + " ns");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testAddItemBook(ISocialNetwork sn, int numBooks) {
try {
Tools.populate(sn, 1, 0, 0, 0);
long averageTime = Tools.populate(sn, 0, numBooks, 0, 0);
System.out.println("Average time for adding " + numBooks + " books: " + averageTime + " ns");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testAddItemFilm(ISocialNetwork sn, int numFilms) {
try {
Tools.populate(sn, 1, 0, 0, 0);
long averageTime = Tools.populate(sn, 0, 0, numFilms, 0);
System.out.println("Average time for adding " + numFilms + " films: " + averageTime + " ns");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testReviewItemBook(ISocialNetwork sn, int numReviews) {
try {
Tools.populate(sn, 5, 5, 0, 0); // Garantiza suficientes datos
long averageTime = Tools.populate(sn, 0, 0, 0, numReviews);
System.out.println("Average for create " + numReviews + " reviews of books: " + averageTime + " ns");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testReviewItemFilm(ISocialNetwork sn, int numReviews) {
try {
Tools.populate(sn, 1, 0, 1, 0);
long averageTime = Tools.populate(sn, 0, 0, 0, numReviews);
System.out.println("Average time for reviewing " + numReviews + " films: " + averageTime + " ns");
} catch (Exception e) {
e.printStackTrace();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment