1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <string>
10
11// string to_string(int val);
12// string to_string(unsigned val);
13// string to_string(long val);
14// string to_string(unsigned long val);
15// string to_string(long long val);
16// string to_string(unsigned long long val);
17// string to_string(float val);
18// string to_string(double val);
19// string to_string(long double val);
20
21#include <string>
22#include <cassert>
23#include <limits>
24
25#include "parse_integer.h"
26#include "test_macros.h"
27
28template <class T>
29void test_signed() {
30 {
31 std::string s = std::to_string(T(0));
32 assert(s.size() == 1);
33 assert(s[s.size()] == 0);
34 assert(s == "0");
35 }
36 {
37 std::string s = std::to_string(T(12345));
38 assert(s.size() == 5);
39 assert(s[s.size()] == 0);
40 assert(s == "12345");
41 }
42 {
43 std::string s = std::to_string(T(-12345));
44 assert(s.size() == 6);
45 assert(s[s.size()] == 0);
46 assert(s == "-12345");
47 }
48 {
49 std::string s = std::to_string(std::numeric_limits<T>::max());
50 assert(s.size() == std::numeric_limits<T>::digits10 + 1);
51 T t = parse_integer<T>(s);
52 assert(t == std::numeric_limits<T>::max());
53 }
54 {
55 std::string s = std::to_string(std::numeric_limits<T>::min());
56 T t = parse_integer<T>(s);
57 assert(t == std::numeric_limits<T>::min());
58 }
59}
60
61template <class T>
62void test_unsigned() {
63 {
64 std::string s = std::to_string(T(0));
65 assert(s.size() == 1);
66 assert(s[s.size()] == 0);
67 assert(s == "0");
68 }
69 {
70 std::string s = std::to_string(T(12345));
71 assert(s.size() == 5);
72 assert(s[s.size()] == 0);
73 assert(s == "12345");
74 }
75 {
76 std::string s = std::to_string(std::numeric_limits<T>::max());
77 assert(s.size() == std::numeric_limits<T>::digits10 + 1);
78 T t = parse_integer<T>(s);
79 assert(t == std::numeric_limits<T>::max());
80 }
81}
82
83template <class T>
84void test_float() {
85 {
86 std::string s = std::to_string(T(0));
87 assert(s.size() == 8);
88 assert(s[s.size()] == 0);
89 assert(s == "0.000000");
90 }
91 {
92 std::string s = std::to_string(T(12345));
93 assert(s.size() == 12);
94 assert(s[s.size()] == 0);
95 assert(s == "12345.000000");
96 }
97 {
98 std::string s = std::to_string(T(-12345));
99 assert(s.size() == 13);
100 assert(s[s.size()] == 0);
101 assert(s == "-12345.000000");
102 }
103}
104
105int main(int, char**) {
106 test_signed<int>();
107 test_signed<long>();
108 test_signed<long long>();
109 test_unsigned<unsigned>();
110 test_unsigned<unsigned long>();
111 test_unsigned<unsigned long long>();
112 test_float<float>();
113 test_float<double>();
114 test_float<long double>();
115
116 return 0;
117}
118

source code of libcxx/test/std/strings/string.conversions/to_string.pass.cpp