Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / strings / string.conversions / to_string.pass.cpp
blob4731a072e89108f7913297f3bf4a2e14a385f0c1
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 //===----------------------------------------------------------------------===//
9 // <string>
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);
21 #include <string>
22 #include <cassert>
23 #include <limits>
25 #include "parse_integer.h"
26 #include "test_macros.h"
28 template <class T>
29 void test_signed() {
31 std::string s = std::to_string(T(0));
32 assert(s.size() == 1);
33 assert(s[s.size()] == 0);
34 assert(s == "0");
37 std::string s = std::to_string(T(12345));
38 assert(s.size() == 5);
39 assert(s[s.size()] == 0);
40 assert(s == "12345");
43 std::string s = std::to_string(T(-12345));
44 assert(s.size() == 6);
45 assert(s[s.size()] == 0);
46 assert(s == "-12345");
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());
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());
61 template <class T>
62 void test_unsigned() {
64 std::string s = std::to_string(T(0));
65 assert(s.size() == 1);
66 assert(s[s.size()] == 0);
67 assert(s == "0");
70 std::string s = std::to_string(T(12345));
71 assert(s.size() == 5);
72 assert(s[s.size()] == 0);
73 assert(s == "12345");
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());
83 template <class T>
84 void test_float() {
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");
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");
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");
105 int 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>();
116 return 0;