Asst 6 Test Program
#include <iostream> #include <vector> #include <list> #include <string> using namespace std; #include "rtpq.h" // Pop one group from the rpt_queue and print the size. If non-zero (there // really is an item), print the item. template<typename T> void maybe_get(rpt_queue<T> &q) { T data; int ct = q.pop(data); if(ct > 0) cout << " " << ct << ": " << data; else cout << " " << ct; } // Test if empty() returns the correct value. template<typename T> void mt_test(const T &q, bool iscorrect) { cout << (!q.empty() ? "not ":"") << "empty"; if(q.empty() != iscorrect) cout << " (should be " << (!iscorrect ? "not ":"") << "empty)"; cout << endl; } int main(int argc, char **argv) { /* Testing the rpt_queue */ rpt_queue<int> irq; cout << "==== Group 1 ====" << endl; mt_test(irq,true); for(int i: { 4, 4, 9, -3, -3, -3, 4, 4, 0, 0, 4, 1, 1, 1 }) irq.push(i); mt_test(irq,false); for(int i = 8; i--; ) maybe_get(irq); cout << endl; mt_test(irq,true); cout << endl; cout << "==== Group 2 ====" << endl; for(int i: { 2, 3, 4, 3, 2 }) irq.push(i); mt_test(irq,false); for(int i = 6; i--; ) maybe_get(irq); cout << endl; mt_test(irq,true); cout << endl; rpt_queue<string> srq; cout << "==== Group 3 ====" << endl; mt_test(srq,true); for(auto i: { "fred", "joe", "joe", "bill", "joe", "fred", "fred", "fred", "fred", "al" }) srq.push(i); mt_test(srq,false); for(int i = 3; i--; ) maybe_get(srq); cout << endl; mt_test(srq,false); srq.push("fred"); srq.push("fred"); srq.push("jo"); mt_test(srq,false); for(int i = 6; i--; ) maybe_get(srq); cout << endl; mt_test(srq,true); cout << endl; /* Test the expand queue. */ cout << "==== Group 4 ====" << endl; expand_queue<int> ieq; mt_test(ieq,true); ieq.push(3,7); ieq.push(1,10); ieq.push(4,0); ieq.push(3,1); ieq.push(0,18); ieq.push(2,1); ieq.push(3,9); mt_test(ieq,false); int val; while(ieq.pop(val)) { cout << val << " "; } cout << endl; mt_test(ieq,true); /* * Now, the obvious test to see if we can compress and exapand a * series of values and get the same thing back. */ cout << "==== Group 5 ====" << endl; // Some data. Push it on the repeat queue. vector<string> values = { "bill", "mike", "bill", "bill", "bill", "alice", "jen", "bill", "jen", "jen", "jen", "jen", "sally", "mike", "mike" }; rpt_queue<string> req2; for(auto s: values) req2.push(s); cout << "Pushed " << values.size() << " items onto repeat queue" << endl; // Now, move each thing to the expand queue. int num = 0, tot = 0; // Number of returns and total count. expand_queue<string> seq; // Put the repeat items here. int cnt; // Size of removed group. do { // Pop a group. string val; cnt = req2.pop(val); // Stats if(cnt != 0) ++num; tot += cnt; // Push it onto the expand queue. If cnt is zero, won't // add anything. seq.push(cnt,val); // Extra test. if(cnt == 3) seq.push(0,"teddy"); // Should disappear. } while(cnt != 0); cout << "Got " << num << " groups for a total of " << tot << " items " << "from the repeat queue." << endl; if(tot != values.size()) cout << "Should have been " << values.size() << " items" << endl; if(num != 9) cout << "Should have been 9 groups." << endl; // Should get the same list back from the expand queue. int errs = 0; int nout = 0; auto scan = values.begin(); string itm; while(seq.pop(itm)) { ++nout; if(*scan++ != itm) ++errs; } cout << "Removed " << nout << " items from the expand queue."; if(errs == 0) cout << " All correct."; else cout << nout << " incorrect."; cout << endl; }

The output should be:

==== Group 1 ==== empty not empty 2: 4 1: 9 3: -3 2: 4 2: 0 1: 4 3: 1 0 empty ==== Group 2 ==== not empty 1: 2 1: 3 1: 4 1: 3 1: 2 0 empty ==== Group 3 ==== empty not empty 1: fred 2: joe 1: bill not empty not empty 1: joe 4: fred 1: al 2: fred 1: jo 0 empty ==== Group 4 ==== empty not empty 7 7 7 10 0 0 0 0 1 1 1 1 1 9 9 9 empty ==== Group 5 ==== Pushed 15 items onto repeat queue Got 9 groups for a total of 15 items from the repeat queue. Removed 15 items from the expand queue. All correct.