diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index 567eea5..c5610cc 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -446,11 +446,19 @@ namespace TinySTL{ namespace { template void _advance(InputIterator& it, Distance n, input_iterator_tag){ + assert(n >= 0); + while (n--){ + ++it; + } + } + template + void _advance(BidirectionIterator& it, Distance n, bidirectional_iterator_tag){ if (n < 0){ while (n++){ --it; } - }else{ + } + else{ while (n--){ ++it; } diff --git a/TinySTL/Test/AlgorithmTest.cpp b/TinySTL/Test/AlgorithmTest.cpp index 9bdd899..79b6afd 100644 --- a/TinySTL/Test/AlgorithmTest.cpp +++ b/TinySTL/Test/AlgorithmTest.cpp @@ -170,16 +170,20 @@ namespace TinySTL{ void testAdvance(){ TinySTL::vector v; TinySTL::list l; + TinySTL::binary_search_tree bst; for (auto i = 0; i != 10; ++i){ v.push_back(i); l.push_back(i); + bst.insert(i); } auto vit = v.begin(); auto lit = l.begin(); + auto bit = bst.cbegin(); TinySTL::advance(vit, 5); TinySTL::advance(lit, 5); - assert(*vit == 5 && *lit == 5); + TinySTL::advance(bit, 5); + assert(*vit == 5 && *lit == 5 && *bit == 5); TinySTL::advance(vit, -5); TinySTL::advance(lit, -5); diff --git a/TinySTL/Test/AlgorithmTest.h b/TinySTL/Test/AlgorithmTest.h index 775acd0..33c13a7 100644 --- a/TinySTL/Test/AlgorithmTest.h +++ b/TinySTL/Test/AlgorithmTest.h @@ -13,6 +13,7 @@ #include #include +#include "../BinarySearchTree.h" #include "../List.h" #include "../Vector.h"