473,881 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

test private member function

Hi,

I want to write a test function to test a class. The class has a
private member function that need to be tested. Although I could
modify the member function as protected or public, I do not want to
modify the class definition. I'm wondering what is the standard way to
test private member functions in a library.

Thanks,
Peng

Jul 8 '07 #1
7 3773
Am Sun, 08 Jul 2007 15:38:36 -0700 schrieb Pe*******@gmail .com:
Hi,

I want to write a test function to test a class. The class has a private
member function that need to be tested. Although I could modify the
member function as protected or public, I do not want to modify the
class definition. I'm wondering what is the standard way to test private
member functions in a library.

Thanks,
Peng
you could make a testclass that is a friend of the class that you're
testing. or add a public wrapper method to the tested class.
Jul 8 '07 #2
On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@ yahoo.co.jpwrot e:
Am Sun, 08 Jul 2007 15:38:36 -0700 schrieb PengYu...@gmail .com:
Hi,
I want to write a test function to test a class. The class has a private
member function that need to be tested. Although I could modify the
member function as protected or public, I do not want to modify the
class definition. I'm wondering what is the standard way to test private
member functions in a library.
Thanks,
Peng

you could make a testclass that is a friend of the class that you're
testing. or add a public wrapper method to the tested class.
Hi,

To make the testclass a friend, I still have to change the source code
of the class to be tested (declaring the testclass as a friend),
right?

Could you elaborate more no 'public wrapper method'? Can it access the
private member?

Thanks,
Peng

Jul 9 '07 #3
Am Mon, 09 Jul 2007 00:01:26 +0000 schrieb Pe*******@gmail .com:
On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@ yahoo.co.jpwrot e:
>Am Sun, 08 Jul 2007 15:38:36 -0700 schrieb PengYu...@gmail .com:
Hi,
I want to write a test function to test a class. The class has a
private member function that need to be tested. Although I could
modify the member function as protected or public, I do not want to
modify the class definition. I'm wondering what is the standard way
to test private member functions in a library.
Thanks,
Peng

you could make a testclass that is a friend of the class that you're
testing. or add a public wrapper method to the tested class.

Hi,

To make the testclass a friend, I still have to change the source code
of the class to be tested (declaring the testclass as a friend), right?

Could you elaborate more no 'public wrapper method'? Can it access the
private member?

Thanks,
Peng
For the wrapper method, you have to change the class too.
You HAVE to change it. There's a reason for the "private"-area.
If you still wanna access it without changing the class, you have to
manipulate the vtable. And that's a pain in the ass to do.

--
/(bb|[^b]{2})/
"Microsoft - We put the bill in billionaire"
Jul 9 '07 #4
On Jul 9, 7:38 am, "PengYu...@gmai l.com" <PengYu...@gmai l.comwrote:
Hi,

I want to write a test function to test a class. The class has a
private member function that need to be tested. Although I could
modify the member function as protected or public, I do not want to
modify the class definition. I'm wondering what is the standard way to
test private member functions in a library.

Thanks,
Peng
The intention of Private Functions are, which is to be used by the
public interfaces, or other methods of your class which actually does
the processing, the best way to test your code is that, you can test
the interface or method which using this private function with
different inputs.

CPP Unit is a good framework for Unit Level Testing.

Regards,
Sarath
http://sarathc.wordpress.com/

Jul 9 '07 #5
"Pe*******@gmai l.com" <Pe*******@gmai l.comwrites:
On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@ yahoo.co.jpwrot e:
Am Sun, 08 Jul 2007 15:38:36 -0700 schrieb PengYu...@gmail .com:
Hi,
I want to write a test function to test a class. The class has a
private member function that need to be tested. Although I could
modify the member function as protected or public, I do not want
to modify the class definition. I'm wondering what is the
standard way to test private member functions in a library.
Thanks,
Peng
you could make a testclass that is a friend of the class that
you're testing. or add a public wrapper method to the tested
class.
[...]
To make the testclass a friend, I still have to change the source code
of the class to be tested (declaring the testclass as a friend),
right?
I'm not sure it's guaranteed to work, but we've used the "#define
private public" hack in our unit tests. It's the only way I know of
to get access to a class' guts for testing purposes *without* changing
the source code.

--
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)
Jul 12 '07 #6
Dave Steffen wrote:
"Pe*******@gmai l.com" <Pe*******@gmai l.comwrites:
>On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@ yahoo.co.jpwrot e:
Am Sun, 08 Jul 2007 15:38:36 -0700 schrieb PengYu...@gmail .com:

Hi,

I want to write a test function to test a class. The class has a
private member function that need to be tested. Although I could
modify the member function as protected or public, I do not want
to modify the class definition. I'm wondering what is the
standard way to test private member functions in a library.

Thanks,
Peng

you could make a testclass that is a friend of the class that
you're testing. or add a public wrapper method to the tested
class.
[...]
>To make the testclass a friend, I still have to change the source code
of the class to be tested (declaring the testclass as a friend),
right?

I'm not sure it's guaranteed to work, but we've used the "#define
private public" hack in our unit tests. It's the only way I know of
to get access to a class' guts for testing purposes *without* changing
the source code.
Formally, that would be undefined behavior in any compilation unit that
includes at least one standard header. See [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to keywords.
Best

Kai-Uwe Bux
Jul 12 '07 #7
Kai-Uwe Bux <jk********@gmx .netwrites:
Dave Steffen wrote:
"Pe*******@gmai l.com" <Pe*******@gmai l.comwrites:
On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@ yahoo.co.jpwrot e:
Am Sun, 08 Jul 2007 15:38:36 -0700 schrieb PengYu...@gmail .com:

Hi,

I want to write a test function to test a class. The class has a
private member function that need to be tested. Although I could
modify the member function as protected or public, I do not want
to modify the class definition. I'm wondering what is the
standard way to test private member functions in a library.
[...]

I'm not sure it's guaranteed to work, but we've used the "#define
private public" hack in our unit tests. It's the only way I know
of to get access to a class' guts for testing purposes *without*
changing the source code.

Formally, that would be undefined behavior in any compilation unit
that includes at least one standard header. See [17.4.3.1.1/2]:
Yeah, I know. :-) It's a mildly tricky game to play. The first time
I saw it, I thought it was the most horrible thing I'd ever seen.

Interestingly, there was a thread a while back on
comp.lang.c++.m oderated a while back about whether or not adding
"#define private public" could actually change the behavior of a
program (assuming the compiler did something reasonable, which isn't
guaranteed by the standard, but is true for popular compilers). The
answer was, IIRC, "Yes, but only if you're doing things you
shouldn't". Overloading member functions with different access
specifiers was the specific example somebody came up with.

So, yeah, not guaranteed to work, not guaranteed portable, but
(practically speaking) workable in some environments. I don't know
of any other way to test private methods *without touching the
source*.

--
Dave Steffen, Ph.D. "I say we invite opportunity inside
Software Engineer IV for a nice cup of tea, then hit her
Numerica Corporation on the head and steal her purse."
dgsteffen numerica dot us -- Shlock Mercenary
Jul 12 '07 #8

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

Similar topics

5
11322
by: David Rubin | last post by:
Is there ever a good reason to declare private non-virtual member functions in a class definition? As far as private virtual function are concerned, my understanding is that, if you have a (public) function which calls an private virtual function in the base class, the behavior can be changed by overriding the private virtual function in the derived class (but leaving the calling function unchanged). This at least seems useful. /david
3
2331
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
19
2356
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
5
2421
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
5
3412
by: Andy | last post by:
How can you unit test nested functions? Or do you have to pull them out to unit test them, which basically means I will never use nested functions. Also, same thing with private member functions protected by __. Seems like there is a conflict there between using these features and unit testing. Andy
4
1762
by: Tony Johansson | last post by:
Hello!! I have done some operator overloading but my main testprogram doesn't work well. Have you any idea which of my methods are wrong? #include <iostream> #include <string> using namespace std;
4
2301
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
9
2757
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using namespace std;
14
4236
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
9928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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
10718
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...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9554
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...
0
7110
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
5781
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...
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.