-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayBagTester2.cxx
153 lines (123 loc) · 5.1 KB
/
ArrayBagTester2.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Sarah Niemeyer
//100027519
//Homework 2
//Matt Sainz
#include <iostream>
#include <string>
#include "ArrayBag.h"
using namespace std;
int main()
{
// here we call the ArrayBag constructor; this particular bag stores strings
ArrayBag<string> aBag;
// here we call the isEmpty function on a new ArrayBag
if (aBag.isEmpty())
cout << "New bag is created empty; that is good" << endl;
else
cout << "New bag is created with stuff; not so good" << endl;
// here we call the getCapacity function on a new ArrayBag
if (aBag.getCapacity() > 0)
cout << "New bag is created with non-zero capacity; that is good" << endl;
else
cout << "New bag is created with zero capacity; not so good" << endl;
// here we add a couple of things
aBag.add("tostada");
aBag.add("tamale");
aBag.add("taco");
// here we check if the ArrayBag is empty
if (aBag.isEmpty())
cout << "Bag after adding thinks it is empty; that is not so good" << endl;
else
cout << "Bag after adding thinks it is not empty; that is good" << endl;
// here we check if it knows its own size
if (aBag.getCurrentSize() != 3)
cout << "Bag after adding has wrong size; that is not so good" << endl;
else
cout << "Bag after adding has right size; that is good" << endl;
if (aBag.contains("tamale") == false)
cout << "Bag can't find added items; that is not so good" << endl;
else
cout << "Bag can find added items; that is good" << endl;
if (aBag.contains("torta") == true)
cout << "Bag can find items we never added; that is not so good" << endl;
else
cout << "Bag doesn't find items we never added; that is good" << endl;
// here we check what happens if we ask to remove something not in the ArrayBag
aBag.remove("torta");
// here we check if the ArrayBag knows its own size after a non-event removal
if (aBag.getCurrentSize() != 3)
cout << "Bag should be the same size as before but isn't; that is not so good" << endl;
else
cout << "Bag is the same size as before; that is good" << endl;
// here we remove something in the ArrayBag
aBag.remove("tamale");
// here we check if the size has changed
if (aBag.getCurrentSize() != 2)
cout << "Bag should be smaller than before but isn't; that is not so good" << endl;
else
cout << "Bag is the right size after removing one item; that is good" << endl;
// empty the bag
aBag.clear();
// check the size of the empty bag
if (aBag.getCurrentSize() != 0)
cout << "Bag after clearing has wrong size; that is not so good" << endl;
else
cout << "Bag after clearing has right size; that is good" << endl;
int tamales = 0;
for (int i = 0; i < aBag.getCapacity(); ++i)
{
aBag.add("tamale");
++tamales;
}
if (aBag.getCurrentSize() != aBag.getCapacity())
cout << "Bag after adding has wrong size; that is not so good" << endl;
else
cout << "Bag after adding has right size; that is good" << endl;
// try adding to a 'full' ArrayBag to trigger resizing
aBag.add("tamale"); ++tamales;
// check if we can count up how often an item appears in the ArrayBag
if (aBag.getFrequencyOf("tamale") != tamales)
cout << "Bag can't count items correctly when they are present; that is not so good" << endl;
else
cout << "Bag can count items correctly when they are present; that is good" << endl;
// check if we get the right answer for an item that isn't in the ArrayBag
if (aBag.getFrequencyOf("torta") != 0)
cout << "Bag can't count items correctly when they are absent; that is not so good" << endl;
else
cout << "Bag can count items correctly when they are absent; that is good" << endl;
// use remove to empty the Bag
for (int i = 0; i < aBag.getCapacity(); ++i)
aBag.remove("tamale");
// check that remove has left the size correct
if (aBag.getCurrentSize() != 0)
cout << "Size of bag is incorrect after removing all the items; that is not so good" << endl;
else
cout << "Size of bag after removing all the items is correct; that is good" << endl;
aBag.add("apple");
aBag.add("banana");
aBag.add("cherries");
// test toArray on the Bag
vector<string> contents = aBag.toVector();
cout << "Here is a vector of the contents of a bag with 3 items" << endl;
for (int i = 0; i < aBag.getCurrentSize(); ++i)
cout << contents[i] << " ";
cout << endl;
// test copy constructor on the bag
cout << "Testing the copy constructor by making another bag with 3 items" << endl;
ArrayBag<string> fruitBag = aBag;
cout << "Adding an item to the copied bag" << endl;
fruitBag.add("durian");
cout << "Here is a vector of the contents of the copied bag with 4 items" << endl;
vector<string> otherContents = fruitBag.toVector();
cout << "Current size is " << fruitBag.getCurrentSize() << endl;
for (int i = 0; i < fruitBag.getCurrentSize(); ++i)
cout << otherContents[i] << " ";
cout << endl;
ArrayBag<string> newBag;
cout << "bag capacity is " << newBag.getCapacity() << endl;
newBag = aBag;
otherContents = newBag.toVector();
for (int i = 0; i < newBag.getCurrentSize(); ++i)
cout << otherContents[i] << " ";
cout << endl;
}