473,766 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with testing method with STL

Hello!
I have two classes called Handle which is a template class and a class
Integer which is not a template class. The Integer class is just a wrapper
class for a
primitive int with some methods. I don't show the Integer class because it
will not add any information to my problem. Main is using some STL function.
In main you put in nodes in the STL list and you can display the STL list
and other things such as erase some value from the list and find a selected
value from the list and assign a list to another list and some more.
I show the complete Handle class and the whole file with main below.

In the file where main is located is there a function that is called
copyList. The definition looks like.
void copyList(list<h andle_t>* myList1, list<handle_t>* myList2)
{ *myList2 = *myList1; }
this copies the whole body of the list so they will point to the same
wrapper object(Integer class object).

if I add 1,2,3 to myList1 and then call copyList the same number will be
written for myList1 and myList2
if I then remove 2 from myList1 then myList1 will contain 1 and 3 myList2
will still contain 1,2 and 3.
if I add 4 to myList1 then myList1 will contain 1,3 and 4 myList2 will still
contain 1,2 and 3. All these are correct.

So in this example Integer object with number 1 and 3 is being shared
between myList1 and myList2..

Now to my question and problems !
I want to check that deep copy is working.
I have one method in the Handle class which have this definition
T* operator->()
{
if (*ref_count > 1)
{
(*ref_count)--;
body = new T(*body);
ref_count = new int(1);
}
return body;
}
this means that a new Integer object will be created if there are more then
1 reference to the the current Integer object.(ref_cou nt > 1)
How do I do for testing this operator-> to see that a new Integer object is
being created.

I don't know how beacause when all these object are in the STL list you
can't access them

Many thanks

//Tony

*************C O D E S E C T I O N**********
I show the definition of the Handle class here.
#include "integer.h"
#include <iostream>
using namespace std;

template<class T>
class Handle
{
public:
Handle()
{
body = new T(0);
ref_count = new int(1);
}

Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}

~Handle() //Destructor
{
(*ref_count)--;
if (!*ref_count)
deleteAll();
}

T operator*() const
{ return *body; }

T& operator*()
{ return *body; }

T* operator->()
{
if (*ref_count > 1)
{
(*ref_count)--;
body = new T(*body);
ref_count = new int(1);
}
return body;
}

T* operator->() const
{ return body; }

bool operator==(cons t T& h)
{ return *body == h; }

bool operator==(cons t Handle& h)
{ return *body == *h.body; }

Handle(const Handle& h) //Copy construktor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}

int getRefCount()
{ return *ref_count; }

void operator()(Hand le& h) //Funktionsanrops operator
{ cout << *h.body << endl; }

const Handle& operator=(const Handle& h) //Tilldelningsope rator
{
if (this != &h)
{
(*ref_count)--;
if (!*ref_count)
deleteAll();

ref_count = h.ref_count;
body = h.body;
(*h.ref_count)+ +;
}
return *this;
}

private:
T* body;
int* ref_count;
void deleteAll()
{
delete body;
body = NULL;
delete ref_count;
ref_count = NULL;
}
};

//Start of file with main
*************** **
#include "handle.h"
#include <list>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
typedef Handle<Integer> handle_t;

static int getValue(string text)
{
int value;
cout << endl << text << " : ";
while (!(cin >> value) || cin.peek() != '\n')
{
cin.clear();
for (; cin.peek() != EOF && cin.get() != '\n' ;)
;
cout << "Du har inte angivit nagon siffra, forsok igen" <<
endl;
cout << "Ange alternativ: ";
}
fflush(stdin);
return value;
}

//*************** *********
int huvudMeny()
{
cout << "VALKOMMEN TILL STL HANTERING" << endl << endl;
cout << "0. => EXIT" << endl << endl;;

cout << " 1. Lagg in noder i STL_listan" << endl;
cout << " 2. Skriv ut STL_listan" << endl;
cout << " 3. Ta bort ett specificerat varde i STL listan" << endl;
cout << " 4. Ta bort samtliga noder i STL listan" << endl;
cout << " 5. Hitta ett specificerat varde i STL listan" << endl;
cout << " 6. Testa da body objektet ar delat" << endl;
cout << " 7. Testa att kopiera hela STL listan" << endl;
return getValue("Ange alternativ: ");
}
//*************** *************** *
void dummy()
{
string dummy;
cout << "Tryck pa valfri tangent for att fortsatta" << endl;
cin >> dummy;
}
//*************** *********
void displaySTL(list <handle_t>* myList1, list<handle_t>* myList2)
{
if (myList1->size())
{
cout << endl << "Lista 1 har foljande innehall" << endl << endl;
for_each(myList 1->begin(), myList1->end(), handle_t());
cout << endl;
}
else
cout << endl << "Lista 1 ar tom" << endl << endl;

if (myList2->size())
{
cout << endl << "Lista 2 har foljande innehall" << endl << endl;
for_each(myList 2->begin(), myList2->end(), handle_t());
cout << endl;
}
else
cout << endl << "Lista 2 ar tom" << endl << endl;

dummy();
}

//*************** *********
void findSelectedVal ue(list<handle_ t>* myList)
{
int value = getValue("Ange vardet som du vill hitta i listan: ");

if (find(myList->begin(), myList->end(), Integer(value)) !=
myList->end())
cout << endl << "Vardet " << value << " fanns i STL-listan" <<
endl << endl;
else
cout << endl << "Vardet " << value << " finns inte i listan" <<
endl << endl;

dummy();
}

//*************** *********
bool existValue(list <handle_t>* myList, int value)
{
if (find(myList->begin(), myList->end(), Integer(value)) !=
myList->end())
return true;
else
return false;
}

//*************** *********
void deleteAll(list< handle_t>* myList)
{
myList->clear();
cout << endl << "Alla noder ar nu borttagna" << endl << endl;
dummy();
}

//*************** *********
void deleteSelectedV alue(list<handl e_t>* myList)
{
int value = getValue("Ange vardet som du vill ta bort fran listan: ");
handle_t temp( new Integer(value)) ;
if (existValue(myL ist, value))
{
myList->erase(remove(m yList->begin(), myList->end(), temp),
myList->end());
cout << endl;
for_each(myList->begin(), myList->end(), handle_t());
cout << endl;
}
else
cout << endl << "Vardet fanns inte i listan!!" << endl << endl;

dummy();
}

//*************** *********
void addNodeToSTL(li st<handle_t>* myList)
{
handle_t myh(new Integer(getValu e("Ange vardet som du vill lagga in i
listan/listorna: ")));
myList->push_front(myh );
}

//*************** *********
void copyList(list<h andle_t>* myList1, list<handle_t>* myList2)
{ *myList2 = *myList1; }

//*************** *********
int main()
{
list<handle_t>* myList1 = new list<handle_t>;
list<handle_t>* myList2 = new list<handle_t>;

do
{
system("cls");

switch (huvudMeny())
{
case 0: return 0;
case 1: addNodeToSTL(my List1);
break;
case 2: displaySTL(myLi st1, myList2);
break;
case 3: deleteSelectedV alue(myList1);
break;
case 4: deleteAll(myLis t1);
break;
case 5: findSelectedVal ue(myList1);
break;
case 6: testDeepCopy(my List1);
break;
case 7: copyList(myList 1, myList2);
break;
}
} while(1);

return 0;
}
Jul 23 '05 #1
0 1472

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
7253
by: Peter Olcott | last post by:
www.halting-problem.com
3
2699
by: SnaiL | last post by:
Hi, guys. I am interested in C++ unit testing. I know only two methods how to test private class methods. 1) Declare friend class in the code. But I don't like this style because tests depends on the code. 2) Cheat compiler. We can do it by: #define private public #include "..."
6
2346
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
1
1979
by: a4w | last post by:
I am not sure if this is too-granular a question to post to this group; however, I did not see a Tablet-specific group. I am experiencing a problem with the Microsoft.Ink.Ink.HitTest(Point , float) method in the Tablet SDK. I have pasted code below that verifies the problem using NUnit. In short, performing a hit-test on a Stroke that I have manually created with a set of input points is generating inconsistent results between...
72
5275
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
1
5136
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers for the two steps, TextBox1.Text ="foo"; and TextBox2.Text = "bar";
0
1333
by: .nu | last post by:
#!/usr/bin/env python # -*- coding: utf-8 -*- # Name: Sleepy Hollow # Author: .nu import wx import os import sys
3
9586
by: Jon L | last post by:
Hi, I'm hoping someone can help me with this problem. I'm not sure whether the problem lies with the software or with my understanding of the language. I'm using the Microsoft.XMLDOM object in an ASP page to read an incoming XML post request and respond to it. Although the XMLDOM object verifies the XML at a basic level, I want to make sure that the request is in the correct format (as per the specification I have to
1
2711
by: AlexW | last post by:
Hi I am in the process of developing an inventory application in visual basic. I keep coming up against a problem with using the dataview.rowfilter property. Basically what happens is this: -a global dataview is created -The user enters new part information into a form. -A function is called to check to see if the part is a new part (so that two identical part numbers are not added to the database. -The check is done by setting the...
2
3014
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my local system that validations work.plz tell that whats the problem in that. Here is my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.