BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
test_set2.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: test_set2.h
4 // Purpose: A test method for set implementations with two int arguments.
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_SET2_INCLUDED
27 #define TEST_SET2_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_CUR2_INCLUDED
50 #include "gen_cur2.h"
51 #endif
52 
53 #ifndef RTEST_INCLUDED
54 #include "rtest.h"
55 #endif
56 
57 //=============================================================================
58 // Private Constants:
59 //=============================================================================
60 
61 //-----------------------------------------------------------------------------
62 // TEST_SET2_MAGIC: Magic number (identifies objects of this class).
63 //-----------------------------------------------------------------------------
64 
65 static const long TEST_SET2_MAGIC = 0x5453320AL; // 'TS2\n'
66 
67 
68 //=============================================================================
69 // Class Template for Testing Sets with two Integer Arguments:
70 //=============================================================================
71 
72 template<class T> class test_set2_c : public rtest_c {
73 
74  public:
75 
76 //-----------------------------------------------------------------------------
77 // Constructor:
78 //-----------------------------------------------------------------------------
79 
80  test_set2_c(T set, int test_id) :
81  rtest_c(set),
82  DataCur(data_rel(test_id))
83  {
84  // Check parameters:
85  CHECK_PAR(set, "test_set2_c::constructor");
86  CHECK(test_id >= 0 && test_id <= 4,
87  "test_set2_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_SET2_MAGIC);
95  }
96 
97 //-----------------------------------------------------------------------------
98 // Destructor:
99 //-----------------------------------------------------------------------------
100 
101  ~test_set2_c()
102  {
103 
104  // Check this object:
105  CHECK_VALID("test_set2_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_set2_c(const test_set2_c& obj); // Not implemented
118  test_set2_c& operator=(const test_set2_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  DataCur.open();
131 
132  // Insert tuples:
133  while(DataCur.fetch()) {
134  tuple_no++;
135 
136  int c1 = DataCur.c1();
137  int c2 = DataCur.c2();
138  bool ok = Set->insert(c1, c2);
139  if(!ok) {
140  DataCur.close();
141  rtest_c::inserted_rows(tuple_no);
146  return tuple_no;
147  }
148  }
149 
150  // Close cursor:
151  DataCur.close();
152 
153  // Remember number of inserted rows:
154  rtest_c::inserted_rows(tuple_no);
155 
156  // Ok:
157  return 0;
158  }
159 
160 //-----------------------------------------------------------------------------
161 // lookup: Lookup all tuples that were inserted into the set.
162 //-----------------------------------------------------------------------------
163 
164  int lookup()
165  {
166  // Count number of tuples that were found:
167  int tuple_no = 0;
168 
169  // Open generator for tuples:
170  DataCur.open();
171 
172  // Lookup tuples:
173  while(DataCur.fetch()) {
174  tuple_no++;
175 
176  int c1 = DataCur.c1();
177  int c2 = DataCur.c2();
178  bool ok = Set->contains(c1, c2);
179  if(!ok) {
180  DataCur.close();
185  return tuple_no;
186  }
187  }
188 
189  // Close cursor:
190  DataCur.close();
191 
192  // Ok:
193  return 0;
194  }
195 
196 
197 //-----------------------------------------------------------------------------
198 // Values of size measures for the set implementation:
199 //-----------------------------------------------------------------------------
200 
201  int size_value(relsize_t relsize) {
202  return Set->size_value(relsize);
203  }
204 
205 
206 //-----------------------------------------------------------------------------
207 // Debugging Support:
208 //-----------------------------------------------------------------------------
209 
210 #if VER_DEBUG
211  public:
212  // Integrity check:
213  str_t check() const
214  {
215  // Check magic number:
216  if(Magic != TEST_SET2_MAGIC)
217  return "wrong magic number";
218 
219  // Check superclass:
220  str_t msg = rtest_c::check();
221  if(msg)
222  return msg;
223 
224  // Ok:
225  return 0;
226  }
227 
228 
229  private:
230  // Magic number (identifies objects of this class for debugging).
231  long Magic; // Must be "RTEST_MAGIC".
232 #endif
233 
234 #if VER_DUMP
235  public:
236  // Display data structure:
237  virtual void dump(str_t headline = STR_NULL) const
238  {
239  // Headline:
240  if(headline == STR_NULL)
241  headline =
242  "Test for sets with one int column (test_set2_c)";
243 
244  // Call dump method of superclass:
245  rtest_c::dump(headline);
246  }
247 #endif
248 
249 //=============================================================================
250 // Private Object Members:
251 //=============================================================================
252 
253  private:
254 
255  // Set (Relation) to test:
256  T Set;
257 
258  // ID of the test:
259  int TestID;
260 
261  // Generator for tuples to insert:
262  gen_cur2_c DataCur;
263 
264 //=============================================================================
265 // Auxiliary Functions:
266 //=============================================================================
267 
268  private:
269 
270 //-----------------------------------------------------------------------------
271 // data_rel: Select tuple generator for initial insertion depending on test ID.
272 //-----------------------------------------------------------------------------
273 
274  int data_rel(int test_id)
275  {
276  // Check parameter:
277  CHECK(test_id >= 0 && test_id <= 4,
278  "test_set2_c::data_rel: unknown test_id");
279 
280  switch(test_id) {
281  case 0:
282  // Small sequential relation:
283  return 0;
284  case 1:
285  // One million tuples, sequential:
286  return 1;
287  case 2:
288  // One million tuples, a bit more "random":
289  return 2;
290  case 3:
291  // 100 million tuples, a bit more "random":
292  return 3;
293  case 4:
294  // One billion tuples, a bit more "random":
295  return 4;
296  default:
297  // Invalid test id:
299  "test_set2_c::data_rel: bad id");
300  return 1;
301  }
302  }
303 
304 
305 //=============================================================================
306 // End of Class Template:
307 //=============================================================================
308 
309 };
310 
311 
312 //=============================================================================
313 // End of Include File:
314 //=============================================================================
315 
316 #endif
317 
#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
Definition: test_set2.h:72
Definition: gen_cur2.h:63
void error_row_open()
Definition: rtest.h:228
Cursor over Generated Relations with two Integer Columns (for Tests).
void error_row_close()
Definition: rtest.h:285
#define STR_NULL
Definition: str.h:52
#define CHECK(EX, MSG)
Definition: check.h:69
#define CHECK_PAR(PAR, PLACE)
Definition: check.h:102