Hi Everyone
Here's a few test cases I wrote to test the fixes for the clz and ctz
functions I wrote for MSVC previously that have been already committed.
They contain some suggested changes from Marshall and I have tested they
compile with clang++/g++ and cl.exe against libcxx.
If the tests fail, they call assert and give some details about the failure
point/reason. If they pass, they produce no output.
The test cases basically just set some bit values in various types and
check the leading and trailing zero count is as expected.
For the test cases to compile with cl.exe, the type_traits patch for
libcxx and MSVC that I previously submitted needs to be applied.
Thanks.
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
// Some simple tests to verify that the functions
// __builtin_ctz, __builtin_ctzl, __builtin_ctzll
// and __ctz
// all work as expected. The latter wraps the former builtins.
#include <cstdio>
#include <cassert>
#include <limits>
// Assume CTZ etc. functions will be indirectly pulled in via <algorithm>.
#include <algorithm>
template<typename T> static int num_bits()
{
return std::numeric_limits<T>::digits;
}
static const int num_int_bits = num_bits<unsigned int>();
static const int num_long_bits = num_bits<unsigned long>();
static const int num_long_long_bits = num_bits<unsigned long long>();
// Number of checks/tests expected to be performed. Count derived from:
// For each supported type (unsigned int, unsigned long, unsigned long long):
// For each unique bit in the type:
// do 2 tests __builtin_ctz, __ctz
static const int expected_tests_run =
(num_int_bits + num_long_bits + num_long_long_bits) * 2;
static int actual_tests_run = 0;
static int num_tests_failed = 0;
static void done()
{
bool failed = false;
if (num_tests_failed) {
printf( "%d tests failed\n", num_tests_failed );
failed = true;
}
if (actual_tests_run != expected_tests_run) {
printf( "Expected %d test to run, but %d were actually run\n",
expected_tests_run, actual_tests_run );
failed = true;
}
assert(!failed);
}
static void check( const char* test_name, int actual, int expected, int line )
{
++actual_tests_run;
if (actual != expected) {
++num_tests_failed;
printf( "Test %s at line %d failed. The expected value is %d but actual value was %d.\n",
test_name, line, expected, actual );
assert(false);
}
}
static void ctz_tests()
{
// Set each bit from LSB to MSB.
// Leading zero bits are counted from MSB to LSB until a set bit is found.
// Trailing zero bits are counted from LSB to MSB until a set bit is found.
// Tests where mask is 0 should not be performed because it is UB.
{
for ( int b = 0; b < num_int_bits; ++b ) {
unsigned int m = 1U << b;
int expected_trailing_zero_bits = b;
check("__builtin_ctz", __builtin_ctz(m),
expected_trailing_zero_bits, __LINE__);
check("__ctz(unsigned int)", std::__ctz(m),
expected_trailing_zero_bits, __LINE__);
}
}
{
for ( int b = 0; b < num_long_bits; ++b ) {
unsigned long m = 1UL << b;
int expected_trailing_zero_bits = b;
check("__builtin_ctzl", __builtin_ctzl(m),
expected_trailing_zero_bits,__LINE__);
check("__ctz(unsigned long)", std::__ctz(m),
expected_trailing_zero_bits, __LINE__);
}
}
{
for ( int b = 0; b < num_long_long_bits; ++b ) {
unsigned long long m = 1ULL << b;
int expected_trailing_zero_bits = b;
check("__builtin_ctzll", __builtin_ctzll(m),
expected_trailing_zero_bits, __LINE__);
check("__ctz(unsigned long long)", std::__ctz(m),
expected_trailing_zero_bits, __LINE__);
}
}
}
int main()
{
ctz_tests();
done();
}
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
// Some simple tests to verify that the functions
// __builtin_clz, __builtin_clzl, __builtin_clzll
// and __clz
// all work as expected. The latter wraps the former builtins.
#include <cstdio>
#include <cassert>
#include <limits>
// Assume CLZ etc. functions will be indirectly pulled in via <algorithm>.
#include <algorithm>
template<typename T> static int num_bits()
{
return std::numeric_limits<T>::digits;
}
static const int num_int_bits = num_bits<unsigned int>();
static const int num_long_bits = num_bits<unsigned long>();
static const int num_long_long_bits = num_bits<unsigned long long>();
// Number of checks/tests expected to be performed. Count derived from:
// For each supported type (unsigned int, unsigned long, unsigned long long):
// For each unique bit in the type:
// do 2 tests __builtin_clz, __clz.
static const int expected_tests_run =
(num_int_bits + num_long_bits + num_long_long_bits) * 2;
static int actual_tests_run = 0;
static int num_tests_failed = 0;
static void done()
{
bool failed = false;
if (num_tests_failed) {
printf( "%d tests failed\n", num_tests_failed );
failed = true;
}
if (actual_tests_run != expected_tests_run) {
printf( "Expected %d test to run, but %d were actually run\n",
expected_tests_run, actual_tests_run );
failed = true;
}
assert(!failed);
}
static void check( const char* test_name, int actual, int expected, int line )
{
++actual_tests_run;
if (actual != expected) {
++num_tests_failed;
printf( "Test %s at line %d failed. The expected value is %d but actual value was %d.\n",
test_name, line, expected, actual );
assert(false);
}
}
static void clz_tests()
{
// Set each bit from LSB to MSB.
// Leading zero bits are counted from MSB to LSB until a set bit is found.
// Trailing zero bits are counted from LSB to MSB until a set bit is found.
// Tests where mask is 0 should not be performed because it is UB.
{
for ( int b = 0; b < num_int_bits; ++b ) {
unsigned int m = 1U << b;
int expected_leading_zero_bits = num_int_bits - (b+1);
check("__builtin_clz", __builtin_clz(m),
expected_leading_zero_bits, __LINE__);
check("__clz(unsigned int)", std::__clz(m),
expected_leading_zero_bits, __LINE__);
}
}
{
for ( int b = 0; b < num_long_bits; ++b ) {
unsigned long m = 1UL << b;
int expected_leading_zero_bits = num_long_bits - (b+1);
check("__builtin_clzl", __builtin_clzl(m),
expected_leading_zero_bits, __LINE__);
check("__clz(unsigned long)", std::__clz(m),
expected_leading_zero_bits, __LINE__);
}
}
{
for ( int b = 0; b < num_long_long_bits; ++b ) {
unsigned long long m = 1ULL << b;
int expected_leading_zero_bits = num_long_long_bits - (b+1);
check("__builtin_clzll", __builtin_clzll(m),
expected_leading_zero_bits,__LINE__);
check("__clz(unsigned long long)", std::__clz(m),
expected_leading_zero_bits,__LINE__);
}
}
}
int main()
{
clz_tests();
done();
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits