#include #include "dlist.h" bool is_positive(int data) { return data > 0; } bool is_even(int data) { return (data % 2) == 0; } bool is_odd(int data) { return !is_even(data); } int main(int argc, char** argv) { printf("Running tests...\n"); int num_errors = 0; printf("creating empty list...\n"); DList* dl1 = new_dlist(); // test that the empty list is, in fact, empty if (first_dlink(dl1) == NULL) { printf("OK: first_dlink(new_dlist()) == NULL\n"); } else { printf("ERROR: first_dlink(new_dlist()) != NULL\n"); num_errors ++; } if (last_dlink(dl1) == NULL) { printf("OK: last_dlink(new_dlist()) == NULL\n"); } else { printf("ERROR: last_dlink(new_dlist()) != NULL\n"); num_errors ++; } // test additions at the first and back printf("adding before and after...\n"); add_before_first(dl1, 3); add_before_first(dl1, 2); add_after_last(dl1, 4); add_after_last(dl1, 5); add_before_first(dl1, 1); add_after_last(dl1, 6); // test that forward iterations work (and that the additions did // what we expected) int i; DLink* n; printf("starting from first_dlink(...)...\n"); for (n = first_dlink(dl1), i = 1; n != NULL; n = next_dlink(n), i++) { if (get_data(n) == i) { printf("OK: get_data(next_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(next_dlink(...)) != %d\n", i); num_errors ++; } } if (i == 7) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // test that reverse iterations work (and that the additions did // what we expected) printf("starting from last_dlink(...)...\n"); for (n = last_dlink(dl1), i = 6; n != NULL; n = prev_dlink(n), i--) { if (get_data(n) == i) { printf("OK: get_data(prev_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(prev_dlink(...)) != %d\n", i); num_errors ++; } } if (i == 0) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // deleting this list printf("deleting...\n"); delete_dlist(dl1); // restarting printf("creating empty list...\n"); dl1 = new_dlist(); // test additions at the back and front printf("adding after and before...\n"); add_after_last(dl1, 4); add_after_last(dl1, 5); add_before_first(dl1, 3); add_before_first(dl1, 2); add_after_last(dl1, 6); add_before_first(dl1, 1); // test that forward iterations work (and that the additions did // what we expected) printf("starting from first_dlink(...)...\n"); for (n = first_dlink(dl1), i = 1; n != NULL; n = next_dlink(n), i++) { if (get_data(n) == i) { printf("OK: get_data(next_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(next_dlink(...)) != %d\n", i); num_errors ++; } } if (i == 7) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // test that reverse iterations work (and that the additions did // what we expected) printf("starting from last_dlink(...)...\n"); for (n = last_dlink(dl1), i = 6; n != NULL; n = prev_dlink(n), i--) { if (get_data(n) == i) { printf("OK: get_data(prev_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(prev_dlink(...)) != %d\n", i); num_errors ++; } } if (i == 0) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // insert before and after some links printf("inserting before and after...\n"); DLink* n1 = insert_before(dl1, first_dlink(dl1), -3); DLink* n2 = insert_after(dl1, n1, -1); DLink* n3 = insert_before(dl1, n2, -2); DLink* n4 = insert_before(dl1, n1, -4); DLink* n5 = insert_after(dl1, n2, 0); n1 = insert_after(dl1, last_dlink(dl1), 10); n2 = insert_before(dl1, n1, 8); n3 = insert_after(dl1, n2, 9); n4 = insert_after(dl1, n1, 11); n5 = insert_before(dl1, n2, 7); // test that forward iterations work (and that the additions did // what we expected) printf("starting from first_dlink(...)...\n"); for (n = first_dlink(dl1), i = -4; n != NULL; n = next_dlink(n), i++) { int d = get_data(n); if (d == i) { printf("OK: get_data(next_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(next_dlink(...)) == %d != %d\n", d, i); num_errors ++; } } if (i == 12) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // test that reverse iterations work (and that the additions did // what we expected) printf("starting from last_dlink(...)...\n"); for (n = last_dlink(dl1), i = 11; n != NULL; n = prev_dlink(n), i--) { int d = get_data(n); if (d == i) { printf("OK: get_data(prev_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(prev_dlink(...)) == %d != %d\n", d, i); num_errors ++; } } if (i == -5) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // test deleting links printf("removing links...\n"); delete_dlink(dl1, first_dlink(dl1)); delete_dlink(dl1, first_dlink(dl1)); delete_dlink(dl1, last_dlink(dl1)); delete_dlink(dl1, last_dlink(dl1)); delete_dlink(dl1, next_dlink(next_dlink(next_dlink(first_dlink(dl1))))); // test that forward iterations work (and that the additions did // what we expected) printf("starting from first_dlink(...)...\n"); for (n = first_dlink(dl1), i = -2; n != NULL; n = next_dlink(n), i++) { int d = get_data(n); if (d == i) { printf("OK: get_data(next_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(next_dlink(...)) == %d != %d\n", d, i); num_errors ++; } if (i == 0) i++; } if (i == 10) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // test that reverse iterations work (and that the additions did // what we expected) printf("starting from last_dlink(...)...\n"); for (n = last_dlink(dl1), i = 9; n != NULL; n = prev_dlink(n), i--) { int d = get_data(n); if (d == i) { printf("OK: get_data(prev_dlink(...)) == %d\n", i); } else { printf("ERROR: get_data(prev_dlink(...)) == %d != %d\n", d, i); num_errors ++; } if (i == 2) i--; } if (i == -3) { printf("OK: right number of links\n"); } else { printf("ERROR: wrong number of links\n"); num_errors ++; } // find first positive elem [REMOVE THIS if extra credit not implemented] printf("finding first positive elem...\n"); n = find(dl1, &is_positive); if (n == NULL) { printf("ERROR: didn't find a positive elem\n"); num_errors ++; } else if (get_data(n) != 2) { printf("ERROR: found an unexpected elem (%d)\n", get_data(n)); num_errors ++; } else { printf("OK: found it\n"); } printf("finding first odd elem...\n"); n = find(dl1, &is_odd); if (n == NULL) { printf("ERROR: didn't find an odd elem\n"); num_errors ++; } else if (get_data(n) != -1) { printf("ERROR: found an unexpected elem (%d)\n", get_data(n)); num_errors ++; } else { printf("OK: found it\n"); } printf("finding first even elem...\n"); n = find(dl1, &is_even); if (n == NULL) { printf("ERROR: didn't find an even elem\n"); num_errors ++; } else if (get_data(n) != -2) { printf("ERROR: found an unexpected elem (%d)\n", get_data(n)); num_errors ++; } else { printf("OK: found it\n"); } // deleting this list printf("deleting...\n"); delete_dlist(dl1); // restarting printf("creating empty list...\n"); dl1 = new_dlist(); // insert a single element printf("inserting link...\n"); add_after_last(dl1, 4); // delete the last element printf("removing link...\n"); delete_dlink(dl1, first_dlink(dl1)); // test that the list is now empty if (first_dlink(dl1) == NULL) { printf("OK: first_dlink(...) == NULL\n"); } else { printf("ERROR: first_dlink(...) != NULL\n"); num_errors ++; } if (last_dlink(dl1) == NULL) { printf("OK: last_dlink(...) == NULL\n"); } else { printf("ERROR: last_dlink(...) != NULL\n"); num_errors ++; } // deleting this list printf("deleting...\n"); delete_dlist(dl1); if (num_errors == 0) { printf("All tests passed!\n"); return 0; } else { printf("There were %d errors\n", num_errors); return -1; } }