Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #define __PROGTEST__
- #include <cstdlib>
- #include <cstring>
- #include <ctype.h>
- #include <map>
- #include <vector>
- #include <limits.h>
- using namespace std;
- class CValue
- {
- public:
- CValue(){v = -1;}
- CValue(const CValue &i){v = i.v;}
- CValue(int i){v = i;}
- ~CValue(){}
- CValue& operator = ( const CValue &i ){v = i.v; return *this;}
- bool operator == ( const CValue &i ) const {return v == i.v;}
- bool operator != ( const CValue &i ) const{return v != i.v;}
- bool operator < ( const CValue &i ) const{return v < i.v;}
- bool operator > ( const CValue &i ) const{return v > i.v;}
- bool operator >= ( const CValue &i ) const{return v >= i.v;}
- bool operator <= ( const CValue &i ) const{return v <= i.v;}
- int v;
- };
- class CKey
- {
- public:
- CKey() {k = -1;}
- CKey(const CKey &i){k = i.k;}
- CKey(int i){k = i;}
- ~CKey(){}
- CKey& operator = ( const CKey &i ){k = i.k; return *this;}
- bool operator == ( const CKey &i ) const{return k == i.k;}
- bool operator != ( const CKey &i ) const{return k != i.k;}
- bool operator < ( const CKey &i ) const{return k < i.k;}
- bool operator > ( const CKey &i ) const{return k > i.k;}
- bool operator >= ( const CKey &i ) const{return k >= i.k;}
- bool operator <= ( const CKey &i ) const{return k <= i.k;}
- int k;
- };
- struct Exc{char name[50];};
- struct InvalidIndexException : Exc
- {InvalidIndexException()
- {strcpy(name,"InvalidIndexException");} };
- struct InvalidTreeException : Exc{
- InvalidTreeException()
- {strcpy(name,"InvalidTreeException");} };
- //>INCLUDE YOUR B-TREE FILE HERE<
- #include "main.cpp"
- //>INCLUDE YOUR B-TREE FILE HERE<
- //these probably compile error (cout for sure, dont use them anyway)
- #include <iostream>
- #include <string>
- #include <sstream>
- //global access-----------------------------------------------------------------
- bool g_Verbose;
- map<int, int> g_Ref;
- vector<CKey> g_Used;
- ostream& out = cout;
- CNode * g_My;
- map<int,int>::iterator
- g_Correct;
- int g_Level;
- int g_LeafLvl;
- int g_Count;
- int g_Last;
- //------------------------------------------------------------------------------
- void printNode(CNode* n, string path){
- out << path << ": ";
- int gamma = n->getGamma();
- if ( gamma == 0 ) {out << "EMPTY" << endl; return;}
- for(int i = 1; i <= gamma; i++)try{
- out << '|'
- << n->getKey(i).k ; //key only
- // << n->getKey(i).k << '-' n->getValue(i).v; //key-value
- }catch(Exc& e){
- out << '|' <<e.name;
- }
- out << "|" << "g(" << gamma <<")" << (n->isLeaf() ? ",L":"") << endl;
- if (!n->isLeaf())
- for (int i = 1; i <= gamma + 1; i++)
- {
- stringstream str; str.str("");
- str << path << "," << i;
- try{
- printNode(n->getChild(i), str.str());
- }catch(Exc& e){
- out << str.str() << ": " << e.name << endl;
- }
- }
- }
- void print(CBTree& t){
- try{
- CNode * r = t.getRoot();
- out << "Tree:" << endl;
- printNode(r, "R");
- }catch(Exc& e){
- out << "Can't print tree: root's missing" << endl;
- }
- }
- void generateKVPair( CKey& key, CValue& value, int keyCeiling = RAND_MAX )
- {
- // key e N+
- key = rand( ) % keyCeiling +1;
- value = rand( );
- }
- bool checkSearch(CBTree& subject, CKey& key)
- {
- if (g_Verbose) out << "search()" << endl;
- int myIdx;
- bool myRet;
- try{
- myRet = (myIdx = subject.bTreeSearch(key, &g_My)) != -1;
- }catch(Exc& e){
- out << e.name <<" caught called after calling: search() for key: " << key.k << endl;
- return false;
- }
- bool correct = (g_Correct = g_Ref.find(key.k)) != g_Ref.end();
- if ( myRet != correct ){
- out << "Bad result for: search() (isPresent?)" << "key: " << key.k << " response value: " << myRet << ", correct value: " << correct << endl;
- return false;
- }
- if( !myRet )
- return true;
- int k, v;
- try{
- k = g_My->getKey(myIdx).k;
- v = g_My->getValue(myIdx).v;
- }catch(Exc& e){
- out << "Bad result for: search() (couldn't extract key/value - invalid index)" << "key: " << key.k << " idx: " << myIdx << endl;
- return false;
- }
- if (k != key.k){
- out << "Bad result for: search() (keys don't match)" << "key: " << key.k << " search returned: " << k << endl;
- return false;
- }
- if (v != g_Correct->second){
- out << "Bad result for: search() (values don't match)" << "key: " << key.k << " value retured: " << v << ", correct value: " << g_Correct->second << endl;
- return false;
- }
- return true;
- }
- bool checkRemove(CBTree& subject, CKey& key)
- {
- CNode * dummy;
- bool correct = g_Ref.erase(key.k) > 0;
- if (g_Verbose) out << "remove() (" << (correct ? "present" : "missing") << " key)" << endl;
- bool my;
- try{
- my = subject.bTreeDelete(key);
- }catch(Exc& e){
- out << e.name <<" caught called after calling: remove() for key: " << key.k << (correct ? " (existing" : " (new") << " key)" << endl;
- return false;
- }
- if(correct != my){
- out << "Bad result for: remove()" << "key: " << key.k << " response(wasDeleted?): " << my << ", correct: " << correct << endl;
- return false;
- }
- return true;
- }
- bool checkInsert(CBTree& subject, CKey& key, CValue& val){
- bool pres_correct = (g_Correct = g_Ref.find(key.k)) != g_Ref.end();
- bool pres_my;
- if( g_Verbose ){
- out << "insert() (" << (pres_correct ? "existing" : "new") << " key)" << endl;
- }
- try{
- pres_my = !subject.bTreeInsert(key, val);
- }catch(Exc& e){
- out << e.name <<" caught called after calling: insert() for key: " << key.k << (pres_correct ? " (existing" : " (new") << " key)" << endl;
- return false;
- }
- g_Ref.operator [](key.k) = val.v;
- if (pres_correct != pres_my){
- out << "Bad result for: insert()" << "key: " << key.k << (pres_correct ? " (existing)" : " (new)") << " val: " << val.v
- << " response: " << pres_my << ", correct value: " << pres_correct << endl;
- return false;
- }
- return true;
- }
- bool InsertCoherencyTest(CBTree& subject, int cycles, int valRange, string comment)
- {
- out << "Insert coherency test (" << comment << "):" << endl;
- CKey k; CValue v;
- for (int i = 0; i < cycles ; i++)
- {
- generateKVPair(k,v, valRange);
- g_Used.push_back(k);
- if (g_Verbose) out << "key: " << k.k << " value: " << v.v << " actions: " << endl;
- if (!checkInsert(subject, k, v) ||
- !checkSearch(subject, k) ) {
- out << "FAILED" << endl;
- return false;}
- }
- return true;
- }
- bool RemoveCoherencyTest(CBTree& subject, int stepSize, string comment)
- {
- out << "Remove coherency test (" << comment << "):" << endl;
- for (vector<CKey>::iterator it = g_Used.begin(); it < g_Used.end() ; it+=stepSize)
- {
- // out << "---------------------------------------------------------" << endl;
- CKey k= it->k;
- if (g_Verbose) out << "key: " << k.k << " actions: " << endl;
- if (!checkRemove(subject, k) ||
- !checkSearch(subject, k) ) {
- out << "FAILED" << endl;
- return false;}
- }
- return true;
- }
- bool CoherencyTestR2S(CBTree& subject)
- {
- CKey k;
- for(g_Correct = g_Ref.begin(); g_Correct != g_Ref.end(); g_Correct++)
- {
- k = g_Correct->first;
- if (g_Verbose) out << "key: " << k.k << " actions: " << endl;
- if (!checkSearch(subject, k)) {
- out << "FAILED" << endl;
- return false;}
- }
- return true;
- }
- //inOrder traverse
- bool CHTS2RSub(CNode * node, string rep){
- stringstream s;
- if (!node->isLeaf())
- try{
- s << rep << ",1";
- if ( !CHTS2RSub(node->getChild(1), s.str()) ) return false;
- } catch(Exc& e){
- out << e.name <<" caught called after calling: getChild(1) on node " << s.str() << endl;
- return false;
- }
- for( int g = 1; g <= node->getGamma(); g++)
- {
- CKey k; CValue v;
- try{
- k = node->getKey(g);
- v = node->getValue(g);
- if ( k.k <= g_Last ){
- out << "Bad invariant: keys aren't in ascending order for key: " << k.k << " previous value " << g_Last << endl;
- return false;
- }
- g_Last = k.k;
- if ( (g_Correct = g_Ref.find(k.k)) == g_Ref.end() ){
- out << "Key: " << k.k << " shouldn't be in the tree" << endl;
- return false;
- }
- if (v.v != g_Correct->second)
- {
- out << "Values don't match for Key: " << k.k << " response:" << v.v <<", correct: " << g_Correct->second << endl;
- return false;
- }
- ++g_Count;
- }catch(Exc& e){
- out << e.name <<" caught called after calling: getKey/Val("<< g << ") on node " << rep << endl;
- return false;
- }
- if (!node->isLeaf())
- try{
- s.str(rep); s << "," << g + 1;
- if ( !CHTS2RSub(node->getChild(g + 1), s.str()) ) return false;
- }catch(Exc& e){
- out << e.name <<" caught called after calling: getChild(" << g+ 1 << ") on node " << s.str() << endl;
- return false;
- }
- }
- return true;
- }
- bool CoherencyTestS2R(CBTree& subject){
- g_Count = 0;
- g_Level = g_LeafLvl = -1;
- g_Last = INT_MIN;
- try{
- if (subject.getRoot() != NULL)
- if ( !CHTS2RSub(subject.getRoot(), "R") ) return false;
- }catch(Exc& e){
- out << e.name <<" caught called after calling: getRoot()" << endl;
- out << "FAILED" << endl;
- return false;
- }
- if ( g_Count != g_Ref.size() ){
- out << "Invalid number of elements in tree: " << g_Count << " correct: " << g_Ref.size() << endl;
- out << "FAILED" << endl;
- return false;
- }
- return true;
- }
- bool TestSuite(CBTree& subject, int multiplier){
- g_Ref.clear(); g_Used.clear();
- subject.bTreeCreate();
- if ( ! InsertCoherencyTest(subject, 20 * multiplier, 40 * multiplier, "KEYS : Wide Range") ||
- ! CoherencyTestR2S(subject) ||
- ! CoherencyTestS2R(subject)) return false;
- print(subject);
- //------------------------------------------------------------------------------------
- g_Ref.clear(); g_Used.clear();
- subject.bTreeCreate();
- if ( ! InsertCoherencyTest(subject, 20 * multiplier, 10 * multiplier, "KEYS : Narrow Range") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- //-----------------------------------------------------------------------------------
- g_Ref.clear(); g_Used.clear();
- subject.bTreeCreate();
- if ( ! InsertCoherencyTest(subject, 20 * multiplier, 20 * multiplier, "General") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- //------------------------------------------------------------------------------
- if ( ! RemoveCoherencyTest(subject, 8, "Present Keys") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- //------------------------------------------------------------------------------
- if ( ! RemoveCoherencyTest(subject, 8, "Missing Keys") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- //------------------------------------------------------------------------------
- if ( ! RemoveCoherencyTest(subject, 6, "Present&Missing Keys") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- if ( ! InsertCoherencyTest(subject, 10 * multiplier, 10 * multiplier, "Inserting more values") ) return false;
- if ( ! RemoveCoherencyTest(subject, 1, "Present&Missing Keys, More Keys") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- g_Used.clear();
- if ( ! RemoveCoherencyTest(subject, 1, "Present&Missing Keys, More Keys") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- if ( ! InsertCoherencyTest(subject, 30 * multiplier, 30 * multiplier, "Inserting more values") ) return false;
- if ( ! RemoveCoherencyTest(subject, 3, "Final") ||
- ! CoherencyTestR2S(subject)||
- ! CoherencyTestS2R(subject)) return false;
- //------------------------------------------------------------------------------
- subject.bTreeCreate();g_Ref.clear();
- out << "Root Delete Test:" << endl;
- CValue v(5); CKey k(1);
- if ( !checkInsert(subject, k, v) || !checkRemove(subject,k) ) return false;
- out << "DONE" << endl;
- return true;
- }
- int main( int argc, char** argv )
- {
- srand( time( 0 ) );
- //suppress additional info
- g_Verbose // = true;
- = false;
- //print(tree) - prints tree structure to out
- out << ">------------------<" << endl;
- out << "SIMPLE TEST ODD (5):" << endl;
- out << ">------------------<" << endl;
- CBTree o(5);
- if (!TestSuite(o, 5) ) { print(o); return 1;};
- out << ">------------------<" << endl;
- out << "SIMPLE TEST EVEN (4):" << endl;
- out << ">------------------<" << endl;
- CBTree e(4);
- if (!TestSuite(e, 5) ) { print(e); return 1;};
- out << ">--------------------<" << endl;
- out << "COMPLEX TEST EVEN/ODD:" << endl;
- out << ">--------------------<" << endl;
- for(int size = 3; size < 13; size+= 1){
- CBTree r(size);
- for(int i = 0; i < 3; i++) {
- out << "M: " << size << " Test Complexity: " << 20+i*15 << endl;
- if ( !TestSuite(r,20+i*15)) {print(r);return 1;}
- }
- }
- return 0;
- }
- #endif
Add Comment
Please, Sign In to add comment