473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3734
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.jpwrote:
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.jpwrote:
>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...@gmail.com" <PengYu...@gmail.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*******@gmail.com" <Pe*******@gmail.comwrites:
On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@yahoo.co.jpwrote:
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*******@gmail.com" <Pe*******@gmail.comwrites:
>On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@yahoo.co.jpwrote:
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*******@gmail.com" <Pe*******@gmail.comwrites:
On Jul 8, 6:42 pm, Simon Wollwage <wollwagesi...@yahoo.co.jpwrote:
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++.moderated 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
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...
3
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...
19
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
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
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...
4
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...
4
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
9
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...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...

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.