By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

test private member function

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.