diff --git a/Tests/Files/WebsiteTest.c b/Tests/Files/WebsiteTest.c index 53133e7..38d39c0 100644 --- a/Tests/Files/WebsiteTest.c +++ b/Tests/Files/WebsiteTest.c @@ -3,17 +3,18 @@ #include #define AVG_USER_TIME 3600 +#define NUM_WEBSITES 50 -Website *get_website_modified(Website *websites, const short NUM_WEBSITES, short previous_website_id, double rand_0_1); +Website *get_website_modified(Website *websites, short previous_website_id, double rand_0_1); void test_get_website_not_previous(CuTest *tc) { SimulationInput sim_input; - sim_input.num_websites = 50; + sim_input.num_websites = NUM_WEBSITES; Website websites[sim_input.num_websites]; load_websites(websites, &sim_input); - Website *website = get_website_modified(websites, sim_input.num_websites, -1, 0.684); + Website *website = get_website_modified(websites, -1, 0.684); int actual_id = website->id; int expected_id = 4; @@ -23,12 +24,12 @@ void test_get_website_not_previous(CuTest *tc) { void test_get_website_is_previous(CuTest *tc) { SimulationInput sim_input; - sim_input.num_websites = 50; + sim_input.num_websites = NUM_WEBSITES; Website websites[sim_input.num_websites]; load_websites(websites, &sim_input); - Website *website = get_website_modified(websites, sim_input.num_websites, 0, 0.3); + Website *website = get_website_modified(websites, 0, 0.3); int actual_id = website->id; @@ -38,11 +39,26 @@ void test_get_website_is_previous(CuTest *tc) { CuAssertTrue(tc, actual_id != expected_id); } -Website *get_website_modified(Website *websites, const short NUM_WEBSITES, short previous_website_id, double rand_0_1) { +void test_get_website_out_of_bounds(CuTest *tc) { + SimulationInput sim_input; + sim_input.num_websites = NUM_WEBSITES; + + Website websites[sim_input.num_websites]; + load_websites(websites, &sim_input); + + Website *website = get_website_modified(websites, -1, 0.99999999999999); + + int actual_id = website->id; + int expected_id = 49; + + CuAssertIntEquals(tc, expected_id, actual_id); +} + +Website *get_website_modified(Website *websites, short previous_website_id, double rand_0_1) { double probabilities_sum = 0; int i = 0; - while (probabilities_sum < rand_0_1 && i < NUM_WEBSITES - 1) { + while (probabilities_sum < rand_0_1) { probabilities_sum += websites[i].weight; i++; } @@ -51,7 +67,7 @@ Website *get_website_modified(Website *websites, const short NUM_WEBSITES, short return &websites[i - 1]; } else { - return get_website(websites, NUM_WEBSITES, previous_website_id); + return get_website(websites, previous_website_id); } } @@ -62,6 +78,7 @@ CuSuite* websiteGetSuite() { SUITE_ADD_TEST(suite, test_get_website_not_previous); SUITE_ADD_TEST(suite, test_get_website_is_previous); - + SUITE_ADD_TEST(suite, test_get_website_out_of_bounds); + return suite; } \ No newline at end of file diff --git a/src/lib/user.c b/src/lib/user.c index bdb2b0b..37f4e7c 100644 --- a/src/lib/user.c +++ b/src/lib/user.c @@ -21,11 +21,11 @@ void handle_user(User *user, Website *websites, WebsiteNode **linked_websites, c while (below_daily_time) { if (user->current_website == NULL) { /* Returns the first website of the day */ - website = get_website(websites, NUM_WEBSITES, NO_WEBSITE_ID); + website = get_website(websites, NO_WEBSITE_ID); } else { /* Returns a new website */ - website = get_website(websites, NUM_WEBSITES, user->current_website->id); + website = get_website(websites, user->current_website->id); } /* If the website's matrix == NULL it is the most sustainable website in the category */ diff --git a/src/lib/website.c b/src/lib/website.c index f1a85a0..da8215d 100644 --- a/src/lib/website.c +++ b/src/lib/website.c @@ -1,14 +1,14 @@ #include "website.h" /* Returns a website based on weighted probability and the previous website's id (if any) */ -Website *get_website(Website *websites, const short NUM_WEBSITES, short previous_website_id) { +Website *get_website(Website *websites, short previous_website_id) { /* Generate random number between 0 and 1 */ double rand_0_1 = (double)rand() / (double)RAND_MAX; double probabilities_sum = 0; int i = 0; /* Iterates through websites until the sum exceeds the random number */ - while (probabilities_sum < rand_0_1 && i < NUM_WEBSITES - 1) { + while (probabilities_sum < rand_0_1) { probabilities_sum += websites[i].weight; i++; } @@ -19,7 +19,7 @@ Website *get_website(Website *websites, const short NUM_WEBSITES, short previous } /* If i == previous id */ else { - return get_website(websites, NUM_WEBSITES, previous_website_id); + return get_website(websites, previous_website_id); } } diff --git a/src/lib/website.h b/src/lib/website.h index 54dc138..9896918 100644 --- a/src/lib/website.h +++ b/src/lib/website.h @@ -55,7 +55,7 @@ struct WebsiteAlternative { }; typedef struct WebsiteAlternative WebsiteAlternative; -Website *get_website(Website *websites, const short NUM_WEBSITES, short previous_website_id); +Website *get_website(Website *websites, short previous_website_id); WebsiteCategory get_category(char *category);