From 27eaa6df0a1a7ceabb27bb3275235cc6096269a0 Mon Sep 17 00:00:00 2001 From: FredTheNoob Date: Tue, 21 Dec 2021 08:57:49 +0100 Subject: [PATCH] Updated tests to match new get_website --- Tests/Files/WebsiteTest.c | 77 ++++++++------------------------------- 1 file changed, 16 insertions(+), 61 deletions(-) diff --git a/Tests/Files/WebsiteTest.c b/Tests/Files/WebsiteTest.c index 6c4d2b2..53133e7 100644 --- a/Tests/Files/WebsiteTest.c +++ b/Tests/Files/WebsiteTest.c @@ -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; @@ -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; } \ No newline at end of file