]> git.proxmox.com Git - ceph.git/blame - ceph/src/rapidjson/test/unittest/itoatest.cpp
buildsys: change download over to reef release
[ceph.git] / ceph / src / rapidjson / test / unittest / itoatest.cpp
CommitLineData
31f18b77
FG
1// Tencent is pleased to support the open source community by making RapidJSON available.
2//
3// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4//
5// Licensed under the MIT License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// http://opensource.org/licenses/MIT
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "unittest.h"
16#include "rapidjson/internal/itoa.h"
17
18#ifdef __GNUC__
19RAPIDJSON_DIAG_PUSH
20RAPIDJSON_DIAG_OFF(type-limits)
21#endif
22
23using namespace rapidjson::internal;
24
25template <typename T>
26struct Traits {
27};
28
29template <>
30struct Traits<uint32_t> {
31 enum { kBufferSize = 11 };
32 enum { kMaxDigit = 10 };
33 static uint32_t Negate(uint32_t x) { return x; }
34};
35
36template <>
37struct Traits<int32_t> {
38 enum { kBufferSize = 12 };
39 enum { kMaxDigit = 10 };
40 static int32_t Negate(int32_t x) { return -x; }
41};
42
43template <>
44struct Traits<uint64_t> {
45 enum { kBufferSize = 21 };
46 enum { kMaxDigit = 20 };
47 static uint64_t Negate(uint64_t x) { return x; }
48};
49
50template <>
51struct Traits<int64_t> {
52 enum { kBufferSize = 22 };
53 enum { kMaxDigit = 20 };
54 static int64_t Negate(int64_t x) { return -x; }
55};
56
57template <typename T>
58static void VerifyValue(T value, void(*f)(T, char*), char* (*g)(T, char*)) {
59 char buffer1[Traits<T>::kBufferSize];
60 char buffer2[Traits<T>::kBufferSize];
61
62 f(value, buffer1);
63 *g(value, buffer2) = '\0';
64
65
66 EXPECT_STREQ(buffer1, buffer2);
67}
68
69template <typename T>
70static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) {
71 // Boundary cases
72 VerifyValue<T>(0, f, g);
73 VerifyValue<T>(std::numeric_limits<T>::min(), f, g);
74 VerifyValue<T>(std::numeric_limits<T>::max(), f, g);
75
76 // 2^n - 1, 2^n, 10^n - 1, 10^n until overflow
77 for (uint32_t power = 2; power <= 10; power += 8) {
78 T i = 1, last;
79 do {
80 VerifyValue<T>(i - 1, f, g);
81 VerifyValue<T>(i, f, g);
82 if (std::numeric_limits<T>::min() < 0) {
83 VerifyValue<T>(Traits<T>::Negate(i), f, g);
84 VerifyValue<T>(Traits<T>::Negate(i + 1), f, g);
85 }
86 last = i;
87 if (i > static_cast<T>(std::numeric_limits<T>::max() / static_cast<T>(power)))
88 break;
89 i *= power;
90 } while (last < i);
91 }
92}
93
94static void u32toa_naive(uint32_t value, char* buffer) {
95 char temp[10];
96 char *p = temp;
97 do {
98 *p++ = static_cast<char>(char(value % 10) + '0');
99 value /= 10;
100 } while (value > 0);
101
102 do {
103 *buffer++ = *--p;
104 } while (p != temp);
105
106 *buffer = '\0';
107}
108
109static void i32toa_naive(int32_t value, char* buffer) {
110 uint32_t u = static_cast<uint32_t>(value);
111 if (value < 0) {
112 *buffer++ = '-';
113 u = ~u + 1;
114 }
115 u32toa_naive(u, buffer);
116}
117
118static void u64toa_naive(uint64_t value, char* buffer) {
119 char temp[20];
120 char *p = temp;
121 do {
122 *p++ = static_cast<char>(char(value % 10) + '0');
123 value /= 10;
124 } while (value > 0);
125
126 do {
127 *buffer++ = *--p;
128 } while (p != temp);
129
130 *buffer = '\0';
131}
132
133static void i64toa_naive(int64_t value, char* buffer) {
134 uint64_t u = static_cast<uint64_t>(value);
135 if (value < 0) {
136 *buffer++ = '-';
137 u = ~u + 1;
138 }
139 u64toa_naive(u, buffer);
140}
141
142TEST(itoa, u32toa) {
143 Verify(u32toa_naive, u32toa);
144}
145
146TEST(itoa, i32toa) {
147 Verify(i32toa_naive, i32toa);
148}
149
150TEST(itoa, u64toa) {
151 Verify(u64toa_naive, u64toa);
152}
153
154TEST(itoa, i64toa) {
155 Verify(i64toa_naive, i64toa);
156}
157
158#ifdef __GNUC__
159RAPIDJSON_DIAG_POP
160#endif