#include #include #include // Section 1: replaced by above include list.hfa //////////////////////////////////////////////////////////// // // Section 2 // // Structure definitions // // Example of declaration-side user code // //////////////////////////////////////////////////////////// // a fred belongs to two doubly-linked lists: mine and yours struct fred { float adatum; DLISTED_MGD_EXPL_IN(fred, mine) DLISTED_MGD_EXPL_IN(fred, yours) }; DLISTED_MGD_EXPL_OUT(fred, mine) DLISTED_MGD_EXPL_OUT(fred, yours) void ?{}(fred &this, float adatum) { (this.adatum){adatum}; } // a mary belongs to just one doubly-linked list: hers struct mary { float anotherdatum; DLISTED_MGD_IMPL_IN(mary) }; DLISTED_MGD_IMPL_OUT(mary) void ?{}(mary &this, float anotherdatum) { (this.anotherdatum){anotherdatum}; } //////////////////////////////////////////////////////////// // // Section 3 // // Test helpers to traverse and print lists. // // These consume framework-provided accessor functions and // do not modify their parameter lists. // //////////////////////////////////////////////////////////// void printMyFredsFwd(fred &f) { while (&f != 0p) { sout | f.adatum; &f = &f`in_mine`next; } } void printMyFredsRev(fred &f) { while (&f != 0p) { sout | f.adatum; &f = &f`in_mine`prev; } } void printMyFreddies(fred &f1, fred &f2, int isBefore) { if (isBefore) { sout | "==== fred by MINE before "; } else { sout | "==== fred by MINE after "; } printMyFredsFwd(f1); sout | '-'; printMyFredsRev(f1); sout | '-'; printMyFredsFwd(f2); sout | '-'; printMyFredsRev(f2); sout | '-'; } void printYourFredsFwd(fred &f) { while (&f != 0p) { sout | f.adatum; &f = &f`in_yours`next; } } void printYourFredsRev(fred &f) { while (&f != 0p) { sout | f.adatum; &f = &f`in_yours`prev; } } void printYourFreddies(fred &f1, fred &f2, int isBefore) { if (isBefore) { sout | "==== fred by YOURS before "; } else { sout | "==== fred by YOURS after "; } printYourFredsFwd(f1); sout | '-'; printYourFredsRev(f1); sout | '-'; printYourFredsFwd(f2); sout | '-'; printYourFredsRev(f2); sout | '-'; } void printMariesFwd(mary &m) { while (&m != 0p) { sout | m.anotherdatum; &m = &m`next; } } void printMariesRev(mary &m) { while (&m != 0p) { sout | m.anotherdatum; &m = &m`prev; } } void printMariatheotokos(mary &m1, mary &m2, int isBefore) { if (isBefore) { sout | "==== mary before "; } else { sout | "==== mary after "; } printMariesFwd(m1); sout | '-'; printMariesRev(m1); sout | '-'; printMariesFwd(m2); sout | '-'; printMariesRev(m2); sout | '-'; } //////////////////////////////////////////////////////////// // // Section 4a.i // // Test cases of insert_after on headless list // // Example of call-side user code // //////////////////////////////////////////////////////////// // All three tests exercise the case of merging two singleton lists into (their respective // positions in) one common list of two elements. // Note that the factoring of sect 4 (vs 3 and 5) keeps section 4 free of strings and IO, // and so keeps its assembly easy to inspect. // Throughout all the 4s, all the print functions called from these tests print: // - from list position #1 moving forward (a) // - from list position #1 moving backward (b) // - from list position #2 moving forward (c) // - from list position #2 moving backward (d) // The expected-output comments are in form a;b;c;d where a::=num,num,num void test__insertafter_singleton_on_singleton__fred_mine () { fred f1 = {3.14}; fred f2 = {0.5}; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_after(f1`in_mine, f2); printMyFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) printYourFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) } void test__insertafter_singleton_on_singleton__fred_yours () { fred f1 = {3.14}; fred f2 = {0.5}; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_after(f1`in_yours, f2); printMyFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) printYourFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } void test__insertafter_singleton_on_singleton__mary () { mary m1 = {3.14}; mary m2 = {0.5}; printMariatheotokos(m1, m2, 1); // 3.14; 3.14; 0.5; 0.5 insert_after(m1, m2); printMariatheotokos(m1, m2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } //////////////////////////////////////////////////////////// // // Section 4a.ii // // Test cases of insert_before on headless list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__insertbefore_singleton_on_singleton__fred_mine () { fred f1 = {3.14}; fred f2 = {0.5}; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_before(f2`in_mine, f1); printMyFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) printYourFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) } void test__insertbefore_singleton_on_singleton__fred_yours () { fred f1 = {3.14}; fred f2 = {0.5}; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_before(f2`in_yours, f1); printMyFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) printYourFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } void test__insertbefore_singleton_on_singleton__mary () { mary m1 = {3.14}; mary m2 = {0.5}; printMariatheotokos(m1, m2, 1); // 3.14; 3.14; 0.5; 0.5 insert_before(m2, m1); printMariatheotokos(m1, m2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } //////////////////////////////////////////////////////////// // // Section 4b.i // // Test cases of insert_first (necessarily headed list) // // Example of call-side user code // //////////////////////////////////////////////////////////// // All three tests exercise the case of creating an empty container and // adding two items to it. void test__insertfirst_two_on_empty__fred_mine() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_mine, fred) lf; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_first(lf, f2); insert_first(lf, f1); printMyFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) printYourFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) } void test__insertfirst_two_on_empty__fred_yours() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_yours, fred) lf; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_first(lf, f2); insert_first(lf, f1); printMyFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) printYourFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } void test__insertfirst_two_on_empty__mary() { mary m1 = {3.14}; mary m2 = {0.5}; dlist(mary, mary) lm; printMariatheotokos(m1, m2, 1); // 3.14; 3.14; 0.5; 0.5 insert_first(lm, m2); insert_first(lm, m1); printMariatheotokos(m1, m2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } //////////////////////////////////////////////////////////// // // Section 4b.ii // // Test cases of insert_last (necessarily headed list) // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__insertlast_two_on_empty__fred_mine() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_mine, fred) lf; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_last(lf, f1); insert_last(lf, f2); printMyFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) printYourFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) } void test__insertlast_two_on_empty__fred_yours() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_yours, fred) lf; printMyFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 printYourFreddies(f1, f2, 1); // 3.14; 3.14; 0.5; 0.5 insert_last(lf, f1); insert_last(lf, f2); printMyFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) printYourFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } void test__insertlast_two_on_empty__mary() { mary m1 = {3.14}; mary m2 = {0.5}; dlist(mary, mary) lm; printMariatheotokos(m1, m2, 1); // 3.14; 3.14; 0.5; 0.5 insert_last(lm, m1); insert_last(lm, m2); printMariatheotokos(m1, m2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) } //////////////////////////////////////////////////////////// // // Section 4c.i // // Test cases of insert_after on headed list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__insertafter_after_last__fred_mine() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_mine, fred) lf; assert(& lf`first == 0p); assert(& lf`last == 0p); insert_first(lf, f1); assert(& lf`first == & f1); assert(& lf`last == & f1); insert_after(f1`in_mine, f2); printMyFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) printYourFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) assert(& lf`first == & f1); assert(& lf`last == & f2); } void test__insertafter_after_last__fred_yours() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_yours, fred) lf; assert(& lf`first == 0p); assert(& lf`last == 0p); insert_first(lf, f1); assert(& lf`first == & f1); assert(& lf`last == & f1); insert_after(f1`in_yours, f2); printMyFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) printYourFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) assert(& lf`first == & f1); assert(& lf`last == & f2); } void test__insertafter_after_last__mary() { mary m1 = {3.14}; mary m2 = {0.5}; dlist(mary, mary) lm; assert(& lm`first == 0p); assert(& lm`last == 0p); insert_first(lm, m1); assert(& lm`first == & m1); assert(& lm`last == & m1); insert_after(m1, m2); printMariatheotokos(m1, m2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) assert(& lm`first == & m1); assert(& lm`last == & m2); } //////////////////////////////////////////////////////////// // // Section 4c.ii // // Test cases of insert_before on headed list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__insertbefore_before_first__fred_mine() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_mine, fred) lf; assert(& lf`first == 0p); assert(& lf`last == 0p); insert_last(lf, f2); assert(& lf`first == & f2); assert(& lf`last == & f2); insert_before(f2`in_mine, f1); printMyFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) printYourFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) assert(& lf`first == & f1); assert(& lf`last == & f2); } void test__insertbefore_before_first__fred_yours() { fred f1 = {3.14}; fred f2 = {0.5}; dlist(fred_in_yours, fred) lf; assert(& lf`first == 0p); assert(& lf`last == 0p); insert_last(lf, f2); assert(& lf`first == & f2); assert(& lf`last == & f2); insert_before(f2`in_yours, f1); printMyFreddies(f1, f2, 0); // 3.14; 3.14; 0.5; 0.5 (unmodified) printYourFreddies(f1, f2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) assert(& lf`first == & f1); assert(& lf`last == & f2); } void test__insertbefore_before_first__mary() { mary m1 = {3.14}; mary m2 = {0.5}; dlist(mary, mary) lm; assert(& lm`first == 0p); assert(& lm`last == 0p); insert_last(lm, m2); assert(& lm`first == & m2); assert(& lm`last == & m2); insert_before(m2, m1); printMariatheotokos(m1, m2, 0); // 3.14, 0.5; 3.14; 0.5; 0.5, 3.14 (modified) assert(& lm`first == & m1); assert(& lm`last == & m2); } //////////////////////////////////////////////////////////// // // Section 4d.i // // Test cases of remove, from middle of headless list // // Example of call-side user code // //////////////////////////////////////////////////////////// // These tests, in the fred cases, set up the my/your lists initially identical, // act on one list, and expect the other unaffected. void test__remove_mid__fred_mine() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; insert_after(f1`in_mine, f2); insert_after(f2`in_mine, f3); insert_after(f1`in_yours, f2); insert_after(f2`in_yours, f3); printMyFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f2`in_mine); printMyFreddies(f1, f3, 0); // 1.7, 3.7; 1.7; 3.7; 3.7, 1.7 (modified) printYourFreddies(f1, f3, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) // observe f2 is now solo in mine; in yours, it was just traversed printMyFreddies(f2, *0p, 0); // 2.7; 2.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f2.$links_mine.next.is_terminator == false); assert(f2.$links_mine.prev.is_terminator == false); } void test__remove_mid__fred_yours() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; insert_after(f1`in_mine, f2); insert_after(f2`in_mine, f3); insert_after(f1`in_yours, f2); insert_after(f2`in_yours, f3); printMyFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f2`in_yours); printMyFreddies(f1, f3, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) printYourFreddies(f1, f3, 0); // 1.7, 3.7; 1.7; 3.7; 3.7, 1.7 (modified) // observe f2 is now solo in yours; in mine, it was just traversed printYourFreddies(f2, *0p, 0); // 2.7; 2.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f2.$links_yours.next.is_terminator == false); assert(f2.$links_yours.prev.is_terminator == false); } void test__remove_mid__mary() { mary m1 = {1.7}; mary m2 = {2.7}; mary m3 = {3.7}; insert_after(m1, m2); insert_after(m2, m3); printMariatheotokos(m1, m3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(m2); printMariatheotokos(m1, m3, 0); // 1.7, 3.7; 1.7; 3.7; 3.7, 1.7 (modified) // observe m2 is now solo printMariatheotokos(m2, *0p, 0); // 2.7; 2.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(m2.$links.next.is_terminator == false); assert(m2.$links.prev.is_terminator == false); } //////////////////////////////////////////////////////////// // // Section 4d.ii // // Test cases of remove, from first position of headless list // // Example of call-side user code // //////////////////////////////////////////////////////////// // TODO: validate headless semantic: remove of a neighbourless element is valid and no-op void test__remove_at_first__fred_mine() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; insert_after(f1`in_mine, f2); insert_after(f2`in_mine, f3); insert_after(f1`in_yours, f2); insert_after(f2`in_yours, f3); printMyFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f1`in_mine); printMyFreddies(f2, f3, 0); // 2.7, 3.7; 2.7; 3.7; 3.7, 2.7 (modified) printYourFreddies(f1, f3, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) // observe f1 is now solo in mine; in yours, it was just traversed printMyFreddies(f1, *0p, 0); // 1.7; 1.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f1.$links_mine.next.is_terminator == false); assert(f1.$links_mine.prev.is_terminator == false); } void test__remove_at_first__fred_yours() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; insert_after(f1`in_mine, f2); insert_after(f2`in_mine, f3); insert_after(f1`in_yours, f2); insert_after(f2`in_yours, f3); printMyFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f1`in_yours); printMyFreddies(f1, f3, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) printYourFreddies(f2, f3, 0); // 2.7, 3.7; 2.7; 3.7; 3.7, 2.7 (modified) // observe f1 is now solo in yours; in mine, it was just traversed printYourFreddies(f1, *0p, 0); // 1.7; 1.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f1.$links_yours.next.is_terminator == false); assert(f1.$links_yours.prev.is_terminator == false); } void test__remove_at_first__mary() { mary m1 = {1.7}; mary m2 = {2.7}; mary m3 = {3.7}; insert_after(m1, m2); insert_after(m2, m3); printMariatheotokos(m1, m3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(m1); printMariatheotokos(m2, m3, 0); // 2.7, 3.7; 2.7; 3.7; 3.7, 2.7 (modified) // observe m2 is now solo printMariatheotokos(m1, *0p, 0); // 1.7; 1.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(m1.$links.next.is_terminator == false); assert(m1.$links.prev.is_terminator == false); } //////////////////////////////////////////////////////////// // // Section 4d.iii // // Test cases of remove, from last position of headless list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__remove_at_last__fred_mine() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; insert_after(f1`in_mine, f2); insert_after(f2`in_mine, f3); insert_after(f1`in_yours, f2); insert_after(f2`in_yours, f3); printMyFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f3`in_mine); printMyFreddies(f1, f2, 0); // 1.7, 2.7; 1.7; 2.7; 2.7, 1.7 (modified) printYourFreddies(f1, f3, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) // observe f3 is now solo in mine; in yours, it was just traversed printMyFreddies(f3, *0p, 0); // 3.7; 3.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f3.$links_mine.next.is_terminator == false); assert(f3.$links_mine.prev.is_terminator == false); } void test__remove_at_last__fred_yours() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; insert_after(f1`in_mine, f2); insert_after(f2`in_mine, f3); insert_after(f1`in_yours, f2); insert_after(f2`in_yours, f3); printMyFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(f1, f3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f3`in_yours); printMyFreddies(f1, f3, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) printYourFreddies(f1, f2, 0); // 1.7, 2.7; 1.7; 2.7; 2.7, 1.7 (modified) // observe f3 is now solo in yours; in mine, it was just traversed printYourFreddies(f3, *0p, 0); // 3.7; 3.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f3.$links_yours.next.is_terminator == false); assert(f3.$links_yours.prev.is_terminator == false); } void test__remove_at_last__mary() { mary m1 = {1.7}; mary m2 = {2.7}; mary m3 = {3.7}; insert_after(m1, m2); insert_after(m2, m3); printMariatheotokos(m1, m3, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(m3); printMariatheotokos(m1, m2, 0); // 1.7, 2.7; 1.7; 2.7; 2.7, 1.7 (modified) // observe m3 is now solo printMariatheotokos(m3, *0p, 0); // 3.7; 3.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(m1.$links.next.is_terminator == false); assert(m1.$links.prev.is_terminator == false); } //////////////////////////////////////////////////////////// // // Section 4e.i // // Test cases of remove, from first position of headed list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__remove_at_head__fred_mine() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; dlist(fred_in_mine, fred) flm; insert_last(flm, f1); insert_last(flm, f2); insert_last(flm, f3); dlist(fred_in_yours, fred) fly; insert_last(fly, f1); insert_last(fly, f2); insert_last(fly, f3); printMyFreddies(flm`first, flm`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(fly`first, fly`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f1`in_mine); printMyFreddies(flm`first, flm`last, 0); // 2.7, 3.7; 2.7; 3.7; 3.7, 2.7 (modified) printYourFreddies(fly`first, fly`last, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) // observe f1 is now solo in mine; in yours, it was just traversed printMyFreddies(f1, *0p, 0); // 1.7; 1.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f1.$links_mine.next.is_terminator == false); assert(f1.$links_mine.prev.is_terminator == false); } void test__remove_at_head__fred_yours() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; dlist(fred_in_mine, fred) flm; insert_last(flm, f1); insert_last(flm, f2); insert_last(flm, f3); dlist(fred_in_yours, fred) fly; insert_last(fly, f1); insert_last(fly, f2); insert_last(fly, f3); printMyFreddies(flm`first, flm`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(fly`first, fly`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f1`in_yours); printMyFreddies(flm`first, flm`last, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) printYourFreddies(fly`first, fly`last, 0); // 2.7, 3.7; 2.7; 3.7; 3.7, 2.7 (modified) // observe f1 is now solo in yours; in mine, it was just traversed printYourFreddies(f1, *0p, 0); // 1.7; 1.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f1.$links_yours.next.is_terminator == false); assert(f1.$links_yours.prev.is_terminator == false); } void test__remove_at_head__mary() { mary m1 = {1.7}; mary m2 = {2.7}; mary m3 = {3.7}; dlist(mary, mary) ml; insert_last(ml, m1); insert_last(ml, m2); insert_last(ml, m3); printMariatheotokos(ml`first, ml`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(m1); printMariatheotokos(ml`first, ml`last, 0); // 2.7, 3.7; 2.7; 3.7; 3.7, 2.7 (modified) // observe m1 is now solo printMariatheotokos(m1, *0p, 0); // 1.7; 1.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(m1.$links.next.is_terminator == false); assert(m1.$links.prev.is_terminator == false); } //////////////////////////////////////////////////////////// // // Section 4e.ii // // Test cases of remove, from last position of headed list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__remove_at_tail__fred_mine() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; dlist(fred_in_mine, fred) flm; insert_last(flm, f1); insert_last(flm, f2); insert_last(flm, f3); dlist(fred_in_yours, fred) fly; insert_last(fly, f1); insert_last(fly, f2); insert_last(fly, f3); printMyFreddies(flm`first, flm`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(fly`first, fly`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f3`in_mine); printMyFreddies(flm`first, flm`last, 0); // 1.7, 2.7; 1.7; 2.7; 2.7, 1.7 (modified) printYourFreddies(fly`first, fly`last, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) // observe f3 is now solo in mine; in yours, it was just traversed printMyFreddies(f3, *0p, 0); // 3.7; 3.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f3.$links_mine.next.is_terminator == false); assert(f3.$links_mine.prev.is_terminator == false); } void test__remove_at_tail__fred_yours() { fred f1 = {1.7}; fred f2 = {2.7}; fred f3 = {3.7}; dlist(fred_in_mine, fred) flm; insert_last(flm, f1); insert_last(flm, f2); insert_last(flm, f3); dlist(fred_in_yours, fred) fly; insert_last(fly, f1); insert_last(fly, f2); insert_last(fly, f3); printMyFreddies(flm`first, flm`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 printYourFreddies(fly`first, fly`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(f3`in_yours); printMyFreddies(flm`first, flm`last, 0); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 (unmodified) printYourFreddies(fly`first, fly`last, 0); // 1.7, 2.7; 1.7; 2.7; 2.7, 1.7 (modified) // observe f3 is now solo in yours; in mine, it was just traversed printYourFreddies(f3, *0p, 0); // 3.7; 3.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f3.$links_yours.next.is_terminator == false); assert(f3.$links_yours.prev.is_terminator == false); } void test__remove_at_tail__mary() { mary m1 = {1.7}; mary m2 = {2.7}; mary m3 = {3.7}; dlist(mary, mary) ml; insert_last(ml, m1); insert_last(ml, m2); insert_last(ml, m3); printMariatheotokos(ml`first, ml`last, 1); // 1.7, 2.7, 3.7; 1.7; 3.7; 3.7, 2.7, 1.7 remove(m3); printMariatheotokos(ml`first, ml`last, 0); // 1.7, 2.7; 1.7; 2.7; 2.7, 1.7 (modified) // observe m3 is now solo printMariatheotokos(m3, *0p, 0); //3.7; 3.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(m3.$links.next.is_terminator == false); assert(m3.$links.prev.is_terminator == false); } //////////////////////////////////////////////////////////// // // Section 4e.iii // // Test cases of remove, of sole element of headed list // // Example of call-side user code // //////////////////////////////////////////////////////////// void test__remove_of_sole__fred_mine() { fred f = {0.7}; dlist(fred_in_mine, fred) flm; insert_last(flm, f); dlist(fred_in_yours, fred) fly; insert_last(fly, f); printMyFreddies(flm`first, flm`last, 1); // 0.7; 0.7; 0.7; 0.7 printYourFreddies(fly`first, fly`last, 1); // 0.7; 0.7; 0.7; 0.7 remove(f`in_mine); assert(& flm`first == 0p); assert(& flm`last == 0p); printYourFreddies(fly`first, fly`last, 0); // 0.7; 0.7; 0.7; 0.7 (unmodified) // observe f is solo in mine (now unlisted); in yours, it was just traversed printMyFreddies(f, *0p, 0); // 0.7; 0.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f.$links_mine.next.is_terminator == false); assert(f.$links_mine.prev.is_terminator == false); insert_last(flm, f); printMyFreddies(flm`first, flm`last, 0); // 0.7; 0.7; 0.7; 0.7 } void test__remove_of_sole__fred_yours() { fred f = {0.7}; dlist(fred_in_mine, fred) flm; insert_last(flm, f); dlist(fred_in_yours, fred) fly; insert_last(fly, f); printMyFreddies(flm`first, flm`last, 1); // 0.7; 0.7; 0.7; 0.7 printYourFreddies(fly`first, fly`last, 1); // 0.7; 0.7; 0.7; 0.7 remove(f`in_yours); assert(& fly`first == 0p); assert(& fly`last == 0p); printYourFreddies(flm`first, flm`last, 0); // 0.7; 0.7; 0.7; 0.7 (unmodified) // observe f is solo in yours (now unlisted); in mine, it was just traversed printYourFreddies(f, *0p, 0); // 0.7; 0.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(f.$links_yours.next.is_terminator == false); assert(f.$links_yours.prev.is_terminator == false); insert_last(fly, f); printYourFreddies(fly`first, fly`last, 0); // 0.7; 0.7; 0.7; 0.7 } void test__remove_of_sole__mary() { mary m = {0.7}; dlist(mary, mary) ml; insert_last(ml, m); printMariatheotokos(ml`first, ml`last, 1); // 0.7; 0.7; 0.7; 0.7 remove(m); assert(& ml`first == 0p); assert(& ml`last == 0p); // observe f is solo in mine (now unlisted); in yours, it was just traversed printMariatheotokos(m, *0p, 0); // 0.7; 0.7; ; // TODO: decide on appropriate ovservable outcome (is_listed?) and its itended semantics assert(m.$links.next.is_terminator == false); assert(m.$links.prev.is_terminator == false); insert_last(ml, m); printMariatheotokos(ml`first, ml`last, 0); // 0.7; 0.7; 0.7; 0.7 } //////////////////////////////////////////////////////////// // // Section 5 // // Simple driver with the inter-scario printing // //////////////////////////////////////////////////////////// int main() { sout | "~~~~~~~~~~~~~~~~~ Headless List Tests - insert_after ~~~~~~~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 1-i: Modifying Freds on MINE "; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertafter_singleton_on_singleton__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 2-i. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertafter_singleton_on_singleton__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 3-i. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertafter_singleton_on_singleton__mary(); sout | ""; sout | "~~~~~~~~~~~~~~~~ Headless List Tests - insert_before ~~~~~~~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 1-ii: Modifying Freds on MINE "; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertbefore_singleton_on_singleton__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 2-ii. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertbefore_singleton_on_singleton__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 3-ii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertbefore_singleton_on_singleton__mary(); sout | ""; sout | "~~~~~~~~~~~~~~~~~ Headed List Tests - insert_first ~~~~~~~~~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 4-i: Modifying Freds on MINE "; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertfirst_two_on_empty__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 5-i: Modifying Freds on YOURS "; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertfirst_two_on_empty__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 6-i. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertfirst_two_on_empty__mary(); sout | ""; sout | "~~~~~~~~~~~~~~~~~ Headed List Tests - insert_last ~~~~~~~~~~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 4-ii: Modifying Freds on MINE "; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertlast_two_on_empty__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 5-ii: Modifying Freds on YOURS "; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertlast_two_on_empty__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 6-ii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertlast_two_on_empty__mary(); sout | ""; sout | "~~~~~~~~~~~ Element ops on Headed List Tests: after, last ~~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 7-i. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertafter_after_last__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 8-i. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertafter_after_last__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 9-i. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertafter_after_last__mary(); sout | ""; sout | "~~~~~~~~~~ Element ops on Headed List Tests: before, first ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 7-ii. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertbefore_before_first__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 8-ii. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertbefore_before_first__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 9-ii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__insertbefore_before_first__mary(); sout | ""; sout | "~~~~~~~~~~ Element removal tests on Headless List: mid ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 10-i. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_mid__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 11-i. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_mid__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 12-i. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_mid__mary(); sout | ""; sout | "~~~~~~~~~~ Element removal tests on Headless List: at first ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 10-ii. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_first__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 11-ii. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_first__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 12-ii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_first__mary(); sout | ""; sout | "~~~~~~~~~~ Element removal tests on Headless List: at last ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 10-iii. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_last__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 11-iii. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_last__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 12-iii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_last__mary(); sout | ""; sout | "~~~~~~~~~~ Element removal tests on Headed List: at first ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 13-i. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_head__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 14-i. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_head__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 15-i. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_head__mary(); sout | ""; sout | "~~~~~~~~~~ Element removal tests on Headed List: at last ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 13-ii. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_tail__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 14-ii. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_tail__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 15-ii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_at_tail__mary(); sout | ""; sout | "~~~~~~~~~~ Element removal tests on Headed List: of sole ~~~~~~~~~~"; sout | ""; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 13-iii. Modifying Freds on MINE"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_of_sole__fred_mine(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 14-iii. Modifying Freds on YOURS"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_of_sole__fred_yours(); sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; sout | "Test 15-iii. Modifying Maries"; sout | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; test__remove_of_sole__mary(); return 0; }