1: // Created by Frank M. Carrano and Tim Henry.
2: // Copyright (c) 2013 __Pearson Education__. All rights reserved.
4: #include <iostream>
5: #include <string>
6: #include "ArrayBag.h"
8: using namespace std;
10: void displayBag(ArrayBag<string>& bag)
11: {
12: cout << "The bag contains " << bag.getCurrentSize()
13: << " items:" << endl;
14: vector<string> bagItems = bag.toVector();
15:
16: int numberOfEntries = (int) bagItems.size();
17: for (int i = 0; i < numberOfEntries; i++)
18: {
19: cout << bagItems[i] << " ";
20: } // end for
21: cout << endl << endl;
22: } // end displayBag
24: void bagTester(ArrayBag<string>& bag)
25: {
26: cout << "isEmpty: returns " << bag.isEmpty()
27: << "; should be 1 (true)" << endl;
28: displayBag(bag);
30: string items[] = {"one", "two", "three", "four", "five", "one"};
31: cout << "Add 6 items to the bag: " << endl;
32: for (int i = 0; i < 6; i++)
33: {
34: bag.add(items[i]);
35: } // end for
36:
37: displayBag(bag);
38:
39: cout << "isEmpty: returns " << bag.isEmpty()
40: << "; should be 0 (false)" << endl;
41:
42: cout << "getCurrentSize: returns " << bag.getCurrentSize()
43: << "; should be 6" << endl;
44:
45: cout << "Try to add another entry: add(\"extra\") returns "
46: << bag.add("extra") << endl;
47:
48: cout << "contains(\"three\"): returns " << bag.contains("three")
49: << "; should be 1 (true)" << endl;
50: cout << "contains(\"ten\"): returns " << bag.contains("ten")
51: << "; should be 0 (false)" << endl;
52: cout << "getFrequencyOf(\"one\"): returns "
53: << bag.getFrequencyOf("one") << " should be 2" << endl;
54: cout << "remove(\"one\"): returns " << bag.remove("one")
55: << "; should be 1 (true)" << endl;
56: cout << "getFrequencyOf(\"one\"): returns "
57: << bag.getFrequencyOf("one") << " should be 1" << endl;
58: cout << "remove(\"one\"): returns " << bag.remove("one")
59: << "; should be 1 (true)" << endl;
60: cout << "remove(\"one\"): returns " << bag.remove("one")
61: << "; should be 0 (false)" << endl;
62: cout << endl;
63:
64: displayBag(bag);
65:
66: cout << "After clearing the bag, ";
67: bag.clear();
68:
69: cout << "isEmpty: returns " << bag.isEmpty()
70: << "; should be 1 (true)" << endl;
71: } // end bagTester
73: int main()
74: {
75: ArrayBag<string> bag;
76: cout << "Testing the Array-Based Bag:" << endl;
77: cout << "The initial bag is empty." << endl;
78: bagTester(bag);
79: cout << "All done!" << endl;
80:
81: return 0;
82: } // end main
84: /*
85: Testing the Array-Based Bag:
86: The initial bag is empty.
87: isEmpty: returns 1; should be 1 (true)
88: The bag contains 0 items:
89:
90:
91: Add 6 items to the bag:
92: The bag contains 6 items:
93: one two three four five one
94:
95: isEmpty: returns 0; should be 0 (false)
96: getCurrentSize: returns 6; should be 6
97: Try to add another entry: add("extra") returns 0
98: contains("three"): returns 1; should be 1 (true)
99: contains("ten"): returns 0; should be 0 (false)
100: getFrequencyOf("one"): returns 2 should be 2
101: remove("one"): returns 1; should be 1 (true)
102: getFrequencyOf("one"): returns 1 should be 1
103: remove("one"): returns 1; should be 1 (true)
104: remove("one"): returns 0; should be 0 (false)
105:
106: The bag contains 4 items:
107: five two three four
108:
109: After clearing the bag, isEmpty: returns 1; should be 1 (true)
110: All done!
111: */