1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/* Copyright 2012 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <limits>
#include "mongo/pch.h" // for malloc/realloc pulled from bson
#include "mongo/bson/bsontypes.h"
#include "mongo/util/safe_num.h"
#include "mongo/unittest/unittest.h"
namespace {
using mongo::SafeNum;
TEST(Basics, Initialization) {
const SafeNum numInt(0);
ASSERT_EQUALS(numInt.type(), mongo::NumberInt);
const SafeNum numLong(0LL);
ASSERT_EQUALS(numLong.type(), mongo::NumberLong);
const SafeNum numDouble(0.0);
ASSERT_EQUALS(numDouble.type(), mongo::NumberDouble);
}
TEST(Comparison, EOO) {
const SafeNum safeNumA;
const SafeNum safeNumB;
ASSERT_FALSE(safeNumA.isValid());
ASSERT_FALSE(safeNumB.isValid());
ASSERT_EQUALS(safeNumA.type(), mongo::EOO);
ASSERT_EQUALS(safeNumB.type(), mongo::EOO);
ASSERT_TRUE(safeNumA.isEquivalent(safeNumB));
ASSERT_FALSE(safeNumA.isIdentical(safeNumB));
const SafeNum one(1);
ASSERT_NOT_EQUALS(one, safeNumA);
}
TEST(Comparison, StrictTypeComparison) {
const SafeNum one(1);
const SafeNum oneLong(1LL);
const SafeNum oneDouble(1.0);
ASSERT_FALSE(one.isIdentical(oneLong));
ASSERT_FALSE(oneLong.isIdentical(oneDouble));
ASSERT_FALSE(oneDouble.isIdentical(one));
}
TEST(Comparison, EquivalenceComparisonNormal) {
const SafeNum one(1);
const SafeNum oneLong(1LL);
const SafeNum oneDouble(1.0);
ASSERT_EQUALS(one, oneLong);
ASSERT_EQUALS(oneLong, oneDouble);
ASSERT_EQUALS(oneDouble, one);
}
TEST(Comparison, MaxIntInDouble) {
const SafeNum okToConvert(SafeNum::maxIntInDouble-1);
ASSERT_EQUALS(okToConvert, SafeNum(SafeNum::maxIntInDouble-1.0));
const SafeNum unsafeToConvert(SafeNum::maxIntInDouble + 100);
ASSERT_NOT_EQUALS(unsafeToConvert, SafeNum(SafeNum::maxIntInDouble+100.0));
}
TEST(Addition, Zero) {
const SafeNum zero(0);
ASSERT_EQUALS(zero + 0, zero);
ASSERT_EQUALS(zero + zero, zero);
const SafeNum minusOne(-1);
const SafeNum plusOne(1);
ASSERT_EQUALS(minusOne + 1, zero);
ASSERT_EQUALS(zero + -1, minusOne);
ASSERT_EQUALS(plusOne + -1, zero);
ASSERT_EQUALS(zero + 1, plusOne);
}
TEST(Addition, UpConvertion) {
const SafeNum zeroInt32(0);
const SafeNum zeroInt64(0LL);
const SafeNum zeroDouble(0.0);
ASSERT_EQUALS((zeroInt32 + zeroInt64).type(), mongo::NumberLong);
ASSERT_EQUALS((zeroInt64 + zeroInt32).type(), mongo::NumberLong);
ASSERT_EQUALS((zeroInt32 + zeroDouble).type(), mongo::NumberDouble);
ASSERT_EQUALS((zeroInt64 + zeroDouble).type(), mongo::NumberDouble);
const SafeNum stillInt32(zeroInt32 + zeroInt32);
const SafeNum stillInt64(zeroInt64 + zeroInt64);
const SafeNum stillDouble(zeroDouble + zeroDouble);
ASSERT_EQUALS(stillInt32.type(), mongo::NumberInt);
ASSERT_EQUALS(stillInt64.type(), mongo::NumberLong);
ASSERT_EQUALS(stillDouble.type(), mongo::NumberDouble);
}
TEST(Addition, Overflow32to64) {
const SafeNum maxInt32(std::numeric_limits<int>::max());
ASSERT_EQUALS(maxInt32.type(), mongo::NumberInt);
const SafeNum int32PlusOne(maxInt32 + 1);
ASSERT_EQUALS(int32PlusOne.type(), mongo::NumberLong);
const SafeNum int32MinusOne(maxInt32 + -1);
ASSERT_EQUALS(int32MinusOne.type(), mongo::NumberInt);
const SafeNum longResult(std::numeric_limits<int>::max() + static_cast<long long>(1));
ASSERT_EQUALS(int32PlusOne, longResult);
}
TEST(Addition, Overflow64toDouble) {
const SafeNum maxInt64(std::numeric_limits<long long>::max());
ASSERT_EQUALS(maxInt64.type(), mongo::NumberLong);
// We don't overflow int64 to double.
const SafeNum int64PlusOne(maxInt64 + 1);
ASSERT_EQUALS(int64PlusOne.type(), mongo::EOO);
const SafeNum int64MinusOne(maxInt64 + -1);
ASSERT_EQUALS(int64MinusOne.type(), mongo::NumberLong);
const SafeNum doubleResult(std::numeric_limits<long long>::max()+static_cast<double>(1));
ASSERT_EQUALS(doubleResult.type(), mongo::NumberDouble);
ASSERT_NOT_EQUALS(int64PlusOne, doubleResult);
}
TEST(Addition, OverflowDouble) {
const SafeNum maxDouble(std::numeric_limits<double>::max());
ASSERT_EQUALS(maxDouble.type(), mongo::NumberDouble);
// can't just add one here, as max double is so sparse max == max+1
const SafeNum doublePlusMax(maxDouble + maxDouble);
ASSERT_EQUALS(doublePlusMax.type(), mongo::NumberDouble);
const SafeNum infinity(std::numeric_limits<double>::infinity());
ASSERT_EQUALS(doublePlusMax, infinity);
}
TEST(Addition, Negative32to64) {
const SafeNum minInt32(std::numeric_limits<int>::min());
ASSERT_EQUALS(minInt32.type(), mongo::NumberInt);
const SafeNum int32MinusOne(minInt32 + -1);
ASSERT_EQUALS(int32MinusOne.type(), mongo::NumberLong);
const SafeNum int32PlusOne(minInt32 + 1);
ASSERT_EQUALS(int32PlusOne.type(), mongo::NumberInt);
const SafeNum longResult(std::numeric_limits<int>::min()-static_cast<long long>(1));
ASSERT_EQUALS(int32MinusOne, longResult);
}
TEST(Addition, Negative64toDouble) {
const SafeNum minInt64(std::numeric_limits<long long>::min());
ASSERT_EQUALS(minInt64.type(), mongo::NumberLong);
// We don't overflow int64 to double.
const SafeNum int64MinusOne(minInt64 + -1);
ASSERT_EQUALS(int64MinusOne.type(), mongo::EOO);
const SafeNum int64PlusOne(minInt64 + 1);
ASSERT_EQUALS(int64PlusOne.type(), mongo::NumberLong);
const SafeNum doubleResult(std::numeric_limits<long long>::min()-static_cast<double>(1));
ASSERT_EQUALS(doubleResult.type(), mongo::NumberDouble);
ASSERT_NOT_EQUALS(int64MinusOne, doubleResult);
}
TEST(BitAnd, DoubleIsIgnored) {
const SafeNum val_int(static_cast<int>(1));
const SafeNum val_ll(static_cast<long long>(1));
const SafeNum val_double(1.0);
ASSERT_FALSE((val_int & val_double).isValid());
ASSERT_FALSE((val_double & val_int).isValid());
ASSERT_FALSE((val_ll & val_double).isValid());
ASSERT_FALSE((val_double & val_ll).isValid());
ASSERT_FALSE((val_double & val_double).isValid());
}
TEST(BitAnd, 32and32) {
const SafeNum val1(static_cast<int>(0xE0F1U));
const SafeNum val2(static_cast<int>(0xDF01U));
const SafeNum expected(static_cast<int>(0xC001U));
const SafeNum result = val1 & val2;
ASSERT_EQUALS(mongo::NumberInt, result.type());
ASSERT_TRUE(expected.isIdentical(result));
}
TEST(BitAnd, 64and64) {
const SafeNum val1(static_cast<long long>(0xE0F1E0F1E0F1ULL));
const SafeNum val2(static_cast<long long>(0xDF01DF01DF01ULL));
const SafeNum expected(static_cast<long long>(0xC001C001C001ULL));
const SafeNum result = val1 & val2;
ASSERT_EQUALS(mongo::NumberLong, result.type());
ASSERT_TRUE(expected.isIdentical(result));
}
TEST(BitAnd, MixedSize) {
const SafeNum val_small(static_cast<int>(0xE0F1U));
const SafeNum val_big(static_cast<long long>(0xDF01U));
const SafeNum expected(static_cast<long long>(0xC001U));
const SafeNum result_s_b = val_small & val_big;
const SafeNum result_b_s = val_big & val_small;
ASSERT_EQUALS(mongo::NumberLong, result_s_b.type());
ASSERT_TRUE(expected.isIdentical(result_s_b));
ASSERT_EQUALS(mongo::NumberLong, result_b_s.type());
ASSERT_TRUE(expected.isIdentical(result_b_s));
}
TEST(BitOr, DoubleIsIgnored) {
const SafeNum val_int(static_cast<int>(1));
const SafeNum val_ll(static_cast<long long>(1));
const SafeNum val_double(1.0);
ASSERT_FALSE((val_int | val_double).isValid());
ASSERT_FALSE((val_double | val_int).isValid());
ASSERT_FALSE((val_ll | val_double).isValid());
ASSERT_FALSE((val_double | val_ll).isValid());
ASSERT_FALSE((val_double | val_double).isValid());
}
TEST(BitOr, 32and32) {
const SafeNum val1(static_cast<int>(0xE0F1U));
const SafeNum val2(static_cast<int>(0xDF01U));
const SafeNum result = val1 | val2;
const SafeNum expected(static_cast<int>(0xFFF1U));
ASSERT_EQUALS(mongo::NumberInt, result.type());
ASSERT_TRUE(expected.isIdentical(result));
}
TEST(BitOr, 64and64) {
const SafeNum val1(static_cast<long long>(0xE0F1E0F1E0F1ULL));
const SafeNum val2(static_cast<long long>(0xDF01DF01DF01ULL));
const SafeNum result = val1 | val2;
const SafeNum expected(static_cast<long long>(0xFFF1FFF1FFF1ULL));
ASSERT_EQUALS(mongo::NumberLong, result.type());
ASSERT_TRUE(expected.isIdentical(result));
}
TEST(BitOr, MixedSize) {
const SafeNum val_small(static_cast<int>(0xE0F1U));
const SafeNum val_big(static_cast<long long>(0xDF01U));
const SafeNum expected(static_cast<long long>(0xFFF1U));
const SafeNum result_s_b = val_small | val_big;
const SafeNum result_b_s = val_big | val_small;
ASSERT_EQUALS(mongo::NumberLong, result_s_b.type());
ASSERT_TRUE(expected.isIdentical(result_s_b));
ASSERT_EQUALS(mongo::NumberLong, result_b_s.type());
ASSERT_TRUE(expected.isIdentical(result_b_s));
}
TEST(BitXor, DoubleIsIgnored) {
const SafeNum val_int(static_cast<int>(1));
const SafeNum val_ll(static_cast<long long>(1));
const SafeNum val_double(1.0);
ASSERT_FALSE((val_int ^ val_double).isValid());
ASSERT_FALSE((val_double ^ val_int).isValid());
ASSERT_FALSE((val_ll ^ val_double).isValid());
ASSERT_FALSE((val_double ^ val_ll).isValid());
ASSERT_FALSE((val_double ^ val_double).isValid());
}
TEST(BitXor, 32and32) {
const SafeNum val1(static_cast<int>(0xE0F1U));
const SafeNum val2(static_cast<int>(0xDF01U));
const SafeNum result = val1 ^ val2;
const SafeNum expected(static_cast<int>(0x3FF0U));
ASSERT_EQUALS(mongo::NumberInt, result.type());
ASSERT_TRUE(expected.isIdentical(result));
}
TEST(BitXor, 64and64) {
const SafeNum val1(static_cast<long long>(0xE0F1E0F1E0F1ULL));
const SafeNum val2(static_cast<long long>(0xDF01DF01DF01ULL));
const SafeNum result = val1 ^ val2;
const SafeNum expected(static_cast<long long>(0x3FF03FF03FF0ULL));
ASSERT_EQUALS(mongo::NumberLong, result.type());
ASSERT_TRUE(expected.isIdentical(result));
}
TEST(BitXor, MixedSize) {
const SafeNum val_small(static_cast<int>(0xE0F1U));
const SafeNum val_big(static_cast<long long>(0xDF01U));
const SafeNum expected(static_cast<long long>(0x3FF0U));
const SafeNum result_s_b = val_small ^ val_big;
const SafeNum result_b_s = val_big ^ val_small;
ASSERT_EQUALS(mongo::NumberLong, result_s_b.type());
ASSERT_TRUE(expected.isIdentical(result_s_b));
ASSERT_EQUALS(mongo::NumberLong, result_b_s.type());
ASSERT_TRUE(expected.isIdentical(result_b_s));
}
TEST(Multiplication, Zero) {
const SafeNum zero(0);
ASSERT_EQUALS(zero * 0, zero);
ASSERT_EQUALS(zero * zero, zero);
}
TEST(Multiplication, LongZero) {
const SafeNum zero(0LL);
ASSERT_EQUALS(zero * 0LL, zero);
ASSERT_EQUALS(zero * zero, zero);
}
TEST(Multiplication, DoubleZero) {
const SafeNum zero(0.0);
ASSERT_EQUALS(zero * 0.0, zero);
ASSERT_EQUALS(zero * zero, zero);
}
TEST(Multiplication, One) {
const SafeNum plusOne(1);
ASSERT_EQUALS(plusOne * 1, plusOne);
ASSERT_EQUALS(plusOne * plusOne, plusOne);
}
TEST(Multiplication, LongOne) {
const SafeNum plusOne(1LL);
ASSERT_EQUALS(plusOne * 1LL, plusOne);
ASSERT_EQUALS(plusOne * plusOne, plusOne);
}
TEST(Multiplication, DoubleOne) {
const SafeNum plusOne(1.0);
ASSERT_EQUALS(plusOne * 1.0, plusOne);
ASSERT_EQUALS(plusOne * plusOne, plusOne);
}
TEST(Multiplication, UpConvertion) {
const SafeNum zeroInt32(0);
const SafeNum zeroInt64(0LL);
const SafeNum zeroDouble(0.0);
ASSERT_EQUALS((zeroInt32 * zeroInt64).type(), mongo::NumberLong);
ASSERT_EQUALS((zeroInt64 * zeroInt32).type(), mongo::NumberLong);
ASSERT_EQUALS((zeroInt32 * zeroDouble).type(), mongo::NumberDouble);
ASSERT_EQUALS((zeroInt64 * zeroDouble).type(), mongo::NumberDouble);
ASSERT_EQUALS((zeroDouble * zeroInt32).type(), mongo::NumberDouble);
ASSERT_EQUALS((zeroDouble * zeroInt64).type(), mongo::NumberDouble);
const SafeNum stillInt32(zeroInt32 * zeroInt32);
const SafeNum stillInt64(zeroInt64 * zeroInt64);
const SafeNum stillDouble(zeroDouble * zeroDouble);
ASSERT_EQUALS(stillInt32.type(), mongo::NumberInt);
ASSERT_EQUALS(stillInt64.type(), mongo::NumberLong);
ASSERT_EQUALS(stillDouble.type(), mongo::NumberDouble);
}
TEST(Multiplication, Overflow32to64) {
const SafeNum maxInt32(std::numeric_limits<int>::max());
ASSERT_EQUALS(maxInt32.type(), mongo::NumberInt);
const SafeNum int32TimesOne(maxInt32 * 1);
ASSERT_EQUALS(int32TimesOne.type(), mongo::NumberInt);
const SafeNum int32TimesTwo(maxInt32 * 2);
ASSERT_EQUALS(int32TimesTwo.type(), mongo::NumberLong);
}
TEST(Multiplication, Overflow64toDouble) {
const SafeNum maxInt64(std::numeric_limits<long long>::max());
ASSERT_EQUALS(maxInt64.type(), mongo::NumberLong);
// We don't overflow int64 to double.
const SafeNum int64TimesTwo(maxInt64 * 2);
ASSERT_EQUALS(int64TimesTwo.type(), mongo::EOO);
const SafeNum doubleResult(std::numeric_limits<long long>::max()*static_cast<double>(2));
ASSERT_EQUALS(doubleResult.type(), mongo::NumberDouble);
ASSERT_NOT_EQUALS(int64TimesTwo, doubleResult);
}
TEST(Multiplication, OverflowDouble) {
const SafeNum maxDouble(std::numeric_limits<double>::max());
ASSERT_EQUALS(maxDouble.type(), mongo::NumberDouble);
const SafeNum doublePlusMax(maxDouble * maxDouble);
ASSERT_EQUALS(doublePlusMax.type(), mongo::NumberDouble);
const SafeNum infinity(std::numeric_limits<double>::infinity());
ASSERT_EQUALS(doublePlusMax, infinity);
}
TEST(Multiplication, Negative32to64) {
const SafeNum minInt32(std::numeric_limits<int>::min());
ASSERT_EQUALS(minInt32.type(), mongo::NumberInt);
const SafeNum int32TimesOne(minInt32 * 1);
ASSERT_EQUALS(int32TimesOne.type(), mongo::NumberInt);
const SafeNum int32TimesTwo(minInt32 * 2);
ASSERT_EQUALS(int32TimesTwo.type(), mongo::NumberLong);
}
TEST(Multiplication, Negative64toDouble) {
const SafeNum minInt64(std::numeric_limits<long long>::min());
ASSERT_EQUALS(minInt64.type(), mongo::NumberLong);
// We don't overflow int64 to double.
const SafeNum int64TimesTwo(minInt64 * 2);
ASSERT_EQUALS(int64TimesTwo.type(), mongo::EOO);
const SafeNum int64TimesOne(minInt64 * 1);
ASSERT_EQUALS(int64TimesOne.type(), mongo::NumberLong);
const SafeNum doubleResult(std::numeric_limits<long long>::min()*static_cast<double>(2));
ASSERT_EQUALS(doubleResult.type(), mongo::NumberDouble);
ASSERT_NOT_EQUALS(int64TimesTwo, doubleResult);
}
TEST(Multiplication, 64OverflowsFourWays) {
const SafeNum maxInt64(std::numeric_limits<long long>::max());
const SafeNum minInt64(std::numeric_limits<long long>::min());
ASSERT_EQUALS(mongo::EOO, (maxInt64 * maxInt64).type());
ASSERT_EQUALS(mongo::EOO, (maxInt64 * minInt64).type());
ASSERT_EQUALS(mongo::EOO, (minInt64 * maxInt64).type());
ASSERT_EQUALS(mongo::EOO, (minInt64 * minInt64).type());
}
TEST(Multiplication, BoundsWithNegativeOne) {
const SafeNum maxInt64(std::numeric_limits<long long>::max());
const SafeNum minInt64(std::numeric_limits<long long>::min());
const SafeNum minusOneInt64(-1LL);
ASSERT_NOT_EQUALS(mongo::EOO, (maxInt64 * minusOneInt64).type());
ASSERT_NOT_EQUALS(mongo::EOO, (minusOneInt64 * maxInt64).type());
ASSERT_EQUALS(mongo::EOO, (minInt64 * minusOneInt64).type());
ASSERT_EQUALS(mongo::EOO, (minusOneInt64 * minInt64).type());
}
} // unnamed namespace
|