-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (100 loc) · 2.86 KB
/
main.cpp
File metadata and controls
126 lines (100 loc) · 2.86 KB
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
#include <iostream>
#include "item.h"
#include "inventory.h"
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
using namespace std;
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();
}
int main() {
doTestBasic();
doTestAdvanced();
doTestBadData();
#ifdef _WIN32
if (_CrtDumpMemoryLeaks()) {
cout << "Memory leaks!" << endl;
}
#endif
return 0;
}