Skip to content

Commit

Permalink
- Removed parameter from get_website
Browse files Browse the repository at this point in the history
- Added out of bounds test function
  • Loading branch information
FredTheNoob committed Dec 21, 2021
1 parent 27eaa6d commit 87129a7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
35 changes: 26 additions & 9 deletions Tests/Files/WebsiteTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
#include <time.h>

#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;
Expand All @@ -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;
Expand All @@ -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++;
}
Expand All @@ -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);
}
}

Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions src/lib/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions src/lib/website.c
Original file line number Diff line number Diff line change
@@ -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++;
}
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/website.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 87129a7

Please sign in to comment.