diff --git a/test/catch2_test_printing.cu b/test/catch2_test_printing.cu new file mode 100644 index 0000000000..5c9eb0dc98 --- /dev/null +++ b/test/catch2_test_printing.cu @@ -0,0 +1,36 @@ +#include "test_util.h" + +#include "catch2_test_helper.h" + +template +std::string print(T val) +{ + std::stringstream ss; + ss << val; + return ss.str(); +} + +#if CUB_IS_INT128_ENABLED +TEST_CASE("Test utils can print __int128", "[test][utils]") +{ + REQUIRE( print(__int128_t{0}) == "0" ); + REQUIRE( print(__int128_t{42}) == "42" ); + REQUIRE( print(__int128_t{-1}) == "-1" ); + REQUIRE( print(__int128_t{-42}) == "-42" ); + REQUIRE( print(__int128_t{-1} << 120) == "-1329227995784915872903807060280344576" ); +} + +TEST_CASE("Test utils can print __uint128", "[test][utils]") +{ + REQUIRE( print(__uint128_t{0}) == "0" ); + REQUIRE( print(__uint128_t{1}) == "1" ); + REQUIRE( print(__uint128_t{42}) == "42" ); + REQUIRE( print(__uint128_t{1} << 120) == "1329227995784915872903807060280344576" ); +} +#endif + +TEST_CASE("Test utils can print KeyValuePair", "[test][utils]") +{ + REQUIRE( print(cub::KeyValuePair{42, -42}) == "(42,-42)" ); +} + diff --git a/test/test_util.h b/test/test_util.h index 31a52f5317..61a1913ec4 100644 --- a/test/test_util.h +++ b/test/test_util.h @@ -733,14 +733,14 @@ static std::ostream& operator<<(std::ostream& os, __uint128_t val) { constexpr int max_digits = 40; char buffer[max_digits] = {}; - char* digit = buffer + max_digits - 1; + char* digit = buffer + max_digits; const char* ascii = "0123456789"; do { + digit--; *digit = ascii[val % 10]; val /= 10; - digit--; } while(val != 0);