/* * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// Should be false regardless of whether T is signed or unsigned.
EXPECT_FALSE(is_power_of_2(std::numeric_limits<T>::min()));
static_assert(!is_power_of_2(std::numeric_limits<T>::min()), "");
// Test true for (T i = max_power_of_2<T>(); i > 0; i = (i >> 1)) {
EXPECT_TRUE(is_power_of_2(i)) << "value = " << T(i);
}
// Test one less for (T i = max_power_of_2<T>(); i > 2; i = (i >> 1)) {
EXPECT_FALSE(is_power_of_2(i - 1)) << "value = " << T(i - 1);
}
// Test one more for (T i = max_power_of_2<T>(); i > 1; i = (i >> 1)) {
EXPECT_FALSE(is_power_of_2(i + 1)) << "value = " << T(i + 1);
}
template <typename T, ENABLE_IF(std::is_integral<T>::value)> void check_log2i_variants_for(T dummy) { int limit = sizeof(T) * BitsPerByte; if (std::is_signed<T>::value) {
T min = std::numeric_limits<T>::min();
EXPECT_EQ(limit - 1, log2i_graceful(min));
EXPECT_EQ(limit - 1, log2i_graceful((T)-1));
limit--;
}
{ // Test log2i_graceful handles 0 input
EXPECT_EQ(-1, log2i_graceful(T(0)));
}
{ // Test the all-1s bit patterns
T var = 1; for (int i = 0; i < limit; i++, var = (var << 1) | 1) {
EXPECT_EQ(i, log2i(var));
}
}
{ // Test the powers of 2 and powers + 1
T var = 1; for (int i = 0; i < limit; i++, var <<= 1) {
EXPECT_EQ(i, log2i(var));
EXPECT_EQ(i, log2i_graceful(var));
EXPECT_EQ(i, log2i_exact(var));
EXPECT_EQ(i, log2i(var | 1));
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.