-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_tests.cpp
610 lines (500 loc) · 13.5 KB
/
unit_tests.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* Title: unit_tests.cpp
* Course: CS 15
* Modified by: Naveed Naeem
* UTLN: nnaeem01
* Date: 7/13/2023
*
* CS 15 - HW3: Binary Search Trees
*
* Purpose: Contains the Unit tests for the BinarySearchTree Class.
*
*/
#include <iostream>
#include <cassert>
#include <math.h>
#include <limits>
#include <random>
#include "BinarySearchTree.h"
using namespace std;
// Test the BST constructor for an empty tree
void test_BST_constructor_empty()
{
BinarySearchTree bst;
assert(bst.node_count() == 0);
assert(bst.find_min() == numeric_limits<int>::max());
assert(bst.find_max() == numeric_limits<int>::min());
assert(bst.tree_height() == - 1);
}
// Test the BST copy constructor
void test_BST_copy_constructor()
{
BinarySearchTree bst1;
bst1.insert(3);
bst1.insert(6);
bst1.insert(9);
assert(bst1.node_count() == 3);
assert(bst1.find_min() == 3);
assert(bst1.find_max() == 9);
BinarySearchTree bst2(bst1);
assert(bst2.node_count() == 3);
assert(bst2.find_min() == 3);
assert(bst2.find_max() == 9);
}
// Test the BST operator overload copy constructor
void test_BST_operator_overload_copy_constructor()
{
BinarySearchTree bst1;
bst1.insert(6);
bst1.insert(3);
bst1.insert(1);
assert(bst1.node_count() == 3);
assert(bst1.find_min() == 1);
assert(bst1.find_max() == 6);
BinarySearchTree bst2 = bst1;
assert(bst2.node_count() == 3);
assert(bst2.find_min() == 1);
assert(bst2.find_max() == 6);
}
// Test the BST assignement operator
void test_BST_assignment_operator()
{
BinarySearchTree bst1;
BinarySearchTree bst2;
BinarySearchTree bst3;
bst1.insert(16);
bst1.insert(8);
bst1.insert(32);
bst1.insert(64);
bst2 = bst1;
assert(bst2.find_max() == 64);
assert(bst2.find_min() == 8);
assert(bst2.contains(16));
assert(bst2.contains(32));
for (int i = 0; i < 100; i++)
{
if (i % 2 != 0)
{
bst3.insert(i);
}
}
assert(bst3.node_count() == 50);
assert(bst3.contains(1));
assert(bst3.contains(49));
assert(bst3.contains(99));
bst1 = bst3;
for (int i = 0; i < 100; i++)
{
if (i % 2 != 0)
{
assert(bst1.contains(i));
}
}
assert(bst1.node_count() == 50);
assert(bst1.contains(1));
assert(bst1.contains(49));
assert(bst1.contains(99));
}
// Test the BST insert method with a single value
void test_BST_insert()
{
BinarySearchTree bst;
assert(bst.node_count() == 0);
bst.insert(1234645);
assert(bst.node_count() == 1);
assert(bst.contains(1234645));
}
// Insert bunch of unique elements into the bst and verify the node count and
// count total are correct
void test_BST_insert_multiple()
{
BinarySearchTree bst;
bst.insert(4);
bst.insert(2);
bst.insert(5);
bst.insert(8);
bst.insert(10);
bst.insert(7);
bst.insert(0);
bst.insert(-1);
bst.insert(1);
bst.insert(6);
bst.insert(3);
bst.insert(69);
bst.insert(42);
bst.insert(12);
bst.insert(18);
bst.insert(13);
assert(bst.node_count() == 16);
assert(bst.count_total() == 199);
}
// Insert the same values into a binary 100 times and confirm the number of
// nodes doesn't change and that the count_total() value makes sense
void test_BST_insert_duplicates()
{
BinarySearchTree bst;
for (int i = 0; i < 100; i++)
{
bst.insert(4);
bst.insert(2);
bst.insert(5);
bst.insert(8);
bst.insert(10);
bst.insert(7);
bst.insert(0);
bst.insert(-1);
bst.insert(1);
bst.insert(6);
bst.insert(3);
bst.insert(69);
bst.insert(42);
bst.insert(12);
bst.insert(18);
bst.insert(13);
}
assert(bst.node_count() == 16);
assert(bst.count_total() == (199 *100));
}
// Insert very large positive numbers and their negation and confirm the min
// value is correct.
void test_BST_find_min()
{
BinarySearchTree bst_neg;
// Test empty tree
assert(bst_neg.find_min() == numeric_limits<int>::max());
bst_neg.insert(50);
// Insert mutiple positive and negative numbers
for (int i = 0; i < 256; i++)
{
bst_neg.insert(-i);
bst_neg.insert(i);
}
assert(bst_neg.find_min() == -255);
// Insert a very large negative number to a populated tree
bst_neg.insert((-1 * pow(2, 31)));
assert(bst_neg.find_min() == (-1 * pow(2, 31)));
BinarySearchTree bst_pos;
//Insert a positive number
bst_pos.insert(1024);
for (size_t i = 0; i < 1000; i++)
{
bst_pos.insert(i*i);
}
// Confirm the min value is 0
assert(bst_pos.find_min() == 0);
}
// Insert a lot of negative numbers and confirm the max is 0
// Insert a lot of large numbers and confirm the max makes sense
void test_BST_find_max()
{
BinarySearchTree bst_neg;
// Test empty tree
assert(bst_neg.find_max() == numeric_limits<int>::min());
bst_neg.insert(-1);
for (int i = 0; i < 100; i++)
{
bst_neg.insert(-i * 1000);
}
assert(bst_neg.find_max() == 0);
BinarySearchTree bst_pos;
bst_pos.insert(32767);
for (int i = 0; i < 1000; i++)
{
bst_pos.insert(i*1000);
}
assert(bst_pos.find_max() == 999000);
}
//insert 50000 random numbers and confirm they exist in the BST
void test_BST_contains_random_mult()
{
BinarySearchTree bst;
assert(not bst.contains(256));
std::random_device rd;
std::mt19937 generator(rd());
vector<int> randomVector;
std::uniform_real_distribution<double> distribution(0, 4096);
for (int i = 0; i < 50000; i++)
{
int randomNumber = (int)distribution(generator);
randomVector.push_back(randomNumber);
bst.insert(randomNumber);
}
for (int i = 0; i < 50000; i++)
{
assert(bst.contains(randomVector[i]));
}
}
// Test BST contains method on an empty tree
void test_BST_contains_empty()
{
BinarySearchTree bst_empty;
assert(not bst_empty.contains(-256));
assert(not bst_empty.contains(0));
assert(not bst_empty.contains(1034));
}
// Test BST contains on a single node tree
void test_BST_contains_single()
{
BinarySearchTree bst_single;
bst_single.insert(0);
assert(bst_single.contains(0));
assert(bst_single.node_count() == 1);
assert(not bst_single.contains(-1));
assert(not bst_single.contains(1));
}
// Test BST remove() on an empty tree
void test_BST_remove_empty()
{
BinarySearchTree bst_empty;
assert(bst_empty.count_total() == 0);
assert(bst_empty.node_count() == 0);
assert(bst_empty.tree_height() == -1);
std::random_device rd;
std::mt19937 generator(rd());
vector<int> randomVector;
std::uniform_real_distribution<double> distribution(0, 4096);
for (int i = 0; i < 50000; i++)
{
int randomNumber = (int)distribution(generator);
assert(not bst_empty.remove(randomNumber));
}
}
// Test BST remove() on a single node tree
void test_BST_remove_single()
{
BinarySearchTree bst_single;
bst_single.insert(300);
assert(bst_single.count_total() == 300);
assert(bst_single.node_count() == 1);
assert(bst_single.remove(300));
assert(bst_single.count_total() == 0);
assert(bst_single.node_count() == 0);
assert(not bst_single.contains(300));
}
// Test BST remove where the node to remove is the root and the root has child
// nodes
void test_BST_remove_root_with_children()
{
BinarySearchTree bst;
bst.insert(11);
bst.insert(7);
bst.remove(11);
assert(not bst.contains(11));
assert(bst.node_count() == 1);
assert(bst.tree_height() == 0);
assert(bst.count_total() == 7);
bst.insert(8);
bst.remove(7);
assert(not bst.contains(7));
assert(bst.node_count() == 1);
assert(bst.tree_height() == 0);
assert(bst.count_total() == 8);
}
// Test BST remove on a tree with multiple nodes
void test_BST_remove_multiple_nodes()
{
BinarySearchTree bst;
int nums[10] = {9, 8, 10, 5, 3, 1, 2, 6, 11, 7};
for (int i = 0; i < 10; i++)
{
bst.insert(nums[i]);
}
for (int i = 0; i < 10; i++)
{
assert(bst.contains(nums[i]));
assert(bst.remove(nums[i]));
assert(not bst.contains(nums[i]));
// Make sure no other nodes got removed unintentionally
for (int j = (i + 1); j < 10; j++)
{
assert(bst.contains(nums[j]));
}
}
}
// Test BST node_count on an empty tree
void test_BST_node_count_empty()
{
BinarySearchTree bst;
assert(bst.node_count() == 0);
assert(bst.count_total() == 0);
}
// Test BST node_count on tree with only a root node
void test_BST_node_count_root()
{
BinarySearchTree bst;
bst.insert(42);
assert(bst.node_count() == 1);
}
// Test BST node_count on a tree with a root, left child, right child
void test_BST_node_count_root_w_children()
{
BinarySearchTree bst;
bst.insert(42);
assert(bst.node_count() == 1);
bst.insert(12);
assert(bst.node_count() == 2);
bst.insert(52);
assert(bst.node_count() == 3);
}
// Test BST node_count on a tree with multiple nodes
void test_BST_node_count_multiple()
{
BinarySearchTree bst;
int nums[11] = {42, 36, 12, 17, 69, 49, 64, 25, 81, 96, 77};
for (int i = 0; i < 11; i++)
{
bst.insert(nums[i]);
}
assert(bst.node_count() == 11);
}
// Test BST count_total on an empty tree
void test_BST_count_total_empty()
{
BinarySearchTree bst;
assert(bst.count_total() == 0);
}
// Test BST count_total the root, whose value has been inserted multiple times
void test_BST_count_total_root()
{
BinarySearchTree bst;
for (int i = 0; i < 10; i++)
{
bst.insert(42);
assert(bst.count_total() == (42 * (i + 1)));
}
}
// Test BST count_total on a tree with a root, left Child, and right child
// Repeat ten times and assert that the count total is correct everytime
// the node's are inserted.
void test_BST_count_total_root_w_children()
{
BinarySearchTree bst;
for (int i = 0; i < 10; i++)
{
bst.insert(10);
bst.insert(1);
bst.insert(14);
assert(bst.count_total() == (25 * (i + 1)));
}
}
// Test BST count_total on a BST that has 50000 random values inserted and
// confirm the count_total is the expected value
void test_BST_count_total_random()
{
BinarySearchTree bst;
std::random_device rd;
std::mt19937 generator(rd());
vector<int> randomVector;
std::uniform_real_distribution<double> distribution(0, 4096);
for (int i = 0; i < 50000; i++)
{
int randomNumber = (int)distribution(generator);
randomVector.push_back(randomNumber);
bst.insert(randomNumber);
}
int randomVectorSum = 0;
for (int i = 0; i < 50000; i++)
{
randomVectorSum += randomVector[i];
}
assert(bst.count_total() == randomVectorSum);
}
// Test BST tree_height on an empty tree
void test_BST_tree_height_empty()
{
BinarySearchTree bst;
assert(bst.tree_height() == -1);
}
// Test BST tree_height on an empty tree
void test_BST_tree_height_root_only()
{
BinarySearchTree bst;
bst.insert(42);
assert(bst.tree_height() == 0);
}
// Test BST tree_height on root with left and right children
void test_BST_tree_height_root_w_children()
{
BinarySearchTree bst;
bst.insert(42);
bst.insert(-19);
bst.insert(52);
assert(bst.tree_height() == 1);
}
// Test BST tree_height by inserting multiple values to the left
void test_BST_tree_height_left_multiple()
{
BinarySearchTree bst;
for (int i = 0; i < 26; i++)
{
bst.insert(-1 * i);
}
assert(bst.tree_height() == 25);
}
// Test BST tree_height by inserting multiple values to the right
void test_BST_tree_height_right_multiple()
{
BinarySearchTree bst;
for (int i = 0; i < 26; i++)
{
bst.insert(i);
}
assert(bst.tree_height() == 25);
}
// Test BST tree_height by inserting random values
void test_BST_tree_height_multiple()
{
BinarySearchTree bst;
bst.insert(4);
bst.insert(2);
bst.insert(5);
bst.insert(8);
bst.insert(10);
bst.insert(7);
bst.insert(0);
bst.insert(-1);
bst.insert(1);
bst.insert(6);
bst.insert(3);
bst.insert(69);
bst.insert(42);
bst.insert(12);
bst.insert(18);
bst.insert(13);
assert(bst.tree_height() == 8);
}
int main()
{
//Test constructors and assignment operator
test_BST_constructor_empty();
test_BST_copy_constructor();
test_BST_operator_overload_copy_constructor();
test_BST_assignment_operator();
//Test the BST public methods
test_BST_insert();
test_BST_insert_multiple();
test_BST_insert_duplicates();
test_BST_find_min();
test_BST_find_max();
test_BST_contains_empty();
test_BST_contains_single();
test_BST_contains_random_mult();
test_BST_node_count_empty();
test_BST_node_count_root_w_children();
test_BST_node_count_multiple();
test_BST_count_total_empty();
test_BST_count_total_root();
test_BST_count_total_root_w_children();
test_BST_count_total_random();
test_BST_remove_empty();
test_BST_remove_single();
test_BST_remove_root_with_children();
test_BST_remove_multiple_nodes();
test_BST_tree_height_empty();
test_BST_tree_height_root_only();
test_BST_tree_height_root_w_children();
test_BST_tree_height_left_multiple();
test_BST_tree_height_right_multiple();
test_BST_tree_height_multiple();
return 0;
}