Index: test/std/numerics/c.math/abs.fail.cpp =================================================================== --- /dev/null +++ test/std/numerics/c.math/abs.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include + +#include "test_macros.h" + +#ifndef _LIBCPP_VERSION +#error _LIBCPP_VERSION not defined +#endif + +int main(int, char**) +{ + unsigned int ui = -5; + std::abs(ui); // expected-error {{call to 'abs' is ambiguous}} + + return 0; +} + Index: test/std/numerics/c.math/abs.pass.cpp =================================================================== --- /dev/null +++ test/std/numerics/c.math/abs.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include +#include +#include +#include + +#include "test_macros.h" + +#ifndef _LIBCPP_VERSION +#error _LIBCPP_VERSION not defined +#endif + +template +void test_abs() +{ + T neg_val = -5; + T pos_val = 5; + R res = 5; + + ASSERT_SAME_TYPE(decltype(std::abs(neg_val)), R); + + assert(std::abs(neg_val) == res); + assert(std::abs(pos_val) == res); +} + +void test_big() +{ + long long int big_value = std::numeric_limits::max(); // a value to big for ints to store + long long int negative_big_value = -big_value; + assert(std::abs(negative_big_value) == big_value); // make sure it doesnt get casted to a smaller type +} + +int main(int, char**) +{ + test_abs(); + test_abs(); + test_abs(); + if (std::is_signed::value) test_abs(); // sometimes chars are unsigned + + test_abs(); + test_abs(); + test_abs(); + + test_abs(); + test_abs(); + test_abs(); + test_abs(); + + test_abs(); + test_abs(); + test_abs(); + + test_big(); + + return 0; +} +