Skip to content

Instantly share code, notes, and snippets.

@markzyu
Created May 19, 2015 01:36
Show Gist options
  • Save markzyu/6eb173c50b0470cfc4d5 to your computer and use it in GitHub Desktop.
Save markzyu/6eb173c50b0470cfc4d5 to your computer and use it in GitHub Desktop.
Test the data structure you wrote in CS 225.
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <cstdlib>
// The following line includes your structure.
// In this example, it is named "Container"
#include "container.h"
using namespace std;
stringstream ss;
template <typename T>
inline T next()
{
T ans;
ss>>ans;
return ans;
}
typedef map<string, Container<int>*> lmap;
lmap getlist;
void sample(Container<int>& a)
{
cout << "Insert tests" << endl;
for(int i=0; i<5; i++){
a.insert_back(i);
}
// [0, 1, 2, 3, 4]
for(int i=0; i<5; i++){
a.insert_front(5+i);
}
a.insert(10, 5);
// [9, 8, 7, 6, 5, 10, 0, 1, 2, 3, 4]
for(int i=0; i<a.size() - 1; i++){
cout << a[i] << ", ";
}
cout << a[a.size()-1] << endl;
cout << "Remove tests" << endl;
a.pop_front();
// [8, 7, 6, 5, 10, 0, 1, 2, 3, 4]
a.pop_back();
// [8, 7, 6, 5, 10, 0, 1, 2, 3]
a.remove(4);
// [8, 7, 6, 5, 0, 1, 2, 3]
for(int i=0; i<a.size() - 1; i++){
cout << a[i] << ", ";
}
cout << a[a.size()-1] << endl;
cout << "Modifying Tests" << endl;
a.update(10, 1);
// [8, 10, 6, 5, 0, 1, 2, 3]
for(int i=0; i<a.size() - 1; i++){
cout << a[i] << ", ";
}
cout << a[a.size()-1] << endl;
cout << "find_min_index tests" << endl;
// Should return 4
cout << a.find_min_index() << endl;
cout << "You should remove(4) now." <<endl;
}
int main()
{
srand(0);
string sNow="";
Container<int>* now=NULL;
while(true)
{
cout<<"DBG-"+sNow+" $ ";
string cmd, op;
ss.str("");
ss.clear();
getline(cin, cmd);
ss<<cmd;ss>>op;
if(op=="use") //select a named list
{
string name=next<string>();
if(getlist.find(name)!=getlist.end())
now=getlist[name];
else getlist[name]=now=new Container<int>;
sNow=name;
}
else if(!now)
{
cout<<"You MUST first 'use' a named list."<<endl;
continue;
}
if(op=="if")
now->insert_front(next<int>());
if(op=="ib")
now->insert_back(next<int>());
if(op=="i")
{
now->insert(next<int>(), next<int>());
}
if(op=="r")
{
now->remove(next<int>());
}
if(op=="up")
{
now->update(next<int>(), next<int>());
}
if(op=="pb")
{
now->pop_back();
}
if(op=="gs")
{
cout<<now->find_min_index()<<endl;
}
if(op=="rgs")
{
cout<<now->find_min_index()<<endl;
now->remove(now->find_min_index());
}
if(op=="ex")
{
sample(*now);
}
if(op=="test")
{
static int start = 0;
int len = next<int>();
vector<int> tmp;
for(int i=0; i<len; i++)
tmp.push_back(start++);
while(!tmp.empty())
{
int i=rand()%tmp.size();
now->insert(tmp[i], rand()%(now->size()+1));
tmp.erase(tmp.begin()+i);
}
}
if(op=="rand")
{
for(int i=0; i<10; i++)
now->insert(rand()%50, now->size()==0? 0 :rand()%now->size());
}
if(op=="pf")
{
now->pop_front();
}
if(op=="get")
{
int index = next<int>();
cout<<'['<<index<<"] = "<<(*now)[index]<<endl;
}
if(op=="p")
{
cout<<"Size: "<<now->size()<<endl<<"[";
if(now->size())
{
for(int i=0; i<now->size()-1; i++)
cout<<(*now)[i]<<',';
cout<<(*now)[now->size()-1];
}
cout<<"]"<<endl;
}
if(op=="q")
{
typedef lmap::iterator _lit_t;
for(_lit_t it=getlist.begin(); it!=getlist.end(); it++)
delete it->second;
break;
}
/*
if(op=="leak") //create a memleak on purpose
{
int* a=new int;
}
if(op=="ivpt") //access invalid pointer on purpose
{
int *b=new int;
delete b;
*b=100;
}*/
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment