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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
#include "flow/CompressedInt.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +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
|
|
|
|
2021-03-11 02:06:03 +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
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +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
|
|
|
|
2021-03-11 02:06:03 +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
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <typename IntType>
|
2017-05-26 04:48:44 +08:00
|
|
|
void testCompressedInt(IntType n, StringRef rep = StringRef()) {
|
2020-09-29 01:58:49 +08:00
|
|
|
BinaryWriter w(AssumeVersion(g_network->protocolVersion()));
|
2017-05-26 04:48:44 +08:00
|
|
|
CompressedInt<IntType> cn(n);
|
|
|
|
|
|
|
|
w << cn;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (rep.size() != 0 && w.toValue() != rep) {
|
2017-05-26 04:48:44 +08:00
|
|
|
printf("WRONG ENCODING:\n");
|
2021-03-11 02:06:03 +08:00
|
|
|
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("");
|
2021-03-11 02:06:03 +08:00
|
|
|
} else
|
2019-03-29 02:52:50 +08:00
|
|
|
rep = w.toValue();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
cn.value = 0;
|
2020-09-29 01:58:49 +08:00
|
|
|
BinaryReader r(rep, AssumeVersion(g_network->protocolVersion()));
|
2017-05-26 04:48:44 +08:00
|
|
|
r >> cn;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (cn.value != n) {
|
2017-05-26 04:48:44 +08:00
|
|
|
printf("FAILURE:\n");
|
2021-03-11 02:06:03 +08:00
|
|
|
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("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:09:58 +08:00
|
|
|
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;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < 10000000; ++i) {
|
2017-05-26 04:48:44 +08:00
|
|
|
n <<= 1;
|
2021-03-11 02:06:03 +08:00
|
|
|
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();
|
|
|
|
}
|