Implement full support for non-pointer pointers in custom allocators for string. This completes the custom pointer support for the entire library.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@185167 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant
2013-06-28 16:59:19 +00:00
parent 2c39cbe020
commit 9dcdcdee25
151 changed files with 5125 additions and 645 deletions

View File

@@ -15,6 +15,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -38,7 +39,36 @@ test()
}
}
#if __cplusplus >= 201103L
template <class S>
void
test2()
{
{
S s;
assert(s.__invariants());
assert(s.data());
assert(s.size() == 0);
assert(s.capacity() >= s.size());
assert(s.get_allocator() == typename S::allocator_type());
}
{
S s(typename S::allocator_type{});
assert(s.__invariants());
assert(s.data());
assert(s.size() == 0);
assert(s.capacity() >= s.size());
assert(s.get_allocator() == typename S::allocator_type());
}
}
#endif
int main()
{
test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
#if __cplusplus >= 201103L
test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
#endif
}

View File

@@ -14,6 +14,8 @@
#include <string>
#include <cassert>
#include "../min_allocator.h"
template <class S>
void
test(S s1, typename S::value_type s2)
@@ -28,9 +30,20 @@ test(S s1, typename S::value_type s2)
int main()
{
{
typedef std::string S;
test(S(), 'a');
test(S("1"), 'a');
test(S("123456789"), 'a');
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), 'a');
}
#if __cplusplus >= 201103L
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 'a');
test(S("1"), 'a');
test(S("123456789"), 'a');
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), 'a');
}
#endif
}

View File

@@ -15,6 +15,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -29,9 +30,20 @@ test(S s1)
int main()
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A(3)));
test(S("1", A(5)));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A{}));
test(S("1", A()));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()));
}
#endif
}

View File

@@ -15,6 +15,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -29,9 +30,20 @@ test(S s1, const typename S::allocator_type& a)
int main()
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(), A(3));
test(S("1"), A(5));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(), A());
test(S("1"), A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
}
#endif
}

View File

@@ -15,6 +15,8 @@
#include <string>
#include <cassert>
#include "../min_allocator.h"
template <class S>
void
test(S s1, const S& s2)
@@ -27,6 +29,7 @@ test(S s1, const S& s2)
int main()
{
{
typedef std::string S;
test(S(), S());
test(S("1"), S());
@@ -43,4 +46,25 @@ int main()
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#if __cplusplus >= 201103L
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S());
test(S("1"), S());
test(S(), S("1"));
test(S("1"), S("2"));
test(S("1"), S("2"));
test(S(),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#endif
}

View File

@@ -15,6 +15,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
int main()
{
@@ -28,5 +29,18 @@ int main()
s = {L'a', L'b', L'c'};
assert(s == L"abc");
}
#if __cplusplus >= 201103L
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s = {'a', 'b', 'c'};
assert(s == "abc");
}
{
typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
S s;
s = {L'a', L'b', L'c'};
assert(s == L"abc");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -14,6 +14,8 @@
#include <string>
#include <cassert>
#include "../min_allocator.h"
int main()
{
#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
@@ -22,5 +24,13 @@ int main()
s = {'a', 'b', 'c'};
assert(s == "abc");
}
#if __cplusplus >= 201103L
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s;
s = {'a', 'b', 'c'};
assert(s == "abc");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -19,6 +19,7 @@
#include "../test_allocator.h"
#include "../input_iterator.h"
#include "../min_allocator.h"
template <class It>
void
@@ -38,14 +39,13 @@ test(It first, It last)
assert(s2.capacity() >= s2.size());
}
template <class It>
template <class It, class A>
void
test(It first, It last, const test_allocator<typename std::iterator_traits<It>::value_type>& a)
test(It first, It last, const A& a)
{
typedef typename std::iterator_traits<It>::value_type charT;
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
S s2(first, last, a);
assert(s2.__invariants());
assert(s2.size() == std::distance(first, last));
@@ -58,6 +58,7 @@ test(It first, It last, const test_allocator<typename std::iterator_traits<It>::
int main()
{
{
typedef test_allocator<char> A;
const char* s = "12345678901234567890123456789012345678901234567890";
@@ -84,4 +85,35 @@ int main()
test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A(2));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
const char* s = "12345678901234567890123456789012345678901234567890";
test(s, s);
test(s, s, A());
test(s, s+1);
test(s, s+1, A());
test(s, s+10);
test(s, s+10, A());
test(s, s+50);
test(s, s+50, A());
test(input_iterator<const char*>(s), input_iterator<const char*>(s));
test(input_iterator<const char*>(s), input_iterator<const char*>(s), A());
test(input_iterator<const char*>(s), input_iterator<const char*>(s+1));
test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A());
test(input_iterator<const char*>(s), input_iterator<const char*>(s+10));
test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A());
test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A());
}
#endif
}

View File

@@ -17,6 +17,7 @@
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -36,10 +37,21 @@ test(S s0)
int main()
{
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A(3)));
test(S("1", A(5)));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A{}));
test(S("1", A()));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()));
}
#endif
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
}

View File

@@ -17,6 +17,8 @@
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -36,10 +38,21 @@ test(S s0, const typename S::allocator_type& a)
int main()
{
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(), A(3));
test(S("1"), A(5));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(), A());
test(S("1"), A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
}
#endif
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
}

View File

@@ -18,6 +18,7 @@
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -36,6 +37,7 @@ test(S s1, S s2)
int main()
{
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
{
typedef std::string S;
test(S(), S());
test(S("1"), S());
@@ -52,5 +54,26 @@ int main()
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#if __cplusplus >= 201103L
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S());
test(S("1"), S());
test(S(), S("1"));
test(S("1"), S("2"));
test(S("1"), S("2"));
test(S(),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#endif
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
}

View File

@@ -17,6 +17,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class charT>
void
@@ -34,13 +35,12 @@ test(const charT* s)
assert(s2.capacity() >= s2.size());
}
template <class charT>
template <class charT, class A>
void
test(const charT* s, const test_allocator<charT>& a)
test(const charT* s, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
unsigned n = T::length(s);
S s2(s, a);
assert(s2.__invariants());
@@ -52,6 +52,7 @@ test(const charT* s, const test_allocator<charT>& a)
int main()
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -66,4 +67,23 @@ int main()
test("123456798012345679801234567980123456798012345679801234567980");
test("123456798012345679801234567980123456798012345679801234567980", A(2));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test("");
test("", A());
test("1");
test("1", A());
test("1234567980");
test("1234567980", A());
test("123456798012345679801234567980123456798012345679801234567980");
test("123456798012345679801234567980123456798012345679801234567980", A());
}
#endif
}

View File

@@ -15,6 +15,8 @@
#include <string>
#include <cassert>
#include "../min_allocator.h"
template <class S>
void
test(S s1, const typename S::value_type* s2)
@@ -29,6 +31,7 @@ test(S s1, const typename S::value_type* s2)
int main()
{
{
typedef std::string S;
test(S(), "");
test(S("1"), "");
@@ -45,4 +48,25 @@ int main()
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
}
#if __cplusplus >= 201103L
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), "");
test(S("1"), "");
test(S(), "1");
test(S("1"), "2");
test(S("1"), "2");
test(S(),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("123456789"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
}
#endif
}

View File

@@ -17,6 +17,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class charT>
void
@@ -33,13 +34,12 @@ test(const charT* s, unsigned n)
assert(s2.capacity() >= s2.size());
}
template <class charT>
template <class charT, class A>
void
test(const charT* s, unsigned n, const test_allocator<charT>& a)
test(const charT* s, unsigned n, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
S s2(s, n, a);
assert(s2.__invariants());
assert(s2.size() == n);
@@ -50,6 +50,7 @@ test(const charT* s, unsigned n, const test_allocator<charT>& a)
int main()
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -64,4 +65,23 @@ int main()
test("123456798012345679801234567980123456798012345679801234567980", 60);
test("123456798012345679801234567980123456798012345679801234567980", 60, A(2));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test("", 0);
test("", 0, A());
test("1", 1);
test("1", 1, A());
test("1234567980", 10);
test("1234567980", 10, A());
test("123456798012345679801234567980123456798012345679801234567980", 60);
test("123456798012345679801234567980123456798012345679801234567980", 60, A());
}
#endif
}

View File

@@ -17,6 +17,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class charT>
void
@@ -34,13 +35,12 @@ test(unsigned n, charT c)
assert(s2.capacity() >= s2.size());
}
template <class charT>
template <class charT, class A>
void
test(unsigned n, charT c, const test_allocator<charT>& a)
test(unsigned n, charT c, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
S s2(n, c, a);
assert(s2.__invariants());
assert(s2.size() == n);
@@ -67,14 +67,13 @@ test(Tp n, Tp c)
assert(s2.capacity() >= s2.size());
}
template <class Tp>
template <class Tp, class A>
void
test(Tp n, Tp c, const test_allocator<char>& a)
test(Tp n, Tp c, const A& a)
{
typedef char charT;
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
S s2(n, c, a);
assert(s2.__invariants());
assert(s2.size() == n);
@@ -86,6 +85,7 @@ test(Tp n, Tp c, const test_allocator<char>& a)
int main()
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -103,4 +103,26 @@ int main()
test(100, 65);
test(100, 65, A(3));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(0, 'a');
test(0, 'a', A());
test(1, 'a');
test(1, 'a', A());
test(10, 'a');
test(10, 'a', A());
test(100, 'a');
test(100, 'a', A());
test(100, 65);
test(100, 65, A());
}
#endif
}

View File

@@ -19,6 +19,7 @@
#include <cassert>
#include "../test_allocator.h"
#include "../min_allocator.h"
template <class S>
void
@@ -54,7 +55,7 @@ test(S str, unsigned pos, unsigned n)
S s2(str, pos, n);
assert(s2.__invariants());
assert(pos <= str.size());
unsigned rlen = std::min(str.size() - pos, n);
unsigned rlen = std::min<unsigned>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
@@ -77,7 +78,7 @@ test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
S s2(str, pos, n, a);
assert(s2.__invariants());
assert(pos <= str.size());
unsigned rlen = std::min(str.size() - pos, n);
unsigned rlen = std::min<unsigned>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == a);
@@ -91,6 +92,7 @@ test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
int main()
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -127,4 +129,45 @@ int main()
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
}
#if __cplusplus >= 201103L
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A()), 0);
test(S(A()), 1);
test(S("1", A()), 0);
test(S("1", A()), 1);
test(S("1", A()), 2);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
test(S(A()), 0, 0);
test(S(A()), 0, 1);
test(S(A()), 1, 0);
test(S(A()), 1, 1);
test(S(A()), 1, 2);
test(S("1", A()), 0, 0);
test(S("1", A()), 0, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
test(S(A()), 0, 0, A());
test(S(A()), 0, 1, A());
test(S(A()), 1, 0, A());
test(S(A()), 1, 1, A());
test(S(A()), 1, 2, A());
test(S("1", A()), 0, 0, A());
test(S("1", A()), 0, 1, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
}
#endif
}