From 7cc48094bf88eb6b263b52b29f1e151eda1a6b71 Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Thu, 16 Oct 2014 11:19:00 -0700 Subject: [PATCH] Fixing broken bits test Summary: Buffer size calculation had fallen out of sync with the actual test code. Good thing we have ASAN! Test Plan: Run the test with address santizer. Reviewed By: mpawlowski@fb.com Subscribers: mpawlowski, trunkagent, njormrod FB internal diff: D1619472 Tasks: 5325399 --- folly/experimental/test/BitsTest.cpp | 68 ++++++++++++++++------------ 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/folly/experimental/test/BitsTest.cpp b/folly/experimental/test/BitsTest.cpp index c0a68e31..3bd90890 100644 --- a/folly/experimental/test/BitsTest.cpp +++ b/folly/experimental/test/BitsTest.cpp @@ -252,50 +252,64 @@ T testValue(int bits) { } } -static_assert((-4) >> 1 == -2, "OH"); template void testConcatenation() { // concatenate fields of length 1, 2, 3, ... 64, all unsigned, storing 2/3s // the maximum value in each. - const size_t bitLimit = 64; - const size_t bitsPerPass = (1 + bitLimit) * bitLimit / 2; - const size_t totalBits = bitsPerPass * 3; - uint8_t buf[(totalBits + 7) / 8]; #define EACH_UNSIGNED_SIZE(MACRO, ARG) \ - MACRO(8, uint8_t, ARG); \ - MACRO(16, uint16_t, ARG); \ - MACRO(32, uint32_t, ARG); \ - MACRO(64, uint64_t, ARG); + MACRO(8, uint8_t, ARG) \ + MACRO(16, uint16_t, ARG) \ + MACRO(32, uint32_t, ARG) \ + MACRO(64, uint64_t, ARG) #define EACH_SIGNED_SIZE(MACRO, ARG) \ - MACRO(7, int8_t, ARG); \ - MACRO(15, int16_t, ARG); \ - MACRO(31, int32_t, ARG); \ - MACRO(63, int64_t, ARG); + MACRO(7, int8_t, ARG) \ + MACRO(15, int16_t, ARG) \ + MACRO(31, int32_t, ARG) \ + MACRO(63, int64_t, ARG) + // calculate how much buffer size we need + size_t bufSize = 0; { - size_t w = 0, s; + size_t w = 0; +#define SIZE_TEST(N, T, NEG) \ + for (size_t s = 0; s <= N; ++s) { \ + w += s; \ + } + EACH_UNSIGNED_SIZE(SIZE_TEST, false) + EACH_SIGNED_SIZE(SIZE_TEST, false) + EACH_SIGNED_SIZE(SIZE_TEST, true) +#undef SIZE_TEST + bufSize = w; + } + // bits->bytes, rounding up + bufSize = (bufSize + 7) / 8; + // round up to next multiple of 8 + bufSize = (bufSize + 7) / 8 * 8; + std::vector buffer(bufSize); + uint8_t *buf = buffer.data(); + { + size_t w = 0; #define WRITE_TEST(N, T, NEG) \ - for (; s <= N; ++s, w += s) { \ + for (size_t s = 0; s <= N; ++s) { \ + CHECK_LE(s + w, 8 * bufSize); \ testSet(buf, w, s, testValue(s)); \ EXPECT_EQ((testValue(s)), (testGet(buf, w, s))) << s; \ + w += s; \ } - s = 0; EACH_UNSIGNED_SIZE(WRITE_TEST, false) - s = 0; EACH_SIGNED_SIZE(WRITE_TEST, false) - s = 0; EACH_SIGNED_SIZE(WRITE_TEST, true) #undef WRITE_TEST } { - size_t r = 0, s; -#define READ_TEST(N, T, NEG) \ - for (; s <= N; ++s, r += s) \ - EXPECT_EQ((testValue(s)), (testGet(buf, r, s))) << s; - s = 0; + size_t r = 0; +#define READ_TEST(N, T, NEG) \ + for (size_t s = 0; s <= N; ++s) { \ + CHECK_LE(s + r, 8 * bufSize); \ + EXPECT_EQ((testValue(s)), (testGet(buf, r, s))) << s; \ + r += s; \ + } EACH_UNSIGNED_SIZE(READ_TEST, false) - s = 0; EACH_SIGNED_SIZE(READ_TEST, false) - s = 0; EACH_SIGNED_SIZE(READ_TEST, true) #undef READ_TEST } @@ -304,9 +318,7 @@ void testConcatenation() { TEST(Bits, ConcatenationUnalignedUnsigned) { testConcatenation(); } -TEST(Bits, ConcatenationAligned) { - testConcatenation(); -} +TEST(Bits, ConcatenationAligned) { testConcatenation(); } int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); -- 2.34.1