int TestSort_LList(Sorter<int> *_sorter) { LList<int> *llist = new LList<int>(); for (int i = 0; i < SORT_ITEMS; i++) { llist->insert(rand()); } llist->sort(_sorter); for (int i = 0; i < SORT_ITEMS - 1; i++) { TEST_ASSERT(llist->get(i) <= llist->get(i+1)); } delete llist; return 0; }
int main() { LList<Pokemon> pokedex; pokedex.push_back(Pokemon(1, "Bulbasaur")); pokedex.push_back(Pokemon(4, "Charmander")); pokedex.push_back(Pokemon(7, "Squirtle")); for (int i = 0; i < pokedex.size(); i++) { std::cout << pokedex.get(i).name << std::endl; } return 0; }
int IRC::connect(char *server,char *nick,u_short port,char *pass) { ccerr = conn->Connect(server,port); if (ccerr != 0) return ccerr; this->my_server = server; if (pass != NULL) { strcpy(this->msg_buf,"PASS "); strcat(this->msg_buf,pass); this->send(this->msg_buf); } this->nick(nick); this->my_nick = nick; this->user(nick,"0",this->my_server); char *line = this->readline(); LList<char *> results = this->split(line," "); int i = 0; while (!results.valid(1)) { char *line = this->readline(); results = this->split(line," "); } while (!strcmp(results.get(1),"001")) { if (strcmp(results.get(1),"PING")) { strcpy(this->msg_buf,"PONG "); strcat(this->msg_buf,results.get(2)); this->send(this->msg_buf); } else if (strcmp(results.get(1),"433") || strcmp(results.get(1),"432")) { stringstream out; out << ++i; this->nick(strcat(nick,out.str().c_str())); this->my_nick = strcat(nick,out.str().c_str()); } line = this->readline(); results = this->split(line," "); } strcpy(this->msg_buf,"WHOIS "); strcat(this->msg_buf,this->my_nick); this->send(this->msg_buf); do { line = this->readline(); results = this->split(line," "); if (strcmp(results.get(1),"311")) this->my_host = strcat(results.get(4),strcat("@",results.get(5))); } while (this->my_host == NULL); return 0; }
int TestInsertionSort_LList() { LList<int> *llist = new LList<int>(); llist->insert(4); llist->insert(2); llist->insert(0); llist->insert(3); llist->insert(1); InsertionSort<int> *is = new InsertionSort<int>(); llist->sort(is); delete is; for (int i = 0; i < 5; i++) { TEST_ASSERT(llist->get(i) == i); } delete llist; return 0; }
int TestQuickSort_LList() { LList<int> *llist = new LList<int>(); llist->insert(4); llist->insert(2); llist->insert(0); llist->insert(3); llist->insert(1); QuickSort<int> *qs = new QuickSort<int>(); llist->sort(qs); delete qs; for (int i = 0; i < 5; i++) { TEST_ASSERT(llist->get(i) == i); } delete llist; return 0; }
int TestShellSort_LList() { LList<int> *llist = new LList<int>(); llist->insert(4); llist->insert(2); llist->insert(0); llist->insert(3); llist->insert(1); ShellSort<int> *ss = new ShellSort<int>(); llist->sort(ss); delete ss; for (int i = 0; i < 5; i++) { TEST_ASSERT(llist->get(i) == i); } delete llist; return 0; }
int TestLList() { LList<char *> *llist = new LList<char *>(); TEST_ASSERT(llist); TEST_ASSERT(!llist->valid((unsigned int)-1)); TEST_ASSERT(!llist->valid(1)); TEST_ASSERT(!llist->valid(0)); llist->insert(newStr("one")); llist->insert(newStr("two")); llist->insert(newStr("three")); llist->insert(newStr("four")); TEST_ASSERT(strcmp(llist->get(0), "one") == 0); TEST_ASSERT(strcmp(llist->get(2), "three") == 0); TEST_ASSERT(strcmp(llist->get(3), "four") == 0); TEST_ASSERT(strcmp(llist->get(1), "two") == 0); delete [] llist->get(1); llist->remove(1); TEST_ASSERT(strcmp(llist->get(0), "one") == 0); TEST_ASSERT(strcmp(llist->get(1), "three") == 0); TEST_ASSERT(strcmp(llist->get(2), "four") == 0); while (llist->valid(0)) { delete [] llist->get(0); llist->remove(0); } TEST_ASSERT(!llist->valid((unsigned int)-1)); TEST_ASSERT(!llist->valid(1)); TEST_ASSERT(!llist->valid(0)); delete llist; return 0; }
void ParseMemoryLeakFile(const char *_inputFilename, const char *_outputFilename) { /* */ /* Start up */ /* */ RedBlackTree<char *, int> combined; RedBlackTree<char *, int> frequency; int unrecognised = 0; /* */ /* Open the file and start parsing */ /* */ FILE *memoryfile = fopen(_inputFilename, "rb"); while (memoryfile && !feof(memoryfile)) { char thisline[1024]; fgets(thisline, 1024, memoryfile); if (!strncmp(thisline, " Data:", 6) == 0) { /* This line is a data line - useless to us */ if (strchr(thisline, ':')) { /* This line does not have a source file location - useless to us */ /* Get the size */ char *lastcomma = strrchr(thisline, ','); if (lastcomma == 0) continue; char *ssize = lastcomma + 2; int size; char unused[32]; sscanf(ssize, "%d %s", &size, unused); /* Get the source file name */ char *sourcelocation = thisline; char *colon = strrchr(thisline, ':'); *(colon - 1) = '\x0'; /* Put the result into our BTree */ int result = 0; bool found = combined.find(sourcelocation, result); if (found) combined.replace(sourcelocation, result + size); else combined.insert(sourcelocation, size); found = frequency.find(sourcelocation, result); if (frequency.exists(sourcelocation)) frequency.replace(sourcelocation, result + size); else frequency.insert(sourcelocation, 1); } else { char *lastcomma = strrchr(thisline, ','); if (lastcomma) { char *ssize = lastcomma + 2; int size; char unused[32]; sscanf(ssize, "%d %s", &size, unused); unrecognised += size; } } } } fclose(memoryfile); /* */ /* Sort the results into a list */ /* */ LList<char *> sorted; DArray<char *> *dataI = combined.ConvertIndexToDArray(); DArray<int> *dataD = combined.ConvertToDArray(); int totalsize = 0; for (size_t i = 0; i < dataI->size(); i++) { if (dataI->valid(i)) { char *newsource = dataI->get(i); int newsize = dataD->get(i); totalsize += newsize; bool inserted = false; for (size_t i = 0; i < sorted.size(); i++) { char *existingsource = sorted.get(i); int existingsize; combined.find(existingsource, existingsize); if (newsize <= existingsize) { sorted.insert_at(newsource, i); inserted = true; break; } } if (!inserted) sorted.insert(newsource); } } delete dataI; delete dataD; /* */ /* Open the output file */ /* */ if (sorted.size()) { FILE *output = fopen(_outputFilename, "wt"); /* */ /* Print out our sorted list */ /* */ fprintf(output, "Total recognised memory leaks : %d Kbytes\n", int( totalsize / 1024 )); fprintf(output, "Total unrecognised memory leaks : %d Kbytes\n\n", int( unrecognised / 1024 )); for (int k = (int)sorted.size() - 1; k >= 0 && k < (int)sorted.size(); k--) { char *source = sorted.get(k); CoreAssert(source); int size; combined.find(source, size); int freq; frequency.find(source, freq); if (size > 1048576) { fprintf(output, "%-95s (%d MB in %d leaks)\n", source, int( size / 1048576 ), freq); } else if (size > 2048) { fprintf(output, "%-95s (%d KB in %d leaks)\n", source, int( size / 1024 ), freq); } else { fprintf(output, "%-95s (%d bytes in %d leaks)\n", source, size, freq); } } /* */ /* Clean up */ fclose(output); } }