Cihan Kurt
Cihan Kurt

Reputation: 373

How to compare values with templates and boost in C++?

I have created a template function in which every type can be put. Now it is true that this function will always have to work. If type T is an int, an int always comes out of the function. If a string comes in, a string comes out.

Now I have just looked at the boost documentation. Boost indicates that I must use a BOOST_AUTO_TEST_CASE_TEMPLATE.

Let's say I have the following situation:

BOOST_AUTO_TEST_SUITE (MyTestSuite)

typedef boost :: mpl :: list <int, long, short, double, float, std :: string> TestTypes;

BOOST_AUTO_TEST_CASE_TEMPLATE (MyTestCase, T, TestTypes)
{
  // These three values ​​will be separated by ',' and can be accessed seprately thanks to their individual getters.
  std :: string input = "5,6,7";
  Myclass <T> myClass (input);

BOOST_TEST (myClass.getFirst() == 5);
BOOST_TEST (myClass.getSecond() == 6);
BOOST_TEST (myClass.getThird() == 7);
}

BOOST_AUTO_TEST_SUITE_END()

As you can see, I do not want to check whether it is the right type that I get back, but whether the value that I get back is the same as the values ​​I have given. So if I give 5 and the type double then my class makes it 5.00. My check currently does not do that. How do I ensure that the number that I want to check is always the same as the type I am giving? It may sound a bit difficult, but if you want more information then I will edit this message.

And let's say this works. Then I want to use the minimum and maximum value of that type as input. How do I do that? How do I get the minimum and maximum value of T?

Upvotes: 0

Views: 94

Answers (1)

Jarod42
Jarod42

Reputation: 217245

You might use decltype, and stl traits:

BOOST_CHECK(std::is_same<T, decltype(myClass.getFirst())>::value);

Then I want to use the minimum and maximum value of that type as input. How do I do that?

using std::numeric_limits

Upvotes: 1

Related Questions