From ab581e79aa3ee4cdbcb30b9c5a76ff4e40281365 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 26 Jul 2016 14:28:34 +0000 Subject: Implement LCM and GCD for Library Fundamentals. Reviewed as https://reviews.llvm.org/D21343. llvm-svn: 276750 --- .../numeric.ops.lcm/lcm.not_integral1.fail.cpp | 24 ++++ .../numeric.ops.lcm/lcm.not_integral2.fail.cpp | 24 ++++ .../numeric.ops/numeric.ops.lcm/lcm.pass.cpp | 131 +++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp create mode 100644 libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp create mode 100644 libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp (limited to 'libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm') diff --git a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp new file mode 100644 index 00000000000..d12b35609b1 --- /dev/null +++ b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral1.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11 +// + +// template +// constexpr common_type_t<_M,_N> lcm(_M __m, _N __n) + +// Remarks: If either M or N is not an integer type, the program is ill-formed. + +#include + + +int main() +{ + std::experimental::lcm(2.0, 4); +} diff --git a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp new file mode 100644 index 00000000000..25bcd51ade7 --- /dev/null +++ b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.not_integral2.fail.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11 +// + +// template +// constexpr common_type_t<_M,_N> lcm(_M __m, _N __n) + +// Remarks: If either M or N is not an integer type, the program is ill-formed. + +#include + + +int main() +{ + std::experimental::lcm(4, 6.0); +} diff --git a/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp new file mode 100644 index 00000000000..21938921d0b --- /dev/null +++ b/libcxx/test/std/experimental/numeric/numeric.ops/numeric.ops.lcm/lcm.pass.cpp @@ -0,0 +1,131 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++98, c++03, c++11 +// + +// template +// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) + +#include +#include +#include +#include + +constexpr struct { + int x; + int y; + int expect; +} Cases[] = { + {0, 0, 0}, + {1, 0, 0}, + {0, 1, 0}, + {1, 1, 1}, + {2, 3, 6}, + {2, 4, 4}, + {3, 17, 51}, + {36, 18, 36} +}; + +template +constexpr bool test0(Input1 in1, Input2 in2, Output out) +{ + static_assert((std::is_same::value), "" ); + static_assert((std::is_same::value), "" ); + return out == std::experimental::lcm(in1, in2) ? true : (std::abort(), false); +} + + +template +constexpr bool do_test(int dummy = 0) +{ + using S1 = typename std::make_signed::type; + using S2 = typename std::make_signed::type; + using U1 = typename std::make_unsigned::type; + using U2 = typename std::make_unsigned::type; + bool accumulate = true; + for (auto TC : Cases) { + { // Test with two signed types + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + accumulate &= test0(-TC.x, -TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + accumulate &= test0(-TC.x, -TC.y, TC.expect); + } + { // test with two unsigned types + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + } + { // Test with mixed signs + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + } + { // Test with mixed signs + using Output = std::common_type_t; + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, TC.y, TC.expect); + accumulate &= test0(-TC.x, TC.y, TC.expect); + accumulate &= test0(TC.x, -TC.y, TC.expect); + } + } + return accumulate; +} + +int main() +{ + auto non_cce = std::rand(); // a value that can't possibly be constexpr + + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + + static_assert(do_test< int8_t>(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert(do_test< int8_t>(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); + assert((do_test(non_cce))); +} -- cgit v1.2.3