/* File: tests.doc (in the Documents group in the project -- not compiled) Author: R. P. Futrelle Date: 8/2/98 Notice: Copyright 1998 by R. P. Futrelle and the College of Computer Science, Northeastern University Class: COM1204, Object-Oriented Design, Summer 1998. Proj: PS2.µ Metrowerks Code Warrior Mac. Done in IDE version 2.0.1. Purpose: Collects together various tests of software as it was developed. Modification history: 8/2/98: RPF. Started. */ /* Test of timer, 8/2/98: cout << "Test some timers\n"; sys = new System(10); Timer *t1 = new Timer; // note, bools print 0 or 1 t1->start(5); cout << "Rang? " << t1->rang() << endl; // time 0, set for 5 sys->time = 20; cout << "Later, rang now? " << t1->rang() << endl; // time 20, should ring Output (3 lines): Test some timers Rang? 0 Later, rang now? 1 Conclusion: Seems to work fine ******************************************** Here's a little test file and results for expts. with vectors, in particular, pointers to vectors of pointers. It works fine for integers. Should therefore work for pointers to anything. (In C and C++, once memory is allocated, it is never moved, so a pointer value never changes and can be used reliably for the equality/inequality tests needed to support containers, vectors in particular. I think they're implemented as binary trees, internally. It would be more awkward if class objects were stored for which no natural comparison operators existed. Then we'd have to create some, possibly forcing a change in the class definition just so we could put some in containers -- awkward to say the very least. /* Hello World for the CodeWarrior Adapted by RPF, 8/3/98 for quick test of vectors, and their required inequality and less than relations In particular, if I want a vector of pointers, are the operators defined for them -- hey they're just integers, why not? Upshot is that inequality and equality have no problem comparing pointers. Now try a simple vector, then one with pointers. It works too, when the proper dereferencing is done. */ #include void main(void) { int i = 3; int *ip = &i; int *iip = &i; int j = 5; int *jp = &j; cout << "ip is: " << ip << endl; cout << "jp is: " << jp << endl; if(ip < jp) cout << "ip < jp" << endl; if(ip > jp) cout << "ip > jp" << endl; if(ip == iip) cout << "ip == iip" << endl; vector > iv; iv.push_back(33); cout << "zeroeth el is: " << iv[0] << endl; // now the daring part, with pointers. // first, a var that is a vector of pointers to ints. vector > ivp; // assign to it ivp.push_back(ip); cout << "pointer is: " << ivp[0] << " val is: " << *ivp[0] << endl; // so far, it all works, now a pointer to a vector of stuff // have to *create* it too! vector > *ivpp = new vector >; //ivpp->push_back(jp); (*ivpp)[0] = ip; (*ivpp)[1] = jp; // 0 worked, how about 1? // note that we dereference the pointer to the vector before indexing into it. cout << "pointer [0] is: " << (*ivpp)[0] << " val is: " << *(*ivpp)[0] << endl; cout << "pointer [1] is: " << (*ivpp)[1] << " val is: " << *(*ivpp)[1] << endl; } /* Output from the above is fine, so must translate it carefully to my pointers to class objects. ip is: 0x036bea64 jp is: 0x036bea58 ip > jp ip == iip zeroeth el is: 33 pointer is: 0x036bea64 val is: 3 pointer [0] is: 0x036bea64 val is: 3 pointer [1] is: 0x036bea58 val is: 5 */ // Failed to get vectors working reliably, strange things happening. // So, cut and run -- go to ordinary arrays -- that'll do! // Below illustrates the problems: void System:: sys_init() { // first, create the vector objects needed // note that superclass is used for switches and phones switches = new vector >; phones = new vector >; links = new vector >; // create phones, switch, links and wire them up (*switches)[0] = new Switch; a_switch = (*switches)[0]; //DEBUG cout << "switch id " << a_switch->id << endl; cout << "switch next_switch_id " << a_switch->next_switch_id << endl; // use phone and link id's as indexes into vectors for(int i = 0; i < num_phones; i++) {a_phone = new Phone(2*i + 1); // exchange is 1 and a phone number (*phones)[a_phone->id] = a_phone; //DEBUG cout << "phone id " << a_phone->id << endl; cout << "phone next_phone_id " << a_phone->next_phone_id << endl; cout << "phone id via phones vec[a_phone->id]: " << (*phones)[i]->id << endl; a_link = new Link; (*links)[a_link->id] = a_link; //DEBUG cout << "link id " << a_link->id << endl; cout << "link next_link_id " << a_link->next_link_id << endl; cout << "link id via links vec[a_link->id]: " << (*links)[a_link->id]->id << endl; cout << "For i: " << i << endl; cout << "Addresses are, " << " a_phone " << a_phone << " a_link " << a_link << " a_switch " << a_switch << endl << endl; /* // now wire them up a_link->source_a = (Node*)(a_switch); a_link->source_b = (Node*)(a_phone); a_phone->my_link = a_link; (*(a_switch->links))[i] = a_link; */ // now, addresses are corrupted -- not before wiring, but after wiring cout << "After wiring, phones vec[i] id: " << (*phones)[i]->id << endl; } // end of loop over phones cout << "after loop is done\n\n"; // during debug noticed that the following contains the address of a link! cout << "phone ptr of phones vec[0]: " << (*phones)[0] << endl; cout << "phone id via phones vec[0]: " << (*phones)[0]->id << endl; cout << "phone id via phones vec[1]: " << (*phones)[1]->id << endl; cout << "phone id via a_phone: " << a_phone->id << endl; } // The above code led to the following bizzare output in which a phone vector had // entry that was a pointer to a link object! /* switch id 0 switch next_switch_id 1 phone id 0 phone next_phone_id 1 phone id via phones vec[a_phone->id]: 0 link id 0 link next_link_id 1 link id via links vec[a_link->id]: 0 For i: 0 Addresses are, a_phone 0x0364e3bc a_link 0x0364e3a4 a_switch 0x0364e400 After wiring, phones vec[i] id: 0 phone id 1 phone next_phone_id 2 phone id via phones vec[a_phone->id]: 1 link id 1 link next_link_id 2 link id via links vec[a_link->id]: 1 For i: 1 Addresses are, a_phone 0x0364e388 a_link 0x0364e370 a_switch 0x0364e400 After wiring, phones vec[i] id: 262144 phone id 2 phone next_phone_id 3 phone id via phones vec[a_phone->id]: 2 link id 2 link next_link_id 3 link id via links vec[a_link->id]: 2 For i: 2 Addresses are, a_phone 0x0364e354 a_link 0x0364e33c a_switch 0x0364e400 After wiring, phones vec[i] id: 56943500 after loop is done phone ptr of phones vec[0]: 0x0364e3a4 <<=== POINTER TO A LINK, NOT A PHONE phone id via phones vec[0]: 0 phone id via phones vec[1]: 262144 phone id via a_phone: 2 Done with init running phone i: 0 with id: 0 Phone: 0 ran running phone i: 1 with id: 262144 Phone: 262144 ran running phone i: 2 with id: 56943500 Phone: 56943500 ran Switch: 0 ran running phone i: 0 with id: 0 Phone: 0 ran running phone i: 1 with id: 262144 Phone: 262144 ran running phone i: 2 with id: 56943500 Phone: 56943500 ran Switch: 0 ran running phone i: 0 with id: 0 Phone: 0 ran running phone i: 1 with id: 262144 Phone: 262144 ran running phone i: 2 with id: 56943500 Phone: 56943500 ran Switch: 0 ran running phone i: 0 with id: 0 Phone: 0 ran running phone i: 1 with id: 262144 Phone: 262144 ran running phone i: 2 with id: 56943500 Phone: 56943500 ran Switch: 0 ran */ // Now that I'm using simple arrays, things are working, e.g., void Phone:: do_states() { // Succesively check states and respond to messages // State: Quiescent = FFFFF = false, false, false, false, false if(is_state(false, false, false, false, false)) { if(!pickup_timer->is_on()) pickup_timer->start(5,12); if(pickup_timer->rang()) { pickup_timer->shut_off(); pickup(); set_state(false, false, true, true, false); // picked up cout << "picked up phone: " << id << " at " << sys->time << endl; return; } } // State: Picked Up = FFTTF = false, false, true, true, false if(is_state(false, false, true, true, false)) { if(!hangup_timer->is_on()) hangup_timer->start(3,6); if(hangup_timer->rang()) { hangup_timer->shut_off(); hangup(); set_state(false, false, false, false, false); // hung up cout << "hung up phone: " << id << " at " << sys->time << endl; return; } } // Here's a sample of what the above produces: /* picked up phone: 1 at 6 picked up phone: 2 at 7 picked up phone: 0 at 9 hung up phone: 1 at 12 hung up phone: 0 at 14 hung up phone: 2 at 14 picked up phone: 0 at 22 picked up phone: 1 at 23 picked up phone: 2 at 23 hung up phone: 0 at 26 hung up phone: 1 at 27 hung up phone: 2 at 28 picked up phone: 0 at 34 picked up phone: 1 at 36 hung up phone: 0 at 40 */