/*---------------------------------------------------------- * HTBLA-Leonding * ---------------------------------------------------------- * Description: * Test functions for customer_list. * ---------------------------------------------------------- */ #include #include "shortcut.h" #include "customer_list.h" #include "test_customer_list.h" #define TEST_ENTRIES_COUNT 13 struct CustomerTestData { int id; const char* name; int revenue; }; struct CustomerTestData customers[] = { /* id, name, revenue */ {200501, "Fabasoft", 5432}, // 0 {201005, "CountIT", 3893}, // 1 {199922, "e-punkt", 4198}, // 2 {200349, "karriere.at", 745}, // 3 {201893, "Google", 2934}, // 4 {200745, "Apple", 1034}, // 5 {200814, "Amazon", 987}, // 6 {201624, "Samsung", 45}, // 7 {201402, "Dynatrace", 857}, // 8 {200911, "BBEdit", 194}, // 9 {201090, "IBM", 893}, // 10 {200296, "AV Stumpfl", 207}, // 11 {201818, "Bosch", 1099}, // 12 }; // #define MORE_ENTRIES_COUNT 12 // static struct CustomerListEntryData *more_entries[] = { // &p2, &p3, &p4, &p5, &p6, &p7, &p8, &p9, &p10, &p11, &p12, &p13 // }; static CustomerList customer_list; /* ---------------------------- Public functions ------------------------------------- */ TEST(test_init_customer_list) { ASSERT_TRUE(get_length(0) < 0, "An invalid list shall provide a negative length"); customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); } static CustomerListEntry create_test_customer(int idx) { return create_customer(customers[idx].id, customers[idx].name, customers[idx].revenue); } TEST(test_add_entry) { bool success = add_customer(0, customers[0].id, customers[0].name, customers[0].revenue); ASSERT_FALSE(success, "Adding a customer to an invalid list must not be possible"); ASSERT_TRUE(get_customer_id(0) < 0, "The ID of an invalid customer must be less than 0"); customer_list = init_customer_list(); success = add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); ASSERT_TRUE(success, "Adding first entry must be possible"); CustomerListEntry res = get_customer_with_index(customer_list, 0); if (res != 0) { ASSERT_EQUALS(200501, get_customer_id(res)); } ASSERT_EQUALS(1, get_length(customer_list)); } /* ---------------------------- Private function ------------------------------------ */ /** make_list_full adds test entries until the grade list is full *** @return True if filling is possible, FALSE otherwise */ static bool make_list_full(); TEST(test_add_entries) { customer_list = init_customer_list(); CustomerListEntry one_entry[] = { create_test_customer(0) }; bool adding_possible = add_customers(customer_list, one_entry, 1); ASSERT_TRUE(adding_possible, "Adding of one entry in empty list must be possible"); ASSERT_EQUALS(1, get_length(customer_list)); customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); adding_possible = make_list_full(); ASSERT_TRUE(adding_possible, "Making an empty list full must be possible"); ASSERT_EQUALS(MAX_LIST_ENTRIES, get_length(customer_list)); CustomerListEntry customer = get_customer_with_index(customer_list, MAX_LIST_ENTRIES - 1); ASSERT_TRUE(customer != 0, "A customer shall be provided"); ASSERT_EQUALS(customers[MAX_LIST_ENTRIES - 1].id, get_customer_id(customer)); adding_possible = add_customers(customer_list, one_entry, 1); ASSERT_FALSE(adding_possible, "Adding entries in full list must NOT be possible"); ASSERT_EQUALS(MAX_LIST_ENTRIES, get_length(customer_list)); } static bool make_list_full() { const int size = MAX_LIST_ENTRIES - get_length(customer_list); CustomerListEntry entries[size]; int start_idx = get_length(customer_list); for(int i = start_idx; i < MAX_LIST_ENTRIES; i++) { entries[i - start_idx] = create_test_customer(i); } return add_customers(customer_list, entries, size); } TEST(test_get_customer_with_id_when_customer_list_is_empty) { customer_list = init_customer_list(); CustomerListEntry customer = get_customer_with_id(customer_list, 1); ASSERT_TRUE(customer == 0, "No customer must be provided if the list is empty"); } TEST(test_get_customer_with_id_with_one_element) { customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); ASSERT_EQUALS(1, get_length(customer_list)); CustomerListEntry customer = get_customer_with_id(customer_list, customers[0].id); ASSERT_TRUE(customer != 0, "A customer shall be provided"); ASSERT_EQUALS(customers[0].id, get_customer_id(customer)); } TEST(test_get_customer_with_id_when_list_is_full) { customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); make_list_full(); CustomerListEntry customer = get_customer_with_id(customer_list, customers[5].id); ASSERT_TRUE(customer != 0, "A customer shall be provided"); ASSERT_EQUALS(customers[5].id, get_customer_id(customer)); } TEST(test_get_customers_with_revenue) { CustomerListEntry result_list[MAX_LIST_ENTRIES]; int length = -1; length = get_customers_with_revenue(0, 0, 10000, result_list, MAX_LIST_ENTRIES); ASSERT_EQUALS(0, length); customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); length = get_customers_with_revenue(customer_list, 0, 10000, result_list, MAX_LIST_ENTRIES); ASSERT_EQUALS(0, length); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); length = get_customers_with_revenue(customer_list, 0, 5431, result_list, MAX_LIST_ENTRIES); ASSERT_EQUALS(0, length); length = get_customers_with_revenue(customer_list, 0, 5432, result_list, MAX_LIST_ENTRIES); ASSERT_EQUALS(1, length); make_list_full(); length = get_customers_with_revenue(customer_list, 893, 1099, result_list, MAX_LIST_ENTRIES); ASSERT_EQUALS(4, length); if (length == 4) { ASSERT_EQUALS(200745, get_customer_id(result_list[0])); ASSERT_EQUALS(200814, get_customer_id(result_list[1])); ASSERT_EQUALS(201090, get_customer_id(result_list[2])); ASSERT_EQUALS(201818, get_customer_id(result_list[3])); } } TEST(test_get_customer_with_highest_revenue) { CustomerListEntry customer = 0; customer = get_customer_with_highest_revenue(0); ASSERT_TRUE(customer == 0, "Invalid customer list can't return any customer, 0 expected"); customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); customer = get_customer_with_highest_revenue(customer_list); ASSERT_TRUE(customer == 0, "Empty customer list can't return any customer, 0 expected"); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); customer = get_customer_with_highest_revenue(customer_list); ASSERT_TRUE(customer != 0, "Customer list with one customer must return first element"); ASSERT_EQUALS(customers[0].id, get_customer_id(customer)); make_list_full(); customer = get_customer_with_highest_revenue(customer_list); ASSERT_TRUE(customer != 0, "Full customer list must return a highest revenue customer"); ASSERT_EQUALS(200501, get_customer_id(customer)); } TEST(test_get_customer_with_lowest_revenue) { CustomerListEntry customer = 0; customer = get_customer_with_lowest_revenue(0); ASSERT_TRUE(customer == 0, "Invalid customer list can't return any customer, 0 expected"); customer_list = init_customer_list(); ASSERT_EQUALS(0, get_length(customer_list)); customer = get_customer_with_lowest_revenue(customer_list); ASSERT_TRUE(customer == 0, "Empty customer list can't return any customer, 0 expected"); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); customer = get_customer_with_lowest_revenue(customer_list); ASSERT_TRUE(customer != 0, "Customer list with one customer must return first element"); ASSERT_EQUALS(customers[0].id, get_customer_id(customer)); make_list_full(); customer = get_customer_with_lowest_revenue(customer_list); ASSERT_TRUE(customer != 0, "Full customer list must return a lowest revenue customer"); ASSERT_EQUALS(201624, get_customer_id(customer)); } TEST(test_get_top_n_customers_revenue) { CustomerListEntry result_list[MAX_LIST_ENTRIES]; int length = -1; customer_list = init_customer_list(); length = get_top_n_customers_revenue(customer_list, 5, result_list); ASSERT_EQUALS(0, length); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); length = get_top_n_customers_revenue(customer_list, 5, result_list); ASSERT_EQUALS(1, length); ASSERT_EQUALS(customers[0].id, get_customer_id(result_list[0])); make_list_full(); length = get_top_n_customers_revenue(customer_list, 1, result_list); ASSERT_EQUALS(1, length); ASSERT_EQUALS(200501, get_customer_id(result_list[0])); length = get_top_n_customers_revenue(customer_list, 5, result_list); ASSERT_EQUALS(5, length); ASSERT_EQUALS(200501, get_customer_id(result_list[0])); ASSERT_EQUALS(199922, get_customer_id(result_list[1])); ASSERT_EQUALS(201005, get_customer_id(result_list[2])); ASSERT_EQUALS(201893, get_customer_id(result_list[3])); ASSERT_EQUALS(201818, get_customer_id(result_list[4])); length = get_top_n_customers_revenue(customer_list, 0, result_list); ASSERT_EQUALS(0, length); length = get_top_n_customers_revenue(customer_list, TEST_ENTRIES_COUNT + 1, result_list); ASSERT_EQUALS(TEST_ENTRIES_COUNT, length); length = get_top_n_customers_revenue(0, TEST_ENTRIES_COUNT + 1, result_list); ASSERT_TRUE(length <= 0, "An invalid list must indicate 0 or less found customers"); } TEST(test_get_bottom_n_customers_revenue) { CustomerListEntry result_list[MAX_LIST_ENTRIES]; int length = -1; customer_list = init_customer_list(); length = get_bottom_n_customers_revenue(customer_list, 5, result_list); ASSERT_EQUALS(0, length); add_customer(customer_list, customers[0].id, customers[0].name, customers[0].revenue); length = get_bottom_n_customers_revenue(customer_list, 5, result_list); ASSERT_EQUALS(1, length); ASSERT_EQUALS(customers[0].id, get_customer_id(result_list[0])); make_list_full(); length = get_bottom_n_customers_revenue(customer_list, 1, result_list); ASSERT_EQUALS(1, length); ASSERT_EQUALS(201624, get_customer_id(result_list[0])); length = get_bottom_n_customers_revenue(customer_list, 7, result_list); ASSERT_EQUALS(7, length); ASSERT_EQUALS(201624, get_customer_id(result_list[0])); ASSERT_EQUALS(200911, get_customer_id(result_list[1])); ASSERT_EQUALS(200296, get_customer_id(result_list[2])); ASSERT_EQUALS(200349, get_customer_id(result_list[3])); ASSERT_EQUALS(201402, get_customer_id(result_list[4])); ASSERT_EQUALS(201090, get_customer_id(result_list[5])); ASSERT_EQUALS(200814, get_customer_id(result_list[6])); length = get_bottom_n_customers_revenue(customer_list, 0, result_list); ASSERT_EQUALS(0, length); length = get_bottom_n_customers_revenue(customer_list, TEST_ENTRIES_COUNT + 1, result_list); ASSERT_EQUALS(TEST_ENTRIES_COUNT, length); length = get_bottom_n_customers_revenue(0, TEST_ENTRIES_COUNT + 1, result_list); ASSERT_TRUE(length <= 0, "An invalid list must indicate 0 or less found customers"); }