[libcxx-commits] [libcxx] cfa1376 - [libc++] Get rid of iostreams in the to_string tests
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 27 10:36:23 PDT 2020
Author: Louis Dionne
Date: 2020-10-27T13:36:13-04:00
New Revision: cfa1376a012bac9cf573a73639d08024512e2346
URL: https://github.com/llvm/llvm-project/commit/cfa1376a012bac9cf573a73639d08024512e2346
DIFF: https://github.com/llvm/llvm-project/commit/cfa1376a012bac9cf573a73639d08024512e2346.diff
LOG: [libc++] Get rid of iostreams in the to_string tests
Added:
libcxx/test/support/parse_integer.h
Modified:
libcxx/test/std/strings/string.conversions/to_string.pass.cpp
libcxx/test/std/strings/string.conversions/to_wstring.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/test/std/strings/string.conversions/to_string.pass.cpp b/libcxx/test/std/strings/string.conversions/to_string.pass.cpp
index 4dafcabdba8e..3de0232e796e 100644
--- a/libcxx/test/std/strings/string.conversions/to_string.pass.cpp
+++ b/libcxx/test/std/strings/string.conversions/to_string.pass.cpp
@@ -18,11 +18,11 @@
// string to_string(double val);
// string to_string(long double val);
-#include <limits>
#include <string>
#include <cassert>
-#include <sstream>
+#include <limits>
+#include "parse_integer.h"
#include "test_macros.h"
template <class T>
@@ -50,16 +50,12 @@ test_signed()
{
std::string s = std::to_string(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
- std::istringstream is(s);
- T t(0);
- is >> t;
+ T t = parse_integer<T>(s);
assert(t == std::numeric_limits<T>::max());
}
{
std::string s = std::to_string(std::numeric_limits<T>::min());
- std::istringstream is(s);
- T t(0);
- is >> t;
+ T t = parse_integer<T>(s);
assert(t == std::numeric_limits<T>::min());
}
}
@@ -83,9 +79,7 @@ test_unsigned()
{
std::string s = std::to_string(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
- std::istringstream is(s);
- T t(0);
- is >> t;
+ T t = parse_integer<T>(s);
assert(t == std::numeric_limits<T>::max());
}
}
diff --git a/libcxx/test/std/strings/string.conversions/to_wstring.pass.cpp b/libcxx/test/std/strings/string.conversions/to_wstring.pass.cpp
index 53ef0789a554..f3b4712f341b 100644
--- a/libcxx/test/std/strings/string.conversions/to_wstring.pass.cpp
+++ b/libcxx/test/std/strings/string.conversions/to_wstring.pass.cpp
@@ -18,11 +18,11 @@
// wstring to_wstring(double val);
// wstring to_wstring(long double val);
-#include <limits>
#include <string>
#include <cassert>
-#include <sstream>
+#include <limits>
+#include "parse_integer.h"
#include "test_macros.h"
template <class T>
@@ -50,16 +50,12 @@ test_signed()
{
std::wstring s = std::to_wstring(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
- std::wistringstream is(s);
- T t(0);
- is >> t;
+ T t = parse_integer<T>(s);
assert(t == std::numeric_limits<T>::max());
}
{
std::wstring s = std::to_wstring(std::numeric_limits<T>::min());
- std::wistringstream is(s);
- T t(0);
- is >> t;
+ T t = parse_integer<T>(s);
assert(t == std::numeric_limits<T>::min());
}
}
@@ -83,9 +79,7 @@ test_unsigned()
{
std::wstring s = std::to_wstring(std::numeric_limits<T>::max());
assert(s.size() == std::numeric_limits<T>::digits10 + 1);
- std::wistringstream is(s);
- T t(0);
- is >> t;
+ T t = parse_integer<T>(s);
assert(t == std::numeric_limits<T>::max());
}
}
diff --git a/libcxx/test/support/parse_integer.h b/libcxx/test/support/parse_integer.h
new file mode 100644
index 000000000000..f985063b7822
--- /dev/null
+++ b/libcxx/test/support/parse_integer.h
@@ -0,0 +1,72 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H
+#define LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H
+
+#include <string>
+
+namespace detail {
+template <class T>
+struct parse_integer_impl;
+
+template <>
+struct parse_integer_impl<int> {
+ template <class CharT>
+ int operator()(std::basic_string<CharT> const& str) const {
+ return std::stoi(str);
+ }
+};
+
+template <>
+struct parse_integer_impl<long> {
+ template <class CharT>
+ long operator()(std::basic_string<CharT> const& str) const {
+ return std::stol(str);
+ }
+};
+
+template <>
+struct parse_integer_impl<long long> {
+ template <class CharT>
+ long long operator()(std::basic_string<CharT> const& str) const {
+ return std::stoll(str);
+ }
+};
+
+template <>
+struct parse_integer_impl<unsigned int> {
+ template <class CharT>
+ unsigned int operator()(std::basic_string<CharT> const& str) const {
+ return std::stoul(str);
+ }
+};
+
+template <>
+struct parse_integer_impl<unsigned long> {
+ template <class CharT>
+ unsigned long operator()(std::basic_string<CharT> const& str) const {
+ return std::stoul(str);
+ }
+};
+
+template <>
+struct parse_integer_impl<unsigned long long> {
+ template <class CharT>
+ unsigned long long operator()(std::basic_string<CharT> const& str) const {
+ return std::stoull(str);
+ }
+};
+} // end namespace detail
+
+template <class T, class CharT>
+T parse_integer(std::basic_string<CharT> const& str) {
+ return detail::parse_integer_impl<T>()(str);
+}
+
+#endif // LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H
More information about the libcxx-commits
mailing list