foundationdb/flow/CompressedInt.actor.cpp

107 lines
2.9 KiB
C++
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* CompressedInt.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
2017-05-26 04:48:44 +08:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2017-05-26 04:48:44 +08:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "flow/UnitTest.h"
#include "flow/CompressedInt.h"
2017-05-26 04:48:44 +08:00
void printBitsLittle(size_t const size, void const* const ptr) {
unsigned char* b = (unsigned char*)ptr;
unsigned char byte;
int i, j;
2017-05-26 04:48:44 +08:00
for (i = size - 1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
printf(" ");
}
puts("");
2017-05-26 04:48:44 +08:00
}
void printBitsBig(size_t const size, void const* const ptr) {
unsigned char* b = (unsigned char*)ptr;
unsigned char byte;
int i, j;
2017-05-26 04:48:44 +08:00
for (i = 0; i < size; ++i) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
printf(" ");
}
puts("");
2017-05-26 04:48:44 +08:00
}
template <typename IntType>
2017-05-26 04:48:44 +08:00
void testCompressedInt(IntType n, StringRef rep = StringRef()) {
BinaryWriter w(AssumeVersion(g_network->protocolVersion()));
2017-05-26 04:48:44 +08:00
CompressedInt<IntType> cn(n);
w << cn;
if (rep.size() != 0 && w.toValue() != rep) {
2017-05-26 04:48:44 +08:00
printf("WRONG ENCODING:\n");
printf(" test value (BigE): ");
printBitsLittle(sizeof(IntType), &n);
printf(" encoded: ");
printBitsBig(w.toValue().size(), w.toValue().begin());
printf(" expected: ");
printBitsBig(rep.size(), rep.begin());
2017-05-26 04:48:44 +08:00
puts("");
} else
rep = w.toValue();
2017-05-26 04:48:44 +08:00
cn.value = 0;
BinaryReader r(rep, AssumeVersion(g_network->protocolVersion()));
2017-05-26 04:48:44 +08:00
r >> cn;
if (cn.value != n) {
2017-05-26 04:48:44 +08:00
printf("FAILURE:\n");
printf(" test value: (Big): ");
printBitsLittle(sizeof(IntType), &n);
printf(" encoded: ");
printBitsBig(rep.size(), rep.begin());
printf(" decoded value: ");
printBitsLittle(sizeof(IntType), &cn.value);
2017-05-26 04:48:44 +08:00
puts("");
}
}
TEST_CASE("/flow/compressed_ints") {
2017-05-26 04:48:44 +08:00
testCompressedInt<int>(-2, LiteralStringRef("\x7e"));
testCompressedInt<int>(-1, LiteralStringRef("\x7f"));
testCompressedInt<int>(0, LiteralStringRef("\x80"));
testCompressedInt<int>(1, LiteralStringRef("\x81"));
testCompressedInt<int>(2, LiteralStringRef("\x82"));
testCompressedInt<int64_t>(0x4000000000000000, LiteralStringRef("\xFF\xC0\x40\x00\x00\x00\x00\x00\x00\x00"));
int64_t n = 0;
for (int i = 0; i < 10000000; ++i) {
2017-05-26 04:48:44 +08:00
n <<= 1;
if (deterministicRandom()->coinflip())
2017-05-26 04:48:44 +08:00
n |= 1;
testCompressedInt<int64_t>(n);
testCompressedInt<int32_t>(n);
testCompressedInt<int16_t>(n);
}
return Void();
}