Advertisement
Guest User

Untitled

a guest
Feb 10th, 2010
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.53 KB | None | 0 0
  1. #include "salist.h"  // this is the file that i have written that is supposed to be able to run though this successfully
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstring>
  6. using namespace std;
  7. const int NUMELEM=100;
  8. struct Book{
  9.   int id_;
  10.   char author_[31];
  11.   char title_[41];
  12.   Book(){
  13.     id_=100;
  14.     strcpy(author_,"someone");
  15.     strcpy(title_,"ABC");
  16.   }
  17. };
  18. ostream& operator<<(ostream& os,const Book& b);
  19. template <class T>
  20. void adjust(int idx,T arr[]);
  21. template <class T>
  22. void remove(int idx,T arr[],int sz);
  23. bool checkInts(const int correctlist[],const int checklist[],int sz);
  24. bool checkBooks(const Book correctlist[],const Book checklist[],int sz);
  25. bool sameBook(const Book& b1,const Book& b2);
  26. bool checkID(const Book& idptr,const Book& B);
  27. bool checkTitle(const Book& ptr,const Book& B);
  28. bool checkInt(const int& numptr,const int& val);
  29. int main(void){
  30.   int numbooks;
  31.   Book* bookrecs;
  32.   Book* bookcheck;
  33.   Book* bookorig;
  34.   Node<Book>* booknode;
  35.   Node<int>* intnode;
  36.   int intrecs[NUMELEM],intcheck[NUMELEM],intorig[NUMELEM];
  37.   FILE* fs=fopen("a1q2data.txt","r");
  38.   bool passtest=true;
  39.   int pick;
  40.   Book Nomatch;
  41.   int ipassback;
  42.   if(fs){
  43.     SAList<int> ilist;
  44.     SAList<Book> blist;
  45.  
  46.     fscanf(fs,"%d\n",&numbooks);
  47.     bookrecs=new Book[numbooks];
  48.     bookcheck=new Book[numbooks];
  49.     bookorig=new Book[numbooks];
  50.     for(int i=0;i<numbooks;i++){
  51.       fscanf(fs,"%d;%[^;];%[^\n]\n",&(bookrecs[i].id_),
  52.       bookrecs[i].author_,bookrecs[i].title_);
  53.     }
  54.     for(int i=0;i<numbooks;i++){
  55.       blist.insert(bookrecs[numbooks-1-i]);
  56.       bookorig[i]=bookrecs[i];
  57.     }
  58.     for(int i=0;i<NUMELEM;i++){
  59.       ilist.insert(i);
  60.       intrecs[NUMELEM-1-i]=i;
  61.     }
  62.     for(int i=0;i<NUMELEM;i++){
  63.       intorig[i]=intrecs[i];
  64.     }
  65.     if(blist.get(bookcheck,numbooks) && !checkBooks(bookcheck,bookrecs,numbooks)){
  66.       passtest=false;
  67.       cout << "Error 1: Bug in either constructor, insert() or get()"<< endl;
  68.     }
  69.     if (passtest){
  70.       ilist.get(intcheck,NUMELEM);
  71.       if(!checkInts(intcheck,intrecs,NUMELEM)){
  72.         passtest=false;
  73.         cout << "Error 2: Bug in either constructor, insert() or get()" << endl;
  74.       }
  75.     }
  76.     SAList<Book> bcopy=blist;
  77.     SAList<int> icopy=ilist;
  78.     if(passtest){
  79.       if(bcopy.get(bookcheck,numbooks) && !checkBooks(bookcheck,bookrecs,numbooks)){
  80.         passtest=false;
  81.         cout << "Error 3: Error in your copy constructor"<< endl;
  82.       }
  83.       if (passtest){
  84.         icopy.get(intcheck,NUMELEM);
  85.         if(!checkInts(intcheck,intrecs,NUMELEM)){
  86.           passtest=false;
  87.           cout << "Error 4: Error in your copy constructor"<< endl;
  88.         }
  89.       }
  90.     }
  91.     if(passtest){
  92.       if(blist.search(Nomatch,checkTitle)){
  93.         passtest=false;
  94.         cout << "Error 5: Failed search() test." << endl;
  95.         cout << "search() function return a non-NULL value"<< endl;
  96.         cout << "It should have returned a NULL value" << endl;
  97.       }
  98.       if(passtest){
  99.         blist.get(bookcheck,numbooks);
  100.         if(!checkBooks(bookcheck,bookrecs,numbooks)){
  101.           passtest=false;
  102.           cout << "Error 6: List was modified even when search() failed"<< endl;
  103.         }
  104.       }
  105.     }
  106.     if(passtest){
  107.       if(blist.search(Nomatch,checkID)){
  108.         passtest=false;
  109.         cout << "Error 7: Failed search() test." << endl;
  110.         cout << "search() function return a non-NULL value"<< endl;
  111.         cout << "It should have returned a NULL value" << endl;
  112.       }
  113.       if(passtest){
  114.         blist.get(bookcheck,numbooks);
  115.         if(!checkBooks(bookcheck,bookrecs,numbooks)){
  116.           passtest=false;
  117.           cout << "Error 8: Test failed. List was modified even when search failed"<< endl;
  118.         }
  119.       }
  120.     }
  121.     if(passtest){
  122.       for(int i=0;i<200 && passtest;i++){
  123.         if(i==0)
  124.           pick=0;
  125.         else
  126.           pick=rand()%numbooks;
  127.         booknode=blist.search(bookrecs[pick],checkTitle);
  128.         if(!booknode){
  129.           passtest=false;
  130.           cout << "Error 9: Failed search() test." << endl;
  131.           cout << "search() function returned a NULL" << endl;
  132.           cout << "It should have returned the address of the Node found" << endl;
  133.         }
  134.         if(passtest && !sameBook(booknode->data(),bookrecs[pick])){
  135.           passtest=false;
  136.           cout << "Error 10: Failed search() test." << endl;
  137.           cout << "search() function return value was correct but the node returned" << endl;
  138.           cout << "was not the correct node" << endl;
  139.         }
  140.         if(passtest){
  141.           blist.get(bookcheck,numbooks);
  142.           adjust(pick,bookrecs);
  143.          blist.get(bookcheck,numbooks);
  144.           if(!checkBooks(bookcheck,bookrecs,numbooks)){
  145.             passtest=false;
  146.             cout << "Error 11: Test failed List was not properly adjusted after search"<< endl;
  147.           }
  148.         }
  149.       }//for
  150.       for(int i=0;i<200 && passtest;i++){
  151.         if(i==100)
  152.           pick=0;
  153.         else
  154.           pick=rand()%numbooks;
  155.         booknode=blist.search(bookrecs[pick],checkID);
  156.         if(!booknode){
  157.           passtest=false;
  158.           cout << "Error 12: Failed search() test." << endl;
  159.           cout << "search() function returned a NULL pointer, when it should have returned" << endl;
  160.           cout << "a node in the list" << endl;
  161.         }
  162.         if(passtest && !sameBook(booknode->data(),bookrecs[pick])){
  163.           passtest=false;
  164.           cout << "Error 13: Failed search() test." << endl;
  165.           cout << "search() function return value was correct but node returned did not have" << endl;
  166.           cout << "the correct book searched for" << endl;
  167.         }
  168.         if(passtest){
  169.           adjust(pick,bookrecs);
  170.           blist.get(bookcheck,numbooks);
  171.           if(!checkBooks(bookcheck,bookrecs,numbooks)){
  172.             passtest=false;
  173.             cout << "Error 14: Test failed List was not properly adjusted after search"<< endl;
  174.           }
  175.         }
  176.       }//for
  177.       for(int i=0;i<500 && passtest;i++){
  178.         if(i==100)
  179.           pick=0;
  180.         else
  181.           pick=rand()%NUMELEM;
  182.         intnode=ilist.search(intrecs[pick],checkInt);
  183.         if(!intnode){
  184.           passtest=false;
  185.           cout << "Error 15: Failed search() test." << endl;
  186.           cout << "search() function returned a NULL pointer, when it should have returned" << endl;
  187.           cout << "a node in the list" << endl;
  188.         }
  189.         if(passtest && intnode->data()!=intrecs[pick]){
  190.           passtest=false;
  191.           cout << "Error 16: Failed search() test." << endl;
  192.           cout << "search() function return value was correct but node returned did not have" << endl;
  193.           cout << "the correct integer searched for" << endl;
  194.         }
  195.         if(passtest){
  196.           adjust(pick,intrecs);
  197.           ilist.get(intcheck,NUMELEM);
  198.           if(!checkInts(intcheck,intrecs,NUMELEM)){
  199.             passtest=false;
  200.             cout << "Error 17: Test failed List was not properly adjusted after search"<< endl;
  201.           }
  202.         }
  203.       }//for
  204.     }
  205.     //test copy constructor
  206.     if(passtest){
  207.       bcopy.get(bookcheck,numbooks);
  208.       if(!checkBooks(bookcheck,bookorig,numbooks)){
  209.         passtest=false;
  210.         cout << "Error 18: Error in your copy constructor"<< endl;
  211.       }
  212.     }
  213.     if (passtest){
  214.       icopy.get(intcheck,NUMELEM);
  215.       if(!checkInts(intcheck,intorig,NUMELEM)){
  216.         passtest=false;
  217.         cout << "Error 19: Error in your copy constructor"<< endl;
  218.       }
  219.     }
  220.     if(passtest){
  221.       bcopy=blist;
  222.       for(int i=0;i<numbooks;i++){
  223.         bookorig[i]=bookrecs[i];
  224.       }
  225.       bcopy.get(bookcheck,numbooks);
  226.       if(!checkBooks(bookcheck,bookrecs,numbooks)){
  227.         passtest=false;
  228.         cout << "Error 20: Error in your assignment operator"<< endl;
  229.       }
  230.     }
  231.     if(passtest){
  232.       icopy=ilist;
  233.       for(int i=0;i<NUMELEM;i++){
  234.         intorig[i]=intrecs[i];
  235.       }
  236.       icopy.get(intcheck,NUMELEM);
  237.       if(!checkInts(intcheck,intrecs,NUMELEM)){
  238.         passtest=false;
  239.         cout << "Error 21: Error in your assignment operator"<< endl;
  240.       }
  241.     }
  242.     //test the remove function
  243.     if(passtest){
  244.       SAList<Book> bempty;  //empty list
  245.       //remove a node that isn't there
  246.       if(blist.remove(Nomatch,checkID)){
  247.         passtest=false;
  248.         cout << "Error 22: remove() function returned true when called with"<< endl;
  249.         cout << "id of book that is not in list" << endl;
  250.       }
  251.       //remove a node from an empty list
  252.       if(passtest && bempty.remove(Nomatch,checkID)){
  253.         passtest=false;
  254.         cout << "Error 23: remove() function returned true when clist is empty"<< endl;
  255.  
  256.       }
  257.       //remove the last node in the list
  258.       if(passtest && !blist.remove(bookrecs[numbooks-1],checkID)){
  259.         passtest=false;
  260.         cout << "Error 24: remove() function returned false when trying to"<< endl;
  261.         cout << "remove last book in the list" << endl;
  262.       }
  263.       if(passtest){
  264.         remove(numbooks-1,bookrecs,numbooks);
  265.         blist.get(bookcheck,numbooks-1);
  266.         if(!checkBooks(bookcheck,bookrecs,numbooks-1)){
  267.           passtest=false;
  268.           cout << "Error 25: remove() function did not properly remove the node"<< endl;
  269.         }
  270.       }
  271.       //remove the first node in the list
  272.       if(passtest && !blist.remove(bookrecs[0],checkID)){
  273.         passtest=false;
  274.         cout << "Error 26: remove() function returned false when trying to"<< endl;
  275.         cout << "remove first book in the list" << endl;
  276.       }
  277.       if(passtest){
  278.         remove(0,bookrecs,numbooks-1);
  279.         blist.get(bookcheck,numbooks-2);
  280.         if(!checkBooks(bookcheck,bookrecs,numbooks-2)){
  281.           passtest=false;
  282.           cout << "Error 27: remove() function did not properly remove the node"<< endl;
  283.         }
  284.       }
  285.       //removes some node in middle of list
  286.       if(passtest && !blist.remove(bookrecs[5],checkID)){
  287.         passtest=false;
  288.         cout << "Error 28: remove() function returned false when trying to"<< endl;
  289.         cout << "remove book in the middle of the list" << endl;
  290.       }
  291.       if(passtest){
  292.         remove(5,bookrecs,numbooks-2);
  293.         blist.get(bookcheck,numbooks-3);
  294.         if(!checkBooks(bookcheck,bookrecs,numbooks-3)){
  295.           passtest=false;
  296.           cout << "Error 29: remove() function did not properly remove the node"<< endl;
  297.         }
  298.       }
  299.     }
  300.     //test for deep copy in = operator
  301.     if(passtest){
  302.       bcopy.get(bookcheck,numbooks);
  303.       if(!checkBooks(bookcheck,bookorig,numbooks)){
  304.         passtest=false;
  305.         cout << "Error 30: Error in your assignment operator"<< endl;
  306.       }
  307.     }
  308.  
  309.     //test the remove function
  310.     if(passtest){
  311.       SAList<int> iempty;
  312.      //remove a node from an empty list
  313.       if(iempty.remove(intrecs[0],checkInt)){
  314.         passtest=false;
  315.         cout << "Error 31: remove() function returned true when clist is empty"<< endl;
  316.  
  317.       }
  318.       //remove the last node in the list
  319.       if(passtest && !ilist.remove(intrecs[NUMELEM-1],checkInt)){
  320.         passtest=false;
  321.         cout << "Error 32: remove() function returned false when trying to"<< endl;
  322.         cout << "remove last node in the list" << endl;
  323.       }
  324.       if(passtest){
  325.         remove(NUMELEM-1,intrecs,NUMELEM);
  326.         ilist.get(intcheck,NUMELEM-1);
  327.         if(!checkInts(intcheck,intrecs,NUMELEM-1)){
  328.           passtest=false;
  329.           cout << "Error 33: remove() function did not properly remove the node"<< endl;
  330.         }
  331.       }
  332.       //remove the first node in the list
  333.       if(passtest && !ilist.remove(intrecs[0],checkInt)){
  334.         passtest=false;
  335.         cout << "Error 34: remove() function returned false when trying to"<< endl;
  336.         cout << "remove first book in the list" << endl;
  337.       }
  338.       if(passtest){
  339.         remove(0,intrecs,NUMELEM-1);
  340.         ilist.get(intcheck,NUMELEM-2);
  341.         if(!checkInts(intcheck,intrecs,NUMELEM-2)){
  342.           passtest=false;
  343.           cout << "Error 35: remove() function did not properly remove the node"<< endl;
  344.         }
  345.       }
  346.       //removes some node in middle of list
  347.       if(passtest && !ilist.remove(intrecs[10],checkInt)){
  348.         passtest=false;
  349.         cout << "Error 36: remove() function returned false when trying to"<< endl;
  350.         cout << "remove an item from middle of the list" << endl;
  351.       }
  352.       if(passtest){
  353.         remove(10,intrecs,NUMELEM-2);
  354.         ilist.get(intcheck,NUMELEM-3);
  355.         if(!checkInts(intcheck,intrecs,NUMELEM-3)){
  356.           passtest=false;
  357.           cout << "Error 37: remove() function did not properly remove the node"<< endl;
  358.         }
  359.       }
  360.     }
  361.     if(passtest){
  362.       bcopy.get(bookcheck,numbooks);
  363.       if(!checkBooks(bookcheck,bookorig,numbooks)){
  364.         passtest=false;
  365.         cout << "Error 38: Error in your assignment operator"<< endl;
  366.       }
  367.     }
  368.  
  369.  
  370.     if(passtest){
  371.       cout << "Testing for Q2 completed.  All tests passed" << endl;
  372.     }
  373.     delete bookrecs;
  374.     delete bookcheck;
  375.   }
  376.   else{
  377.     cout << "Error: You need to have the file a1q2data.txt to run this main" << endl;
  378.   }
  379. }
  380. template <class T>
  381. void adjust(int idx,T arr[]){
  382.   T temp=arr[idx];
  383.   for (int i=idx;i>0 ;i--){
  384.     arr[i]=arr[i-1];
  385.   }/*for*/
  386.   arr[0]=temp;
  387. }
  388. template <class T>
  389. void remove(int idx,T arr[],int sz){
  390.   for(int i=idx;i<sz-1;i++){
  391.     arr[i]=arr[i+1];
  392.   }
  393. }
  394. bool checkInts(const int checklist[],const int correctlist[],int sz){
  395.   bool rc=true;
  396.   for(int i=0;i<sz && rc;i++){
  397.     if(correctlist[i]!=checklist[i])
  398.       rc=false;
  399.   }
  400.   return rc;
  401. }
  402.  
  403. bool checkBooks(const Book correctlist[],const Book checklist[],int sz){
  404.   bool rc=true;
  405.   for(int i=0;i<sz && rc;i++){
  406.     if(!sameBook(correctlist[i],checklist[i]))
  407.       rc=false;
  408.   }
  409.   return rc;
  410. }
  411. bool sameBook(const Book& b1,const Book& b2){
  412.   bool rc=true;
  413.   if((b1.id_!=b2.id_) ||
  414.       strcmp(b1.author_,b2.author_)!=0 ||
  415.       strcmp(b1.title_,b2.title_)!=0 ){
  416.       rc=false;
  417.   }
  418.   return rc;
  419. }
  420.  
  421. bool checkID(const Book& b1,const Book& b2){
  422.   bool rc=false;
  423.   if(b1.id_==b2.id_){
  424.     rc=true;
  425.   }
  426.   return rc;
  427. }
  428. bool checkTitle(const Book& b1,const Book& b2){
  429.   bool rc=false;
  430.   if(strcmp(b1.title_,b2.title_)==0){
  431.     rc=true;
  432.   }
  433.   return rc;
  434. }
  435. bool checkInt(const int& n1,const int& val){
  436.   bool rc=false;
  437.   if(n1==val){
  438.     rc=true;
  439.   }
  440.   return rc;
  441. }
  442. ostream& operator<<(ostream& os,const Book& b){
  443.   os << "ID: " << b.id_ << endl;
  444.   os << "Author: " << b.author_ << endl;
  445.   os << "Title: " << b.title_ << endl;
  446.   return os;
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement