mem-leak-test.cpp (2678B)
1 #include <unistd.h> 2 3 #include <csignal> 4 #include <cstdlib> 5 #include <ctime> 6 #include <iostream> 7 #include <thread> 8 #include <vector> 9 10 #include "../include/browser.hpp" 11 12 std::string genRandom(const int len) { 13 static const char alphanum[] = 14 "0123456789" 15 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 16 "abcdefghijklmnopqrstuvwxyz"; 17 std::string tmp_s; 18 tmp_s.reserve(len); 19 20 for (int i = 0; i < len; ++i) { 21 tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)]; 22 } 23 24 return tmp_s; 25 } 26 27 void traversal() { 28 Browser b{}; 29 b.goToSite("gemini://localhost", true); 30 31 for (int i = 0; i < 100; ++i) { 32 auto* lls = b.getLinkLines(); 33 std::size_t count = lls->size(); 34 delete lls; 35 36 if (b.getCurrentLink() != nullptr) { 37 std::cout << "CURRENTLY AT: " 38 << b.getCurrentLink()->getLinkDestination().to_string() 39 << std::endl; 40 } 41 42 if (count == 0 && rand() % 10 == 0) { 43 std::cout << "GOING BACK" << std::endl; 44 b.goBack(); 45 } else if (rand() % 5 == 0) { 46 std::cout << "GOING FORWARD" << std::endl; 47 b.goForward(); 48 } else if (rand() % 10 == 0) { 49 std::cout << "GOING BACK" << std::endl; 50 b.goBack(); 51 } else if (rand() % 5 == 0) { 52 if (rand() % 5 == 0) { 53 std::string dst = std::string{"gemini://"} + 54 genRandom(rand() % 5000) + ".com"; 55 std::cout << "TRAVELLING TO: " << dst << std::endl; 56 b.goToSite(dst); 57 } else { 58 std::string dst = 59 std::string{"gemini://"} + genRandom(rand() % 15) + ".com"; 60 std::cout << "TRAVELLING TO: " << dst << std::endl; 61 b.goToSite(dst); 62 } 63 } else { 64 if (count == 0) { 65 count = 10; 66 } 67 int num = ((rand() % count) + 1) + (rand() % 5); 68 std::cout << "LINK NUMBER(2): " << num << std::endl; 69 b.followLinkNumber(num); 70 } 71 } 72 } 73 74 // NOTE: When running this you probably want to run something like: 75 // `watch pkill {browser}` 76 // because it'll probably try to follow http links that are found. 77 78 int main() { 79 std::signal(SIGPIPE, SIG_IGN); // this is also done in main for the actual 80 // executable, this is for consistency. 81 std::vector<std::thread*> vec{}; 82 for (int x = 0; x < 5; ++x) { 83 auto* t = new std::thread(&traversal); 84 vec.push_back(t); 85 } 86 for (auto& th : vec) { 87 th->join(); 88 delete th; 89 } 90 }