From 5bc907602c3175e4dc3b51eccd1489322823a101 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Tue, 29 May 2018 14:49:52 -0700 Subject: [PATCH] [flang] unit testing for LZBC Original-commit: flang-compiler/f18@c8541fb8fbdd0ee35afe53ed1fb685573712b950 Reviewed-on: https://github.com/flang-compiler/f18/pull/101 Tree-same-pre-rewrite: false --- flang/.gitignore | 1 + flang/CMakeLists.txt | 4 + flang/test/CMakeLists.txt | 15 ++++ flang/test/evaluate/CMakeLists.txt | 21 +++++ flang/test/evaluate/leading-zero-bit-count.cc | 39 +++++++++ flang/test/evaluate/testing.h | 82 +++++++++++++++++++ flang/tools/f18/CMakeLists.txt | 4 +- 7 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 flang/test/CMakeLists.txt create mode 100644 flang/test/evaluate/CMakeLists.txt create mode 100644 flang/test/evaluate/leading-zero-bit-count.cc create mode 100644 flang/test/evaluate/testing.h diff --git a/flang/.gitignore b/flang/.gitignore index 0a9c73698b8b..c32f5e3cec7b 100644 --- a/flang/.gitignore +++ b/flang/.gitignore @@ -1,6 +1,7 @@ Debug Release MinSizeRel +build tags *.o *~ diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt index d8d2de0b6911..323eef209fe1 100644 --- a/flang/CMakeLists.txt +++ b/flang/CMakeLists.txt @@ -97,8 +97,12 @@ include_directories(BEFORE add_subdirectory(include/flang) add_subdirectory(lib) add_subdirectory(runtime) +add_subdirectory(test) add_subdirectory(tools) configure_file( ${FLANG_SOURCE_DIR}/include/flang/Config/config.h.cmake ${FLANG_BINARY_DIR}/include/flang/Config/config.h) + +enable_testing() +add_test(NAME LZBC COMMAND leading-zero-bit-count-test) diff --git a/flang/test/CMakeLists.txt b/flang/test/CMakeLists.txt new file mode 100644 index 000000000000..054392444568 --- /dev/null +++ b/flang/test/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +add_subdirectory(evaluate) diff --git a/flang/test/evaluate/CMakeLists.txt b/flang/test/evaluate/CMakeLists.txt new file mode 100644 index 000000000000..c72bd325bbf3 --- /dev/null +++ b/flang/test/evaluate/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +add_executable(leading-zero-bit-count-test + leading-zero-bit-count.cc +) + +target_link_libraries(leading-zero-bit-count-test + FortranEvaluate +) diff --git a/flang/test/evaluate/leading-zero-bit-count.cc b/flang/test/evaluate/leading-zero-bit-count.cc new file mode 100644 index 000000000000..c7a9fda6b73c --- /dev/null +++ b/flang/test/evaluate/leading-zero-bit-count.cc @@ -0,0 +1,39 @@ +#include "../../lib/evaluate/leading-zero-bit-count.h" +#include "testing.h" +#include +#include +#include + +using Fortran::evaluate::LeadingZeroBitCount; + +int main() { + CHECK(64, LeadingZeroBitCount(std::uint64_t{0})); + for (int j{0}; j < 64; ++j) { + for (int k{0}; k < j; ++k) { + std::uint64_t x = (std::uint64_t{1} << j) | (std::uint64_t{1} << k); + CHECK_CASE(x, 63 - j, LeadingZeroBitCount(x)); + } + } + CHECK(32, LeadingZeroBitCount(std::uint32_t{0})); + for (int j{0}; j < 32; ++j) { + for (int k{0}; k < j; ++k) { + std::uint32_t x = (std::uint32_t{1} << j) | (std::uint32_t{1} << k); + CHECK_CASE(x, 31 - j, LeadingZeroBitCount(x)); + } + } + CHECK(16, LeadingZeroBitCount(std::uint16_t{0})); + for (int j{0}; j < 16; ++j) { + for (int k{0}; k < j; ++k) { + std::uint16_t x = (std::uint16_t{1} << j) | (std::uint16_t{1} << k); + CHECK_CASE(x, 15 - j, LeadingZeroBitCount(x)); + } + } + CHECK(8, LeadingZeroBitCount(std::uint8_t{0})); + for (int j{0}; j < 8; ++j) { + for (int k{0}; k < j; ++k) { + std::uint8_t x = (std::uint8_t{1} << j) | (std::uint8_t{1} << k); + CHECK_CASE(x, 7 - j, LeadingZeroBitCount(x)); + } + } + return testing::Complete(); +} diff --git a/flang/test/evaluate/testing.h b/flang/test/evaluate/testing.h new file mode 100644 index 000000000000..81da7287b0cb --- /dev/null +++ b/flang/test/evaluate/testing.h @@ -0,0 +1,82 @@ +// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef FORTRAN_EVALUATE_TESTING_H_ +#define FORTRAN_EVALUATE_TESTING_H_ + +#include +#include +#include + +namespace testing { + +int passes{0}; +int failures{0}; + +void Check(const char *file, int line, std::uint64_t want, std::uint64_t got) { + if (want != got) { + ++failures; + std::cerr << file << ':' << std::dec << line << '(' << (passes + failures) + << "): want 0x" << std::hex << want + << ", got 0x" << got << '\n' << std::dec; + } else { + ++passes; + } +} + +void Check(const char *file, int line, std::uint64_t x, std::uint64_t want, + std::uint64_t got) { + if (want != got) { + ++failures; + std::cerr << file << ':' << std::dec << line << '(' << (passes + failures) + << ")[0x" << std::hex << x << "]: want 0x" << want + << ", got 0x" << got << '\n' << std::hex; + } else { + ++passes; + } +} + +int Complete() { + if (failures == 0) { + if (passes == 1) { + std::cout << "test PASSES\n"; + } else { + std::cout << "all " << std::dec << passes << " tests PASS\n"; + } + passes = 0; + return EXIT_SUCCESS; + } else { + if (passes == 1) { + std::cerr << std::dec << "1 test passes, "; + } else { + std::cerr << std::dec << passes << " tests pass, "; + } + if (failures == 1) { + std::cerr << "1 test FAILS\n"; + } else { + std::cerr << std::dec << failures << " tests FAIL\n"; + } + passes = failures = 0; + return EXIT_FAILURE; + } +} + +} // namespace testing + +#define CHECK(want, got) \ + testing::Check(__FILE__, __LINE__, (want), (got)) +#define CHECK_CASE(n, want, got) \ + testing::Check(__FILE__, __LINE__, (n), (want), (got)) + +#endif // FORTRAN_EVALUATE_TESTING_H_ diff --git a/flang/tools/f18/CMakeLists.txt b/flang/tools/f18/CMakeLists.txt index 4c9d134eba81..c06dd00689a5 100644 --- a/flang/tools/f18/CMakeLists.txt +++ b/flang/tools/f18/CMakeLists.txt @@ -12,13 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. - -######## f18 ########## - add_executable(f18 f18.cc dump.cc ) + target_link_libraries(f18 FortranParser FlangSemantics