/** * collection-expt-2.c * Experiments with linear structures, particularly the copy command. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include "collection.h" #include "expt.h" // +---------+-------------------------------------------------------- // | Helpers | // +---------+ #define BLOCK(CODE) do { CODE } while (0) /** * Print information about a collection. */ #define PRINT_INFO(COLLECTION) \ BLOCK ( \ PRINT_INT (collection_size (COLLECTION)); \ PRINT_INT (collection_is_empty (COLLECTION)); \ PRINT_INT (collection_is_full (COLLECTION)); \ ) #define PRINT_ELEMENTS(COLLECTION) \ BLOCK ( \ while (! collection_is_empty (COLLECTION)) \ { \ EXPERIMENT (result = collection_get (COLLECTION)) \ PRINT_STR (result); \ } \ ) // +------+----------------------------------------------------------- // | Main | // +------+ int main () { struct collection *stuff; struct collection *ffuts; char *result; EXPERIMENT (stuff = new_collection (4)); // Add a few things to the collection EXPERIMENT (collection_put (stuff, "adam")); EXPERIMENT (collection_put (stuff, "barb")); EXPERIMENT (collection_put (stuff, "chas")); // Make a copy of the collection EXPERIMENT (ffuts = collection_copy (stuff)); // Grab the first element from each queue (they should be the same) EXPERIMENT (result = collection_get (stuff)) PRINT_STR (result); EXPERIMENT (result = collection_get (ffuts)) PRINT_STR (result); PRINT_INFO (stuff); PRINT_INFO (ffuts); // Grab another element from just the original and check sizes. EXPERIMENT (result = collection_get (stuff)) PRINT_STR (result); PRINT_INFO (stuff); PRINT_INFO (ffuts); // Add an element to each queue and check sizes. EXPERIMENT (collection_put (stuff, "dawn")); EXPERIMENT (collection_put (ffuts, "evan")); // Print the elements of the original PRINT_ELEMENTS (stuff); // Add an element or two to the orignal EXPERIMENT (collection_put (stuff, "fawn")); EXPERIMENT (collection_put (stuff, "greg")); // Print the elements of the copy PRINT_ELEMENTS (ffuts); // Add some more elements EXPERIMENT (collection_put (stuff, "hope")); EXPERIMENT (collection_put (ffuts, "irma")); EXPERIMENT (collection_put (ffuts, "jack")); // Print 'em both out PRINT_ELEMENTS (stuff); PRINT_ELEMENTS (ffuts); // And we're done. return 0; } // main