BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
test_set1.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: test_set1.h
4 // Purpose: A test method for set implementations with one int argument.
5 // Last Change: 11.08.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 2016-2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef TEST_SET1_INCLUDED
27 #define TEST_SET1_INCLUDED
28 
29 //=============================================================================
30 // Used Types and Macros:
31 //=============================================================================
32 
33 #ifndef VER_INCLUDED
34 #include "../base/ver.h"
35 #endif
36 
37 #ifndef STR_INCLUDED
38 #include "../base/str.h"
39 #endif
40 
41 #ifndef CHECK_INCLUDED
42 #include "../base/check.h"
43 #endif
44 
45 #ifndef RELSIZE_INCLUDED
46 #include "../rel/relsize.h"
47 #endif
48 
49 #ifndef GEN_CUR1_INCLUDED
50 #include "gen_cur1.h"
51 #endif
52 
53 #ifndef RTEST_INCLUDED
54 #include "rtest.h"
55 #endif
56 
57 //=============================================================================
58 // Private Constants:
59 //=============================================================================
60 
61 //-----------------------------------------------------------------------------
62 // TEST_SET1_MAGIC: Magic number (identifies objects of this class).
63 //-----------------------------------------------------------------------------
64 
65 static const long TEST_SET1_MAGIC = 0x5453310AL; // 'TS1\n'
66 
67 
68 //=============================================================================
69 // Class Template for Testing Sets with one Integer Argument:
70 //=============================================================================
71 
72 template<class T> class test_set1_c : public rtest_c {
73 
74  public:
75 
76 //-----------------------------------------------------------------------------
77 // Constructor:
78 //-----------------------------------------------------------------------------
79 
80  test_set1_c(T set, int test_id) :
81  rtest_c(set),
82  Cur1(rel1(test_id))
83  {
84  // Check parameters:
85  CHECK_PAR(set, "test_set1_c::constructor");
86  CHECK(test_id >= 0 && test_id <= 4,
87  "test_set1_c::constructor: unknown test_id");
88 
89  // Initialize attributes:
90  Set = set;
91  TestID = test_id;
92 
93  // Set Magic number:
94  CHECK_CODE(Magic = TEST_SET1_MAGIC);
95  }
96 
97 //-----------------------------------------------------------------------------
98 // Destructor:
99 //-----------------------------------------------------------------------------
100 
101  ~test_set1_c()
102  {
103 
104  // Check this object:
105  CHECK_VALID("test_set1_c::destructor");
106 
107  // Make this object invalid:
108  CHECK_CODE(Magic = 0);
109  }
110 
111 //-----------------------------------------------------------------------------
112 // Copy-constructor and assignment operator are not supported for this class:
113 //-----------------------------------------------------------------------------
114 
115  private:
116 
117  test_set1_c(const test_set1_c& obj); // Not implemented
118  test_set1_c& operator=(const test_set1_c& obj); // Not implemented
119 
120 //-----------------------------------------------------------------------------
121 // insert: Insert tuples into set.
122 //-----------------------------------------------------------------------------
123 
124  int insert()
125  {
126  // Count number of tuples inserted:
127  int tuple_no = 0;
128 
129  // Open generator for tuples:
130  Cur1.open();
131 
132  // Insert tuples:
133  while(Cur1.fetch()) {
134  tuple_no++;
135 
136  int c1 = Cur1.c1();
137  bool ok = Set->insert(c1);
138  if(!ok) {
139  Cur1.close();
140  rtest_c::inserted_rows(tuple_no);
144  return tuple_no;
145  }
146  }
147 
148  // Close cursor:
149  Cur1.close();
150 
151  // Remember number of inserted rows:
152  rtest_c::inserted_rows(tuple_no);
153 
154  // Ok:
155  return 0;
156  }
157 
158 //-----------------------------------------------------------------------------
159 // lookup: Lookup all tuples that were inserted into the set.
160 //-----------------------------------------------------------------------------
161 
162  int lookup()
163  {
164  // Count number of tuples that were found:
165  int tuple_no = 0;
166 
167  // Open generator for tuples:
168  Cur1.open();
169 
170  // Lookup tuples:
171  while(Cur1.fetch()) {
172  tuple_no++;
173 
174  int c1 = Cur1.c1();
175  bool ok = Set->contains(c1);
176  if(!ok) {
177  Cur1.close();
181  return tuple_no;
182  }
183  }
184 
185  // Close cursor:
186  Cur1.close();
187 
188  // Ok:
189  return 0;
190  }
191 
192 
193 //-----------------------------------------------------------------------------
194 // Values of size measures for the set implementation:
195 //-----------------------------------------------------------------------------
196 
197  int size_value(relsize_t relsize) {
198  return Set->size_value(relsize);
199  }
200 
201 
202 //-----------------------------------------------------------------------------
203 // Debugging Support:
204 //-----------------------------------------------------------------------------
205 
206 #if VER_DEBUG
207  public:
208  // Integrity check:
209  str_t check() const
210  {
211  // Check magic number:
212  if(Magic != TEST_SET1_MAGIC)
213  return "wrong magic number";
214 
215  // Check superclass:
216  str_t msg = rtest_c::check();
217  if(msg)
218  return msg;
219 
220  // Ok:
221  return 0;
222  }
223 
224 
225  private:
226  // Magic number (identifies objects of this class for debugging).
227  long Magic; // Must be "RTEST_MAGIC".
228 #endif
229 
230 #if VER_DUMP
231  public:
232  // Display data structure:
233  virtual void dump(str_t headline = STR_NULL) const
234  {
235  // Headline:
236  if(headline == STR_NULL)
237  headline =
238  "Test for sets with one int column (test_set1_c)";
239 
240  // Call dump method of superclass:
241  rtest_c::dump(headline);
242  }
243 #endif
244 
245 //=============================================================================
246 // Private Object Members:
247 //=============================================================================
248 
249  private:
250 
251  // Set (Relation) to test:
252  T Set;
253 
254  // ID of the test:
255  int TestID;
256 
257  // Generator for tuples to insert:
258  gen_cur1_c Cur1;
259 
260 //=============================================================================
261 // Auxiliary Functions:
262 //=============================================================================
263 
264  private:
265 
266 //-----------------------------------------------------------------------------
267 // rel1: Select tuple generator for initial insertion depending on test ID.
268 //-----------------------------------------------------------------------------
269 
270  int rel1(int test_id)
271  {
272  // Check parameter:
273  CHECK(test_id >= 0 && test_id <= 4,
274  "test_set1_c::rel1: unknown test_id");
275 
276  switch(test_id) {
277  case 0:
278  // Small sequential relation:
279  return 0;
280  case 1:
281  // One million tuples, sequential:
282  return 1;
283  case 2:
284  // One million tuples, mixed sequences:
285  return 2;
286  case 3:
287  // 100 million tuples, mixed sequences:
288  return 3;
289  case 4:
290  // One billion tuples, mixed sequences:
291  return 4;
292  default:
293  // Invalid test id:
294  CHECK_IMPOSSIBLE("test_set1_c::rel1: bad id");
295  return 1;
296  }
297  }
298 
299 
300 //=============================================================================
301 // End of Class Template:
302 //=============================================================================
303 
304 };
305 
306 
307 //=============================================================================
308 // End of Include File:
309 //=============================================================================
310 
311 #endif
312 
#define CHECK_CODE(CODE)
Definition: check.h:167
Superclass for Standard Tests of Relation-like Data Structures.
void inserted_rows(int n)
Definition: rtest.h:209
#define CHECK_IMPOSSIBLE(MSG)
Definition: check.h:151
Abstract superclass for Standard Tests/Benchmarks of relation-like data structures with insert() and ...
Definition: rtest.h:81
void error_row_int(int val)
Definition: rtest.h:256
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
Cursor over Generated Relations with one Integer Column (for Tests)
Definition: test_set1.h:72
void error_row_open()
Definition: rtest.h:228
void error_row_close()
Definition: rtest.h:285
Definition: gen_cur1.h:63
#define STR_NULL
Definition: str.h:52
#define CHECK(EX, MSG)
Definition: check.h:69
#define CHECK_PAR(PAR, PLACE)
Definition: check.h:102