Skip to content

Commit

Permalink
Updated tests to match new get_website
Browse files Browse the repository at this point in the history
  • Loading branch information
FredTheNoob committed Dec 21, 2021
1 parent 9514d79 commit 27eaa6d
Showing 1 changed file with 16 additions and 61 deletions.
77 changes: 16 additions & 61 deletions Tests/Files/WebsiteTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Website *get_website_modified(Website *websites, const short NUM_WEBSITES, short previous_website_id, double rand_0_1);

void test_get_website_no_previous(CuTest *tc) {
void test_get_website_not_previous(CuTest *tc) {
SimulationInput sim_input;
sim_input.num_websites = 50;

Expand All @@ -21,92 +21,47 @@ void test_get_website_no_previous(CuTest *tc) {
CuAssertIntEquals(tc, expected_id, actual_id);
}

void test_get_website_different_from_previous(CuTest *tc) {
void test_get_website_is_previous(CuTest *tc) {
SimulationInput sim_input;
sim_input.num_websites = 50;

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, sim_input.num_websites, 0, 0.3);

int actual_id = website->id;
int expected_id = 5;

CuAssertIntEquals(tc, expected_id, actual_id);
}

void test_get_website_not_last(CuTest *tc) {
SimulationInput sim_input;
sim_input.num_websites = 50;

Website websites[sim_input.num_websites];
load_websites(websites, &sim_input);

Website *website = get_website_modified(websites, sim_input.num_websites, 0, 0.38);

int actual_id = website->id;
int expected_id = 1;
int expected_id = 0;

CuAssertIntEquals(tc, expected_id, actual_id);
}

void test_get_website_last(CuTest *tc) {

SimulationInput sim_input;
sim_input.num_websites = 50;

Website websites[sim_input.num_websites];
load_websites(websites, &sim_input);

Website *website = get_website_modified(websites, sim_input.num_websites, 49, 0.998);

int actual_id = website->id;
int expected_id = 48;

CuAssertIntEquals(tc, expected_id, actual_id);
/* If the function does not return the same index again, we know it works since we shouldn't be able to get previous again */
CuAssertTrue(tc, actual_id != expected_id);
}

Website *get_website_modified(Website *websites, const short NUM_WEBSITES, short previous_website_id, double rand_0_1) {
double propabilities_sum = 0;
double probabilities_sum = 0;
int i = 0;

/* Iterates through websites until the sum exceeds the random number */
while (propabilities_sum < rand_0_1 && i < NUM_WEBSITES - 1) {
propabilities_sum += websites[i].weight;
while (probabilities_sum < rand_0_1 && i < NUM_WEBSITES - 1) {
probabilities_sum += websites[i].weight;
i++;
}

/* If we have no previous id */
if (previous_website_id == -1) {
if (i - 1 != previous_website_id) {
return &websites[i - 1];
}
/* If i is not the previous id */
else if (i != previous_website_id) {
return &websites[i];
}
/* If i is not the last index */
else if (i != NUM_WEBSITES - 1) {
return &websites[i + 1];
}
/* If i is the last index (very unlikely) */
else if (i == NUM_WEBSITES - 1) {
return &websites[i - 1];
}
/* Should not happen */
else {
printf("[ERROR] Failed to get website in website.c");
exit(EXIT_FAILURE);
else {
return get_website(websites, NUM_WEBSITES, previous_website_id);
}
}

CuSuite* websiteGetSuite() {
srand(time(NULL));

CuSuite* suite = CuSuiteNew();

SUITE_ADD_TEST(suite, test_get_website_no_previous);
SUITE_ADD_TEST(suite, test_get_website_different_from_previous);
SUITE_ADD_TEST(suite, test_get_website_not_last);
SUITE_ADD_TEST(suite, test_get_website_last);
SUITE_ADD_TEST(suite, test_get_website_not_previous);
SUITE_ADD_TEST(suite, test_get_website_is_previous);

return suite;
}

0 comments on commit 27eaa6d

Please sign in to comment.