Fixed bug in a timsort algorithm.

This commit is contained in:
Yuri Gorshenin 2015-02-27 11:50:48 +03:00 committed by Alex Zolotarev
parent ceb09ce7b7
commit 291b5dae75
3 changed files with 72 additions and 37 deletions

View file

@ -12,46 +12,47 @@ include($$ROOT_DIR/common.pri)
win32-g++: LIBS *= -lpthread
SOURCES += ../../testing/testingmain.cpp \
endianness_test.cpp \
varint_test.cpp \
mem_file_reader_test.cpp \
mem_file_writer_test.cpp \
var_serial_vector_test.cpp \
hex_test.cpp \
dd_vector_test.cpp \
diff_test.cpp \
png_decoder_test.cpp \
reader_test.cpp \
writer_test.cpp \
var_record_reader_test.cpp \
file_sort_test.cpp \
reader_cache_test.cpp \
file_container_test.cpp \
url_encode_test.cpp \
bzip2_test.cpp \
gzip_test.cpp \
coder_util_test.cpp \
bit_shift_test.cpp \
base64_test.cpp \
base64_for_user_id_test.cpp \
sha2_test.cpp \
value_opt_string_test.cpp \
multilang_utf8_string_test.cpp \
file_data_test.cpp \
zip_reader_test.cpp \
trie_test.cpp \
reader_writer_ops_test.cpp \
blob_storage_test.cpp \
uri_test.cpp \
zip_creator_test.cpp \
file_utils_test.cpp \
varint_vector_test.cpp \
arithmetic_codec_test.cpp \
base64_for_user_id_test.cpp \
base64_test.cpp \
bit_shift_test.cpp \
bit_streams_test.cpp \
blob_storage_test.cpp \
bzip2_test.cpp \
coder_util_test.cpp \
compressed_bit_vector_test.cpp \
compressed_varnum_vector_test.cpp \
bit_streams_test.cpp \
dd_vector_test.cpp \
diff_test.cpp \
endianness_test.cpp \
file_container_test.cpp \
file_data_test.cpp \
file_sort_test.cpp \
file_utils_test.cpp \
gzip_test.cpp \
hex_test.cpp \
mem_file_reader_test.cpp \
mem_file_writer_test.cpp \
multilang_utf8_string_test.cpp \
png_decoder_test.cpp \
reader_cache_test.cpp \
reader_test.cpp \
reader_writer_ops_test.cpp \
sha2_test.cpp \
timsort_test.cpp \
trie_test.cpp \
uri_test.cpp \
url_encode_test.cpp \
value_opt_string_test.cpp \
var_record_reader_test.cpp \
var_serial_vector_test.cpp \
varint_test.cpp \
varint_vector_test.cpp \
writer_test.cpp \
zip_creator_test.cpp \
zip_reader_test.cpp \
HEADERS += \
reader_test.hpp \
coder_test.hpp \
compressor_test_utils.hpp \
reader_test.hpp \

View file

@ -0,0 +1,33 @@
#include "../../testing/testing.hpp"
#include "../timsort/timsort.hpp"
#include "../../std/algorithm.hpp"
#include "../../std/vector.hpp"
int intCmp(void const * plhs, void const * prhs)
{
int const lhs = *static_cast<int const *>(plhs);
int const rhs = *static_cast<int const *>(prhs);
if (lhs < rhs)
return -1;
if (lhs == rhs)
return 0;
return 1;
}
UNIT_TEST(TimSort_Empty) {
vector<int> buffer;
timsort(&buffer[0], 0, sizeof(int), intCmp);
}
UNIT_TEST(Timsort_Simple) {
vector<int> buffer = { 5, 1, 0, 7, 9, 10, 1, 3, 4 };
vector<int> sorted_buffer(buffer);
sort(sorted_buffer.begin(), sorted_buffer.end());
timsort(&buffer[0], buffer.size(), sizeof(int), intCmp);
TEST_EQUAL(sorted_buffer, buffer, ());
}

View file

@ -1183,7 +1183,8 @@ static void timMergeCollapse(timMergeState *aState, cmpFunc *aCmpCb)
{
n = aState->mPendingRunCnt - 2;
if (n > 0 && sSlice[n-1].mLen <= sSlice[n].mLen + sSlice[n+1].mLen)
if ((n > 0 && sSlice[n-1].mLen <= sSlice[n].mLen + sSlice[n+1].mLen) ||
(n-1 > 0 && sSlice[n-2].mLen <= sSlice[n].mLen + sSlice[n-1].mLen))
{
if (sSlice[n-1].mLen < sSlice[n+1].mLen)
{