AVLSanityCheck.cxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <random>
  5. #include <vector>
  6. #include "AVLCommands.h"
  7. #define SAMPLE_SIZE 1000
  8. #define NUM_TESTS 10000
  9. int main() {
  10. //int whileLoopNum = 0;
  11. // C++11 random number tutorial: https://gist.github.com/PhDP/5289449
  12. // Seed random number generator
  13. std::mt19937_64 rng(time(0));
  14. // Create uniform distribution
  15. std::uniform_int_distribution<int> unif(
  16. std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
  17. std::uniform_int_distribution<int> op(0,10);
  18. std::vector<int> sampleData, AVLSortedData;
  19. sampleData.reserve(SAMPLE_SIZE);
  20. AVLSortedData.reserve(SAMPLE_SIZE);
  21. std::cout << "Running tests..." << std::flush;
  22. for (unsigned int sample = 0; sample < NUM_TESTS; sample++) {
  23. AVLCommands avl;
  24. // On size_t usage here: https://stackoverflow.com/questions/131803/unsigned-int-vs-size-t
  25. for (size_t i = 0; i < SAMPLE_SIZE; i++) {
  26. //std::cout << "Loop number: " << i << std::endl;
  27. //std::cout << "for sample: " << sample << std::endl;
  28. if (op(rng) == 0 && !avl.empty()) {
  29. //std::cout << "I will delete!" << std::endl;
  30. avl.Delete(sampleData.back());
  31. //std::cout << "I have deleted!" << std::endl;
  32. sampleData.pop_back();
  33. } else {
  34. // Add random integer to array
  35. int x = unif(rng);
  36. //std::cout << "I will insert!" << std::endl;
  37. avl.Insert(x);
  38. //std::cout << "I have inserted!" << std::endl;
  39. sampleData.push_back(x);
  40. }
  41. }
  42. while (!avl.empty()) {
  43. //std::cout << "I will delete the min!" << std::endl;
  44. AVLSortedData.push_back(avl.DeleteMin());
  45. //whileLoopNum++;
  46. //std::cout << "I have delete " << whileLoopNum << " mins!" << std::endl;
  47. }
  48. //whileLoopNum = 0;
  49. std::sort(sampleData.begin(), sampleData.end());
  50. assert(sampleData == AVLSortedData);
  51. AVLSortedData.clear();
  52. sampleData.clear();
  53. if (sample % (NUM_TESTS / 10) == 0) {
  54. std::cout << "." << std::flush;
  55. }
  56. }
  57. std::cout << "Tests complete.\n";
  58. }