diff options
Diffstat (limited to 'libcxx/test/std/strings/basic.string/string.capacity')
11 files changed, 698 insertions, 0 deletions
diff --git a/libcxx/test/std/strings/basic.string/string.capacity/capacity.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/capacity.pass.cpp new file mode 100644 index 00000000000..bae76215552 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/capacity.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// size_type capacity() const; + +#include <string> +#include <cassert> + +#include "test_allocator.h" +#include "min_allocator.h" + +template <class S> +void +test(S s) +{ + S::allocator_type::throw_after = 0; + try + { + while (s.size() < s.capacity()) + s.push_back(typename S::value_type()); + assert(s.size() == s.capacity()); + } + catch (...) + { + assert(false); + } + S::allocator_type::throw_after = INT_MAX; +} + +int main() +{ + { + typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S; + S s; + test(s); + s.assign(10, 'a'); + s.erase(5); + test(s); + s.assign(100, 'a'); + s.erase(50); + test(s); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + S s; + assert(s.capacity() > 0); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/clear.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/clear.pass.cpp new file mode 100644 index 00000000000..b73af759934 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/clear.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// void clear(); + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(S s) +{ + s.clear(); + assert(s.size() == 0); +} + +int main() +{ + { + typedef std::string S; + S s; + test(s); + + s.assign(10, 'a'); + s.erase(5); + test(s); + + s.assign(100, 'a'); + s.erase(50); + test(s); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + S s; + test(s); + + s.assign(10, 'a'); + s.erase(5); + test(s); + + s.assign(100, 'a'); + s.erase(50); + test(s); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/empty.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/empty.pass.cpp new file mode 100644 index 00000000000..ac65f514485 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/empty.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// bool empty() const; + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(const S& s) +{ + assert(s.empty() == (s.size() == 0)); +} + +int main() +{ + { + typedef std::string S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/length.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/length.pass.cpp new file mode 100644 index 00000000000..d3ae1aaee03 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/length.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// size_type length() const; + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(const S& s) +{ + assert(s.length() == s.size()); +} + +int main() +{ + { + typedef std::string S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/max_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/max_size.pass.cpp new file mode 100644 index 00000000000..f42642563bd --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/max_size.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// size_type max_size() const; + +// NOTE: asan and msan will fail for one of two reasons +// 1. If allocator_may_return_null=0 then they will fail because the allocation +// returns null. +// 2. If allocator_may_return_null=1 then they will fail because the allocation +// is too large to succeed. +// UNSUPPORTED: asan, msan + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test1(const S& s) +{ + S s2(s); + const size_t sz = s2.max_size() - 1; + try { s2.resize(sz, 'x'); } + catch ( const std::bad_alloc & ) { return ; } + assert ( s2.size() == sz ); +} + +template <class S> +void +test2(const S& s) +{ + S s2(s); + const size_t sz = s2.max_size(); + try { s2.resize(sz, 'x'); } + catch ( const std::bad_alloc & ) { return ; } + assert ( s.size() == sz ); +} + +template <class S> +void +test(const S& s) +{ + assert(s.max_size() >= s.size()); + test1(s); + test2(s); +} + +int main() +{ + { + typedef std::string S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/over_max_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/over_max_size.pass.cpp new file mode 100644 index 00000000000..bbadb9ccc5a --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/over_max_size.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// size_type max_size() const; + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(const S& s) +{ + assert(s.max_size() >= s.size()); + S s2(s); + const size_t sz = s2.max_size() + 1; + try { s2.resize(sz, 'x'); } + catch ( const std::length_error & ) { return ; } + assert ( false ); +} + +int main() +{ + { + typedef std::string S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S()); + test(S("123")); + test(S("12345678901234567890123456789012345678901234567890")); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp new file mode 100644 index 00000000000..a155825be75 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp @@ -0,0 +1,117 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// void reserve(size_type res_arg=0); + +#include <string> +#include <stdexcept> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(S s) +{ + typename S::size_type old_cap = s.capacity(); + S s0 = s; + s.reserve(); + assert(s.__invariants()); + assert(s == s0); + assert(s.capacity() <= old_cap); + assert(s.capacity() >= s.size()); +} + +template <class S> +void +test(S s, typename S::size_type res_arg) +{ + typename S::size_type old_cap = s.capacity(); + S s0 = s; + try + { + s.reserve(res_arg); + assert(res_arg <= s.max_size()); + assert(s == s0); + assert(s.capacity() >= res_arg); + assert(s.capacity() >= s.size()); + } + catch (std::length_error&) + { + assert(res_arg > s.max_size()); + } +} + +int main() +{ + { + typedef std::string S; + { + S s; + test(s); + + s.assign(10, 'a'); + s.erase(5); + test(s); + + s.assign(100, 'a'); + s.erase(50); + test(s); + } + { + S s; + test(s, 5); + test(s, 10); + test(s, 50); + } + { + S s(100, 'a'); + s.erase(50); + test(s, 5); + test(s, 10); + test(s, 50); + test(s, 100); + test(s, S::npos); + } + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + { + S s; + test(s); + + s.assign(10, 'a'); + s.erase(5); + test(s); + + s.assign(100, 'a'); + s.erase(50); + test(s); + } + { + S s; + test(s, 5); + test(s, 10); + test(s, 50); + } + { + S s(100, 'a'); + s.erase(50); + test(s, 5); + test(s, 10); + test(s, 50); + test(s, 100); + test(s, S::npos); + } + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/resize_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/resize_size.pass.cpp new file mode 100644 index 00000000000..14fe209fd59 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/resize_size.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// void resize(size_type n); + +#include <string> +#include <stdexcept> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(S s, typename S::size_type n, S expected) +{ + try + { + s.resize(n); + assert(s.__invariants()); + assert(n <= s.max_size()); + assert(s == expected); + } + catch (std::length_error&) + { + assert(n > s.max_size()); + } +} + +int main() +{ + { + typedef std::string S; + test(S(), 0, S()); + test(S(), 1, S(1, '\0')); + test(S(), 10, S(10, '\0')); + test(S(), 100, S(100, '\0')); + test(S("12345"), 0, S()); + test(S("12345"), 2, S("12")); + test(S("12345"), 5, S("12345")); + test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15)); + test(S("12345678901234567890123456789012345678901234567890"), 0, S()); + test(S("12345678901234567890123456789012345678901234567890"), 10, + S("1234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 50, + S("12345678901234567890123456789012345678901234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 60, + S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); + test(S(), S::npos, S("not going to happen")); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S(), 0, S()); + test(S(), 1, S(1, '\0')); + test(S(), 10, S(10, '\0')); + test(S(), 100, S(100, '\0')); + test(S("12345"), 0, S()); + test(S("12345"), 2, S("12")); + test(S("12345"), 5, S("12345")); + test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15)); + test(S("12345678901234567890123456789012345678901234567890"), 0, S()); + test(S("12345678901234567890123456789012345678901234567890"), 10, + S("1234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 50, + S("12345678901234567890123456789012345678901234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 60, + S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); + test(S(), S::npos, S("not going to happen")); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/resize_size_char.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/resize_size_char.pass.cpp new file mode 100644 index 00000000000..f293df971f2 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/resize_size_char.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// void resize(size_type n, charT c); + +#include <string> +#include <stdexcept> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(S s, typename S::size_type n, typename S::value_type c, S expected) +{ + try + { + s.resize(n, c); + assert(s.__invariants()); + assert(n <= s.max_size()); + assert(s == expected); + } + catch (std::length_error&) + { + assert(n > s.max_size()); + } +} + +int main() +{ + { + typedef std::string S; + test(S(), 0, 'a', S()); + test(S(), 1, 'a', S("a")); + test(S(), 10, 'a', S(10, 'a')); + test(S(), 100, 'a', S(100, 'a')); + test(S("12345"), 0, 'a', S()); + test(S("12345"), 2, 'a', S("12")); + test(S("12345"), 5, 'a', S("12345")); + test(S("12345"), 15, 'a', S("12345aaaaaaaaaa")); + test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S()); + test(S("12345678901234567890123456789012345678901234567890"), 10, 'a', + S("1234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 50, 'a', + S("12345678901234567890123456789012345678901234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 60, 'a', + S("12345678901234567890123456789012345678901234567890aaaaaaaaaa")); + test(S(), S::npos, 'a', S("not going to happen")); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S(), 0, 'a', S()); + test(S(), 1, 'a', S("a")); + test(S(), 10, 'a', S(10, 'a')); + test(S(), 100, 'a', S(100, 'a')); + test(S("12345"), 0, 'a', S()); + test(S("12345"), 2, 'a', S("12")); + test(S("12345"), 5, 'a', S("12345")); + test(S("12345"), 15, 'a', S("12345aaaaaaaaaa")); + test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S()); + test(S("12345678901234567890123456789012345678901234567890"), 10, 'a', + S("1234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 50, 'a', + S("12345678901234567890123456789012345678901234567890")); + test(S("12345678901234567890123456789012345678901234567890"), 60, 'a', + S("12345678901234567890123456789012345678901234567890aaaaaaaaaa")); + test(S(), S::npos, 'a', S("not going to happen")); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp new file mode 100644 index 00000000000..aacbffd098f --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// void shrink_to_fit(); + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(S s) +{ + typename S::size_type old_cap = s.capacity(); + S s0 = s; + s.shrink_to_fit(); + assert(s.__invariants()); + assert(s == s0); + assert(s.capacity() <= old_cap); + assert(s.capacity() >= s.size()); +} + +int main() +{ + { + typedef std::string S; + S s; + test(s); + + s.assign(10, 'a'); + s.erase(5); + test(s); + + s.assign(100, 'a'); + s.erase(50); + test(s); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + S s; + test(s); + + s.assign(10, 'a'); + s.erase(5); + test(s); + + s.assign(100, 'a'); + s.erase(50); + test(s); + } +#endif +} diff --git a/libcxx/test/std/strings/basic.string/string.capacity/size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/size.pass.cpp new file mode 100644 index 00000000000..21b475a8113 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.capacity/size.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// size_type size() const; + +#include <string> +#include <cassert> + +#include "min_allocator.h" + +template <class S> +void +test(const S& s, typename S::size_type c) +{ + assert(s.size() == c); +} + +int main() +{ + { + typedef std::string S; + test(S(), 0); + test(S("123"), 3); + test(S("12345678901234567890123456789012345678901234567890"), 50); + } +#if __cplusplus >= 201103L + { + typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; + test(S(), 0); + test(S("123"), 3); + test(S("12345678901234567890123456789012345678901234567890"), 50); + } +#endif +} |