-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tester.h
196 lines (162 loc) · 4.81 KB
/
Tester.h
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/************************************************
* Tester<Class_name>
* The top-level testing class. Each derived
* class inherits the routines:
*
* int run()
*
* For a derived class to function, it must
* override the void process() member function.
* The process function should read in a line from
* the input file and tests the routine based on
* those instructions.
*
* The member variables are:
*
* Class_name *object A pointer to the object
* being tested.
*
* string command The string read in from
* the input file indicating
* the next command to be
* tested.
*
* int count The number of the current
* test being run.
*
* The member functions are:
*
* int run() Start a test run testing until
* the end of the file, one test
* per line.
* void process() Process and run an individual
* test.
*
* Author: Douglas Wilhelm Harder
* Copyright (c) 2006-9 by Douglas Wilhelm Harder. All rights reserved.
*
* DO NOT EDIT THIS FILE
************************************************/
#ifndef TESTER_H
#define TESTER_H
#include <iostream>
#include <string>
#include <sstream>
#include "ece250.h"
template <class Class_name>
class Tester {
protected:
Class_name *object;
std::string command;
public:
Tester( Class_name *obj = 0 ):
object( obj ) {
// emtpy constructor
}
int run();
virtual void process() = 0;
};
/****************************************************
* int run()
*
* Indefinite run of test cases: continue reading console
* input until the end of the file.
****************************************************/
template <class Class_name>
int Tester<Class_name>::run() {
// read the flag which indicates the command to be test and
// stop if we have reached the end of the file
ece250::allocation_table.stop_recording();
const static std::string prompt = " % ";
int ptr = 3;
while ( true ) {
// terminate if there is an end-of-file or the user types 'exit'
if ( std::cin.eof() ) {
break;
}
++ece250::count;
std::cout << ece250::count << prompt;
std::cin >> command;
// Remove any comments
if ( command.substr( 0, 2 ) == "//" ) {
char comment[1024];
std::cin.getline( comment, 1024 );
std::cout << command << comment << std::endl;
continue;
}
// terminate if there is an end-of-file or the user types 'exit'
if ( std::cin.eof() ) {
std::cout << "Exiting..." << std::endl;
break;
}
// If the user enters !!,
// set the command to be the last command
// If the user enters !n where n is a number, (1 <= n < count)
// set the command ot be the nth command
if ( command == "!!" ) {
if ( ece250::count == 1 ) {
std::cout << "Event not found" << std::endl;
continue;
}
command = ece250::history[ece250::count - 1];
} else if ( command[0] == '!' ) {
int n;
std::istringstream number( command.substr( 1, command.length() - 1 ) );
number >> n;
if ( n <= 0 || n > n || n > 100 ) {
std::cout << "Event not found" << std::endl;
continue;
}
command = ece250::history[n];
}
// only track the last 100 commands
if ( ece250::count <= 100 ) {
ece250::history[ece250::count] = command;
}
// start tracking any memory allocations made
ece250::allocation_table.start_recording();
// There are five key commands
if ( command == "exit" ) {
std::cout << "Okay" << std::endl;
ece250::allocation_table.stop_recording();
break;
} else if ( command == "new" ) {
object = new Class_name();
std::cout << "Okay" << std::endl;
} else if ( command == "new:" ) {
int n;
std::cin >> n;
object = new Class_name();
std::cout << "Okay" << std::endl;
} else if ( command == "delete" ) {
delete object;
object = 0;
std::cout << "Okay" << std::endl;
} else if ( command == "summary" ) {
ece250::allocation_table.summary();
} else if ( command == "details" ) {
ece250::allocation_table.details();
} else if ( command == "memory" ) {
int n;
std::cin >> n;
if ( n == ece250::allocation_table.memory_alloc() ) {
std::cout << "Okay" << std::endl;
} else {
std::cout << "Failure in memory allocation: expecting " << n << " bytes to be allocated, but " << ece250::allocation_table.memory_alloc() << " bytes were allocated" << std::endl;
}
} else if ( command == "memory_store" ) {
ece250::allocation_table.memory_store();
std::cout << "Okay" << std::endl;
} else if ( command == "memory_change" ) {
int n;
std::cin >> n;
ece250::allocation_table.memory_change( n );
} else {
process();
}
// stop tracking any memory allocations made
ece250::allocation_table.stop_recording();
}
return 0;
}
#endif