Path: blob/master/test/hotspot/gtest/utilities/test_count_trailing_zeros.cpp
41144 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "utilities/count_trailing_zeros.hpp"26#include "utilities/globalDefinitions.hpp"27#include "unittest.hpp"282930template <typename T> static void test_one_or_two_set_bits() {31unsigned i = 0; // Position of a set bit.32unsigned max = sizeof(T) * BitsPerByte;33for (T ix = T(1); i < max; ix <<= 1, ++i) {34unsigned j = 0; // Position of a set bit.35for (T jx = T(1); j < max; jx <<= 1, ++j) {36T value = ix | jx;37EXPECT_EQ(MIN2(i, j), count_trailing_zeros(value))38<< "value = " << value;39}40}41}4243TEST(count_trailing_zeros, one_or_two_set_bits) {44test_one_or_two_set_bits<int8_t>();45test_one_or_two_set_bits<int16_t>();46test_one_or_two_set_bits<int32_t>();47test_one_or_two_set_bits<int64_t>();48test_one_or_two_set_bits<uint8_t>();49test_one_or_two_set_bits<uint16_t>();50test_one_or_two_set_bits<uint32_t>();51test_one_or_two_set_bits<uint64_t>();52}5354template <typename T> static void test_high_zeros_low_ones() {55T value = std::numeric_limits<T>::max();56for ( ; value != 0; value >>= 1) {57EXPECT_EQ(0u, count_trailing_zeros(value))58<< "value = " << value;59}60}6162TEST(count_trailing_zeros, high_zeros_low_ones) {63test_high_zeros_low_ones<int8_t>();64test_high_zeros_low_ones<int16_t>();65test_high_zeros_low_ones<int32_t>();66test_high_zeros_low_ones<int64_t>();67test_high_zeros_low_ones<uint8_t>();68test_high_zeros_low_ones<uint16_t>();69test_high_zeros_low_ones<uint32_t>();70test_high_zeros_low_ones<uint64_t>();71}7273template <typename T> static void test_high_ones_low_zeros() {74unsigned i = 0; // Index of least significant set bit.75T value = ~T(0);76unsigned max = sizeof(T) * BitsPerByte;77for ( ; i < max; value <<= 1, ++i) {78EXPECT_EQ(i, count_trailing_zeros(value))79<< "value = " << value;80}81}8283TEST(count_trailing_zeros, high_ones_low_zeros) {84test_high_ones_low_zeros<int8_t>();85test_high_ones_low_zeros<int16_t>();86test_high_ones_low_zeros<int32_t>();87test_high_ones_low_zeros<int64_t>();88test_high_ones_low_zeros<uint8_t>();89test_high_ones_low_zeros<uint16_t>();90test_high_ones_low_zeros<uint32_t>();91test_high_ones_low_zeros<uint64_t>();92}939495