-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·137 lines (110 loc) · 3.11 KB
/
main.cpp
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
//
// main.cpp
// CS260-Project1
//
// Jake Romer
// PCC Summer 2014
// CS 260. Data Structures
// Assignment 1: Inventory
//
#include "Item.h"
#include "Inventory.h"
#include <iostream>
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
using namespace std;
void doTestBadData();
void doTestAdvanced();
void doTestBasic();
void AddItem(Inventory& inv,char* name,double weight);
void RemoveItem(Inventory& inv,char* name);
void keepConsoleOpen(){ cin.get(); } // keeps console open in VC++
int main() {
doTestBasic();
doTestAdvanced();
doTestBadData();
#ifdef _WIN32
if (_CrtDumpMemoryLeaks())
cout << "Memory leaks!" << endl;
keepConsoleOpen();
#endif
return 0;
}
void AddItem(Inventory& inv,char* name,double weight){
cout << "Adding " << name << " with a weight of " << weight << "." << endl;
inv.AddItem(Item(name, weight));
}
void RemoveItem(Inventory& inv,char* name){
cout << "Removing " << name << "." << endl;
inv.RemoveItem(name);
}
void doTestBasic(){
Inventory inv;
// Make sure printing an empty inventory works
inv.PrintInventory();
// Make sure adding the first one works
AddItem(inv,"helmet",5);
inv.PrintInventory();
// Add some more items
AddItem(inv,"bracelet of power",1);
AddItem(inv,"red potion",2);
inv.PrintInventory();
// Add some duplicates
AddItem(inv,"bracelet of power",1);
AddItem(inv,"red potion",2);
inv.PrintInventory();
// Add some heavy stuff
AddItem(inv,"bag of gold coins",50);
AddItem(inv,"bag of gold coins",50);
// Now some removes
RemoveItem(inv,"short sword");
RemoveItem(inv,"helmet");
RemoveItem(inv,"bracelet of power");
inv.PrintInventory();
}
void doTestAdvanced(){
Inventory inv;
// Add items in reverse order
AddItem(inv,"yellow potion",0.5);
AddItem(inv,"trident",1.0);
AddItem(inv,"helmet",1.0);
AddItem(inv,"amulet",1.0);
inv.PrintInventory();
// Add items at the beginning, middle, end
AddItem(inv,"aardvark skin",5.0);
AddItem(inv,"newt's eye",0.1);
AddItem(inv,"zebra feather",0.1);
inv.PrintInventory();
// Remove items at the beginning, middle, end
RemoveItem(inv,"aardvark skin");
RemoveItem(inv,"newt's eye");
RemoveItem(inv,"zebra feather");
inv.PrintInventory();
// Add items with different case
AddItem(inv,"helmet",1.0);
AddItem(inv,"Helmet",1.0);
AddItem(inv,"HELMET",1.0);
inv.PrintInventory();
// Remove items with case insensitivity
RemoveItem(inv,"HELMET");
inv.PrintInventory();
}
void doTestBadData(){
Inventory inv(50);
// Adding too much
AddItem(inv,"bag of gold coins",50); // should work
AddItem(inv,"feather",0.1); // should fail
inv.PrintInventory();
RemoveItem(inv,"bag of gold coins"); // should work
inv.PrintInventory();
// Using long strings
AddItem(inv,"this is such a long item and nothing should have a name thing long but we have no guarantee that some crazy person wouldn't make such an item and then we need to make sure our program doesn't break. Hint: don't use char[] to store data. Only use char*.",1.0);
inv.PrintInventory();
// Same item with wrong weight
AddItem(inv,"helmet",1.0);
AddItem(inv,"helmet",10.0);
inv.PrintInventory();
}