1

I'm writing some testing functions for a project. The test function takes a function pointer, an expected value and a test value and checks them:

int TestIntInIntOut(int (*func)(int val), int expected, int testvalue);

However, I'm also testing functions which can take an enum as a parameter or as an exit value.

Suppose the following:

typedef enum { Zero, One, Two } MyEnum;

Is there a way I can pass an entire enum as a type? For any enum. For example:

int TestIntInEnumOut(?enum? (*func)(int val), ?enum? expected, int testvalue);

or

int TestEnumInIntOut(int (*func)(?enum? val), int expected, ?enum? testvalue);

(Sort of like what a generic in C# would allow.)

2 Answers 2

4

Do you mean any enum, or just MyEnum?

If you only need a function to support MyEnum, you could do:

int TestEnumInIntOut(int (*func)(MyEnum val), int expected, MyEnum testvalue);

Otherwise you probably can't do it in C, with much type safety. But an enum is essentially the same as an int, so you could just treat them all as int types. If the type safety is necessary, consider providing different integer value ranges for different enum types.

Sign up to request clarification or add additional context in comments.

2 Comments

It would be for any enum, yes.
Missed that part in my (now deleted) answer below. Agree with @Rob.
0

Not directly an answer to your question, however as you said you are writing some test functions: You might want to have a look at googletest or cppunit - could save you some trouble.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.