bits_util_test.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "bits_util.h"
  15. #include <gmock/gmock.h>
  16. #include <gtest/gtest.h>
  17. #include "absl/numeric/int128.h"
  18. using ::testing::Eq;
  19. namespace {
  20. TEST(BitsUtilTest, CountOnes64Works) {
  21. EXPECT_THAT(rlwe::internal::CountOnes64(0xFF000000000000FF), Eq(16));
  22. EXPECT_THAT(rlwe::internal::CountOnes64(0xFF000000000000FE), Eq(15));
  23. EXPECT_THAT(rlwe::internal::CountOnes64(0xFF0000000000FF00), Eq(16));
  24. EXPECT_THAT(rlwe::internal::CountOnes64(0x1111111111111111), Eq(16));
  25. EXPECT_THAT(rlwe::internal::CountOnes64(0x0321212121212121), Eq(16));
  26. }
  27. TEST(BitsUtilTest, CountOnesInByte) {
  28. EXPECT_THAT(rlwe::internal::CountOnesInByte(0x00), Eq(0));
  29. EXPECT_THAT(rlwe::internal::CountOnesInByte(0x01), Eq(1));
  30. EXPECT_THAT(rlwe::internal::CountOnesInByte(0x11), Eq(2));
  31. EXPECT_THAT(rlwe::internal::CountOnesInByte(0x22), Eq(2));
  32. EXPECT_THAT(rlwe::internal::CountOnesInByte(0x44), Eq(2));
  33. EXPECT_THAT(rlwe::internal::CountOnesInByte(0xFF), Eq(8));
  34. EXPECT_THAT(rlwe::internal::CountOnesInByte(0xEE), Eq(6));
  35. }
  36. TEST(BitsUtilTest, CountLeadingZeros64Works) {
  37. rlwe::Uint64 value = 0x8000000000000000;
  38. for (int i = 0; i < 64; i++) {
  39. EXPECT_THAT(rlwe::internal::CountLeadingZeros64(value), Eq(i));
  40. value >>= 1;
  41. }
  42. }
  43. TEST(BitsUtilTest, BitLengthWorks) {
  44. absl::uint128 value = absl::MakeUint128(0x8000000000000000, 0);
  45. for (int i = 0; i <= 128; i++) {
  46. EXPECT_THAT(rlwe::internal::BitLength(value), Eq(128 - i));
  47. value >>= 1;
  48. }
  49. }
  50. } // namespace