Skip to content

Commit

Permalink
fix varint64 encode / decode (#101)
Browse files Browse the repository at this point in the history
* fix varint64 encode / decode
* update clang for tests
  • Loading branch information
artpaul authored Feb 23, 2020
1 parent bdcbfb0 commit 9af720d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
13 changes: 4 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ language: cpp
matrix:
include:
- os: linux
dist: trusty
dist: bionic
sudo: required
compiler: gcc
addons:
Expand All @@ -17,22 +17,17 @@ matrix:
- MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"

- os: linux
dist: trusty
dist: bionic
sudo: required
compiler: clang
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-6.0
- sourceline: 'ppa:ubuntu-toolchain-r/test'
packages:
- g++-7
- llvm-6.0
- clang-6.0
- clang-7
- libstdc++6
env:
- MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"
- MATRIX_EVAL="CC=clang-7 && CXX=clang++-7"

- os: osx
osx_image: xcode8.2
Expand Down
6 changes: 3 additions & 3 deletions clickhouse/base/coded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ bool CodedInputStream::Skip(size_t count) {
bool CodedInputStream::ReadVarint64(uint64_t* value) {
*value = 0;

for (size_t i = 0; i < 9; ++i) {
for (size_t i = 0; i < MAX_VARINT_BYTES; ++i) {
uint8_t byte;

if (!input_->ReadByte(&byte)) {
return false;
} else {
*value |= (byte & 0x7F) << (7 * i);
*value |= uint64_t(byte & 0x7F) << (7 * i);

if (!(byte & 0x80)) {
return true;
Expand Down Expand Up @@ -81,7 +81,7 @@ void CodedOutputStream::WriteVarint64(uint64_t value) {
uint8_t bytes[MAX_VARINT_BYTES];
int size = 0;

for (size_t i = 0; i < 9; ++i) {
for (size_t i = 0; i < MAX_VARINT_BYTES; ++i) {
uint8_t byte = value & 0x7F;
if (value > 0x7F)
byte |= 0x80;
Expand Down
7 changes: 4 additions & 3 deletions ut/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
ADD_EXECUTABLE (clickhouse-cpp-ut
main.cpp

columns_ut.cpp
types_ut.cpp
type_parser_ut.cpp
client_ut.cpp
columns_ut.cpp
socket_ut.cpp
stream_ut.cpp
tcp_server.cpp
type_parser_ut.cpp
types_ut.cpp
)

TARGET_LINK_LIBRARIES (clickhouse-cpp-ut
Expand Down
23 changes: 23 additions & 0 deletions ut/stream_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <clickhouse/base/coded.h>
#include <contrib/gtest/gtest.h>

using namespace clickhouse;

TEST(CodedStreamCase, Varint64) {
Buffer buf;

{
BufferOutput output(&buf);
CodedOutputStream coded(&output);
coded.WriteVarint64(18446744071965638648ULL);
}


{
ArrayInput input(buf.data(), buf.size());
CodedInputStream coded(&input);
uint64_t value;
ASSERT_TRUE(coded.ReadVarint64(&value));
ASSERT_EQ(value, 18446744071965638648ULL);
}
}

0 comments on commit 9af720d

Please sign in to comment.