[libcxx-commits] [PATCH] D57778: std::abs should not return double (2735)

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 6 10:16:20 PST 2019


zoecarver updated this revision to Diff 185584.
zoecarver edited the summary of this revision.
zoecarver added a comment.

Fix review comments.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57778/new/

https://reviews.llvm.org/D57778

Files:
  test/std/numerics/c.math/abs.fail.cpp
  test/std/numerics/c.math/abs.pass.cpp


Index: test/std/numerics/c.math/abs.pass.cpp
===================================================================
--- /dev/null
+++ test/std/numerics/c.math/abs.pass.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <assert.h>
+#include <cmath>
+#include <type_traits>
+
+#include "test_macros.h"
+
+#ifndef _LIBCPP_VERSION
+#error _LIBCPP_VERSION not defined
+#endif
+
+template <class T, class R>
+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 && "Absolute value returned incorrect value");
+    assert(std::abs(pos_val) == res && "Absolute value returned incorrect value");
+}
+
+void test_big()
+{
+    long long int big_value = std::numeric_limits<long long int>::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<int, int>();
+    test_abs<long int, long int>();
+    test_abs<long long int, long long int>();
+    test_abs<long double, long double>();
+    test_abs<double, double>();
+    test_abs<float, float>();
+    test_abs<short int, int>();
+    test_abs<char, int>();
+    
+    test_big();
+    
+    return 0;
+}
+
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 <cmath>
+
+#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;
+}
+


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57778.185584.patch
Type: text/x-patch
Size: 2570 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190206/577e0556/attachment.bin>


More information about the libcxx-commits mailing list