Skip to content

Commit 1564461

Browse files
committed
Merge pull request #15 from gfx/issue14
add check code for #14
2 parents e1a3dd1 + 71ae327 commit 1564461

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ test-with-cpp11: test/test.cpp timsort.hpp .bin
2828
$(COMPILE) -std=c++11 $< $(LIB_BOOST_TEST) -o .bin/$@
2929
time ./.bin/$@
3030

31+
test-issue14: valgrind/issue14.cpp
32+
$(COMPILE) -DENABLE_TIMSORT_LOG -std=c++11 -fsanitize=address $< -o .bin/$@
33+
./.bin/$@
34+
3135
bench: example/bench.cpp timsort.hpp .bin
3236
$(CXX) -v
3337
$(COMPILE) $(OPTIMIZE) -std=c++11 $< -o .bin/$@

test/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <vector>
3+
#include <deque>
34
#include <algorithm>
45
#include <utility>
56

@@ -607,4 +608,13 @@ BOOST_AUTO_TEST_CASE(shuffle10k_for_move_only_types) {
607608
}
608609
}
609610

611+
BOOST_AUTO_TEST_CASE(issue14) {
612+
int a[] = {15, 7, 16, 20, 25, 28, 13, 27, 34, 24, 19, 1, 6, 30, 32, 29, 10, 9,
613+
3, 31, 21, 26, 8, 2, 22, 14, 4, 12, 5, 0, 23, 33, 11, 17, 18};
614+
std::deque<int> c(std::begin(a), std::end(a));
615+
616+
gfx::timsort(std::begin(c), std::end(c));
617+
BOOST_CHECK(std::is_sorted(std::begin(c), std::end(c)));
618+
}
619+
610620
#endif // if std::move available

timsort.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,7 @@ template <typename RandomAccessIterator, typename LessFunction> class TimSort {
626626
if (len2 == 1) {
627627
assert(len1 > 0);
628628
dest -= len1;
629-
cursor1 -= len1;
630-
GFX_TIMSORT_MOVE_BACKWARD(cursor1 + 1, cursor1 + (1 + len1), dest + (1 + len1));
629+
GFX_TIMSORT_MOVE_BACKWARD(cursor1 + (1 - len1), cursor1 + 1, dest + (1 + len1));
631630
*dest = GFX_TIMSORT_MOVE(*cursor2);
632631
} else {
633632
assert(len2 != 0 && "Comparison function violates its general contract");

valgrind/issue14.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "timsort.hpp"
2+
#include <deque>
3+
#include <cstdlib>
4+
5+
int main() {
6+
std::deque<int> collection = {15, 7, 16, 20, 25, 28, 13, 27, 34, 24, 19, 1, 6, 30, 32, 29, 10, 9,
7+
3, 31, 21, 26, 8, 2, 22, 14, 4, 12, 5, 0, 23, 33, 11, 17, 18};
8+
9+
gfx::timsort(std::begin(collection), std::end(collection));
10+
assert(std::is_sorted(std::begin(collection), std::end(collection)));
11+
12+
return 0;
13+
}

0 commit comments

Comments
 (0)