From 1b99b2dcde9d8bf1a5944a7e0dac81ca33459af5 Mon Sep 17 00:00:00 2001 From: thestig Date: Fri, 20 May 2016 11:50:06 -0700 Subject: Merge GetBits32() impls into one. Put it in fxcrt. Review-Url: https://codereview.chromium.org/1990153003 --- core/fxcrt/fx_basic_util_unittest.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core/fxcrt/fx_basic_util_unittest.cpp (limited to 'core/fxcrt/fx_basic_util_unittest.cpp') diff --git a/core/fxcrt/fx_basic_util_unittest.cpp b/core/fxcrt/fx_basic_util_unittest.cpp new file mode 100644 index 0000000000..3272eab70c --- /dev/null +++ b/core/fxcrt/fx_basic_util_unittest.cpp @@ -0,0 +1,30 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/include/fx_basic.h" +#include "testing/fx_string_testhelpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) { + int result = 0; + for (int i = 0; i < nbits; i++) { + if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) + result |= 1 << (nbits - i - 1); + } + return result; +} + +} // namespace + +TEST(fxge, GetBits32) { + unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56}; + for (int nbits = 1; nbits <= 32; ++nbits) { + for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) { + EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits), + GetBits32(data, bitpos, nbits)); + } + } +} -- cgit v1.2.3