[flang] unit testing for LZBC

Original-commit: flang-compiler/f18@c8541fb8fb
Reviewed-on: https://github.com/flang-compiler/f18/pull/101
Tree-same-pre-rewrite: false
This commit is contained in:
peter klausler 2018-05-29 14:49:52 -07:00
parent 330a3a135d
commit 5bc907602c
7 changed files with 163 additions and 3 deletions

1
flang/.gitignore vendored
View File

@ -1,6 +1,7 @@
Debug
Release
MinSizeRel
build
tags
*.o
*~

View File

@ -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)

15
flang/test/CMakeLists.txt Normal file
View File

@ -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)

View File

@ -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
)

View File

@ -0,0 +1,39 @@
#include "../../lib/evaluate/leading-zero-bit-count.h"
#include "testing.h"
#include <cinttypes>
#include <cstdlib>
#include <iostream>
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();
}

View File

@ -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 <cinttypes>
#include <cstdlib>
#include <iostream>
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_

View File

@ -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