itoatest.cpp
Go to the documentation of this file.
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"
17 
18 #ifdef __GNUC__
19 RAPIDJSON_DIAG_PUSH
20 RAPIDJSON_DIAG_OFF(type-limits)
21 #endif
22 
23 using namespace rapidjson::internal;
24 
25 template <typename T>
26 struct Traits {
27 };
28 
29 template <>
30 struct Traits<uint32_t> {
31  enum { kBufferSize = 11 };
32  enum { kMaxDigit = 10 };
33  static uint32_t Negate(uint32_t x) { return x; }
34 };
35 
36 template <>
37 struct Traits<int32_t> {
38  enum { kBufferSize = 12 };
39  enum { kMaxDigit = 10 };
40  static int32_t Negate(int32_t x) { return -x; }
41 };
42 
43 template <>
44 struct Traits<uint64_t> {
45  enum { kBufferSize = 21 };
46  enum { kMaxDigit = 20 };
47  static uint64_t Negate(uint64_t x) { return x; }
48 };
49 
50 template <>
51 struct Traits<int64_t> {
52  enum { kBufferSize = 22 };
53  enum { kMaxDigit = 20 };
54  static int64_t Negate(int64_t x) { return -x; }
55 };
56 
57 template <typename T>
58 static 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 
69 template <typename T>
70 static 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 (int 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 *= static_cast<T>(power);
90  } while (last < i);
91  }
92 }
93 
94 static 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 
109 static 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 
118 static 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 
133 static 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 
142 TEST(itoa, u32toa) {
144 }
145 
146 TEST(itoa, i32toa) {
148 }
149 
150 TEST(itoa, u64toa) {
152 }
153 
154 TEST(itoa, i64toa) {
156 }
157 
158 #ifdef __GNUC__
159 RAPIDJSON_DIAG_POP
160 #endif
static void u64toa_naive(uint64_t value, char *buffer)
Definition: itoatest.cpp:118
char * u64toa(uint64_t value, char *buffer)
Definition: itoa.h:123
static uint32_t Negate(uint32_t x)
Definition: itoatest.cpp:33
static void VerifyValue(T value, void(*f)(T, char *), char *(*g)(T, char *))
Definition: itoatest.cpp:58
f
static uint64_t Negate(uint64_t x)
Definition: itoatest.cpp:47
static int32_t Negate(int32_t x)
Definition: itoatest.cpp:40
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
static void i64toa_naive(int64_t value, char *buffer)
Definition: itoatest.cpp:133
static void i32toa_naive(int32_t value, char *buffer)
Definition: itoatest.cpp:109
char * u32toa(uint32_t value, char *buffer)
Definition: itoa.h:39
#define T(x)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1222
signed __int64 int64_t
Definition: stdint.h:135
static int64_t Negate(int64_t x)
Definition: itoatest.cpp:54
static void Verify(void(*f)(T, char *), char *(*g)(T, char *))
Definition: itoatest.cpp:70
signed int int32_t
Definition: stdint.h:123
char * i64toa(int64_t value, char *buffer)
Definition: itoa.h:291
char * i32toa(int32_t value, char *buffer)
Definition: itoa.h:113
TEST(itoa, u32toa)
Definition: itoatest.cpp:142
static void u32toa_naive(uint32_t value, char *buffer)
Definition: itoatest.cpp:94


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09